view = new View(__DIR__ . '/views'); } 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')); $this->view->clear('test'); $this->assertNull($this->view->get('test')); } public function testMultipleVariables(): void { $this->view->set([ 'test' => 123, 'foo' => 'bar' ]); $this->assertEquals(123, $this->view->get('test')); $this->assertEquals('bar', $this->view->get('foo')); $this->view->clear(); $this->assertNull($this->view->get('test')); $this->assertNull($this->view->get('foo')); } public function testTemplateExists(): void { $this->assertTrue($this->view->exists('hello.php')); $this->assertTrue(!$this->view->exists('unknown.php')); } public function testRender(): void { $this->view->render('hello', ['name' => 'Bob']); $this->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 ); $this->expectExceptionMessage($exception_message); $this->view->render('badfile'); } public function testFetch(): void { $output = $this->view->fetch('hello', ['name' => 'Bob']); $this->assertEquals('Hello, Bob!', $output); } public function testTemplateWithExtension(): void { $this->view->set('name', 'Bob'); $this->view->render('hello.php'); $this->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; $this->expectOutputString("Hello world, Bob!"); } public function testGetTemplateAbsolutePath(): void { $tmpfile = tmpfile(); $this->view->extension = ''; $file_path = stream_get_meta_data($tmpfile)['uri']; $this->assertEquals($file_path, $this->view->getTemplate($file_path)); } public function testE(): void { $this->expectOutputString('<script>'); $result = $this->view->e('