Files
oleibman e9cf27354d PhpUnit 10 Compatibility Part 2 (#3526)
Successor to PR #3523. There are 494 single-line changes (`public function provider` to `public static function provider`) in this PR. None of these were made manually; they were all created with the following script (adapted from
https://stackoverflow.com/questions/25909820/how-to-recursively-iterate-through-files-in-php):
```php
$dir = 'C:/git/unit10prep2/tests/PhpSpreadsheetTests';
$it = new RecursiveDirectoryIterator($dir);

// Loop through files
foreach(new RecursiveIteratorIterator($it) as $file) {
    if ($file->getExtension() === 'php') {
        $contents = file_get_contents($file);
        $new = preg_replace('/public function (\\w*)([Pp])rovider/', 'public static function $1$2rovider', $contents);
        if ($new !== $contents) {
            echo "changing $file\n";
            file_put_contents($file, $new);
        }
    }
}
```

After this PR, there will be one more, with a small number of test changes, and enabling PhpUnit 10 for Php 8.1+.
2023-04-20 13:48:00 -07:00

162 lines
5.1 KiB
PHP

<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Month;
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 EoMonthTest extends TestCase
{
/**
* @var string
*/
private $returnDateType;
protected function setUp(): void
{
parent::setUp();
$this->returnDateType = Functions::getReturnDateType();
}
protected function tearDown(): void
{
parent::tearDown();
Functions::setReturnDateType($this->returnDateType);
}
/**
* @dataProvider providerEOMONTH
*
* @param mixed $expectedResult
*/
public function testDirectCallToEOMONTH($expectedResult, ...$args): void
{
$result = Month::lastDay(...$args);
self::assertSame($expectedResult, $result);
}
/**
* @dataProvider providerEOMONTH
*
* @param mixed $expectedResult
*/
public function testEOMONTHAsFormula($expectedResult, ...$args): void
{
$arguments = new FormulaArguments(...$args);
$calculation = Calculation::getInstance();
$formula = "=EOMONTH({$arguments})";
$result = $calculation->_calculateFormulaValue($formula);
self::assertSame($expectedResult, $result);
}
/**
* @dataProvider providerEOMONTH
*
* @param mixed $expectedResult
*/
public function testEOMONTHInWorksheet($expectedResult, ...$args): void
{
$arguments = new FormulaArguments(...$args);
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$argumentCells = $arguments->populateWorksheet($worksheet);
$formula = "=EOMONTH({$argumentCells})";
$result = $worksheet->setCellValue('A1', $formula)
->getCell('A1')
->getCalculatedValue();
self::assertSame($expectedResult, $result);
$spreadsheet->disconnectWorksheets();
}
public static function providerEOMONTH(): array
{
return require 'tests/data/Calculation/DateTime/EOMONTH.php';
}
/**
* @dataProvider providerUnhappyEOMONTH
*/
public function testEOMONTHUnhappyPath(string $expectedException, ...$args): void
{
$arguments = new FormulaArguments(...$args);
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$argumentCells = $arguments->populateWorksheet($worksheet);
$formula = "=EOMONTH({$argumentCells})";
$this->expectException(CalculationException::class);
$this->expectExceptionMessage($expectedException);
$worksheet->setCellValue('A1', $formula)
->getCell('A1')
->getCalculatedValue();
$spreadsheet->disconnectWorksheets();
}
public static function providerUnhappyEOMONTH(): array
{
return [
['Formula Error: Wrong number of arguments for EOMONTH() function'],
['Formula Error: Wrong number of arguments for EOMONTH() function', 22669],
];
}
public function testEOMONTHtoUnixTimestamp(): void
{
Functions::setReturnDateType(Functions::RETURNDATE_UNIX_TIMESTAMP);
$result = Month::lastDay('2012-1-26', -1);
self::assertEquals(1325289600, $result);
}
public function testEOMONTHtoDateTimeObject(): void
{
Functions::setReturnDateType(Functions::RETURNDATE_PHP_DATETIME_OBJECT);
$result = Month::lastDay('2012-1-26', -1);
// Must return an object...
self::assertIsObject($result);
// ... of the correct type
self::assertTrue(is_a($result, 'DateTimeInterface'));
// ... with the correct value
self::assertSame($result->format('d-M-Y'), '31-Dec-2011');
}
/**
* @dataProvider providerEoMonthArray
*/
public function testEoMonthArray(array $expectedResult, string $dateValues, string $methods): void
{
$calculation = Calculation::getInstance();
$formula = "=EOMONTH({$dateValues}, {$methods})";
$result = $calculation->_calculateFormulaValue($formula);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
}
public static function providerEoMonthArray(): array
{
return [
'row vector #1' => [[[44620, 44651, 45351]], '{"2022-01-01", "2022-02-12", "2024-01-15"}', '1'],
'column vector #1' => [[[44620], [44651], [45351]], '{"2022-01-01"; "2022-02-12"; "2024-01-15"}', '1'],
'matrix #1' => [[[44620, 44651], [44681, 45351]], '{"2022-01-01", "2022-02-12"; "2022-03-01", "2024-01-21"}', '1'],
'row vector #2' => [[[44592, 44620, 44651]], '"2022-02-12"', '{-1, 0, 1}'],
'column vector #2' => [[[44592], [44620], [44651]], '"2022-02-12"', '{-1; 0; 1}'],
'matrix #2' => [[[44592, 44620], [44651, 45351]], '"2022-02-12"', '{-1, 0; 1, 24}'],
];
}
}