DMaxTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 DMaxTest extends TestCase
  7. {
  8. protected function setUp(): void
  9. {
  10. Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
  11. }
  12. /**
  13. * @dataProvider providerDMax
  14. *
  15. * @param mixed $expectedResult
  16. * @param mixed $database
  17. * @param mixed $field
  18. * @param mixed $criteria
  19. */
  20. public function testDMax($expectedResult, $database, $field, $criteria): void
  21. {
  22. $result = Database::DMAX($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 providerDMax(): array
  60. {
  61. return [
  62. [
  63. 96,
  64. $this->database1(),
  65. 'Profit',
  66. [
  67. ['Tree', 'Height', 'Height'],
  68. ['=Apple', '>10', '<16'],
  69. ['=Pear', null, null],
  70. ],
  71. ],
  72. [
  73. 340000,
  74. $this->database2(),
  75. 'Sales',
  76. [
  77. ['Quarter', 'Area'],
  78. [2, 'North'],
  79. ],
  80. ],
  81. [
  82. 460000,
  83. $this->database2(),
  84. 'Sales',
  85. [
  86. ['Sales Rep.', 'Quarter'],
  87. ['Carol', '>1'],
  88. ],
  89. ],
  90. [
  91. null,
  92. $this->database1(),
  93. null,
  94. $this->database1(),
  95. ],
  96. ];
  97. }
  98. }