Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assertTemplateName() checking only the first leaf in the view model tree. #87

Merged
merged 8 commits into from
Sep 16, 2024
15 changes: 12 additions & 3 deletions src/PHPUnit/Controller/AbstractControllerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

/**
Expand All @@ -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)
);
}

/**
Expand All @@ -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;
Expand Down
62 changes: 62 additions & 0 deletions test/PHPUnit/Controller/AbstractControllerTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
17 changes: 15 additions & 2 deletions test/_files/Baz/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@
],
],
],
'childview' => [
'type' => 'literal',
'options' => [
'route' => '/childview',
'defaults' => [
'controller' => 'baz_index',
'action' => 'childview',
],
],
],
],
],
'controllers' => [
Expand All @@ -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',
Expand Down
16 changes: 16 additions & 0 deletions test/_files/Baz/src/Baz/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Laminas\Http\Response;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
use RuntimeException;

class IndexController extends AbstractActionController
Expand Down Expand Up @@ -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;
}
}
2 changes: 2 additions & 0 deletions test/_files/Baz/view/baz/index/child1.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p>Child 1</p>
<?php echo $this->child3; ?>
1 change: 1 addition & 0 deletions test/_files/Baz/view/baz/index/child2.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Child 2</p>
1 change: 1 addition & 0 deletions test/_files/Baz/view/baz/index/child3.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Child 3</p>
3 changes: 3 additions & 0 deletions test/_files/Baz/view/baz/index/childview.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>Parent</p>
<?php echo $this->child1; ?>
<?php echo $this->child2; ?>