DSumTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 DSumTest extends TestCase
  7. {
  8. protected function setUp(): void
  9. {
  10. Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
  11. }
  12. /**
  13. * @dataProvider providerDSum
  14. *
  15. * @param mixed $expectedResult
  16. * @param mixed $database
  17. * @param mixed $field
  18. * @param mixed $criteria
  19. */
  20. public function testDSum($expectedResult, $database, $field, $criteria): void
  21. {
  22. $result = Database::DSUM($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, 77],
  34. ['Apple', 8, 9, 6, 45],
  35. ];
  36. }
  37. private function database2(): array
  38. {
  39. return [
  40. ['Quarter', 'Area', 'Sales Rep.', 'Sales'],
  41. [1, 'North', 'Jeff', 223000],
  42. [1, 'North', 'Chris', 125000],
  43. [1, 'South', 'Carol', 456000],
  44. [1, 'South', 'Tina', 289000],
  45. [2, 'North', 'Jeff', 322000],
  46. [2, 'North', 'Chris', 340000],
  47. [2, 'South', 'Carol', 198000],
  48. [2, 'South', 'Tina', 222000],
  49. [3, 'North', 'Jeff', 310000],
  50. [3, 'North', 'Chris', 250000],
  51. [3, 'South', 'Carol', 460000],
  52. [3, 'South', 'Tina', 395000],
  53. [4, 'North', 'Jeff', 261000],
  54. [4, 'North', 'Chris', 389000],
  55. [4, 'South', 'Carol', 305000],
  56. [4, 'South', 'Tina', 188000],
  57. ];
  58. }
  59. public function providerDSum(): array
  60. {
  61. return [
  62. [
  63. 225,
  64. $this->database1(),
  65. 'Profit',
  66. [
  67. ['Tree'],
  68. ['=Apple'],
  69. ],
  70. ],
  71. [
  72. 248,
  73. $this->database1(),
  74. 'Profit',
  75. [
  76. ['Tree', 'Height', 'Height'],
  77. ['=Apple', '>10', '<16'],
  78. ['=Pear', null, null],
  79. ],
  80. ],
  81. [
  82. 1210000,
  83. $this->database2(),
  84. 'Sales',
  85. [
  86. ['Quarter', 'Area'],
  87. ['>2', 'North'],
  88. ],
  89. ],
  90. [
  91. 710000,
  92. $this->database2(),
  93. 'Sales',
  94. [
  95. ['Quarter', 'Sales Rep.'],
  96. ['3', 'C*'],
  97. ],
  98. ],
  99. [
  100. 705000,
  101. $this->database2(),
  102. 'Sales',
  103. [
  104. ['Quarter', 'Sales Rep.'],
  105. ['3', '<>C*'],
  106. ],
  107. ],
  108. [
  109. null,
  110. $this->database1(),
  111. null,
  112. $this->database1(),
  113. ],
  114. ];
  115. }
  116. }