From 46f7228ea59034541d92b0843d01f547554b1cd1 Mon Sep 17 00:00:00 2001 From: Lionel Vinceslas Date: Sun, 9 Aug 2020 14:01:25 +0200 Subject: [PATCH] :ambulance: Fix minor issue --- src/HtmlTemplate.php | 26 +++++++++++--------------- tests/HtmlTemplateTest.php | 16 ++++++++++++---- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/HtmlTemplate.php b/src/HtmlTemplate.php index 3a18bee..8a1aa0e 100755 --- a/src/HtmlTemplate.php +++ b/src/HtmlTemplate.php @@ -57,10 +57,11 @@ class HtmlTemplate */ public function __construct(string $filepath) { - if (!is_string($filepath) || !file_exists($filepath)) - throw new \InvalidArgumentException('Invalid Html template filepath !'); + if (!file_exists($filepath)) + throw new \InvalidArgumentException("Error : path '$filepath' does not exist !"); if (!is_readable($filepath)) - throw new \RuntimeException('Html template file not readable !'); + throw new \RuntimeException("Error : file '$filepath' not readable !"); + $this->filepath = $filepath; $this->html = file_get_contents($filepath); } @@ -90,10 +91,11 @@ public function getFilepath(): string */ public function setFilepath(string $filepath): bool { - if (!is_string($filepath) || !file_exists($filepath)) - throw new \InvalidArgumentException('Invalid Html template filepath !'); + if (!file_exists($filepath)) + throw new \InvalidArgumentException("Error : path '$filepath' does not exist !"); if (!is_readable($filepath)) - throw new \RuntimeException('Html template file not readable !'); + throw new \RuntimeException("Error : file '$filepath' not readable !"); + $this->filepath = $filepath; $this->html = file_get_contents($filepath); return true; @@ -105,16 +107,10 @@ public function setFilepath(string $filepath): bool * @param string $value * @return bool */ - public function set(string $name, string $value): bool + public function set(string $name, $value = null): bool { - if (!is_string($name)) { - throw new \TypeError('Invalid name : string expected !'); - } elseif (!is_null($value) && !is_string($value)) { - throw new \TypeError('Invalid value : string expected !'); - } else { - $this->html = str_replace("{%".$name."%}", $value, $this->html); - return true; - } + $this->html = str_replace("{%".$name."%}", $value, $this->html); + return true; } /** diff --git a/tests/HtmlTemplateTest.php b/tests/HtmlTemplateTest.php index 3613c08..d1b5d71 100755 --- a/tests/HtmlTemplateTest.php +++ b/tests/HtmlTemplateTest.php @@ -6,6 +6,7 @@ use Lvinceslas\Html\HtmlTemplate; use PHPUnit\Framework\TestCase; +use stdClass; class HtmlTemplateTest extends TestCase { @@ -38,15 +39,15 @@ public function testGetFilepath():void $this->assertTrue(is_string($v->getFilepath())); } - public function testSetFilepathWithIntegerInsteadOfValidFilepath(): void + public function testSetFilepathWithInvalidFilepath(): void { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(\TypeError::class); $v = new HtmlTemplate(__DIR__.'/test.html'); - $v->setFilepath(12); + $v->setFilepath(new stdClass()); } - public function testSetFilepathWithInvalidFilepath(): void + public function testSetFilepathWithUnfoundilepath(): void { $this->expectException(\InvalidArgumentException::class); $v = new HtmlTemplate(__DIR__.'/test.html'); @@ -60,4 +61,11 @@ public function testSetFilepathWithValidFilepath(): void $this->assertEquals(true, $v->setFilepath(__DIR__.'/test.html')); } + + public function testSetWithInvalidName(): void + { + $this->expectException(\TypeError::class); + $v = new HtmlTemplate(__DIR__.'/test.html'); + $v->set(new stdClass()); + } }