|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls; |
| 4 | + |
| 5 | +use PhpOffice\PhpSpreadsheet\Cell\DataValidation; |
| 6 | +use PhpOffice\PhpSpreadsheet\Reader\Xls; |
| 7 | +use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +class DataValidationTest extends TestCase |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var Worksheet |
| 14 | + */ |
| 15 | + protected $sheet; |
| 16 | + |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + $filename = 'tests/data/Reader/XLS/DataValidation.xls'; |
| 20 | + $reader = new Xls(); |
| 21 | + $spreadsheet = $reader->load($filename); |
| 22 | + $this->sheet = $spreadsheet->getActiveSheet(); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @dataProvider dataValidationProvider |
| 27 | + */ |
| 28 | + public function testDataValidation(string $expectedRange, array $expectedRule): void |
| 29 | + { |
| 30 | + $hasDataValidation = $this->sheet->dataValidationExists($expectedRange); |
| 31 | + self::assertTrue($hasDataValidation); |
| 32 | + |
| 33 | + $dataValidation = $this->sheet->getDataValidation($expectedRange); |
| 34 | + self::assertSame($expectedRule['type'], $dataValidation->getType()); |
| 35 | + self::assertSame($expectedRule['operator'], $dataValidation->getOperator()); |
| 36 | + self::assertSame($expectedRule['formula'], $dataValidation->getFormula1()); |
| 37 | + } |
| 38 | + |
| 39 | + public function dataValidationProvider(): array |
| 40 | + { |
| 41 | + return [ |
| 42 | + [ |
| 43 | + 'B2', |
| 44 | + [ |
| 45 | + 'type' => DataValidation::TYPE_WHOLE, |
| 46 | + 'operator' => DataValidation::OPERATOR_GREATERTHANOREQUAL, |
| 47 | + 'formula' => '18', |
| 48 | + ], |
| 49 | + ], |
| 50 | + [ |
| 51 | + 'B3', |
| 52 | + [ |
| 53 | + 'type' => DataValidation::TYPE_LIST, |
| 54 | + 'operator' => DataValidation::OPERATOR_BETWEEN, |
| 55 | + 'formula' => '"Blocked,Pending,Approved"', |
| 56 | + ], |
| 57 | + ], |
| 58 | + ]; |
| 59 | + } |
| 60 | +} |
0 commit comments