@ -14,101 +14,83 @@ class ViewTest extends TestCase
protected function setUp(): void
{
$this->view = new View();
$this->view->path = __DIR__ . '/views';
$this->view = new View(__DIR__ . '/views');
}
// Set template variables
public function testVariables(): void
{
$this->view->set('test', 123);
$this->assertEquals(123, $this->view->get('test'));
$this->assertTrue($this->view->has('test'));
$this->assertTrue(!$this->view->has('unknown'));
self::assertSame(123, $this->view->get('test'));
self::assertTrue($this->view->has('test'));
self::assertFalse($this->view->has('unknown'));
$this->view->clear('test');
$this-> assertNull($this->view->get('test'));
self:: assertNull($this->view->get('test'));
}
public function testMultipleVariables(): void
{
$this->view->set([
'test' => 123,
'foo' => 'bar'
]);
$this->view->set(['test' => 123, 'foo' => 'bar']);
$this->assertEquals (123, $this->view->get('test'));
$this->assertEquals ('bar', $this->view->get('foo'));
self::assertSame(123, $this->view->get('test'));
self::assertSame('bar', $this->view->get('foo'));
$this->view->clear();
$this-> assertNull($this->view->get('test'));
$this-> assertNull($this->view->get('foo'));
self:: assertNull($this->view->get('test'));
self:: assertNull($this->view->get('foo'));
}
// Check if template files exist
public function testTemplateExists(): void
{
$this-> assertTrue($this->view->exists('hello.php'));
$this->assertTrue(! $this->view->exists('unknown.php'));
self:: assertTrue($this->view->exists('hello.php'));
self::assertFalse( $this->view->exists('unknown.php'));
}
// Render a template
public function testRender(): void
{
$this->view->render('hello', ['name' => 'Bob']);
$this-> expectOutputString('Hello, Bob!');
self:: expectOutputString('Hello, Bob!');
}
public function testRenderBadFilePath(): void
{
$this->expectException(Exception::class);
$exception_message = sprintf(
'Template file not found: %s%sviews%sbadfile.php',
__DIR__,
DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR
DIRECTORY_SEPARATOR,
);
$this->expectExceptionMessage($exception_message);
self::expectException(Exception::class);
self::expectExceptionMessage($exception_message);
$this->view->render('badfile');
}
// Fetch template output
public function testFetch(): void
{
$output = $this->view->fetch('hello', ['name' => 'Bob']);
$this->assertEquals ('Hello, Bob!', $output);
self::assertSame ('Hello, Bob!', $output);
}
// Default extension
public function testTemplateWithExtension(): void
{
$this->view->set('name', 'Bob' );
$this->view->render('hello.php', ['name' => 'Bob'] );
$this->view->render('hello.php');
$this->expectOutputString('Hello, Bob!');
self::expectOutputString('Hello, Bob!');
}
// Custom extension
public function testTemplateWithCustomExtension(): void
{
$this->view->set('name', 'Bob');
$this->view->extension = '.html';
ob_start();
$this->view->render('world');
$html = ob_get_clean();
$html = str_replace(["\r\n", "\n"], '', $html);
echo $html;
self::expectOutputString('Hello world, Bob!');
$this->expectOutputString("Hello world, Bob!");
$this->view->extension = '.html';
$this->view->render('world', ['name' => 'Bob']);
}
public function testGetTemplateAbsolutePath(): void
@ -116,48 +98,64 @@ class ViewTest extends TestCase
$tmpfile = tmpfile();
$this->view->extension = '';
$file_path = stream_get_meta_data($tmpfile)['uri'];
$this->assertEquals($file_path, $this->view->getTemplate($file_path));
self::assertSame($file_path, $this->view->getTemplate($file_path));
}
public function testE(): void
{
$this->expectOutputString('< script> ');
$expectedString = '< script> ';
self::expectOutputString($expectedString);
$result = $this->view->e('< script > ' ) ;
$this->assertEquals('< script> ', $result);
self::assertSame($expectedString, $result);
}
public function testeNoNeedToEscape(): void
{
$this->expectOutputString('script');
$result = $this->view->e('script');
$this->assertEquals('script', $result);
$expectedString = 'script';
self::expectOutputString($expectedString);
$result = $this->view->e($expectedString);
self::assertSame($expectedString, $result);
}
public function testNormalizePath(): void
{
$viewMock = new class extends View
{
public static function normalizePath(string $path, string $separator = DIRECTORY_SEPARATOR): string
{
public static function normalizePath(
string $path,
string $separator = DIRECTORY_SEPARATOR
): string {
return parent::normalizePath($path, $separator);
}
};
$this-> assertSame(
self:: assertSame(
'C:/xampp/htdocs/libs/Flight/core/index.php',
$viewMock::normalizePath('C:\xampp\htdocs\libs\Flight/core/index.php', '/')
);
$this->assertSame(
self::assertSame(
'C:\xampp\htdocs\libs\Flight\core\index.php',
$viewMock::normalizePath('C:/xampp/htdocs/libs/Flight\core\index.php', '\\')
);
$this->assertSame(
self::assertSame(
'C:°xampp°htdocs°libs°Flight°core°index.php',
$viewMock::normalizePath('C:/xampp/htdocs/libs/Flight\core\index.php', '°')
);
}
/** @dataProvider renderDataProvider */
/**
* @dataProvider renderDataProvider
* @param array{string, array< string , mixed > } $renderParams
*/
public function testDoesNotPreserveVarsWhenFlagIsDisabled(
string $output,
array $renderParams,
@ -165,11 +163,11 @@ class ViewTest extends TestCase
): void {
$this->view->preserveVars = false;
$this-> expectOutputString($output);
self:: expectOutputString($output);
$this->view->render(...$renderParams);
set_error_handler(function (int $code, string $message) use ($regexp): void {
$this-> assertMatchesRegularExpression($regexp, $message);
set_error_handler(static function (int $code, string $message) use ($regexp): void {
self:: assertMatchesRegularExpression($regexp, $message);
});
$this->view->render($renderParams[0]);
@ -179,17 +177,14 @@ class ViewTest extends TestCase
public function testKeepThePreviousStateOfOneViewComponentByDefault(): void
{
$html = < < < 'html'
$html = self::removeLineEndings( < < < 'html'
< div > Hi< / div >
< div > Hi< / div >
< input type = "number" / >
< input type = "number" / >
html; // phpcs:ignore
// if windows replace \n with \r\n
$html = str_replace(["\n", "\r"], '', $html);
html);
$this-> expectOutputString($html);
self::expectOutputString($html);
$this->view->render('myComponent', ['prop' => 'Hi']);
$this->view->render('myComponent');
@ -200,17 +195,14 @@ class ViewTest extends TestCase
public function testKeepThePreviousStateOfDataSettedBySetMethod(): void
{
$this->view->preserveVars = false;
$this->view->set('prop', 'bar');
$html = < < < 'html'
$html = self::removeLineEndings( < < < 'html'
< div > qux< / div >
< div > bar< / div >
html; // phpcs:ignore
html) ;
$html = str_replace(["\n", "\r"], '', $html);
$this->expectOutputString($html);
self::expectOutputString($html);
$this->view->render('myComponent', ['prop' => 'qux']);
$this->view->render('myComponent');
@ -218,18 +210,15 @@ class ViewTest extends TestCase
public static function renderDataProvider(): array
{
$html1 = < < < 'html'
$html1 = self::removeLineEndings( < < < 'html'
< div > Hi< / div >
< div > < / div >
html; // phpcs:ignore
html) ;
$html2 = < < < 'html'
$html2 = self::removeLineEndings( < < < 'html'
< input type = "number" / >
< input type = "text" / >
html; // phpcs:ignore
$html1 = str_replace(["\n", "\r"], '', $html1);
$html2 = str_replace(["\n", "\r"], '', $html2);
html);
return [
[
@ -244,4 +233,250 @@ class ViewTest extends TestCase
],
];
}
/** @dataProvider pagesDataProvider */
public function testItRendersComponent(
string $page,
string $expected,
array $props = []
): void {
$view = new View(__DIR__ . '/views');
$view->preserveVars = false;
$actual = $view->fetch("pages/$page", $props);
self::assertSame(
self::removeIndentation(self::removeLineEndings($expected)),
self::removeIndentation(self::removeLineEndings($actual)),
);
}
public function testItRendersComponentFromAnotherPath(): void
{
$view = new View(__DIR__ . '/views', 'f', __DIR__ . '/components');
$view->preserveVars = false;
$actual = $view->fetch('pages/page-with-component-from-another-path');
$expected = 'my-component-from-another-path';
self::assertSame(
self::removeIndentation(self::removeLineEndings($expected)),
self::removeIndentation(self::removeLineEndings($actual)),
);
}
public static function pagesDataProvider(): array
{
return [
[
'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'
< div >
my-component-with-subcomponent
subcomponent
< / div >
html,
],
[
'page-with-multiple-components',
< < < 'html'
< ul >
< li > my-component< / li >
< li > my-component< / li >
< / ul >
html,
],
[
'page-with-functional-component',
< < < 'html'
my-functional-component
html,
],
[
'page-with-class-component',
< < < 'html'
my-class-component
html,
],
[
'page-with-functional-component-with-props',
< < < 'html'
functional-component-with-props: Astronaut Victoria
html,
['name' => 'Victoria', 'occupation' => 'Astronaut'],
],
[
'page-with-functional-component-with-individual-props',
< < < 'html'
functional-component-with-individual-props: Astronaut Victoria
html,
['name' => 'Victoria', 'occupation' => 'Astronaut'],
],
[
'page-with-class-component-with-props',
< < < 'html'
class-component-with-props: Astronaut Victoria
html,
['name' => 'Victoria', 'occupation' => 'Astronaut'],
],
[
'page-with-class-component-with-styles',
< < < 'html'
< span class = "my-class-component-with-styles" >
my-class-component-with-styles
< / span >
< style >
.my-class-component-with-styles {
color: red;
}
< / style >
html,
],
[
'page-with-class-component-with-custom-style-tag',
< < < 'html'
< span class = "my-class-component-with-custom-style-tag" >
my-class-component-with-custom-style-tag
< / span >
< style media = "print" data-component = "my-class-component-with-custom-style-tag" >
.my-class-component-with-custom-style-tag {
color: purple;
}
< / style >
html,
],
[
'page-with-class-component-with-scripts',
< < < 'html'
my-class-component-with-scripts
< script > console . log ( 'my-class-component-with-scripts' ) < / script >
html,
],
[
'page-with-class-component-with-custom-script-tag',
< < < 'html'
my-class-component-with-custom-script-tag
< script type = "module" data-component = "my-class-component-with-custom-script-tag" >
console.log('my-class-component-with-custom-script-tag')
< / script >
html,
],
[
'page-with-class-component-that-extends-another-class-component',
< < < 'html'
another-class-component extended by my-class-component-that-extends-another-class-component
html,
],
[
'page-with-component-with-one-prop',
< < < 'html'
< html >
< body >
< h1 > Hello, James< / h1 >
< / body >
< / html >
html,
['name' => 'James'],
],
[
'page-with-component-with-one-prop',
< < < 'html'
< html >
< body >
< h1 > Hello, Victoria< / h1 >
< / body >
< / html >
html,
['name' => 'Victoria'],
],
[
'page-with-component-with-two-props',
< < < 'html'
< html >
< body >
< h1 > Hello, Astronaut Victoria< / h1 >
< / body >
< / html >
html,
['name' => 'Victoria', 'occupation' => 'Astronaut'],
],
[
'page-with-three-different-components',
< < < 'html'
my-component
my-functional-component
my-class-component
html,
],
[
'page-with-component-with-prop-which-value-contains-spaces-and-numbers',
< < < 'html'
< h1 > Hello, Constantine 1st the Great< / h1 >
html,
['name' => 'Constantine 1st the Great'],
],
[
'page-two-same-components-with-one-style-and-script-tag',
< < < 'html'
< span class = "my-class-component-with-styles" >
my-class-component-with-styles
< / span >
< style >
.my-class-component-with-styles {
color: red;
}
< / style >
< span class = "my-class-component-with-styles" >
my-class-component-with-styles
< / span >
my-class-component-with-scripts
< script > console . log ( 'my-class-component-with-scripts' ) < / script >
my-class-component-with-scripts
html,
],
];
}
/** @dataProvider prefixesDataProvider */
public function testRenderComponentsWithDifferentPrefixes(string $prefix): void
{
$view = new View(__DIR__ . '/views', $prefix);
$view->preserveVars = false;
$actual = $view->fetch('pages/page-with-component-with-custom-prefix', compact('prefix'));
$expected = 'my-component';
self::assertSame(
self::removeIndentation(self::removeLineEndings($expected)),
self::removeIndentation(self::removeLineEndings($actual)),
);
}
public static function prefixesDataProvider(): array
{
return [
['x'],
];
}
private static function removeLineEndings(string $subject): string
{
return str_replace(["\r", "\n"], '', $subject);
}
private static function removeIndentation(string $subject): string
{
return preg_replace('/\s{2,}/', '', $subject);
}
}