mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-17 05:26:45 +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.
168 lines
5.5 KiB
PHP
168 lines
5.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\CellIterator;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\RowIterator;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class RowIteratorEmptyTest extends TestCase
|
|
{
|
|
private static function getPopulatedSheet(Spreadsheet $spreadsheet): Worksheet
|
|
{
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->setCellValueExplicit('A1', 'Hello World', DataType::TYPE_STRING);
|
|
$sheet->setCellValueExplicit('B3', null, DataType::TYPE_NULL);
|
|
$sheet->setCellValueExplicit('B4', '', DataType::TYPE_STRING);
|
|
$sheet->setCellValueExplicit('B5', null, DataType::TYPE_NULL);
|
|
$sheet->setCellValueExplicit('C5', '', DataType::TYPE_STRING);
|
|
$sheet->setCellValueExplicit('B6', null, DataType::TYPE_NULL);
|
|
$sheet->setCellValueExplicit('C6', 'PHP', DataType::TYPE_STRING);
|
|
$sheet->setCellValueExplicit('B7', '', DataType::TYPE_STRING);
|
|
$sheet->setCellValueExplicit('C7', 'PHP', DataType::TYPE_STRING);
|
|
$sheet->setCellValueExplicit('B8', null, DataType::TYPE_NULL);
|
|
$sheet->setCellValueExplicit('C8', '', DataType::TYPE_STRING);
|
|
$sheet->setCellValueExplicit('D8', 'PHP', DataType::TYPE_STRING);
|
|
|
|
return $sheet;
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('emptyRowBasic')]
|
|
public function testIteratorEmptyRow(int $rowId, bool $expectedEmpty): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = self::getPopulatedSheet($spreadsheet);
|
|
$iterator = new RowIterator($sheet, 1, 9);
|
|
$iterator->seek($rowId);
|
|
$row = $iterator->current();
|
|
|
|
$isEmpty = $row->isEmpty();
|
|
self::assertSame($expectedEmpty, $isEmpty);
|
|
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public static function emptyRowBasic(): array
|
|
{
|
|
return [
|
|
[1, false],
|
|
[2, true],
|
|
[3, false],
|
|
[4, false],
|
|
[5, false],
|
|
[6, false],
|
|
[7, false],
|
|
[8, false],
|
|
[9, true],
|
|
];
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('emptyRowNullAsEmpty')]
|
|
public function testIteratorEmptyRowWithNull(int $rowId, bool $expectedEmpty): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = self::getPopulatedSheet($spreadsheet);
|
|
$iterator = new RowIterator($sheet, 1, 9);
|
|
$iterator->seek($rowId);
|
|
$row = $iterator->current();
|
|
$isEmpty = $row->isEmpty(CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL);
|
|
self::assertSame($expectedEmpty, $isEmpty);
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public static function emptyRowNullAsEmpty(): array
|
|
{
|
|
return [
|
|
[1, false],
|
|
[2, true],
|
|
[3, true],
|
|
[4, false],
|
|
[5, false],
|
|
[6, false],
|
|
[7, false],
|
|
[8, false],
|
|
[9, true],
|
|
];
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('emptyRowEmptyStringAsEmpty')]
|
|
public function testIteratorEmptyRowWithEmptyString(int $rowId, bool $expectedEmpty): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = self::getPopulatedSheet($spreadsheet);
|
|
$iterator = new RowIterator($sheet, 1, 9);
|
|
$iterator->seek($rowId);
|
|
$row = $iterator->current();
|
|
$isEmpty = $row->isEmpty(CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL);
|
|
self::assertSame($expectedEmpty, $isEmpty);
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public static function emptyRowEmptyStringAsEmpty(): array
|
|
{
|
|
return [
|
|
[1, false],
|
|
[2, true],
|
|
[3, false],
|
|
[4, true],
|
|
[5, false],
|
|
[6, false],
|
|
[7, false],
|
|
[8, false],
|
|
[9, true],
|
|
];
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('emptyRowNullAndEmptyStringAsEmpty')]
|
|
public function testIteratorEmptyRowWithNullAndEmptyString(int $rowId, bool $expectedEmpty): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = self::getPopulatedSheet($spreadsheet);
|
|
$iterator = new RowIterator($sheet, 1, 9);
|
|
$iterator->seek($rowId);
|
|
$row = $iterator->current();
|
|
$isEmpty = $row->isEmpty(
|
|
CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL | CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL
|
|
);
|
|
self::assertSame($expectedEmpty, $isEmpty);
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public static function emptyRowNullAndEmptyStringAsEmpty(): array
|
|
{
|
|
return [
|
|
[1, false],
|
|
[2, true],
|
|
[3, true],
|
|
[4, true],
|
|
[5, true],
|
|
[6, false],
|
|
[7, false],
|
|
[8, false],
|
|
[9, true],
|
|
];
|
|
}
|
|
|
|
public function testIteratorEmptyRowWithColumnLimit(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = self::getPopulatedSheet($spreadsheet);
|
|
$sheet->setCellValue('E3', 'NO LONGER EMPTY');
|
|
|
|
$iterator = new RowIterator($sheet, 3, 3);
|
|
$row = $iterator->current();
|
|
|
|
$isEmpty = $row->isEmpty(CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL);
|
|
self::assertFalse($isEmpty);
|
|
$isEmpty = $row->isEmpty(CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL, 'A', 'D');
|
|
self::assertTrue($isEmpty);
|
|
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
}
|