DataValidatorTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Cell;
  3. use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. use PHPUnit\Framework\TestCase;
  6. class DataValidatorTest extends TestCase
  7. {
  8. public function testNoValidation(): void
  9. {
  10. $spreadsheet = new Spreadsheet();
  11. $sheet = $spreadsheet->getActiveSheet();
  12. $testCell = $sheet->getCell('A1');
  13. self::assertTrue($testCell->hasValidValue(), 'a cell without any validation data is always valid');
  14. }
  15. public function testUnsupportedType(): void
  16. {
  17. $spreadsheet = new Spreadsheet();
  18. $sheet = $spreadsheet->getActiveSheet();
  19. $testCell = $sheet->getCell('A1');
  20. $validation = $testCell->getDataValidation();
  21. $validation->setType(DataValidation::TYPE_CUSTOM);
  22. $validation->setAllowBlank(true);
  23. self::assertFalse($testCell->hasValidValue(), 'cannot assert that value is valid when the validation type is not supported');
  24. }
  25. public function testList(): void
  26. {
  27. $spreadsheet = new Spreadsheet();
  28. $sheet = $spreadsheet->getActiveSheet();
  29. $testCell = $sheet->getCell('A1');
  30. $validation = $testCell->getDataValidation();
  31. $validation->setType(DataValidation::TYPE_LIST);
  32. // blank value
  33. $testCell->setValue('');
  34. $validation->setAllowBlank(true);
  35. self::assertTrue($testCell->hasValidValue(), 'cell can be empty');
  36. $validation->setAllowBlank(false);
  37. self::assertFalse($testCell->hasValidValue(), 'cell can not be empty');
  38. // inline list
  39. $validation->setFormula1('"yes,no"');
  40. $testCell->setValue('foo');
  41. self::assertFalse($testCell->hasValidValue(), "cell value ('foo') is not allowed");
  42. $testCell->setValue('yes');
  43. self::assertTrue($testCell->hasValidValue(), "cell value ('yes') has to be allowed");
  44. // list from cells
  45. $sheet->getCell('B1')->setValue(5);
  46. $sheet->getCell('B2')->setValue(6);
  47. $sheet->getCell('B3')->setValue(7);
  48. $testCell = $sheet->getCell('A1'); // redefine $testCell, because it has broken coordinates after using other cells
  49. $validation->setFormula1('B1:B3');
  50. $testCell->setValue('10');
  51. self::assertFalse($testCell->hasValidValue(), "cell value ('10') is not allowed");
  52. $testCell = $sheet->getCell('A1'); // redefine $testCell, because it has broken coordinates after using other cells
  53. $testCell->setValue('5');
  54. self::assertTrue($testCell->hasValidValue(), "cell value ('5') has to be allowed");
  55. $testCell = $sheet->getCell('A1'); // redefine $testCell, because it has broken coordinates after using other cells
  56. $validation->setFormula1('broken : cell : coordinates');
  57. self::assertFalse($testCell->hasValidValue(), 'invalid formula should not throw exceptions');
  58. }
  59. }