BlueprintSchemaTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. use RocketTheme\Toolbox\Blueprints\BlueprintSchema;
  3. use RocketTheme\Toolbox\File\YamlFile;
  4. require_once 'helper.php';
  5. class BlueprintsBlueprintSchemaTest extends PHPUnit_Framework_TestCase
  6. {
  7. public function testCreation()
  8. {
  9. $blueprints = new BlueprintSchema;
  10. $this->assertEquals(
  11. [
  12. 'items' => [],
  13. 'rules' => [],
  14. 'nested' => [],
  15. 'dynamic' => [],
  16. 'filter' => ['validation' => true]
  17. ],
  18. $blueprints->getState());
  19. $this->assertEquals([], $blueprints->getDefaults());
  20. }
  21. /**
  22. * @dataProvider dataProvider
  23. */
  24. public function testLoad($test)
  25. {
  26. $input = $this->loadYaml($test);
  27. $blueprint = new BlueprintSchema;
  28. $blueprint->embed('', $input);
  29. // Save test results if they do not exist (data needs to be verified by human!)
  30. /*
  31. $resultFile = YamlFile::instance(__DIR__ . '/data/schema/state/' . $test . '.yaml');
  32. if (!$resultFile->exists()) {
  33. $resultFile->content($blueprint->getState());
  34. $resultFile->save();
  35. }
  36. */
  37. // Test 1: Internal state.
  38. $this->assertEquals($this->loadYaml($test, 'schema/state'), $blueprint->getState());
  39. // Save test results if they do not exist (data needs to be verified by human!)
  40. $resultFile = YamlFile::instance(__DIR__ . '/data/schema/init/' . $test . '.yaml');
  41. if (!$resultFile->exists()) {
  42. $resultFile->content($blueprint->init()->getState());
  43. $resultFile->save();
  44. }
  45. // Test 2: Initialize blueprint.
  46. $this->assertEquals($this->loadYaml($test, 'schema/init'), $blueprint->init()->getState());
  47. // Test 3: Default values.
  48. $this->assertEquals($this->loadYaml($test, 'schema/defaults'), $blueprint->getDefaults());
  49. // Test 4: Extra values.
  50. $this->assertEquals($this->loadYaml($test, 'schema/extra'), $blueprint->extra($this->loadYaml($test, 'input')));
  51. // Test 5: Merge data.
  52. $this->assertEquals(
  53. $this->loadYaml($test, 'schema/merge'),
  54. $blueprint->mergeData($blueprint->getDefaults(), $this->loadYaml($test, 'input'))
  55. );
  56. }
  57. public function dataProvider()
  58. {
  59. return [
  60. ['empty'],
  61. ['basic'],
  62. ];
  63. }
  64. protected function loadYaml($test, $type = 'blueprint')
  65. {
  66. $file = YamlFile::instance(__DIR__ . "/data/{$type}/{$test}.yaml");
  67. $content = $file->content();
  68. $file->free();
  69. return $content;
  70. }
  71. }