DAverageTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 DAverageTest extends TestCase
  7. {
  8. protected function setUp(): void
  9. {
  10. Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
  11. }
  12. /**
  13. * @dataProvider providerDAverage
  14. *
  15. * @param mixed $expectedResult
  16. * @param mixed $database
  17. * @param mixed $field
  18. * @param mixed $criteria
  19. */
  20. public function testDAverage($expectedResult, $database, $field, $criteria): void
  21. {
  22. $result = Database::DAVERAGE($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. ['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 providerDAverage(): array
  60. {
  61. return [
  62. [
  63. 12,
  64. $this->database1(),
  65. 'Yield',
  66. [
  67. ['Tree', 'Height'],
  68. ['=Apple', '>10'],
  69. ],
  70. ],
  71. [
  72. 13,
  73. $this->database1(),
  74. 3,
  75. $this->database1(),
  76. ],
  77. [
  78. 268333.333333333333,
  79. $this->database2(),
  80. 'Sales',
  81. [
  82. ['Quarter', 'Sales Rep.'],
  83. ['>1', 'Tina'],
  84. ],
  85. ],
  86. [
  87. 372500,
  88. $this->database2(),
  89. 'Sales',
  90. [
  91. ['Quarter', 'Area'],
  92. ['1', 'South'],
  93. ],
  94. ],
  95. [
  96. null,
  97. $this->database1(),
  98. null,
  99. $this->database1(),
  100. ],
  101. ];
  102. }
  103. }