|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SWP\Behat\Contexts; |
|
6
|
|
|
|
|
7
|
|
|
use Behat\Behat\Context\Context; |
|
8
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
|
9
|
|
|
use SWP\Bundle\ContentBundle\Loader\ArticleLoader; |
|
10
|
|
|
use SWP\Bundle\ContentBundle\Twig\Cache\CacheBlockTagsCollectorInterface; |
|
11
|
|
|
use Twig\Environment; |
|
12
|
|
|
|
|
13
|
|
|
final class TemplatingContext implements Context |
|
14
|
|
|
{ |
|
15
|
|
|
private $templating; |
|
16
|
|
|
|
|
17
|
|
|
private $articleLoader; |
|
18
|
|
|
|
|
19
|
|
|
private $lastRenderedContent; |
|
20
|
|
|
|
|
21
|
|
|
private $cacheBlockTagsCollector; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct( |
|
24
|
|
|
Environment $templating, |
|
25
|
|
|
ArticleLoader $articleLoader, |
|
26
|
|
|
CacheBlockTagsCollectorInterface $cacheBlockTagsCollector |
|
27
|
|
|
) { |
|
28
|
|
|
$this->templating = $templating; |
|
29
|
|
|
$this->articleLoader = $articleLoader; |
|
30
|
|
|
$this->cacheBlockTagsCollector = $cacheBlockTagsCollector; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @Given I set :slug as a current article in the context |
|
35
|
|
|
*/ |
|
36
|
|
|
public function ISetAsACurrentArticleInTheContext(string $slug): void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->articleLoader->load('article', ['slug' => $slug]); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @Given I render a template with content: |
|
43
|
|
|
*/ |
|
44
|
|
|
public function iRenderATemplateWithContent(PyStringNode $templateContent): void |
|
45
|
|
|
{ |
|
46
|
|
|
$template = $this->templating->createTemplate($templateContent->getRaw()); |
|
47
|
|
|
$this->lastRenderedContent = $template->render(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @Then rendered template should contain :searchString |
|
52
|
|
|
*/ |
|
53
|
|
|
public function renderedTemplateShouldContain(string $searchString): void |
|
54
|
|
|
{ |
|
55
|
|
|
if (false === \strpos($this->lastRenderedContent, $searchString)) { |
|
56
|
|
|
throw new \Exception('Searched string was not found in rendered template.'); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @Then rendered template should not contain :searchString |
|
62
|
|
|
*/ |
|
63
|
|
|
public function renderedTemplateShouldNotContain(string $searchString) |
|
64
|
|
|
{ |
|
65
|
|
|
if (false !== \strpos($this->lastRenderedContent, $searchString)) { |
|
66
|
|
|
throw new \Exception('Searched string was found in rendered template (and was not expected).'); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @Then rendered template should be equal to: |
|
72
|
|
|
*/ |
|
73
|
|
|
public function renderedTemplateShouldBeEqualTo(PyStringNode $templateContent): void |
|
74
|
|
|
{ |
|
75
|
|
|
if ($this->lastRenderedContent !== $templateContent->getRaw()) { |
|
76
|
|
|
throw new \Exception('The content is not equal!'); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @Then CacheBlockTagsCollector should have tag :tagName |
|
82
|
|
|
*/ |
|
83
|
|
|
public function cacheblocktagscollectorShouldHaveTag(string $tagName) |
|
84
|
|
|
{ |
|
85
|
|
|
if (!in_array($tagName, $this->cacheBlockTagsCollector->getCurrentCacheBlockTags())) { |
|
86
|
|
|
throw new \Exception( |
|
87
|
|
|
sprintf( |
|
88
|
|
|
'Tag %s was not found. Found tags: %s', |
|
89
|
|
|
$tagName, |
|
90
|
|
|
implode(', ', $this->cacheBlockTagsCollector->getCurrentCacheBlockTags()) |
|
91
|
|
|
) |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|