Skip to content

Commit cd714a8

Browse files
authored
Merge pull request #87 from visto9259/fix/3
Fix `assertTemplateName()` checking only the first leaf in the view model tree.
2 parents a8de7f8 + 907d763 commit cd714a8

File tree

8 files changed

+112
-5
lines changed

8 files changed

+112
-5
lines changed

src/PHPUnit/Controller/AbstractControllerTestCase.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,10 @@ public function assertTemplateName($templateName)
792792
}
793793

794794
$viewModel = $application->getMvcEvent()->getViewModel();
795-
$this->assertTrue($this->searchTemplates($viewModel, $templateName));
795+
$this->assertTrue(
796+
$this->searchTemplates($viewModel, $templateName),
797+
sprintf('Failed asserting that view model tree contains template "%s"', $templateName)
798+
);
796799
}
797800

798801
/**
@@ -805,7 +808,10 @@ public function assertTemplateName($templateName)
805808
public function assertNotTemplateName($templateName)
806809
{
807810
$viewModel = $this->getApplication()->getMvcEvent()->getViewModel();
808-
$this->assertFalse($this->searchTemplates($viewModel, $templateName));
811+
$this->assertFalse(
812+
$this->searchTemplates($viewModel, $templateName),
813+
sprintf('Failed asserting that view model tree does not contain template "%s"', $templateName)
814+
);
809815
}
810816

811817
/**
@@ -820,8 +826,11 @@ protected function searchTemplates($viewModel, $templateName)
820826
if ($viewModel->getTemplate($templateName) === $templateName) {
821827
return true;
822828
}
829+
823830
foreach ($viewModel->getChildren() as $child) {
824-
return $this->searchTemplates($child, $templateName);
831+
if ($this->searchTemplates($child, $templateName)) {
832+
return true;
833+
}
825834
}
826835

827836
return false;

test/PHPUnit/Controller/AbstractControllerTestCaseTest.php

+62
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,68 @@ public function testAssertNotTemplateName(): void
538538
$this->assertNotTemplateName('template/does/not/exist');
539539
}
540540

541+
/**
542+
* Test case for a controller returning a view with deeply nested children
543+
* View hierarchy:
544+
* layout/layout -> baz/index/childview -> child1 -> child3
545+
* -> child2
546+
*/
547+
public function testSearchTemplatesVerifiesDeeplyNestedTemplateName(): void
548+
{
549+
$this->dispatch('/childview');
550+
551+
// Check that the rendered content
552+
$this->assertQueryContentContains('p', 'Parent');
553+
$this->assertQueryContentContains('p', 'Child 1');
554+
$this->assertQueryContentContains('p', 'Child 2');
555+
$this->assertQueryContentContains('p', 'Child 3');
556+
557+
$this->assertTemplateName('layout/layout');
558+
$this->assertTemplateName('baz/index/childview');
559+
$this->assertTemplateName('child1');
560+
$this->assertTemplateName('child2');
561+
$this->assertTemplateName('child3');
562+
$this->assertNotTemplateName('foo');
563+
}
564+
565+
/**
566+
* Check that the assertion fails when template is NOT found where it was supposed to found
567+
*/
568+
public function testAssertTemplateNameFailsWhenNotFound(): void
569+
{
570+
$this->dispatch('/childview');
571+
572+
try {
573+
$this->assertTemplateName('foo');
574+
} catch (ExpectationFailedException $exception) {
575+
$this->assertStringContainsString(
576+
'Failed asserting that view model tree contains template "foo"',
577+
$exception->getMessage()
578+
);
579+
return;
580+
}
581+
$this->fail('Expected Exception not thrown');
582+
}
583+
584+
/**
585+
* Check that the assertion fails when template is found where it was NOT supposed to found
586+
*/
587+
public function testAssertNotTemplateNameFailsWhenFound(): void
588+
{
589+
$this->dispatch('/childview');
590+
591+
try {
592+
$this->assertNotTemplateName('child1');
593+
} catch (ExpectationFailedException $exception) {
594+
$this->assertStringContainsString(
595+
'Failed asserting that view model tree does not contain template "child1"',
596+
$exception->getMessage()
597+
);
598+
return;
599+
}
600+
$this->fail('Expected Exception not thrown');
601+
}
602+
541603
public function testCustomResponseObject(): void
542604
{
543605
$this->dispatch('/custom-response');

test/_files/Baz/config/module.config.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@
108108
],
109109
],
110110
],
111+
'childview' => [
112+
'type' => 'literal',
113+
'options' => [
114+
'route' => '/childview',
115+
'defaults' => [
116+
'controller' => 'baz_index',
117+
'action' => 'childview',
118+
],
119+
],
120+
],
111121
],
112122
],
113123
'controllers' => [
@@ -117,8 +127,11 @@
117127
],
118128
'view_manager' => [
119129
'template_map' => [
120-
'404' => __DIR__ . '/../view/baz/error/404.phtml',
121-
'error' => __DIR__ . '/../view/baz/error/error.phtml',
130+
'404' => __DIR__ . '/../view/baz/error/404.phtml',
131+
'error' => __DIR__ . '/../view/baz/error/error.phtml',
132+
'child1' => __DIR__ . '/../view/baz/index/child1.phtml',
133+
'child2' => __DIR__ . '/../view/baz/index/child2.phtml',
134+
'child3' => __DIR__ . '/../view/baz/index/child3.phtml',
122135
],
123136
'template_path_stack' => [
124137
__DIR__ . '/../view',

test/_files/Baz/src/Baz/Controller/IndexController.php

+16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Laminas\Http\Response;
88
use Laminas\Mvc\Controller\AbstractActionController;
9+
use Laminas\View\Model\ViewModel;
910
use RuntimeException;
1011

1112
class IndexController extends AbstractActionController
@@ -61,4 +62,19 @@ public function customResponseAction()
6162
public function registerxpathnamespaceAction()
6263
{
6364
}
65+
66+
public function childViewAction(): ViewModel
67+
{
68+
$child1 = new ViewModel();
69+
$child1->setTemplate('child1');
70+
$child2 = new ViewModel();
71+
$child2->setTemplate('child2');
72+
$child3 = new ViewModel();
73+
$child3->setTemplate('child3');
74+
$view = new ViewModel();
75+
$view->addChild($child1, 'child1');
76+
$child1->addChild($child3, 'child3');
77+
$view->addChild($child2, 'child2');
78+
return $view;
79+
}
6480
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<p>Child 1</p>
2+
<?php echo $this->child3; ?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>Child 2</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>Child 3</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p>Parent</p>
2+
<?php echo $this->child1; ?>
3+
<?php echo $this->child2; ?>

0 commit comments

Comments
 (0)