UniqueTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
  4. use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
  5. use PHPUnit\Framework\TestCase;
  6. class UniqueTest extends TestCase
  7. {
  8. /**
  9. * @dataProvider uniqueTestProvider
  10. */
  11. public function testUnique(array $expectedResult, array $lookupRef, bool $byColumn = false, bool $exactlyOnce = false): void
  12. {
  13. $result = LookupRef\Unique::unique($lookupRef, $byColumn, $exactlyOnce);
  14. self::assertEquals($expectedResult, $result);
  15. }
  16. public function testUniqueException(): void
  17. {
  18. $rowLookupData = [
  19. ['Andrew', 'Brown'],
  20. ['Betty', 'Johnson'],
  21. ['Betty', 'Johnson'],
  22. ['Andrew', 'Brown'],
  23. ['David', 'White'],
  24. ['Andrew', 'Brown'],
  25. ['David', 'White'],
  26. ];
  27. $columnLookupData = [
  28. ['PHP', 'Rocks', 'php', 'rocks'],
  29. ];
  30. $result = LookupRef\Unique::unique($rowLookupData, false, true);
  31. self::assertEquals(ExcelError::CALC(), $result);
  32. $result = LookupRef\Unique::unique($columnLookupData, true, true);
  33. self::assertEquals(ExcelError::CALC(), $result);
  34. }
  35. public function testUniqueWithScalar(): void
  36. {
  37. $lookupData = 123;
  38. $result = LookupRef\Unique::unique($lookupData);
  39. self::assertSame($lookupData, $result);
  40. }
  41. public function uniqueTestProvider(): array
  42. {
  43. return [
  44. [
  45. [['Red'], ['Green'], ['Blue'], ['Orange']],
  46. [
  47. ['Red'],
  48. ['Green'],
  49. ['Green'],
  50. ['Blue'],
  51. ['Blue'],
  52. ['Orange'],
  53. ['Green'],
  54. ['Blue'],
  55. ['Red'],
  56. ],
  57. ],
  58. [
  59. [['Red'], ['Green'], ['Blue'], ['Orange']],
  60. [
  61. ['Red'],
  62. ['Green'],
  63. ['GrEEn'],
  64. ['Blue'],
  65. ['BLUE'],
  66. ['Orange'],
  67. ['GReeN'],
  68. ['blue'],
  69. ['RED'],
  70. ],
  71. ],
  72. [
  73. ['Orange'],
  74. [
  75. ['Red'],
  76. ['Green'],
  77. ['Green'],
  78. ['Blue'],
  79. ['Blue'],
  80. ['Orange'],
  81. ['Green'],
  82. ['Blue'],
  83. ['Red'],
  84. ],
  85. false,
  86. true,
  87. ],
  88. [
  89. ['Andrew', 'Betty', 'Robert', 'David'],
  90. [['Andrew', 'Betty', 'Robert', 'Andrew', 'Betty', 'Robert', 'David', 'Andrew']],
  91. true,
  92. ],
  93. [
  94. ['David'],
  95. [['Andrew', 'Betty', 'Robert', 'Andrew', 'Betty', 'Robert', 'David', 'Andrew']],
  96. true,
  97. true,
  98. ],
  99. [
  100. [1, 1, 2, 2, 3],
  101. [[1, 1, 2, 2, 3]],
  102. ],
  103. [
  104. [1, 2, 3],
  105. [[1, 1, 2, 2, 3]],
  106. true,
  107. ],
  108. [
  109. [
  110. [1, 1, 2, 3],
  111. [1, 2, 2, 3],
  112. ],
  113. [
  114. [1, 1, 2, 2, 3],
  115. [1, 2, 2, 2, 3],
  116. ],
  117. true,
  118. ],
  119. [
  120. [
  121. ['Andrew', 'Brown'],
  122. ['Betty', 'Johnson'],
  123. ['David', 'White'],
  124. ],
  125. [
  126. ['Andrew', 'Brown'],
  127. ['Betty', 'Johnson'],
  128. ['Betty', 'Johnson'],
  129. ['Andrew', 'Brown'],
  130. ['David', 'White'],
  131. ['Andrew', 'Brown'],
  132. ['David', 'White'],
  133. ],
  134. ],
  135. [
  136. [[1.2], [2.1], [2.2], [3.0]],
  137. [
  138. [1.2],
  139. [1.2],
  140. [2.1],
  141. [2.2],
  142. [3.0],
  143. ],
  144. ],
  145. ];
  146. }
  147. }