From 576801967cb345d32cf33289f44ae7a6262b1579 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Tue, 23 Jun 2026 19:30:51 -0400 Subject: [PATCH] simplify ViewTest --- tests/ViewTest.php | 135 +++++++++++++++++++++++---------------------- 1 file changed, 68 insertions(+), 67 deletions(-) diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 2ead7fe..91e7b85 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -21,55 +21,52 @@ class ViewTest extends TestCase { $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')); } 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')); } 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'); } @@ -78,30 +75,22 @@ class ViewTest extends TestCase { $output = $this->view->fetch('hello', ['name' => 'Bob']); - $this->assertEquals('Hello, Bob!', $output); + self::assertSame('Hello, Bob!', $output); } 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!'); } 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 @@ -109,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('