DGetTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Database;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Database;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  5. use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
  6. use PHPUnit\Framework\TestCase;
  7. class DGetTest extends TestCase
  8. {
  9. protected function setUp(): void
  10. {
  11. Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
  12. }
  13. /**
  14. * @dataProvider providerDGet
  15. *
  16. * @param mixed $expectedResult
  17. * @param mixed $database
  18. * @param mixed $field
  19. * @param mixed $criteria
  20. */
  21. public function testDGet($expectedResult, $database, $field, $criteria): void
  22. {
  23. $result = Database::DGET($database, $field, $criteria);
  24. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12);
  25. }
  26. private function database1(): array
  27. {
  28. return [
  29. ['Tree', 'Height', 'Age', 'Yield', 'Profit'],
  30. ['Apple', 18, 20, 14, 105],
  31. ['Pear', 12, 12, 10, 96],
  32. ['Cherry', 13, 14, 9, 105],
  33. ['Apple', 14, 15, 10, 75],
  34. ['Pear', 9, 8, 8, 77],
  35. ['Apple', 8, 9, 6, 45],
  36. ];
  37. }
  38. private function database2(): array
  39. {
  40. return [
  41. ['Quarter', 'Area', 'Sales Rep.', 'Sales'],
  42. [1, 'North', 'Jeff', 223000],
  43. [1, 'North', 'Chris', 125000],
  44. [1, 'South', 'Carol', 456000],
  45. [1, 'South', 'Tina', 289000],
  46. [2, 'North', 'Jeff', 322000],
  47. [2, 'North', 'Chris', 340000],
  48. [2, 'South', 'Carol', 198000],
  49. [2, 'South', 'Tina', 222000],
  50. [3, 'North', 'Jeff', 310000],
  51. [3, 'North', 'Chris', 250000],
  52. [3, 'South', 'Carol', 460000],
  53. [3, 'South', 'Tina', 395000],
  54. [4, 'North', 'Jeff', 261000],
  55. [4, 'North', 'Chris', 389000],
  56. [4, 'South', 'Carol', 305000],
  57. [4, 'South', 'Tina', 188000],
  58. ];
  59. }
  60. public function providerDGet(): array
  61. {
  62. return [
  63. [
  64. ExcelError::NAN(),
  65. $this->database1(),
  66. 'Yield',
  67. [
  68. ['Tree'],
  69. ['=Apple'],
  70. ['=Pear'],
  71. ],
  72. ],
  73. [
  74. 10,
  75. $this->database1(),
  76. 'Yield',
  77. [
  78. ['Tree', 'Height', 'Height'],
  79. ['=Apple', '>10', '<16'],
  80. ['=Pear', '>12', null],
  81. ],
  82. ],
  83. [
  84. 188000,
  85. $this->database2(),
  86. 'Sales',
  87. [
  88. ['Sales Rep.', 'Quarter'],
  89. ['Tina', 4],
  90. ],
  91. ],
  92. [
  93. ExcelError::NAN(),
  94. $this->database2(),
  95. 'Sales',
  96. [
  97. ['Area', 'Quarter'],
  98. ['South', 4],
  99. ],
  100. ],
  101. [
  102. null,
  103. $this->database1(),
  104. null,
  105. $this->database1(),
  106. ],
  107. ];
  108. }
  109. }