zend-blog-3-backend
164 строки · 4.4 Кб
1<?php
2
3namespace Mtt\TestBundle\Features\Context;
4
5use Behat\Behat\Context\Context;
6use Behat\Behat\Hook\Scope\AfterStepScope;
7use Behat\Gherkin\Node\TableNode;
8use Behat\MinkExtension\Context\MinkContext;
9use Symfony\Component\HttpKernel\Profiler\Profiler;
10use Symfony\Component\PropertyAccess\PropertyAccess;
11
12/**
13* Defines application features from the specific context.
14*/
15class FeatureContext extends MinkContext implements Context
16{
17/**
18* @var Profiler
19*/
20protected $profiler;
21
22/**
23* Initializes context.
24*
25* Every scenario gets its own context instance.
26* You can also pass arbitrary arguments to the
27* context constructor through behat.yml.
28*
29* @param Profiler $profiler
30*/
31public function __construct(Profiler $profiler)
32{
33$this->profiler = $profiler;
34}
35
36/**
37* @When I logged in as admin
38*/
39public function iLoggedInAsAdmin()
40{
41$this->visit('/login');
42$this->fillField('_username', 'admin');
43$this->fillField('_password', 'test');
44$this->pressButton('login');
45}
46
47/**
48* @Then the json path :path should contain :string
49*
50* @param $path
51* @param $string
52*/
53public function theJsonPathShouldContain($path, $string)
54{
55$json = $this->getClientJSON();
56$accessor = PropertyAccess::createPropertyAccessor();
57
58$this->assert(
59$accessor->getValue($json, $path) === $string,
60sprintf('the json path "%s" should contain "%s"', $path, $string)
61);
62}
63
64/**
65* @When I send :method to :url with data:
66*
67* @param $method
68* @param $url
69* @param TableNode $table
70*/
71public function iSendToWithData($method, $url, TableNode $table)
72{
73$baseUrl = rtrim($this->getMinkParameter('base_url'), '/');
74$client = $this->getClient();
75
76$client->request($method, $baseUrl . $url, $table->getRowsHash());
77}
78
79/**
80* @Then :recipient should receive email with the text :text
81*
82* @param string $recipient
83* @param string $text
84*
85* @throws \Exception
86*/
87public function shouldReceiveEmailWithTheText($recipient, $text)
88{
89/* @var \Behat\Mink\Driver\Goutte\Client $client */
90$client = $this->getClient();
91
92$response = $client->getResponse();
93$token = $response->getHeader('X-Debug-Token');
94if (!$token) {
95throw new \Exception('X-Debug-Token not available');
96}
97
98$profile = $this->profiler->loadProfile($token);
99if (!$profile) {
100throw new \Exception('Symfony profile not available');
101}
102
103/* @var \Symfony\Bundle\SwiftmailerBundle\DataCollector\MessageDataCollector $mailCollector */
104$mailCollector = $profile->getCollector('swiftmailer');
105
106$collectedMessages = $mailCollector->getMessages();
107/* @var \Swift_Message $message */
108$message = $collectedMessages[0];
109
110$this->assert($message instanceof \Swift_Message, 'Message not instanceof Swift_Message');
111$this->assert(key($message->getTo()) === $recipient, 'The recipient is not ' . $recipient);
112$this->assert(mb_strpos($message->getBody(), $text) !== false, 'The message not contains text: ' . $text);
113}
114
115/**
116* @AfterStep
117*
118* @param AfterStepScope $event
119*/
120public function printLastResponseOnError(AfterStepScope $event)
121{
122if (!$event->getTestResult()->isPassed() && getenv('CIRCLE_ARTIFACTS')) {
123file_put_contents(
124getenv('CIRCLE_ARTIFACTS') . '/' . uniqid() . '.html',
125$this->getSession()->getDriver()->getContent()
126);
127}
128}
129
130/**
131* @return array
132*/
133protected function getClientJSON(): array
134{
135return json_decode($this->getSession()->getPage()->getContent(), true);
136}
137
138/**
139* @return \Behat\Mink\Driver\Goutte\Client
140*/
141protected function getClient()
142{
143/* @var \Behat\Mink\Driver\Goutte\Client $client */
144$client = $this
145->getSession()
146->getDriver()
147->getClient();
148
149return $client;
150}
151
152/**
153* @param bool $condition
154* @param string $message
155*
156* @throws \Exception
157*/
158protected function assert($condition, $message)
159{
160if (!$condition) {
161throw new \Exception($message);
162}
163}
164}
165