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
|
|
|
|
11
|
|
|
final class TemplatingContext implements Context |
12
|
|
|
{ |
13
|
|
|
private $templating; |
14
|
|
|
|
15
|
|
|
private $articleLoader; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $lastRenderedContent; |
21
|
|
|
|
22
|
|
|
public function __construct(\Twig_Environment $templating, ArticleLoader $articleLoader) |
23
|
|
|
{ |
24
|
|
|
$this->templating = $templating; |
25
|
|
|
$this->articleLoader = $articleLoader; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @Given I set :slug as a current article in the context |
30
|
|
|
*/ |
31
|
|
|
public function IsetAsACurrentArticleInTheContext(string $slug): void |
32
|
|
|
{ |
33
|
|
|
$this->articleLoader->load('article', ['slug' => $slug]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @Given I render a template with content: |
38
|
|
|
*/ |
39
|
|
|
public function iRenderATemplateWithContent(PyStringNode $templateContent): void |
40
|
|
|
{ |
41
|
|
|
$template = $this->templating->createTemplate($templateContent->getRaw()); |
42
|
|
|
$this->lastRenderedContent = $template->render(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @Then rendered template should contain :searchString |
47
|
|
|
*/ |
48
|
|
|
public function renderedTemplateShouldContain(string $searchString): void |
49
|
|
|
{ |
50
|
|
|
if (false === \strpos($this->lastRenderedContent, $searchString)) { |
51
|
|
|
throw new \Exception('Searched string was not found in rendered template.'); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @Then rendered template should not contain :searchString |
57
|
|
|
*/ |
58
|
|
|
public function renderedTemplateShouldNotContain(string $searchString) |
59
|
|
|
{ |
60
|
|
|
if (false !== \strpos($this->lastRenderedContent, $searchString)) { |
61
|
|
|
throw new \Exception('Searched string was found in rendered template (and was not expected).'); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @Then rendered template should be equal to: |
67
|
|
|
*/ |
68
|
|
|
public function renderedTemplateShouldBeEqualTo(PyStringNode $templateContent): void |
69
|
|
|
{ |
70
|
|
|
if ($this->lastRenderedContent !== $templateContent->getRaw()) { |
71
|
|
|
throw new \Exception('The content is not equal!'); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|