Completed
Pull Request — 1.5 (#758)
by Paweł
11:13
created

TemplatingContext   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 43
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A iRenderATemplateWithContent() 0 5 1
A renderedTemplateShouldContain() 0 6 2
A renderedTemplateShouldNotContain() 0 6 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
10
final class TemplatingContext implements Context
11
{
12
    private $templating;
13
14
    /**
15
     * @var string
16
     */
17
    private $lastRenderedContent;
18
19
    public function __construct(\Twig_Environment $templating)
20
    {
21
        $this->templating = $templating;
22
    }
23
24
    /**
25
     * @Given I render a template with content:
26
     */
27
    public function iRenderATemplateWithContent(PyStringNode $templateContent): void
28
    {
29
        $template = $this->templating->createTemplate($templateContent->getRaw());
30
        $this->lastRenderedContent = $template->render([]);
31
    }
32
33
    /**
34
     * @Then rendered template should contain :searchString
35
     */
36
    public function renderedTemplateShouldContain(string $searchString): void
37
    {
38
        if (false === \strpos($this->lastRenderedContent, $searchString)) {
39
            throw new \Exception('Searched string was not found in rendered template.');
40
        }
41
    }
42
43
    /**
44
     * @Then rendered template should not contain :searchString
45
     */
46
    public function renderedTemplateShouldNotContain(string $searchString)
47
    {
48
        if (false !== \strpos($this->lastRenderedContent, $searchString)) {
49
            throw new \Exception('Searched string was found in rendered template (and was not expected).');
50
        }
51
    }
52
}
53