Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 075eb68

Browse files
committed
Merge branch 'hotfix/51'
Close #51
2 parents ea90428 + 0ea4d3b commit 075eb68

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5+
## 1.4.2 - 2018-03-15
6+
7+
### Added
8+
9+
- Nothing.
10+
11+
### Changed
12+
13+
- Nothing.
14+
15+
### Deprecated
16+
17+
- Nothing.
18+
19+
### Removed
20+
21+
- Nothing.
22+
23+
### Fixed
24+
25+
- [#51](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/51)
26+
fixes teh behavior of `addDefaultParam()` such that it properly affects
27+
layouts as well as templates.
28+
529
## 1.4.1 - 2017-12-12
630

731
### Added

src/ZendViewRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ private function prepareLayout(ModelInterface $viewModel)
342342
if ($layout) {
343343
$layout->addChild($viewModel);
344344
$viewModel = $layout;
345-
$viewModel->setVariables($this->mergeParams(self::TEMPLATE_ALL, []));
345+
$viewModel->setVariables($this->mergeParams($layout->getTemplate(), (array) $layout->getVariables()));
346346
}
347347

348348
return $viewModel;

test/ZendViewRendererTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,79 @@ public function testTemplateDefaultParameterIsNotAvailableInLayout()
246246
$this->assertContains($expected, $result, sprintf('Received %s', $result));
247247
}
248248

249+
public function testLayoutTemplateDefaultParameterIsAvailableInLayout()
250+
{
251+
$renderer = new ZendViewRenderer(null, 'zendview-layout-variable');
252+
$renderer->addPath(__DIR__ . '/TestAsset');
253+
$title = uniqid('ZendViewTitle', true);
254+
$name = uniqid('ZendViewName', true);
255+
$renderer->addDefaultParam('zendview-layout-variable', 'title', $title);
256+
$result = $renderer->render('zendview', ['name' => $name]);
257+
$this->assertContains($title, $result);
258+
$this->assertContains($name, $result);
259+
260+
$content = file_get_contents(__DIR__ . '/TestAsset/zendview.phtml');
261+
$content = str_replace('<?php echo $name ?>', $name, $content);
262+
$layout = file_get_contents(__DIR__ . '/TestAsset/zendview-layout-variable.phtml');
263+
$layout = str_replace('<?= $this->title ?>', $title, $layout);
264+
$layout = str_replace('<?= $this->content ?>' . PHP_EOL, $content, $layout);
265+
$this->assertContains($layout, $result);
266+
267+
$expected = sprintf('<title>Layout Page: %s</title>', $title);
268+
$this->assertContains($expected, $result, sprintf('Received %s', $result));
269+
}
270+
271+
public function testVariableInProvidedLayoutViewModelOverridesTemplateDefaultParameter()
272+
{
273+
$renderer = new ZendViewRenderer(null);
274+
$renderer->addPath(__DIR__ . '/TestAsset');
275+
$titleToBeOverriden = uniqid('ZendViewTitleToBeOverriden', true);
276+
$title = uniqid('ZendViewTitle', true);
277+
$name = uniqid('ZendViewName', true);
278+
$renderer->addDefaultParam('zendview-layout-variable', 'title', $titleToBeOverriden);
279+
280+
$layout = new ViewModel(['title' => $title]);
281+
$layout->setTemplate('zendview-layout-variable');
282+
$result = $renderer->render('zendview', ['name' => $name, 'layout' => $layout]);
283+
$this->assertContains($title, $result);
284+
$this->assertContains($name, $result);
285+
286+
$content = file_get_contents(__DIR__ . '/TestAsset/zendview.phtml');
287+
$content = str_replace('<?php echo $name ?>', $name, $content);
288+
$layout = file_get_contents(__DIR__ . '/TestAsset/zendview-layout-variable.phtml');
289+
$layout = str_replace('<?= $this->title ?>', $title, $layout);
290+
$layout = str_replace('<?= $this->content ?>' . PHP_EOL, $content, $layout);
291+
$this->assertContains($layout, $result);
292+
293+
$expected = sprintf('<title>Layout Page: %s</title>', $title);
294+
$this->assertContains($expected, $result, sprintf('Received %s', $result));
295+
}
296+
297+
public function testTemplateDefaultParameterIsAvailableInLayoutProvidedWithViewModel()
298+
{
299+
$renderer = new ZendViewRenderer(null);
300+
$renderer->addPath(__DIR__ . '/TestAsset');
301+
$title = uniqid('ZendViewTitle', true);
302+
$name = uniqid('ZendViewName', true);
303+
$renderer->addDefaultParam('zendview-layout-variable', 'title', $title);
304+
305+
$layout = new ViewModel();
306+
$layout->setTemplate('zendview-layout-variable');
307+
$result = $renderer->render('zendview', ['name' => $name, 'layout' => $layout]);
308+
$this->assertContains($title, $result);
309+
$this->assertContains($name, $result);
310+
311+
$content = file_get_contents(__DIR__ . '/TestAsset/zendview.phtml');
312+
$content = str_replace('<?php echo $name ?>', $name, $content);
313+
$layout = file_get_contents(__DIR__ . '/TestAsset/zendview-layout-variable.phtml');
314+
$layout = str_replace('<?= $this->title ?>', $title, $layout);
315+
$layout = str_replace('<?= $this->content ?>' . PHP_EOL, $content, $layout);
316+
$this->assertContains($layout, $result);
317+
318+
$expected = sprintf('<title>Layout Page: %s</title>', $title);
319+
$this->assertContains($expected, $result, sprintf('Received %s', $result));
320+
}
321+
249322
/**
250323
* @group layout
251324
*/

0 commit comments

Comments
 (0)