mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-27 03:15:21 +00:00
e9cf27354d
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+.
111 lines
4.5 KiB
PHP
111 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Style\ConditionalFormatting\Wizard;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Exception;
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
use PhpOffice\PhpSpreadsheet\Style\Conditional;
|
|
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class WizardFactoryTest extends TestCase
|
|
{
|
|
/**
|
|
* @var Wizard
|
|
*/
|
|
protected $wizardFactory;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$range = '$C$3:$E$5';
|
|
$this->wizardFactory = new Wizard($range);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider basicWizardFactoryProvider
|
|
*
|
|
* @psalm-param class-string<object> $expectedWizard
|
|
*/
|
|
public function testBasicWizardFactory(string $ruleType, string $expectedWizard): void
|
|
{
|
|
$wizard = $this->wizardFactory->newRule($ruleType);
|
|
self::assertInstanceOf($expectedWizard, $wizard);
|
|
}
|
|
|
|
public static function basicWizardFactoryProvider(): array
|
|
{
|
|
return [
|
|
'CellValue Wizard' => [Wizard::CELL_VALUE, Wizard\CellValue::class],
|
|
'TextValue Wizard' => [Wizard::TEXT_VALUE, Wizard\TextValue::class],
|
|
'Blanks Wizard' => [Wizard::BLANKS, Wizard\Blanks::class],
|
|
'Blanks Wizard (NOT)' => [Wizard::NOT_BLANKS, Wizard\Blanks::class],
|
|
'Errors Wizard' => [Wizard::ERRORS, Wizard\Errors::class],
|
|
'Errors Wizard (NOT)' => [Wizard::NOT_ERRORS, Wizard\Errors::class],
|
|
'Expression Wizard' => [Wizard::EXPRESSION, Wizard\Expression::class],
|
|
'DateValue Wizard' => [Wizard::DATES_OCCURRING, Wizard\DateValue::class],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider conditionalProvider
|
|
*/
|
|
public function testWizardFromConditional(string $sheetName, string $cellAddress, array $expectedWizads): void
|
|
{
|
|
$filename = 'tests/data/Style/ConditionalFormatting/CellMatcher.xlsx';
|
|
$reader = IOFactory::createReader('Xlsx');
|
|
$spreadsheet = $reader->load($filename);
|
|
$worksheet = $spreadsheet->getSheetByNameOrThrow($sheetName);
|
|
$cell = $worksheet->getCell($cellAddress);
|
|
|
|
$cfRange = $worksheet->getConditionalRange($cell->getCoordinate());
|
|
if ($cfRange === null) {
|
|
self::markTestSkipped("{$cellAddress} is not in a Conditional Format range");
|
|
}
|
|
$conditionals = $worksheet->getConditionalStyles($cfRange);
|
|
|
|
foreach ($conditionals as $index => $conditional) {
|
|
$wizard = Wizard::fromConditional($conditional);
|
|
self::assertEquals($expectedWizads[$index], get_class($wizard));
|
|
}
|
|
}
|
|
|
|
public static function conditionalProvider(): array
|
|
{
|
|
return [
|
|
'cellIs Comparison A2' => ['cellIs Comparison', 'A2', [Wizard\CellValue::class, Wizard\CellValue::class, Wizard\CellValue::class]],
|
|
'cellIs Expression A2' => ['cellIs Expression', 'A2', [Wizard\Expression::class, Wizard\Expression::class]],
|
|
'Text Expressions A2' => ['Text Expressions', 'A2', [Wizard\TextValue::class]],
|
|
'Text Expressions A8' => ['Text Expressions', 'A8', [Wizard\TextValue::class]],
|
|
'Text Expressions A14' => ['Text Expressions', 'A14', [Wizard\TextValue::class]],
|
|
'Text Expressions A20' => ['Text Expressions', 'A20', [Wizard\TextValue::class]],
|
|
'Blank Expressions A2' => ['Blank Expressions', 'A2', [Wizard\Blanks::class, Wizard\Blanks::class]],
|
|
'Error Expressions C2' => ['Error Expressions', 'C2', [Wizard\Errors::class, Wizard\Errors::class]],
|
|
'Date Expressions B10' => ['Date Expressions', 'B10', [Wizard\DateValue::class]],
|
|
'Duplicates Expressions A2' => ['Duplicates Expressions', 'A2', [Wizard\Duplicates::class, Wizard\Duplicates::class]],
|
|
];
|
|
}
|
|
|
|
public function testWizardFactoryException(): void
|
|
{
|
|
$ruleType = 'Unknown';
|
|
$this->expectException(Exception::class);
|
|
$this->expectExceptionMessage('No wizard exists for this CF rule type');
|
|
$this->wizardFactory->newRule($ruleType);
|
|
|
|
$conditional = new Conditional();
|
|
$conditional->setConditionType('UNKNOWN');
|
|
Wizard::fromConditional($conditional);
|
|
}
|
|
|
|
public function testWizardFactoryFromConditionalException(): void
|
|
{
|
|
$ruleType = 'Unknown';
|
|
$this->expectException(Exception::class);
|
|
$this->expectExceptionMessage('No wizard exists for this CF rule type');
|
|
|
|
$conditional = new Conditional();
|
|
$conditional->setConditionType($ruleType);
|
|
Wizard::fromConditional($conditional);
|
|
}
|
|
}
|