diff --git a/.editorconfig b/.editorconfig index d6b66b8..6110c5b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,5 +11,5 @@ insert_final_newline = true [*.md] indent_size = 2 -[tests/views/*] +[tests/views/**/*] insert_final_newline = false diff --git a/flight/template/Component.php b/flight/template/Component.php new file mode 100644 index 0000000..83a465f --- /dev/null +++ b/flight/template/Component.php @@ -0,0 +1,20 @@ +html(); + $css = $component->css(); + $js = $component->js(); + + if ($css) { + $view .= << + $css + + html; + } + + if ($js) { + $view .= << + $js + + html; + } + + ob_end_clean(); + + break; + default: + $view = ob_get_clean(); + } + preg_match('/[a-z-]+)\s*\/>/', $view, $matches); if ($matches) { diff --git a/tests/ViewTest.php b/tests/ViewTest.php index d71e166..b649a56 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -239,24 +239,82 @@ class ViewTest extends TestCase { $view = new View(__DIR__ . '/views'); $view->preserveVars = false; - $actual = $view->fetch($page); + $actual = $view->fetch("pages/$page"); - self::assertSame(self::removeLineEndings($expected), self::removeLineEndings($actual)); + self::assertSame( + self::removeIndentation(self::removeLineEndings($expected)), + self::removeIndentation(self::removeLineEndings($actual)), + ); } public static function pagesDataProvider(): array { return [ - ['pages/page-with-component-with-old-syntax', <<<'html' - - value - - html], - ['pages/page-with-component-with-new-syntax', <<<'html' - - value - - html], + [ + 'page-with-component-with-old-syntax', + <<<'html' + my-component + html, + ], + [ + 'page-with-component-with-new-syntax', + <<<'html' + my-component + html, + ], + [ + 'page-with-component-with-subcomponent', + <<<'html' +
+ my-component-with-subcomponent + subcomponent +
+ html, + ], + [ + 'page-with-multiple-components', + <<<'html' + + html, + ], + [ + 'page-with-functional-component', + <<<'html' + my-functional-component + html, + ], + [ + 'page-with-class-component', + <<<'html' + my-class-component + html, + ], + [ + 'page-with-class-component-with-styles', + <<<'html' + + my-class-component-with-styles + + + + html, + ], + [ + 'page-with-class-component-with-scripts', + <<<'html' + my-class-component-with-scripts + + + html, + ], + ]; } @@ -264,4 +322,9 @@ class ViewTest extends TestCase { return str_replace(["\r", "\n"], '', $subject); } + + private static function removeIndentation(string $subject): string + { + return str_replace(' ', '', $subject); + } } diff --git a/tests/views/components/my-class-component-with-scripts.php b/tests/views/components/my-class-component-with-scripts.php new file mode 100644 index 0000000..280307e --- /dev/null +++ b/tests/views/components/my-class-component-with-scripts.php @@ -0,0 +1,24 @@ + + my-class-component-with-styles + + html; + } + + #[Override] + public function css(): string + { + return <<<'css' + .my-class-component-with-styles { + color: red; + } + css; + } +}; \ No newline at end of file diff --git a/tests/views/components/my-class-component.php b/tests/views/components/my-class-component.php new file mode 100644 index 0000000..be86579 --- /dev/null +++ b/tests/views/components/my-class-component.php @@ -0,0 +1,16 @@ + + my-component-with-subcomponent + + \ No newline at end of file diff --git a/tests/views/components/my-component.php b/tests/views/components/my-component.php index 6d4e150..e3ecf45 100644 --- a/tests/views/components/my-component.php +++ b/tests/views/components/my-component.php @@ -1 +1 @@ -value +my-component \ No newline at end of file diff --git a/tests/views/components/my-functional-component.php b/tests/views/components/my-functional-component.php new file mode 100644 index 0000000..0bff949 --- /dev/null +++ b/tests/views/components/my-functional-component.php @@ -0,0 +1,7 @@ + <<<'html' +my-functional-component +html; \ No newline at end of file diff --git a/tests/views/components/subcomponent.php b/tests/views/components/subcomponent.php new file mode 100644 index 0000000..8aca66b --- /dev/null +++ b/tests/views/components/subcomponent.php @@ -0,0 +1 @@ +subcomponent \ No newline at end of file diff --git a/tests/views/pages/page-with-class-component-with-scripts.php b/tests/views/pages/page-with-class-component-with-scripts.php new file mode 100644 index 0000000..0fcd56f --- /dev/null +++ b/tests/views/pages/page-with-class-component-with-scripts.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/views/pages/page-with-class-component-with-styles.php b/tests/views/pages/page-with-class-component-with-styles.php new file mode 100644 index 0000000..3ca6a35 --- /dev/null +++ b/tests/views/pages/page-with-class-component-with-styles.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/views/pages/page-with-class-component.php b/tests/views/pages/page-with-class-component.php new file mode 100644 index 0000000..76a7423 --- /dev/null +++ b/tests/views/pages/page-with-class-component.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/views/pages/page-with-component-with-new-syntax.php b/tests/views/pages/page-with-component-with-new-syntax.php index f02d75a..31ebaab 100644 --- a/tests/views/pages/page-with-component-with-new-syntax.php +++ b/tests/views/pages/page-with-component-with-new-syntax.php @@ -1,3 +1 @@ - - - + \ No newline at end of file diff --git a/tests/views/pages/page-with-component-with-old-syntax.php b/tests/views/pages/page-with-component-with-old-syntax.php index ff5cf50..1aa9702 100644 --- a/tests/views/pages/page-with-component-with-old-syntax.php +++ b/tests/views/pages/page-with-component-with-old-syntax.php @@ -1,3 +1 @@ - - render('components/my-component') ?> - +render('components/my-component') ?> \ No newline at end of file diff --git a/tests/views/pages/page-with-component-with-subcomponent.php b/tests/views/pages/page-with-component-with-subcomponent.php new file mode 100644 index 0000000..d032809 --- /dev/null +++ b/tests/views/pages/page-with-component-with-subcomponent.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/views/pages/page-with-functional-component.php b/tests/views/pages/page-with-functional-component.php new file mode 100644 index 0000000..07fcec2 --- /dev/null +++ b/tests/views/pages/page-with-functional-component.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/views/pages/page-with-multiple-components.php b/tests/views/pages/page-with-multiple-components.php new file mode 100644 index 0000000..f7836b4 --- /dev/null +++ b/tests/views/pages/page-with-multiple-components.php @@ -0,0 +1,7 @@ +
    + +
  • + +
  • + +
\ No newline at end of file