Completed
Push — 2.0 ( 789055...a14c2b )
by Paweł
08:38 queued 10s
created

TemplatingContext   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 3
cbo 5
dl 0
loc 83
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A ISetAsACurrentArticleInTheContext() 0 4 1
A iRenderATemplateWithContent() 0 5 1
A renderedTemplateShouldContain() 0 6 2
A renderedTemplateShouldNotContain() 0 6 2
A renderedTemplateShouldBeEqualTo() 0 6 2
A cacheblocktagscollectorShouldHaveTag() 0 12 2
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