ThoughtsContext::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 10
rs 10
1
<?php
2
3
/**
4
 * Donut Social Network - Yet another experimental social network.
5
 * Copyright (C) 2016-2017, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Donut Social Network.
8
 *
9
 * Donut Social Network is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Donut Social Network is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Donut Social Network.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Donut Social Network
23
 * @copyright Copyright (C) 2016-2017, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/donut/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace AppBundle\FeatureContexts;
29
30
use Behat\Behat\Context\Context;
31
use Angelov\Donut\Behat\Pages\Thoughts\HomePage;
32
use Angelov\Donut\Behat\Pages\Users\BrowsingUsersPage;
33
use Angelov\Donut\Behat\Service\Storage\StorageInterface;
34
use Angelov\Donut\Behat\Service\ValidationErrorsChecker\ValidationErrorsCheckerInterface;
35
use Webmozart\Assert\Assert;
36
37
class ThoughtsContext implements Context
38
{
39
    private $storage;
40
    private $homePage;
41
    private $validationErrorsChecker;
42
    private $browsingUsersPage;
43
44
    public function __construct(
45
        HomePage $homePage,
46
        BrowsingUsersPage $browsingUsersPage,
47
        StorageInterface $storage,
48
        ValidationErrorsCheckerInterface $validationErrorsChecker
49
    ) {
50
        $this->storage = $storage;
51
        $this->homePage = $homePage;
52
        $this->validationErrorsChecker = $validationErrorsChecker;
53
        $this->browsingUsersPage = $browsingUsersPage;
54
    }
55
56
    /**
57
     * @When I want to share a thought
58
     * @When I want to browse the thoughts
59
     * @Given I am browsing the thoughts
60
     */
61
    public function iWantToShareAThought() : void
62
    {
63
        $this->homePage->open();
64
    }
65
66
    /**
67
     * @When I specify its content as :content
68
     * @When I don't specify its content
69
     */
70
    public function iSpecifyItsContentAs($content = '') : void
71
    {
72
        $this->homePage->specifyThoughtContent($content);
73
74
        $this->storage->set('thought_content', $content);
75
    }
76
77
    /**
78
     * @Given I specify its content as something longer than :length characters
79
     */
80
    public function iSpecifyItsContentAsSomethingLongerThanCharacters(int $length) : void
81
    {
82
        // @todo extract string generating
83
        $list = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>?:"{}!@#$%^&';
84
        $list = str_shuffle($list);
85
86
        if (strlen($list) < $length+1) {
87
            $list .= $list;
88
        }
89
90
        $content = substr($list, 0, $length+1);
91
92
        $this->iSpecifyItsContentAs($content);
93
    }
94
95
    /**
96
     * @When I try to share it
97
     */
98
    public function iTryToShareIt() : void
99
    {
100
        $this->homePage->shareThought();
101
    }
102
103
    /**
104
     * @Then I should see it in the list of latest thoughts
105
     */
106
    public function iShouldSeeItInTheListOfLatestThoughts() : void
107
    {
108
        $shared = $this->storage->get('thought_content');
109
110
        Assert::true(
111
            $this->homePage->containsThought($shared)
112
        );
113
    }
114
115
    /**
116
     * @Then I shouldn't see it in the list of (latest) thoughts
117
     */
118
    public function iShouldnTSeeItInTheListOfThoughts() : void
119
    {
120
        $content = $this->storage->get('deleted_thought_content');
121
122
        Assert::false(
123
            $this->homePage->containsThought($content)
124
        );
125
    }
126
127
    /**
128
     * @Then I should be notified that the maximum length is :length characters
129
     */
130
    public function iShouldBeNotifiedThatTheMaximumLengthIsCharacters(int $length) : void
131
    {
132
        $message = sprintf('Thoughts can\'t be longer than %d characters.', $length);
133
134
        Assert::true(
135
            $this->validationErrorsChecker->checkMessageForField('Content', $message)
136
        );
137
    }
138
139
    /**
140
     * @Then I should be notified that the thought must have content
141
     */
142
    public function iShouldBeNotifiedThatTheThoughtMustHaveContent() : void
143
    {
144
        Assert::true(
145
            $this->validationErrorsChecker->checkMessageForField('Content', 'Please write the content of your thought.')
146
        );
147
    }
148
149
    /**
150
     * @Then I should see (the rest) :count thoughts from :name
151
     */
152
    public function iShouldSeeThoughtsFrom(int $count, string $name) : void
153
    {
154
        $counted = $this->homePage->countThoughtsFromAuthor($name);
155
156
        Assert::same($counted, $count, 'Counted %d instead of %d');
157
    }
158
159
    /**
160
     * @Then I should see the :count thoughts of mine
161
     */
162
    public function iShouldSeeTheThoughtsOfMine(int $count) : void
163
    {
164
        $name = $this->storage->get('logged_user')->getName();
165
        $counted = $this->homePage->countThoughtsFromAuthor($name);
166
167
        Assert::same($counted, $count, 'Counted %d instead of %d');
168
    }
169
170
    /**
171
     * @When I delete the :content thought
172
     */
173
    public function iDeleteTheThought(string $content) : void
174
    {
175
        $this->homePage->deleteThought($content);
176
177
        $this->storage->set('deleted_thought_content', $content);
178
    }
179
180
    /**
181
     * @Then I should not be allowed to delete the :content thought
182
     */
183
    public function iShouldNotBeAllowedToDeleteTheThought(string $content) : void
184
    {
185
        try {
186
            $this->homePage->deleteThought($content);
187
188
            throw new \Exception('Found a forbidden delete button.');
189
        } catch (\Exception $e) {
190
            // it's alright
191
        }
192
    }
193
194
    /**
195
     * @Then my number of shared thoughts should be :number
196
     */
197
    public function myNumberOfSharedThoughtsShouldBe(int $number) : void
198
    {
199
        $this->browsingUsersPage->open();
200
        $name = $this->storage->get('logged_user')->getName();
201
        $card = $this->browsingUsersPage->getUserCard($name);
202
203
        Assert::eq($number, $card->getNumberOfThoughts(), 'Expected number of thoughts to be %s, but it is %s.');
204
    }
205
}
206