mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-19 14:36:48 +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+.
206 lines
8.2 KiB
PHP
206 lines
8.2 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xls;
|
|
use PhpOffice\PhpSpreadsheet\Style\Border;
|
|
use PhpOffice\PhpSpreadsheet\Style\Conditional;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class ConditionalFormattingBasicTest extends TestCase
|
|
{
|
|
/**
|
|
* @dataProvider conditionalFormattingProvider
|
|
*/
|
|
public function testReadConditionalFormatting(string $expectedRange, array $expectedRules): void
|
|
{
|
|
$filename = 'tests/data/Reader/XLS/CF_Basic_Comparisons.xls';
|
|
$reader = new Xls();
|
|
$spreadsheet = $reader->load($filename);
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$hasConditionalStyles = $sheet->conditionalStylesExists($expectedRange);
|
|
self::assertTrue($hasConditionalStyles);
|
|
|
|
$conditionalStyles = $sheet->getConditionalStyles($expectedRange);
|
|
|
|
foreach ($conditionalStyles as $index => $conditionalStyle) {
|
|
self::assertSame($expectedRules[$index]['type'], $conditionalStyle->getConditionType());
|
|
self::assertSame($expectedRules[$index]['operator'], $conditionalStyle->getOperatorType());
|
|
self::assertSame($expectedRules[$index]['conditions'], $conditionalStyle->getConditions());
|
|
}
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public static function conditionalFormattingProvider(): array
|
|
{
|
|
return [
|
|
[
|
|
'A2:E5',
|
|
[
|
|
[
|
|
'type' => Conditional::CONDITION_CELLIS,
|
|
'operator' => Conditional::OPERATOR_EQUAL,
|
|
'conditions' => [
|
|
0,
|
|
],
|
|
],
|
|
[
|
|
'type' => Conditional::CONDITION_CELLIS,
|
|
'operator' => Conditional::OPERATOR_GREATERTHAN,
|
|
'conditions' => [
|
|
0,
|
|
],
|
|
],
|
|
[
|
|
'type' => Conditional::CONDITION_CELLIS,
|
|
'operator' => Conditional::OPERATOR_LESSTHAN,
|
|
'conditions' => [
|
|
0,
|
|
],
|
|
],
|
|
],
|
|
],
|
|
[
|
|
'A10:E13',
|
|
[
|
|
[
|
|
'type' => Conditional::CONDITION_CELLIS,
|
|
'operator' => Conditional::OPERATOR_EQUAL,
|
|
'conditions' => [
|
|
'$H$9',
|
|
],
|
|
],
|
|
[
|
|
'type' => Conditional::CONDITION_CELLIS,
|
|
'operator' => Conditional::OPERATOR_GREATERTHAN,
|
|
'conditions' => [
|
|
'$H$9',
|
|
],
|
|
],
|
|
[
|
|
'type' => Conditional::CONDITION_CELLIS,
|
|
'operator' => Conditional::OPERATOR_LESSTHAN,
|
|
'conditions' => [
|
|
'$H$9',
|
|
],
|
|
],
|
|
],
|
|
],
|
|
[
|
|
'A18:A20',
|
|
[
|
|
[
|
|
'type' => Conditional::CONDITION_CELLIS,
|
|
'operator' => Conditional::OPERATOR_BETWEEN,
|
|
'conditions' => [
|
|
'$B1',
|
|
'$C1',
|
|
],
|
|
],
|
|
],
|
|
],
|
|
[
|
|
'A24:E27',
|
|
[
|
|
[
|
|
'type' => Conditional::CONDITION_CELLIS,
|
|
'operator' => Conditional::OPERATOR_BETWEEN,
|
|
'conditions' => [
|
|
'AVERAGE($A$24:$E$27)-STDEV($A$24:$E$27)',
|
|
'AVERAGE($A$24:$E$27)+STDEV($A$24:$E$27)',
|
|
],
|
|
],
|
|
[
|
|
'type' => Conditional::CONDITION_CELLIS,
|
|
'operator' => Conditional::OPERATOR_GREATERTHAN,
|
|
'conditions' => [
|
|
'AVERAGE($A$24:$E$27)+STDEV($A$24:$E$27)',
|
|
],
|
|
],
|
|
[
|
|
'type' => Conditional::CONDITION_CELLIS,
|
|
'operator' => Conditional::OPERATOR_LESSTHAN,
|
|
'conditions' => [
|
|
'AVERAGE($A$24:$E$27)-STDEV($A$24:$E$27)',
|
|
],
|
|
],
|
|
],
|
|
],
|
|
[
|
|
'A31:A33',
|
|
[
|
|
[
|
|
'type' => Conditional::CONDITION_CELLIS,
|
|
'operator' => Conditional::OPERATOR_EQUAL,
|
|
'conditions' => [
|
|
'"LOVE"',
|
|
],
|
|
],
|
|
[
|
|
'type' => Conditional::CONDITION_CELLIS,
|
|
'operator' => Conditional::OPERATOR_EQUAL,
|
|
'conditions' => [
|
|
'"PHP"',
|
|
],
|
|
],
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
public function testReadConditionalFormattingStyles(): void
|
|
{
|
|
$filename = 'tests/data/Reader/XLS/CF_Basic_Comparisons.xls';
|
|
$reader = new Xls();
|
|
$spreadsheet = $reader->load($filename);
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$expectedRange = 'A2:E5';
|
|
$hasConditionalStyles = $sheet->conditionalStylesExists($expectedRange);
|
|
self::assertTrue($hasConditionalStyles);
|
|
|
|
$conditionalStyles = $sheet->getConditionalStyles($expectedRange);
|
|
self::assertCount(3, $conditionalStyles);
|
|
|
|
$style = $conditionalStyles[0]->getStyle();
|
|
$font = $style->getFont();
|
|
self::assertSame('FF0000FF', $font->getColor()->getArgb());
|
|
self::assertNull($font->getItalic());
|
|
self::assertNull($font->getStrikethrough());
|
|
// Fill not handled correctly - forget for now
|
|
$borders = $style->getBorders();
|
|
self::assertSame(Border::BORDER_OMIT, $borders->getLeft()->getBorderStyle());
|
|
self::assertSame(Border::BORDER_OMIT, $borders->getRight()->getBorderStyle());
|
|
self::assertSame(Border::BORDER_OMIT, $borders->getTop()->getBorderStyle());
|
|
self::assertSame(Border::BORDER_OMIT, $borders->getBottom()->getBorderStyle());
|
|
self::assertNull($style->getNumberFormat()->getFormatCode());
|
|
|
|
$style = $conditionalStyles[1]->getStyle();
|
|
$font = $style->getFont();
|
|
self::assertSame('FF800000', $font->getColor()->getArgb());
|
|
self::assertNull($font->getItalic());
|
|
self::assertNull($font->getStrikethrough());
|
|
// Fill not handled correctly - forget for now
|
|
$borders = $style->getBorders();
|
|
self::assertSame(Border::BORDER_OMIT, $borders->getLeft()->getBorderStyle());
|
|
self::assertSame(Border::BORDER_OMIT, $borders->getRight()->getBorderStyle());
|
|
self::assertSame(Border::BORDER_OMIT, $borders->getTop()->getBorderStyle());
|
|
self::assertSame(Border::BORDER_OMIT, $borders->getBottom()->getBorderStyle());
|
|
self::assertNull($style->getNumberFormat()->getFormatCode());
|
|
|
|
$style = $conditionalStyles[2]->getStyle();
|
|
$font = $style->getFont();
|
|
self::assertSame('FF00FF00', $font->getColor()->getArgb());
|
|
self::assertNull($font->getItalic());
|
|
self::assertNull($font->getStrikethrough());
|
|
// Fill not handled correctly - forget for now
|
|
$borders = $style->getBorders();
|
|
self::assertSame(Border::BORDER_OMIT, $borders->getLeft()->getBorderStyle());
|
|
self::assertSame(Border::BORDER_OMIT, $borders->getRight()->getBorderStyle());
|
|
self::assertSame(Border::BORDER_OMIT, $borders->getTop()->getBorderStyle());
|
|
self::assertSame(Border::BORDER_OMIT, $borders->getBottom()->getBorderStyle());
|
|
self::assertNull($style->getNumberFormat()->getFormatCode());
|
|
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
}
|