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.
154 lines
5.1 KiB
PHP
154 lines
5.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\DateParts;
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException;
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class DayTest extends TestCase
|
|
{
|
|
private string $compatibilityMode;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->compatibilityMode = Functions::getCompatibilityMode();
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
parent::tearDown();
|
|
|
|
Functions::setCompatibilityMode($this->compatibilityMode);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerDAY')]
|
|
public function testDirectCallToDAY(mixed $expectedResultExcel, mixed ...$args): void
|
|
{
|
|
$result = DateParts::day(...$args);
|
|
self::assertSame($expectedResultExcel, $result);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerDAY')]
|
|
public function testDAYAsFormula(mixed $expectedResultExcel, mixed ...$args): void
|
|
{
|
|
$arguments = new FormulaArguments(...$args);
|
|
|
|
$calculation = Calculation::getInstance();
|
|
$formula = "=DAY({$arguments})";
|
|
|
|
$result = $calculation->_calculateFormulaValue($formula);
|
|
self::assertSame($expectedResultExcel, $result);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerDAY')]
|
|
public function testDAYInWorksheet(mixed $expectedResult, mixed ...$args): void
|
|
{
|
|
$arguments = new FormulaArguments(...$args);
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
$argumentCells = $arguments->populateWorksheet($worksheet);
|
|
$formula = "=DAY({$argumentCells})";
|
|
|
|
$result = $worksheet->setCellValue('A1', $formula)
|
|
->getCell('A1')
|
|
->getCalculatedValue();
|
|
self::assertSame($expectedResult, $result);
|
|
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public static function providerDAY(): array
|
|
{
|
|
return require 'tests/data/Calculation/DateTime/DAY.php';
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerDAYOpenOffice')]
|
|
public function testDirectCallToDAYOpenOffice(mixed $expectedResultOpenOffice, mixed ...$args): void
|
|
{
|
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
|
|
|
|
$result = DateParts::day(...$args);
|
|
self::assertSame($expectedResultOpenOffice, $result);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerDAYOpenOffice')]
|
|
public function testDAYAsFormulaOpenOffice(mixed $expectedResultOpenOffice, mixed ...$args): void
|
|
{
|
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
|
|
|
|
$arguments = new FormulaArguments(...$args);
|
|
|
|
$calculation = Calculation::getInstance();
|
|
$formula = "=DAY({$arguments})";
|
|
|
|
$result = $calculation->_calculateFormulaValue($formula);
|
|
self::assertSame($expectedResultOpenOffice, $result);
|
|
}
|
|
|
|
public static function providerDAYOpenOffice(): array
|
|
{
|
|
return require 'tests/data/Calculation/DateTime/DAYOpenOffice.php';
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerUnhappyDAY')]
|
|
public function testDAYUnhappyPath(string $expectedException, mixed ...$args): void
|
|
{
|
|
$arguments = new FormulaArguments(...$args);
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
$argumentCells = $arguments->populateWorksheet($worksheet);
|
|
$formula = "=DAY({$argumentCells})";
|
|
|
|
$this->expectException(CalculationException::class);
|
|
$this->expectExceptionMessage($expectedException);
|
|
$worksheet->setCellValue('A1', $formula)
|
|
->getCell('A1')
|
|
->getCalculatedValue();
|
|
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public static function providerUnhappyDAY(): array
|
|
{
|
|
return [
|
|
['Formula Error: Wrong number of arguments for DAY() function'],
|
|
];
|
|
}
|
|
|
|
public function testDirectCallToDAYWithNull(): void
|
|
{
|
|
$result = DateParts::day(null);
|
|
self::assertSame(0, $result);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('providerDayArray')]
|
|
public function testDayArray(array $expectedResult, string $array): void
|
|
{
|
|
$calculation = Calculation::getInstance();
|
|
|
|
$formula = "=DAY({$array})";
|
|
$result = $calculation->_calculateFormulaValue($formula);
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
|
|
}
|
|
|
|
public static function providerDayArray(): array
|
|
{
|
|
return [
|
|
'row vector' => [[[1, 12, 22]], '{"2022-01-01", "2022-06-12", "2023-07-22"}'],
|
|
'column vector' => [[[1], [3], [6]], '{"2022-01-01"; "2022-01-03"; "2022-01-06"}'],
|
|
'matrix' => [[[1, 10], [15, 31]], '{"2022-01-01", "2022-01-10"; "2022-08-15", "2022-12-31"}'],
|
|
];
|
|
}
|
|
}
|