diff --git a/src/PHPUnit/Controller/AbstractControllerTestCase.php b/src/PHPUnit/Controller/AbstractControllerTestCase.php index 8006e2a8..76455766 100644 --- a/src/PHPUnit/Controller/AbstractControllerTestCase.php +++ b/src/PHPUnit/Controller/AbstractControllerTestCase.php @@ -792,7 +792,10 @@ public function assertTemplateName($templateName) } $viewModel = $application->getMvcEvent()->getViewModel(); - $this->assertTrue($this->searchTemplates($viewModel, $templateName)); + $this->assertTrue( + $this->searchTemplates($viewModel, $templateName), + sprintf('Failed asserting that view model tree contains template "%s"', $templateName) + ); } /** @@ -805,7 +808,10 @@ public function assertTemplateName($templateName) public function assertNotTemplateName($templateName) { $viewModel = $this->getApplication()->getMvcEvent()->getViewModel(); - $this->assertFalse($this->searchTemplates($viewModel, $templateName)); + $this->assertFalse( + $this->searchTemplates($viewModel, $templateName), + sprintf('Failed asserting that view model tree does not contain template "%s"', $templateName) + ); } /** @@ -820,8 +826,11 @@ protected function searchTemplates($viewModel, $templateName) if ($viewModel->getTemplate($templateName) === $templateName) { return true; } + foreach ($viewModel->getChildren() as $child) { - return $this->searchTemplates($child, $templateName); + if ($this->searchTemplates($child, $templateName)) { + return true; + } } return false; diff --git a/test/PHPUnit/Controller/AbstractControllerTestCaseTest.php b/test/PHPUnit/Controller/AbstractControllerTestCaseTest.php index 5e36cb63..29e124c8 100644 --- a/test/PHPUnit/Controller/AbstractControllerTestCaseTest.php +++ b/test/PHPUnit/Controller/AbstractControllerTestCaseTest.php @@ -538,6 +538,68 @@ public function testAssertNotTemplateName(): void $this->assertNotTemplateName('template/does/not/exist'); } + /** + * Test case for a controller returning a view with deeply nested children + * View hierarchy: + * layout/layout -> baz/index/childview -> child1 -> child3 + * -> child2 + */ + public function testSearchTemplatesVerifiesDeeplyNestedTemplateName(): void + { + $this->dispatch('/childview'); + + // Check that the rendered content + $this->assertQueryContentContains('p', 'Parent'); + $this->assertQueryContentContains('p', 'Child 1'); + $this->assertQueryContentContains('p', 'Child 2'); + $this->assertQueryContentContains('p', 'Child 3'); + + $this->assertTemplateName('layout/layout'); + $this->assertTemplateName('baz/index/childview'); + $this->assertTemplateName('child1'); + $this->assertTemplateName('child2'); + $this->assertTemplateName('child3'); + $this->assertNotTemplateName('foo'); + } + + /** + * Check that the assertion fails when template is NOT found where it was supposed to found + */ + public function testAssertTemplateNameFailsWhenNotFound(): void + { + $this->dispatch('/childview'); + + try { + $this->assertTemplateName('foo'); + } catch (ExpectationFailedException $exception) { + $this->assertStringContainsString( + 'Failed asserting that view model tree contains template "foo"', + $exception->getMessage() + ); + return; + } + $this->fail('Expected Exception not thrown'); + } + + /** + * Check that the assertion fails when template is found where it was NOT supposed to found + */ + public function testAssertNotTemplateNameFailsWhenFound(): void + { + $this->dispatch('/childview'); + + try { + $this->assertNotTemplateName('child1'); + } catch (ExpectationFailedException $exception) { + $this->assertStringContainsString( + 'Failed asserting that view model tree does not contain template "child1"', + $exception->getMessage() + ); + return; + } + $this->fail('Expected Exception not thrown'); + } + public function testCustomResponseObject(): void { $this->dispatch('/custom-response'); diff --git a/test/_files/Baz/config/module.config.php b/test/_files/Baz/config/module.config.php index 5de1e01a..f1f1889d 100644 --- a/test/_files/Baz/config/module.config.php +++ b/test/_files/Baz/config/module.config.php @@ -108,6 +108,16 @@ ], ], ], + 'childview' => [ + 'type' => 'literal', + 'options' => [ + 'route' => '/childview', + 'defaults' => [ + 'controller' => 'baz_index', + 'action' => 'childview', + ], + ], + ], ], ], 'controllers' => [ @@ -117,8 +127,11 @@ ], 'view_manager' => [ 'template_map' => [ - '404' => __DIR__ . '/../view/baz/error/404.phtml', - 'error' => __DIR__ . '/../view/baz/error/error.phtml', + '404' => __DIR__ . '/../view/baz/error/404.phtml', + 'error' => __DIR__ . '/../view/baz/error/error.phtml', + 'child1' => __DIR__ . '/../view/baz/index/child1.phtml', + 'child2' => __DIR__ . '/../view/baz/index/child2.phtml', + 'child3' => __DIR__ . '/../view/baz/index/child3.phtml', ], 'template_path_stack' => [ __DIR__ . '/../view', diff --git a/test/_files/Baz/src/Baz/Controller/IndexController.php b/test/_files/Baz/src/Baz/Controller/IndexController.php index 5ed765bd..9063d251 100644 --- a/test/_files/Baz/src/Baz/Controller/IndexController.php +++ b/test/_files/Baz/src/Baz/Controller/IndexController.php @@ -6,6 +6,7 @@ use Laminas\Http\Response; use Laminas\Mvc\Controller\AbstractActionController; +use Laminas\View\Model\ViewModel; use RuntimeException; class IndexController extends AbstractActionController @@ -61,4 +62,19 @@ public function customResponseAction() public function registerxpathnamespaceAction() { } + + public function childViewAction(): ViewModel + { + $child1 = new ViewModel(); + $child1->setTemplate('child1'); + $child2 = new ViewModel(); + $child2->setTemplate('child2'); + $child3 = new ViewModel(); + $child3->setTemplate('child3'); + $view = new ViewModel(); + $view->addChild($child1, 'child1'); + $child1->addChild($child3, 'child3'); + $view->addChild($child2, 'child2'); + return $view; + } } diff --git a/test/_files/Baz/view/baz/index/child1.phtml b/test/_files/Baz/view/baz/index/child1.phtml new file mode 100644 index 00000000..272f39d4 --- /dev/null +++ b/test/_files/Baz/view/baz/index/child1.phtml @@ -0,0 +1,2 @@ +

Child 1

+child3; ?> diff --git a/test/_files/Baz/view/baz/index/child2.phtml b/test/_files/Baz/view/baz/index/child2.phtml new file mode 100644 index 00000000..a3efbc73 --- /dev/null +++ b/test/_files/Baz/view/baz/index/child2.phtml @@ -0,0 +1 @@ +

Child 2

diff --git a/test/_files/Baz/view/baz/index/child3.phtml b/test/_files/Baz/view/baz/index/child3.phtml new file mode 100644 index 00000000..8aadab86 --- /dev/null +++ b/test/_files/Baz/view/baz/index/child3.phtml @@ -0,0 +1 @@ +

Child 3

diff --git a/test/_files/Baz/view/baz/index/childview.phtml b/test/_files/Baz/view/baz/index/childview.phtml new file mode 100644 index 00000000..755b48c3 --- /dev/null +++ b/test/_files/Baz/view/baz/index/childview.phtml @@ -0,0 +1,3 @@ +

Parent

+child1; ?> +child2; ?>