DCountATest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Database;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  5. use PHPUnit\Framework\TestCase;
  6. class DCountATest extends TestCase
  7. {
  8. protected function setUp(): void
  9. {
  10. Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
  11. }
  12. /**
  13. * @dataProvider providerDCountA
  14. *
  15. * @param mixed $expectedResult
  16. * @param mixed $database
  17. * @param mixed $field
  18. * @param mixed $criteria
  19. */
  20. public function testDCountA($expectedResult, $database, $field, $criteria): void
  21. {
  22. $result = Database::DCOUNTA($database, $field, $criteria);
  23. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
  24. }
  25. private function database1(): array
  26. {
  27. return [
  28. ['Tree', 'Height', 'Age', 'Yield', 'Profit'],
  29. ['Apple', 18, 20, 14, 105],
  30. ['Pear', 12, 12, 10, 96],
  31. ['Cherry', 13, 14, 9, 105],
  32. ['Apple', 14, 15, 10, 75],
  33. ['Pear', 9, 8, 8, 76.8],
  34. ['Apple', 8, 9, 6, 45],
  35. ];
  36. }
  37. private function database2(): array
  38. {
  39. return [
  40. ['Name', 'Gender', 'Age', 'Subject', 'Score'],
  41. ['Amy', 'Female', 8, 'Math', 0.63],
  42. ['Amy', 'Female', 8, 'English', 0.78],
  43. ['Amy', 'Female', 8, 'Science', 0.39],
  44. ['Bill', 'Male', 8, 'Math', 0.55],
  45. ['Bill', 'Male', 8, 'English', 0.71],
  46. ['Bill', 'Male', 8, 'Science', 'awaiting'],
  47. ['Sue', 'Female', 9, 'Math', null],
  48. ['Sue', 'Female', 9, 'English', 0.52],
  49. ['Sue', 'Female', 9, 'Science', 0.48],
  50. ['Tom', 'Male', 9, 'Math', 0.78],
  51. ['Tom', 'Male', 9, 'English', 0.69],
  52. ['Tom', 'Male', 9, 'Science', 0.65],
  53. ];
  54. }
  55. public function providerDCountA(): array
  56. {
  57. return [
  58. [
  59. 1,
  60. $this->database1(),
  61. 'Profit',
  62. [
  63. ['Tree', 'Height', 'Height'],
  64. ['=Apple', '>10', '<16'],
  65. ],
  66. ],
  67. [
  68. 2,
  69. $this->database2(),
  70. 'Score',
  71. [
  72. ['Subject', 'Gender'],
  73. ['Science', 'Male'],
  74. ],
  75. ],
  76. [
  77. 1,
  78. $this->database2(),
  79. 'Score',
  80. [
  81. ['Subject', 'Gender'],
  82. ['Math', 'Female'],
  83. ],
  84. ],
  85. [
  86. 3,
  87. $this->database2(),
  88. 'Score',
  89. [
  90. ['Subject', 'Score'],
  91. ['English', '>60%'],
  92. ],
  93. ],
  94. ];
  95. }
  96. }