Skip to content
This repository has been archived by the owner on Jul 31, 2022. It is now read-only.

Commit

Permalink
Merge pull request #20 from tbreuss/develop
Browse files Browse the repository at this point in the history
Added unittests & type hints
  • Loading branch information
tbreuss committed Oct 10, 2018
2 parents 4f19599 + 2f32d95 commit e22f77e
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function __call(string $name, array $args)
* @param callback $callback;
* @return $this
*/
public function registerHelper($name, $callback)
public function registerHelper(string $name, callable $callback): self
{
$this->helpers->add($name, $callback);
return $this;
Expand All @@ -131,7 +131,7 @@ public function registerHelper($name, $callback)
* @param string $name;
* @return $this
*/
public function removeHelper($name)
public function removeHelper(string $name): self
{
$this->helpers->remove($name);
return $this;
Expand All @@ -142,7 +142,7 @@ public function removeHelper($name)
* @param string $name
* @return callable
*/
public function getHelper($name)
public function getHelper(string $name): callable
{
return $this->helpers->get($name);
}
Expand All @@ -152,7 +152,7 @@ public function getHelper($name)
* @param string $name
* @return boolean
*/
public function doesHelperExist($name)
public function doesHelperExist(string $name): bool
{
return $this->helpers->exists($name);
}
Expand All @@ -161,7 +161,7 @@ public function doesHelperExist($name)
* @param ViewExtension $extension
* @return $this
*/
public function registerExtension(ViewExtension $extension)
public function registerExtension(ViewExtension $extension): self
{
$extension->register($this);
return $this;
Expand Down
10 changes: 5 additions & 5 deletions src/View/ViewHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ViewHelpers
{
/**
* Array of template functions.
* @var array
* @var callable[]
*/
private $helpers = [];

Expand All @@ -23,7 +23,7 @@ class ViewHelpers
* @param callback $callback
* @return ViewHelpers
*/
public function add($name, callable $callback)
public function add(string $name, callable $callback): self
{
if ($this->exists($name)) {
throw new LogicException(
Expand All @@ -41,7 +41,7 @@ public function add($name, callable $callback)
* @param string $name
* @return ViewHelpers
*/
public function remove($name)
public function remove(string $name): self
{
if (!$this->exists($name)) {
throw new LogicException(
Expand All @@ -59,7 +59,7 @@ public function remove($name)
* @param string $name
* @return callable
*/
public function get($name)
public function get(string $name): callable
{
if (!$this->exists($name)) {
throw new LogicException('The template function "' . $name . '" was not found.');
Expand All @@ -73,7 +73,7 @@ public function get($name)
* @param string $name
* @return boolean
*/
public function exists($name)
public function exists(string $name): bool
{
return isset($this->helpers[$name]);
}
Expand Down
126 changes: 126 additions & 0 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace Tebe\Pvc\Tests;

use ArgumentCountError;
use LogicException;
use PHPUnit\Framework\TestCase;
use Tebe\Pvc\Exception\SystemException;
use Tebe\Pvc\View\View;
use Tebe\Pvc\View\ViewHelpers;
use TypeError;

class ViewTest extends TestCase
{
/**
* @var View
*/
private $view;

public function setUp()
{
$helpers = new ViewHelpers();
$helpers->add('upper', function ($str) {
return strtoupper($str);
});
$helpers->add('lower', function ($str) {
return strtolower($str);
});
$this->view = new View(__DIR__ . '/resources/views', $helpers);
}

public function testConstructorException1()
{
$this->expectException(SystemException::class);
new View(__DIR__ . '/not-existing-path', new ViewHelpers());
}

public function testConstructorException2()
{
$this->expectException(ArgumentCountError::class);
new View();
}

public function testConstructorException3()
{
$this->expectException(TypeError::class);
new View(__DIR__ . '/resources/views', false);
}

public function testRender()
{
// render without params
$testValue = $this->view->render('index');
$this->assertEquals('Output from view "index"', $testValue);

// render with params
$testValue = $this->view->render('index/vars', [
'int' => 123,
'string' => 'ABC'
]);
$this->assertEquals("Output from view \"index/vars\"<br>\n123<br>\nABC", $testValue);

// file not exist
$this->expectException(SystemException::class);
$this->view->render('not-existing-view');
}

public function testFileExist()
{
// existing view
$testValue = $this->view->fileExist('index/vars');
$this->assertEquals(true, $testValue);

// not existing view
$testValue = $this->view->fileExist('not-existing-view');
$this->assertEquals(false, $testValue);
}

public function testGetViewsPath()
{
$testValue = $this->view->getViewsPath();
$this->assertEquals(__DIR__ . '/resources/views', $testValue);
}

public function testCall()
{
$upper = $this->view->upper('foo');
$this->assertEquals('FOO', $upper);

$lower = $this->view->upper('BAR');
$this->assertEquals('BAR', $lower);

$this->expectException(LogicException::class);
$this->view->notExisting();
}

public function testData()
{
// __set / __get
$boolVal = true;
$this->view->boolVal = $boolVal;
$this->assertEquals($boolVal, $this->view->boolVal);

$arrayVal = ['a', 'b', 'c'];
$this->view->arrayVal = $arrayVal;
$this->assertEquals($arrayVal, $this->view->arrayVal);

$intVal = 123;
$this->view->intVal = $intVal;
$this->assertEquals($intVal, $this->view->intVal);

$stringVal = 'abc';
$this->view->stringVal = $stringVal;
$this->assertEquals($stringVal, $this->view->stringVal);

// __isset / __unset
$boolVal = isset($this->view->boolVal);
$this->assertEquals(true, $boolVal);
unset($this->view->boolVal);
$boolVal = isset($this->view->boolVal);
$this->assertEquals(false, $boolVal);

$nullVal = $this->view->boolVal;
$this->assertEquals(null, $nullVal);
}
}
1 change: 1 addition & 0 deletions tests/resources/views/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?='Output from view "index"' ?>
3 changes: 3 additions & 0 deletions tests/resources/views/index/vars.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?='Output from view "index/vars"' ?><br>
<?=$int?><br>
<?=$string?>
3 changes: 3 additions & 0 deletions tests/resources/views/layouts/default.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>Header</div>
<?php echo $content ?>
<div>Footer</div>

0 comments on commit e22f77e

Please sign in to comment.