Skip to content

Commit fe90bca

Browse files
authored
Added Twig assertions (Codeception#102)
1 parent 3da97ac commit fe90bca

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

src/Codeception/Module/Symfony.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Codeception\Module\Symfony\SecurityAssertionsTrait;
2424
use Codeception\Module\Symfony\ServicesAssertionsTrait;
2525
use Codeception\Module\Symfony\SessionAssertionsTrait;
26+
use Codeception\Module\Symfony\TwigAssertionsTrait;
2627
use Codeception\TestInterface;
2728
use Exception;
2829
use ReflectionClass;
@@ -153,7 +154,8 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
153154
RouterAssertionsTrait,
154155
SecurityAssertionsTrait,
155156
ServicesAssertionsTrait,
156-
SessionAssertionsTrait
157+
SessionAssertionsTrait,
158+
TwigAssertionsTrait
157159
;
158160

159161
private static $possibleKernelClasses = [
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\Module\Symfony;
6+
7+
use Symfony\Bridge\Twig\DataCollector\TwigDataCollector;
8+
9+
trait TwigAssertionsTrait
10+
{
11+
/**
12+
* Asserts that a template was not rendered in the response.
13+
*
14+
* ```php
15+
* <?php
16+
* $I->dontSeeRenderedTemplate('home.html.twig');
17+
* ```
18+
*
19+
* @param string $template
20+
*/
21+
public function dontSeeRenderedTemplate(string $template): void
22+
{
23+
$twigCollector = $this->grabTwigCollector(__FUNCTION__);
24+
25+
$templates = (array) $twigCollector->getTemplates();
26+
27+
$this->assertArrayNotHasKey(
28+
$template,
29+
$templates,
30+
"Template {$template} was rendered."
31+
);
32+
}
33+
34+
/**
35+
* Asserts that the current template matches the expected template.
36+
*
37+
* ```php
38+
* <?php
39+
* $I->seeCurrentTemplateIs('home.html.twig');
40+
* ```
41+
*
42+
* @param string $expectedTemplate
43+
*/
44+
public function seeCurrentTemplateIs(string $expectedTemplate): void
45+
{
46+
$twigCollector = $this->grabTwigCollector(__FUNCTION__);
47+
48+
$templates = (array) $twigCollector->getTemplates();
49+
$actualTemplate = array_key_first($templates);
50+
51+
$this->assertEquals(
52+
$expectedTemplate,
53+
$actualTemplate,
54+
"Actual template {$actualTemplate} does not match expected template {$expectedTemplate}."
55+
);
56+
}
57+
58+
/**
59+
* Asserts that a template was rendered in the response.
60+
* That includes templates built with [inheritance](https://twig.symfony.com/doc/3.x/templates.html#template-inheritance).
61+
*
62+
* ```php
63+
* <?php
64+
* $I->seeRenderedTemplate('home.html.twig');
65+
* $I->seeRenderedTemplate('layout.html.twig');
66+
* ```
67+
*
68+
* @param string $template
69+
*/
70+
public function seeRenderedTemplate(string $template): void
71+
{
72+
$twigCollector = $this->grabTwigCollector(__FUNCTION__);
73+
74+
$templates = (array) $twigCollector->getTemplates();
75+
76+
$this->assertArrayHasKey(
77+
$template,
78+
$templates,
79+
"Template {$template} was not rendered."
80+
);
81+
}
82+
83+
protected function grabTwigCollector(string $function): TwigDataCollector
84+
{
85+
return $this->grabCollector('twig', $function);
86+
}
87+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy