Skip to content

Commit

Permalink
🚑 Fix minor issue
Browse files Browse the repository at this point in the history
  • Loading branch information
lvinceslas committed Aug 9, 2020
1 parent 98c35b1 commit 46f7228
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
26 changes: 11 additions & 15 deletions src/HtmlTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down
16 changes: 12 additions & 4 deletions tests/HtmlTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Lvinceslas\Html\HtmlTemplate;
use PHPUnit\Framework\TestCase;
use stdClass;

class HtmlTemplateTest extends TestCase
{
Expand Down Expand Up @@ -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');
Expand All @@ -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());
}
}

0 comments on commit 46f7228

Please sign in to comment.