mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-18 14:06:34 +00:00
d647fe7ee7
With PhpUnit 10 came the ability to use Php attributes rather than doc-block annotations for things like "data provider". PhpUnit 11 deprecates the use of annotations, and PhpUnit 12 will not not permit their use. Since PhpUnit 11 requires Php8.2+, we cannot adopt it as long as we support Php8.1, which will continue to be the case for some time. However, there is no penalty for early adoption. Php-cs-fixer can use: ``` 'php_unit_attributes' => ['keep_annotations' => false], ``` This allows us to run `composer fix` to automate all the needed changes. No manual changes were needed for any of the test members. With this change, PhpUnit 9 can no longer be used with the test suite. File composer.json is updated to reflect that reality, and phpunit9.xml.dist, which has been supplied in case anyone needed to use PhpUnit 9, is no longer required, and is thus deleted. For now, PhpUnit 11 is not being added as a possibility. No source code is changed in this PR.
202 lines
6.8 KiB
PHP
202 lines
6.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
|
|
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef\Sort;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class SortTest extends TestCase
|
|
{
|
|
public function testSortOnScalar(): void
|
|
{
|
|
$value = 'NON-ARRAY';
|
|
|
|
$result = Sort::sort($value, 1, -1);
|
|
self::assertSame($value, $result);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerSortWithScalarArgumentErrorReturns')]
|
|
public function testSortWithScalarArgumentErrorReturns(mixed $sortIndex, mixed $sortOrder = 1): void
|
|
{
|
|
$value = [[1, 2], [3, 4], [5, 6]];
|
|
|
|
$result = Sort::sort($value, $sortIndex, $sortOrder);
|
|
self::assertSame(ExcelError::VALUE(), $result);
|
|
}
|
|
|
|
public static function providerSortWithScalarArgumentErrorReturns(): array
|
|
{
|
|
return [
|
|
'Negative sortIndex' => [-1, -1],
|
|
'Non-numeric sortIndex' => ['A', -1],
|
|
'Zero sortIndex' => [0, -1],
|
|
'Too high sortIndex' => [3, -1],
|
|
'Non-numeric sortOrder' => [1, 'A'],
|
|
'Invalid negative sortOrder' => [1, -2],
|
|
'Zero sortOrder' => [1, 0],
|
|
'Invalid positive sortOrder' => [1, 2],
|
|
'Too many sortOrders (scalar and array)' => [1, [-1, 1]],
|
|
'Too many sortOrders (both array)' => [[1, 2], [1, 2, 3]],
|
|
'Zero positive sortIndex in vector' => [[0, 1]],
|
|
'Too high sortIndex in vector' => [[1, 3]],
|
|
'Invalid sortOrder in vector' => [[1, 2], [1, -2]],
|
|
];
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerSortByRow')]
|
|
public function testSortByRow(array $expectedResult, array $matrix, int $sortIndex, int $sortOrder = Sort::ORDER_ASCENDING): void
|
|
{
|
|
$result = Sort::sort($matrix, $sortIndex, $sortOrder);
|
|
self::assertSame($expectedResult, $result);
|
|
}
|
|
|
|
public static function providerSortByRow(): array
|
|
{
|
|
return [
|
|
[
|
|
[[142], [378], [404], [445], [483], [622], [650], [691], [783], [961]],
|
|
self::sampleDataForRow(),
|
|
1,
|
|
],
|
|
[
|
|
[[961], [783], [691], [650], [622], [483], [445], [404], [378], [142]],
|
|
self::sampleDataForRow(),
|
|
1,
|
|
Sort::ORDER_DESCENDING,
|
|
],
|
|
[
|
|
[['Peaches', 25], ['Cherries', 29], ['Grapes', 31], ['Lemons', 34], ['Oranges', 36], ['Apples', 38], ['Pears', 40]],
|
|
[['Apples', 38], ['Cherries', 29], ['Grapes', 31], ['Lemons', 34], ['Oranges', 36], ['Peaches', 25], ['Pears', 40]],
|
|
2,
|
|
],
|
|
];
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerSortByRowMultiLevel')]
|
|
public function testSortByRowMultiLevel(array $expectedResult, array $matrix, array $sortIndex, int $sortOrder = Sort::ORDER_ASCENDING): void
|
|
{
|
|
$result = Sort::sort($matrix, $sortIndex, $sortOrder);
|
|
self::assertSame($expectedResult, $result);
|
|
}
|
|
|
|
public static function providerSortByRowMultiLevel(): array
|
|
{
|
|
return [
|
|
[
|
|
[
|
|
['East', 'Grapes', 31],
|
|
['East', 'Lemons', 36],
|
|
['North', 'Cherries', 29],
|
|
['North', 'Grapes', 27],
|
|
['North', 'Peaches', 25],
|
|
['South', 'Apples', 38],
|
|
['South', 'Cherries', 28],
|
|
['South', 'Oranges', 36],
|
|
['South', 'Pears', 40],
|
|
['West', 'Apples', 30],
|
|
['West', 'Lemons', 34],
|
|
['West', 'Oranges', 25],
|
|
],
|
|
self::sampleDataForMultiRow(),
|
|
[1, 2],
|
|
],
|
|
[
|
|
[
|
|
['East', 'Grapes', 31],
|
|
['East', 'Lemons', 36],
|
|
['North', 'Peaches', 25],
|
|
['North', 'Grapes', 27],
|
|
['North', 'Cherries', 29],
|
|
['South', 'Cherries', 28],
|
|
['South', 'Oranges', 36],
|
|
['South', 'Apples', 38],
|
|
['South', 'Pears', 40],
|
|
['West', 'Oranges', 25],
|
|
['West', 'Apples', 30],
|
|
['West', 'Lemons', 34],
|
|
],
|
|
self::sampleDataForMultiRow(),
|
|
[1, 3],
|
|
],
|
|
[
|
|
[
|
|
['West', 'Apples', 30],
|
|
['South', 'Apples', 38],
|
|
['South', 'Cherries', 28],
|
|
['North', 'Cherries', 29],
|
|
['North', 'Grapes', 27],
|
|
['East', 'Grapes', 31],
|
|
['West', 'Lemons', 34],
|
|
['East', 'Lemons', 36],
|
|
['West', 'Oranges', 25],
|
|
['South', 'Oranges', 36],
|
|
['North', 'Peaches', 25],
|
|
['South', 'Pears', 40],
|
|
],
|
|
self::sampleDataForMultiRow(),
|
|
[2, 3],
|
|
],
|
|
];
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerSortByColumn')]
|
|
public function testSortByColumn(array $expectedResult, array $matrix, int $sortIndex, int $sortOrder): void
|
|
{
|
|
$result = Sort::sort($matrix, $sortIndex, $sortOrder, true);
|
|
self::assertSame($expectedResult, $result);
|
|
}
|
|
|
|
public static function providerSortByColumn(): array
|
|
{
|
|
return [
|
|
[
|
|
[[142, 378, 404, 445, 483, 622, 650, 691, 783, 961]],
|
|
self::sampleDataForColumn(),
|
|
1,
|
|
Sort::ORDER_ASCENDING,
|
|
],
|
|
[
|
|
[[961, 783, 691, 650, 622, 483, 445, 404, 378, 142]],
|
|
self::sampleDataForColumn(),
|
|
1,
|
|
Sort::ORDER_DESCENDING,
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function sampleDataForRow(): array
|
|
{
|
|
return [
|
|
[622], [961], [691], [445], [378], [483], [650], [783], [142], [404],
|
|
];
|
|
}
|
|
|
|
public static function sampleDataForMultiRow(): array
|
|
{
|
|
return [
|
|
['South', 'Pears', 40],
|
|
['South', 'Apples', 38],
|
|
['South', 'Oranges', 36],
|
|
['East', 'Lemons', 36],
|
|
['West', 'Lemons', 34],
|
|
['East', 'Grapes', 31],
|
|
['West', 'Apples', 30],
|
|
['North', 'Cherries', 29],
|
|
['South', 'Cherries', 28],
|
|
['North', 'Grapes', 27],
|
|
['North', 'Peaches', 25],
|
|
['West', 'Oranges', 25],
|
|
];
|
|
}
|
|
|
|
public static function sampleDataForColumn(): array
|
|
{
|
|
return [
|
|
[622, 961, 691, 445, 378, 483, 650, 783, 142, 404],
|
|
];
|
|
}
|
|
}
|