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

Commit 89d45b1

Browse files
committed
Merge branch 'hotfix/51' into release-2.0.0
Forward port #51 Conflicts: CHANGELOG.md
2 parents 8a1f5b8 + 0ea4d3b commit 89d45b1

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
@@ -102,6 +102,30 @@ All notable changes to this project will be documented in this file, in reverse
102102

103103
- Nothing.
104104

105+
## 1.4.2 - 2018-03-15
106+
107+
### Added
108+
109+
- Nothing.
110+
111+
### Changed
112+
113+
- Nothing.
114+
115+
### Deprecated
116+
117+
- Nothing.
118+
119+
### Removed
120+
121+
- Nothing.
122+
123+
### Fixed
124+
125+
- [#51](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/51)
126+
fixes teh behavior of `addDefaultParam()` such that it properly affects
127+
layouts as well as templates.
128+
105129
## 1.4.1 - 2017-12-12
106130

107131
### Added

src/ZendViewRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ private function prepareLayout(ModelInterface $viewModel) : ModelInterface
322322
if ($layout) {
323323
$layout->addChild($viewModel);
324324
$viewModel = $layout;
325-
$viewModel->setVariables($this->mergeParams(self::TEMPLATE_ALL, []));
325+
$viewModel->setVariables($this->mergeParams($layout->getTemplate(), (array) $layout->getVariables()));
326326
}
327327

328328
return $viewModel;

test/ZendViewRendererTest.php

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

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

0 commit comments

Comments
 (0)