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