BlueprintTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. use Grav\Common\Config\Config;
  3. use Grav\Common\Data\Blueprint;
  4. use Grav\Common\Grav;
  5. /**
  6. * Class InstallCommandTest
  7. */
  8. class BlueprintTest extends \Codeception\TestCase\Test
  9. {
  10. /**
  11. */
  12. public function testValidateStrict(): void
  13. {
  14. $blueprint = $this->loadBlueprint('strict');
  15. $blueprint->validate(['test' => 'string']);
  16. }
  17. /**
  18. * @depends testValidateStrict
  19. */
  20. public function testValidateStrictRequired(): void
  21. {
  22. $blueprint = $this->loadBlueprint('strict');
  23. $this->expectException(\Grav\Common\Data\ValidationException::class);
  24. $blueprint->validate([]);
  25. }
  26. /**
  27. * @depends testValidateStrict
  28. */
  29. public function testValidateStrictExtra(): void
  30. {
  31. $blueprint = $this->loadBlueprint('strict');
  32. $blueprint->validate(['test' => 'string', 'wrong' => 'field']);
  33. }
  34. /**
  35. * @depends testValidateStrict
  36. */
  37. public function testValidateStrictExtraException(): void
  38. {
  39. $blueprint = $this->loadBlueprint('strict');
  40. /** @var Config $config */
  41. $config = Grav::instance()['config'];
  42. $var = 'system.strict_mode.blueprint_strict_compat';
  43. $config->set($var, false);
  44. $this->expectException(\Grav\Common\Data\ValidationException::class);
  45. $blueprint->validate(['test' => 'string', 'wrong' => 'field']);
  46. $config->set($var, true);
  47. }
  48. /**
  49. * @param string $filename
  50. * @return Blueprint
  51. */
  52. protected function loadBlueprint($filename): Blueprint
  53. {
  54. $blueprint = new Blueprint('strict');
  55. $blueprint->setContext(dirname(__DIR__, 3). '/data/blueprints');
  56. $blueprint->load()->init();
  57. return $blueprint;
  58. }
  59. }