DCountTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 DCountTest extends TestCase
  7. {
  8. protected function setUp(): void
  9. {
  10. Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
  11. }
  12. /**
  13. * @dataProvider providerDCount
  14. *
  15. * @param mixed $expectedResult
  16. * @param mixed $database
  17. * @param mixed $field
  18. * @param mixed $criteria
  19. */
  20. public function testDCount($expectedResult, $database, $field, $criteria): void
  21. {
  22. $result = Database::DCOUNT($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, 'N/A', 10, 75],
  33. ['Pear', 9, 8, 8, 77],
  34. ['Apple', 12, 11, 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. private function database3(): array
  56. {
  57. return [
  58. ['Status', 'Value'],
  59. [false, 1],
  60. [true, 2],
  61. [true, 4],
  62. [false, 8],
  63. [true, 16],
  64. [false, 32],
  65. [false, 64],
  66. [false, 128],
  67. ];
  68. }
  69. public function providerDCount(): array
  70. {
  71. return [
  72. [
  73. 1,
  74. $this->database1(),
  75. 'Age',
  76. [
  77. ['Tree', 'Height', 'Height'],
  78. ['=Apple', '>10', '<16'],
  79. ],
  80. ],
  81. [
  82. 1,
  83. $this->database2(),
  84. 'Score',
  85. [
  86. ['Subject', 'Gender'],
  87. ['Science', 'Male'],
  88. ],
  89. ],
  90. [
  91. 1,
  92. $this->database2(),
  93. 'Score',
  94. [
  95. ['Subject', 'Gender'],
  96. ['Math', 'Female'],
  97. ],
  98. ],
  99. [
  100. 3,
  101. $this->database2(),
  102. null,
  103. [
  104. ['Subject', 'Score'],
  105. ['English', '>63%'],
  106. ],
  107. ],
  108. [
  109. 3,
  110. $this->database3(),
  111. 'Value',
  112. [
  113. ['Status'],
  114. [true],
  115. ],
  116. ],
  117. [
  118. 5,
  119. $this->database3(),
  120. 'Value',
  121. [
  122. ['Status'],
  123. ['<>true'],
  124. ],
  125. ],
  126. ];
  127. }
  128. }