BlueprintFormTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. use RocketTheme\Toolbox\Blueprints\BlueprintForm;
  3. use RocketTheme\Toolbox\File\YamlFile;
  4. require_once 'helper.php';
  5. class BlueprintsBlueprintFormTest extends PHPUnit_Framework_TestCase
  6. {
  7. /**
  8. * @dataProvider dataProvider
  9. */
  10. public function testLoad($test)
  11. {
  12. $blueprint = new Blueprint($test);
  13. $blueprint->setOverrides(
  14. ['extends' => ['extends', 'basic']]
  15. );
  16. $blueprint->load();
  17. // Save test results if they do not exist (data needs to be verified by human!)
  18. /*
  19. $resultFile = YamlFile::instance(__DIR__ . '/data/form/items/' . $test . '.yaml');
  20. if (!$resultFile->exists()) {
  21. $resultFile->content($blueprint->toArray());
  22. $resultFile->save();
  23. }
  24. */
  25. // Test 1: Loaded form.
  26. $this->assertEquals($this->loadYaml($test, 'form/items'), $blueprint->toArray());
  27. }
  28. public function dataProvider()
  29. {
  30. return [
  31. ['empty'],
  32. ['basic'],
  33. ['import'],
  34. ['extends']
  35. ];
  36. }
  37. protected function loadYaml($test, $type = 'blueprint')
  38. {
  39. $file = YamlFile::instance(__DIR__ . "/data/{$type}/{$test}.yaml");
  40. $content = $file->content();
  41. $file->free();
  42. return $content;
  43. }
  44. }
  45. class Blueprint extends BlueprintForm
  46. {
  47. /**
  48. * @param string $filename
  49. * @return string
  50. */
  51. protected function loadFile($filename)
  52. {
  53. $file = YamlFile::instance(__DIR__ . "/data/blueprint/{$filename}.yaml");
  54. $content = $file->content();
  55. $file->free();
  56. return $content;
  57. }
  58. /**
  59. * @param string|array $path
  60. * @param string $context
  61. * @return array
  62. */
  63. protected function getFiles($path, $context = null)
  64. {
  65. if (is_string($path)) {
  66. // Resolve filename.
  67. if (isset($this->overrides[$path])) {
  68. $path = $this->overrides[$path];
  69. } else {
  70. if ($context === null) {
  71. $context = $this->context;
  72. }
  73. if ($context && $context[strlen($context)-1] !== '/') {
  74. $context .= '/';
  75. }
  76. $path = $context . $path;
  77. }
  78. }
  79. $files = (array) $path;
  80. return $files;
  81. }
  82. }