zend-blog-3-backend

Форк
0
164 строки · 4.4 Кб
1
<?php
2

3
namespace Mtt\TestBundle\Features\Context;
4

5
use Behat\Behat\Context\Context;
6
use Behat\Behat\Hook\Scope\AfterStepScope;
7
use Behat\Gherkin\Node\TableNode;
8
use Behat\MinkExtension\Context\MinkContext;
9
use Symfony\Component\HttpKernel\Profiler\Profiler;
10
use Symfony\Component\PropertyAccess\PropertyAccess;
11

12
/**
13
 * Defines application features from the specific context.
14
 */
15
class FeatureContext extends MinkContext implements Context
16
{
17
    /**
18
     * @var Profiler
19
     */
20
    protected $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
     */
31
    public function __construct(Profiler $profiler)
32
    {
33
        $this->profiler = $profiler;
34
    }
35

36
    /**
37
     * @When I logged in as admin
38
     */
39
    public 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
     */
53
    public function theJsonPathShouldContain($path, $string)
54
    {
55
        $json = $this->getClientJSON();
56
        $accessor = PropertyAccess::createPropertyAccessor();
57

58
        $this->assert(
59
            $accessor->getValue($json, $path) === $string,
60
            sprintf('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
     */
71
    public 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
     */
87
    public 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');
94
        if (!$token) {
95
            throw new \Exception('X-Debug-Token not available');
96
        }
97

98
        $profile = $this->profiler->loadProfile($token);
99
        if (!$profile) {
100
            throw 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
     */
120
    public function printLastResponseOnError(AfterStepScope $event)
121
    {
122
        if (!$event->getTestResult()->isPassed() && getenv('CIRCLE_ARTIFACTS')) {
123
            file_put_contents(
124
                getenv('CIRCLE_ARTIFACTS') . '/' . uniqid() . '.html',
125
                $this->getSession()->getDriver()->getContent()
126
            );
127
        }
128
    }
129

130
    /**
131
     * @return array
132
     */
133
    protected function getClientJSON(): array
134
    {
135
        return json_decode($this->getSession()->getPage()->getContent(), true);
136
    }
137

138
    /**
139
     * @return \Behat\Mink\Driver\Goutte\Client
140
     */
141
    protected function getClient()
142
    {
143
        /* @var \Behat\Mink\Driver\Goutte\Client $client */
144
        $client = $this
145
            ->getSession()
146
            ->getDriver()
147
            ->getClient();
148

149
        return $client;
150
    }
151

152
    /**
153
     * @param bool $condition
154
     * @param string $message
155
     *
156
     * @throws \Exception
157
     */
158
    protected function assert($condition, $message)
159
    {
160
        if (!$condition) {
161
            throw new \Exception($message);
162
        }
163
    }
164
}
165

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.