forked from Codeception/module-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwigAssertionsTrait.php
More file actions
83 lines (69 loc) · 2.2 KB
/
TwigAssertionsTrait.php
File metadata and controls
83 lines (69 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
declare(strict_types=1);
namespace Codeception\Module\Symfony;
use Symfony\Bridge\Twig\DataCollector\TwigDataCollector;
use function array_key_first;
trait TwigAssertionsTrait
{
/**
* Asserts that a template was not rendered in the response.
*
* ```php
* <?php
* $I->dontSeeRenderedTemplate('home.html.twig');
* ```
*/
public function dontSeeRenderedTemplate(string $template): void
{
$twigCollector = $this->grabTwigCollector(__FUNCTION__);
$templates = $twigCollector->getTemplates();
$this->assertArrayNotHasKey(
$template,
$templates,
"Template {$template} was rendered."
);
}
/**
* Asserts that the current template matches the expected template.
*
* ```php
* <?php
* $I->seeCurrentTemplateIs('home.html.twig');
* ```
*/
public function seeCurrentTemplateIs(string $expectedTemplate): void
{
$twigCollector = $this->grabTwigCollector(__FUNCTION__);
$templates = $twigCollector->getTemplates();
$actualTemplate = $templates === [] ? 'N/A' : (string) array_key_first($templates);
$this->assertSame(
$expectedTemplate,
$actualTemplate,
"Actual template {$actualTemplate} does not match expected template {$expectedTemplate}."
);
}
/**
* Asserts that a template was rendered in the response.
* That includes templates built with [inheritance](https://twig.symfony.com/doc/3.x/templates.html#template-inheritance).
*
* ```php
* <?php
* $I->seeRenderedTemplate('home.html.twig');
* $I->seeRenderedTemplate('layout.html.twig');
* ```
*/
public function seeRenderedTemplate(string $template): void
{
$twigCollector = $this->grabTwigCollector(__FUNCTION__);
$templates = $twigCollector->getTemplates();
$this->assertArrayHasKey(
$template,
$templates,
"Template {$template} was not rendered."
);
}
protected function grabTwigCollector(string $function): TwigDataCollector
{
return $this->grabCollector(DataCollectorName::TWIG, $function);
}
}