Skip to content

Commit 12a771c

Browse files
committed
Added Attributes to Renderer, updated PHPUnit version
1 parent 444aeeb commit 12a771c

3 files changed

Lines changed: 105 additions & 3 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
}
2020
},
2121
"require-dev": {
22-
"phpunit/phpunit": "^4.0",
22+
"phpunit/phpunit": "^5.0",
2323
"slim/slim" : "^3.0"
2424
}
2525
}

src/PhpRenderer.php

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
namespace Slim\Views;
1010

11+
use \InvalidArgumentException;
1112
use Psr\Http\Message\ResponseInterface;
1213

1314
/**
@@ -23,14 +24,21 @@ class PhpRenderer
2324
*/
2425
protected $templatePath;
2526

27+
/**
28+
* @var array
29+
*/
30+
protected $attributes;
31+
2632
/**
2733
* SlimRenderer constructor.
2834
*
2935
* @param string $templatePath
36+
* @param array $attributes
3037
*/
31-
public function __construct($templatePath = "")
38+
public function __construct($templatePath = "", $attributes = [])
3239
{
3340
$this->templatePath = $templatePath;
41+
$this->attributes = $attributes;
3442
}
3543

3644
/**
@@ -58,6 +66,46 @@ public function render(ResponseInterface $response, $template, array $data = [])
5866
return $response;
5967
}
6068

69+
/**
70+
* Get the attributes for the renderer
71+
*
72+
* @return array
73+
*/
74+
public function getAttributes()
75+
{
76+
return $this->attributes;
77+
}
78+
79+
/**
80+
* Set the attributes for the renderer
81+
*
82+
* @param array $attributes
83+
*/
84+
public function setAttributes(array $attributes)
85+
{
86+
$this->attributes = $attributes;
87+
}
88+
89+
/**
90+
* Get the template path
91+
*
92+
* @return string
93+
*/
94+
public function getTemplatePath()
95+
{
96+
return $this->templatePath;
97+
}
98+
99+
/**
100+
* Set the template path
101+
*
102+
* @param string $templatePath
103+
*/
104+
public function setTemplatePath($templatePath)
105+
{
106+
$this->templatePath = $templatePath;
107+
}
108+
61109
/**
62110
* Renders a template and returns the result as a string
63111
*
@@ -82,6 +130,12 @@ public function fetch($template, array $data = []) {
82130
throw new \RuntimeException("View cannot render `$template` because the template does not exist");
83131
}
84132

133+
foreach ($data as $k=>$val) {
134+
if (in_array($k, array_keys($this->attributes))) {
135+
throw new \InvalidArgumentException("Duplicate key found in data and renderer attributes. " . $k);
136+
}
137+
}
138+
85139
ob_start();
86140
$this->protectedIncludeScope($this->templatePath . $template, $data);
87141
$output = ob_get_clean();

tests/PhpRendererTest.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Date: 2015-11-12
1010
* Time: 1:19 PM
1111
*/
12-
class PhpRendererTest extends \PHPUnit_Framework_TestCase
12+
class PhpRendererTest extends PHPUnit_Framework_TestCase
1313
{
1414

1515
public function testRenderer() {
@@ -25,4 +25,52 @@ public function testRenderer() {
2525

2626
$this->assertEquals("Hi", $newResponse->getBody()->getContents());
2727
}
28+
29+
/**
30+
* @expectedException InvalidArgumentException
31+
*/
32+
public function testExceptionForAttr() {
33+
34+
$renderer = new \Slim\Views\PhpRenderer("tests/", [
35+
"hello" => "Hi"
36+
]);
37+
38+
$headers = new Headers();
39+
$body = new Body(fopen('php://temp', 'r+'));
40+
$response = new Response(200, $headers, $body);
41+
42+
$renderer->render($response, "testTemplate.php", [
43+
"hello" => "Hi"
44+
]);
45+
}
46+
47+
/**
48+
* @expectedException InvalidArgumentException
49+
*/
50+
public function testExceptionForTemplateInData() {
51+
52+
$renderer = new \Slim\Views\PhpRenderer("tests/");
53+
54+
$headers = new Headers();
55+
$body = new Body(fopen('php://temp', 'r+'));
56+
$response = new Response(200, $headers, $body);
57+
58+
$renderer->render($response, "testTemplate.php", [
59+
"template" => "Hi"
60+
]);
61+
}
62+
63+
/**
64+
* @expectedException RuntimeException
65+
*/
66+
public function testTemplateNotFound() {
67+
68+
$renderer = new \Slim\Views\PhpRenderer("tests/");
69+
70+
$headers = new Headers();
71+
$body = new Body(fopen('php://temp', 'r+'));
72+
$response = new Response(200, $headers, $body);
73+
74+
$renderer->render($response, "adfadftestTemplate.php", []);
75+
}
2876
}

0 commit comments

Comments
 (0)