simplify ViewTest

580-view-class-steroids
fadrian06 1 day ago
parent a6dbd321ae
commit 576801967c

@ -21,55 +21,52 @@ class ViewTest extends TestCase
{ {
$this->view->set('test', 123); $this->view->set('test', 123);
$this->assertEquals(123, $this->view->get('test')); self::assertSame(123, $this->view->get('test'));
self::assertTrue($this->view->has('test'));
$this->assertTrue($this->view->has('test')); self::assertFalse($this->view->has('unknown'));
$this->assertTrue(!$this->view->has('unknown'));
$this->view->clear('test'); $this->view->clear('test');
$this->assertNull($this->view->get('test')); self::assertNull($this->view->get('test'));
} }
public function testMultipleVariables(): void public function testMultipleVariables(): void
{ {
$this->view->set([ $this->view->set(['test' => 123, 'foo' => 'bar']);
'test' => 123,
'foo' => 'bar'
]);
$this->assertEquals(123, $this->view->get('test')); self::assertSame(123, $this->view->get('test'));
$this->assertEquals('bar', $this->view->get('foo')); self::assertSame('bar', $this->view->get('foo'));
$this->view->clear(); $this->view->clear();
$this->assertNull($this->view->get('test')); self::assertNull($this->view->get('test'));
$this->assertNull($this->view->get('foo')); self::assertNull($this->view->get('foo'));
} }
public function testTemplateExists(): void public function testTemplateExists(): void
{ {
$this->assertTrue($this->view->exists('hello.php')); self::assertTrue($this->view->exists('hello.php'));
$this->assertTrue(!$this->view->exists('unknown.php')); self::assertFalse($this->view->exists('unknown.php'));
} }
public function testRender(): void public function testRender(): void
{ {
$this->view->render('hello', ['name' => 'Bob']); $this->view->render('hello', ['name' => 'Bob']);
$this->expectOutputString('Hello, Bob!'); self::expectOutputString('Hello, Bob!');
} }
public function testRenderBadFilePath(): void public function testRenderBadFilePath(): void
{ {
$this->expectException(Exception::class);
$exception_message = sprintf( $exception_message = sprintf(
'Template file not found: %s%sviews%sbadfile.php', 'Template file not found: %s%sviews%sbadfile.php',
__DIR__, __DIR__,
DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR DIRECTORY_SEPARATOR,
); );
$this->expectExceptionMessage($exception_message);
self::expectException(Exception::class);
self::expectExceptionMessage($exception_message);
$this->view->render('badfile'); $this->view->render('badfile');
} }
@ -78,30 +75,22 @@ class ViewTest extends TestCase
{ {
$output = $this->view->fetch('hello', ['name' => 'Bob']); $output = $this->view->fetch('hello', ['name' => 'Bob']);
$this->assertEquals('Hello, Bob!', $output); self::assertSame('Hello, Bob!', $output);
} }
public function testTemplateWithExtension(): void public function testTemplateWithExtension(): void
{ {
$this->view->set('name', 'Bob'); $this->view->render('hello.php', ['name' => 'Bob']);
$this->view->render('hello.php'); self::expectOutputString('Hello, Bob!');
$this->expectOutputString('Hello, Bob!');
} }
public function testTemplateWithCustomExtension(): void public function testTemplateWithCustomExtension(): void
{ {
$this->view->set('name', 'Bob'); self::expectOutputString('Hello world, Bob!');
$this->view->extension = '.html';
ob_start();
$this->view->render('world');
$html = ob_get_clean();
$html = str_replace(["\r\n", "\n"], '', $html);
echo $html;
$this->expectOutputString("Hello world, Bob!"); $this->view->extension = '.html';
$this->view->render('world', ['name' => 'Bob']);
} }
public function testGetTemplateAbsolutePath(): void public function testGetTemplateAbsolutePath(): void
@ -109,48 +98,64 @@ class ViewTest extends TestCase
$tmpfile = tmpfile(); $tmpfile = tmpfile();
$this->view->extension = ''; $this->view->extension = '';
$file_path = stream_get_meta_data($tmpfile)['uri']; $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 public function testE(): void
{ {
$this->expectOutputString('<script>'); $expectedString = '<script>';
self::expectOutputString($expectedString);
$result = $this->view->e('<script>'); $result = $this->view->e('<script>');
$this->assertEquals('&lt;script&gt;', $result);
self::assertSame($expectedString, $result);
} }
public function testeNoNeedToEscape(): void public function testeNoNeedToEscape(): void
{ {
$this->expectOutputString('script'); $expectedString = 'script';
$result = $this->view->e('script');
$this->assertEquals('script', $result); self::expectOutputString($expectedString);
$result = $this->view->e($expectedString);
self::assertSame($expectedString, $result);
} }
public function testNormalizePath(): void public function testNormalizePath(): void
{ {
$viewMock = new class extends View $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); return parent::normalizePath($path, $separator);
} }
}; };
$this->assertSame( self::assertSame(
'C:/xampp/htdocs/libs/Flight/core/index.php', 'C:/xampp/htdocs/libs/Flight/core/index.php',
$viewMock::normalizePath('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', 'C:\xampp\htdocs\libs\Flight\core\index.php',
$viewMock::normalizePath('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', 'C:°xampp°htdocs°libs°Flight°core°index.php',
$viewMock::normalizePath('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( public function testDoesNotPreserveVarsWhenFlagIsDisabled(
string $output, string $output,
array $renderParams, array $renderParams,
@ -158,11 +163,11 @@ class ViewTest extends TestCase
): void { ): void {
$this->view->preserveVars = false; $this->view->preserveVars = false;
$this->expectOutputString($output); self::expectOutputString($output);
$this->view->render(...$renderParams); $this->view->render(...$renderParams);
set_error_handler(function (int $code, string $message) use ($regexp): void { set_error_handler(static function (int $code, string $message) use ($regexp): void {
$this->assertMatchesRegularExpression($regexp, $message); self::assertMatchesRegularExpression($regexp, $message);
}); });
$this->view->render($renderParams[0]); $this->view->render($renderParams[0]);
@ -172,17 +177,14 @@ class ViewTest extends TestCase
public function testKeepThePreviousStateOfOneViewComponentByDefault(): void public function testKeepThePreviousStateOfOneViewComponentByDefault(): void
{ {
$html = <<<'html' $html = self::removeLineEndings(<<<'html'
<div>Hi</div> <div>Hi</div>
<div>Hi</div> <div>Hi</div>
<input type="number" /> <input type="number" />
<input type="number" /> <input type="number" />
html; // phpcs:ignore html);
// if windows replace \n with \r\n
$html = str_replace(["\n", "\r"], '', $html);
$this->expectOutputString($html); self::expectOutputString($html);
$this->view->render('myComponent', ['prop' => 'Hi']); $this->view->render('myComponent', ['prop' => 'Hi']);
$this->view->render('myComponent'); $this->view->render('myComponent');
@ -193,17 +195,14 @@ class ViewTest extends TestCase
public function testKeepThePreviousStateOfDataSettedBySetMethod(): void public function testKeepThePreviousStateOfDataSettedBySetMethod(): void
{ {
$this->view->preserveVars = false; $this->view->preserveVars = false;
$this->view->set('prop', 'bar'); $this->view->set('prop', 'bar');
$html = <<<'html' $html = self::removeLineEndings(<<<'html'
<div>qux</div> <div>qux</div>
<div>bar</div> <div>bar</div>
html; // phpcs:ignore html);
$html = str_replace(["\n", "\r"], '', $html); self::expectOutputString($html);
$this->expectOutputString($html);
$this->view->render('myComponent', ['prop' => 'qux']); $this->view->render('myComponent', ['prop' => 'qux']);
$this->view->render('myComponent'); $this->view->render('myComponent');
@ -211,18 +210,15 @@ class ViewTest extends TestCase
public static function renderDataProvider(): array public static function renderDataProvider(): array
{ {
$html1 = <<<'html' $html1 = self::removeLineEndings(<<<'html'
<div>Hi</div> <div>Hi</div>
<div></div> <div></div>
html; // phpcs:ignore html);
$html2 = <<<'html' $html2 = self::removeLineEndings(<<<'html'
<input type="number" /> <input type="number" />
<input type="text" /> <input type="text" />
html; // phpcs:ignore html);
$html1 = str_replace(["\n", "\r"], '', $html1);
$html2 = str_replace(["\n", "\r"], '', $html2);
return [ return [
[ [
@ -237,4 +233,9 @@ class ViewTest extends TestCase
], ],
]; ];
} }
private static function removeLineEndings(string $subject): string
{
return str_replace(["\r", "\n"], '', $subject);
}
} }

Loading…
Cancel
Save