Completed
Pull Request — 3.1 (#348)
by Piotr
09:36 queued 07:40
created

NavigationContext::iPressButtonAtPagination()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminBundle\Behat\Context;
13
14
use Behat\Gherkin\Node\TableNode;
15
use Exception;
16
use FSi\Bundle\AdminBundle\Behat\Page\AdminPanel;
17
use FSi\Bundle\AdminBundle\Behat\Page\CategoryList;
18
use FSi\Bundle\AdminBundle\Behat\Page\CategoryNewsCreate;
19
use FSi\Bundle\AdminBundle\Behat\Page\CategoryNewsEdit;
20
use FSi\Bundle\AdminBundle\Behat\Page\CategoryNewsList;
21
use FSi\Bundle\AdminBundle\Behat\Page\CustomNewsEdit;
22
use FSi\Bundle\AdminBundle\Behat\Page\CustomNewsList;
23
use FSi\Bundle\AdminBundle\Behat\Page\CustomSubscribersList;
24
use FSi\Bundle\AdminBundle\Behat\Page\DefaultPage;
25
use FSi\Bundle\AdminBundle\Behat\Page\DTOForm;
26
use FSi\Bundle\AdminBundle\Behat\Page\NewsDisplay;
27
use FSi\Bundle\AdminBundle\Behat\Page\HomePageEdit;
28
use FSi\Bundle\AdminBundle\Behat\Page\NewsCreate;
29
use FSi\Bundle\AdminBundle\Behat\Page\NewsEdit;
30
use FSi\Bundle\AdminBundle\Behat\Page\NewsList;
31
use FSi\Bundle\AdminBundle\Behat\Page\Page;
32
use FSi\Bundle\AdminBundle\Behat\Page\PersonAddForm;
33
use FSi\Bundle\AdminBundle\Behat\Page\PersonEditForm;
34
use FSi\Bundle\AdminBundle\Behat\Page\PersonList;
35
use FSi\Bundle\AdminBundle\Behat\Page\SubscriberEdit;
36
use FSi\Bundle\AdminBundle\Behat\Page\SubscriberForm;
37
use FSi\Bundle\AdminBundle\Behat\Page\SubscribersList;
38
use InvalidArgumentException;
39
40
class NavigationContext extends AbstractContext
41
{
42
    /**
43
     * @var DefaultPage
44
     */
45
    private $defaultPage;
46
47
    /**
48
     * @var AdminPanel
49
     */
50
    private $adminPanelPage;
51
52
    /**
53
     * @var CustomNewsEdit
54
     */
55
    private $customNewsEditPage;
56
57
    /**
58
     * @var CustomNewsList
59
     */
60
    private $customNewsListPage;
61
62
    /**
63
     * @var CustomSubscribersList
64
     */
65
    private $customSubscribersListPage;
66
67
    /**
68
     * @var DTOForm
69
     */
70
    private $dtoFormPage;
71
72
    /**
73
     * @var HomePageEdit
74
     */
75
    private $homePageEditPage;
76
77
    /**
78
     * @var NewsCreate
79
     */
80
    private $newsCreatePage;
81
82
    /**
83
     * @var NewsDisplay
84
     */
85
    private $newsDisplayPage;
86
87
    /**
88
     * @var NewsEdit
89
     */
90
    private $newsEditPage;
91
92
    /**
93
     * @var CategoryList
94
     */
95
    private $categoryListPage;
96
97
    /**
98
     * @var CategoryNewsList
99
     */
100
    private $categoryNewsListPage;
101
102
    /**
103
     * @var CategoryNewsCreate
104
     */
105
    private $categoryNewsCreatePage;
106
107
    /**
108
     * @var CategoryNewsEdit
109
     */
110
    private $categoryNewsEditPage;
111
112
    /**
113
     * @va NewsList
114
     */
115
    private $newsListPage;
116
117
    /**
118
     * @var PersonAddForm
119
     */
120
    private $personAddFormPage;
121
122
    /**
123
     * @var PersonEditForm
124
     */
125
    private $personEditFormPage;
126
127
    /**
128
     * @var PersonList
129
     */
130
    private $personListPage;
131
132
    /**
133
     * @var SubscriberEdit
134
     */
135
    private $subscriberEditPage;
136
137
    /**
138
     * @var SubscriberForm
139
     */
140
    private $subscriberFormPage;
141
142
    /**
143
     * @var SubscribersList
144
     */
145
    private $subscribersListPage;
146
147
    public function __construct(
148
        DefaultPage $defaultPage,
149
        AdminPanel $adminPanelPage,
150
        CustomNewsEdit $customNewsEditPage,
151
        CustomNewsList $customNewsListPage,
152
        CustomSubscribersList $customSubscribersListPage,
153
        DTOForm $dtoFormPage,
154
        HomePageEdit $homePageEditPage,
155
        NewsCreate $newsCreatePage,
156
        NewsDisplay $newsDisplayPage,
157
        NewsEdit $newsEditPage,
158
        NewsList $newsListPage,
159
        CategoryList $categoryListPage,
160
        CategoryNewsList $categoryNewsListPage,
161
        CategoryNewsCreate $categoryNewsCreatePage,
162
        CategoryNewsEdit $categoryNewsEditPage,
163
        PersonAddForm $personAddFormPage,
164
        PersonEditForm $personEditFormPage,
165
        PersonList $personListPage,
166
        SubscriberEdit $subscriberEditPage,
167
        SubscriberForm $subscriberFormPage,
168
        SubscribersList $subscribersListPage
169
    ) {
170
        $this->defaultPage = $defaultPage;
171
        $this->adminPanelPage = $adminPanelPage;
172
        $this->customNewsEditPage = $customNewsEditPage;
173
        $this->customNewsListPage = $customNewsListPage;
174
        $this->customSubscribersListPage = $customSubscribersListPage;
175
        $this->dtoFormPage = $dtoFormPage;
176
        $this->homePageEditPage = $homePageEditPage;
177
        $this->newsCreatePage = $newsCreatePage;
178
        $this->newsDisplayPage = $newsDisplayPage;
179
        $this->newsEditPage = $newsEditPage;
180
        $this->newsListPage = $newsListPage;
181
        $this->categoryListPage = $categoryListPage;
182
        $this->categoryNewsListPage = $categoryNewsListPage;
183
        $this->categoryNewsCreatePage = $categoryNewsCreatePage;
184
        $this->categoryNewsEditPage = $categoryNewsEditPage;
185
        $this->personAddFormPage = $personAddFormPage;
186
        $this->personEditFormPage = $personEditFormPage;
187
        $this->personListPage = $personListPage;
188
        $this->subscriberEditPage = $subscriberEditPage;
189
        $this->subscriberFormPage = $subscriberFormPage;
190
        $this->subscribersListPage = $subscribersListPage;
191
    }
192
193
    /**
194
     * @transform :page
195
     */
196
    public function transformToPageObject($pageName)
197
    {
198
        switch ($pageName) {
199
            case 'News list':
200
                return $this->newsListPage;
201
            case 'Admin panel':
202
                return $this->adminPanelPage;
203
            case 'Custom news edit':
204
                return $this->customNewsEditPage;
205
            case 'Custom news list':
206
                return $this->customNewsListPage;
207
            case 'Custom subscribers list':
208
                return $this->customSubscribersListPage;
209
            case 'DTO Form':
210
                return $this->dtoFormPage;
211
            case 'Home page edit':
212
                return $this->homePageEditPage;
213
            case 'News create':
214
                return $this->newsCreatePage;
215
            case 'News display':
216
                return $this->newsDisplayPage;
217
            case 'News edit':
218
                return $this->newsEditPage;
219
            case 'Category list':
220
                return $this->categoryListPage;
221
            case 'Category news list':
222
                return $this->categoryNewsListPage;
223
            case 'Category news edit':
224
                return $this->categoryNewsEditPage;
225
            case 'Category news create':
226
                return $this->categoryNewsCreatePage;
227
            case 'Person add form':
228
                return $this->personAddFormPage;
229
            case 'Person edit form':
230
                return $this->personEditFormPage;
231
            case 'Person list':
232
                return $this->personListPage;
233
            case 'Subscriber edit':
234
                return $this->subscriberEditPage;
235
            case 'Subscriber form':
236
                return $this->subscriberFormPage;
237
            case 'Subscribers list':
238
                return $this->subscribersListPage;
239
            default:
240
                throw new InvalidArgumentException(
241
                    sprintf('Could not transform "%s" to any page object', $pageName)
242
                );
243
        }
244
    }
245
246
    /**
247
     * @When /^I follow "([^"]*)" url from top bar$/
248
     * @Given /^I follow "([^"]*)" menu element$/
249
     */
250
    public function iFollowUrlFromTopBar($menuElement)
251
    {
252
        $this->adminPanelPage->getMenu()->clickLink($menuElement);
253
    }
254
255
    /**
256
     * @Then /^menu with following elements should be visible at the top of the page$/
257
     */
258
    public function menuWithFollowingElementsShouldBeVisibleAtTheTopOfThePage(TableNode $table)
259
    {
260
        expect($this->adminPanelPage->getMenuElementsCount())->toBe(count($table->getHash()));
261
262
        foreach ($table->getHash() as $elementRow) {
263
            expect($this->adminPanelPage->hasMenuElement(
264
                $elementRow['Element name'],
265
                empty($elementRow['Element group']) ? null : $elementRow['Element group'])
266
            )->toBe(true);
267
        }
268
    }
269
270
    /**
271
     * @Then :link link in the top bar should be highlighted
272
     */
273
    public function linkInTheTopBarShouldBeHighlighted($link)
274
    {
275
        $linkNode = $this->adminPanelPage->getMenu()->findLink($link);
276
277
        expect($linkNode->getParent()->hasClass('active'))->toBe(true);
278
    }
279
280
    /**
281
     * @Given I am on the :page page
282
     */
283
    public function iAmOnThePage(Page $page)
284
    {
285
        $page->open();
286
    }
287
288
    /**
289
     * @Given I am on the :page page with id :id
290
     */
291
    public function iAmOnThePageWithId(Page $page, $id)
292
    {
293
        $page->open(['id' => $id]);
294
    }
295
296
    /**
297
     * @Given I should be on the :page page
298
     * @Given I should be redirected to :page page
299
     */
300
    public function iShouldBeOnThePage(Page $page)
301
    {
302
        expect($page->isOpen())->toBe(true);
303
    }
304
305
    /**
306
     * @Given I try to open the :page page
307
     */
308
    public function iTryToOpenPage(Page $page)
309
    {
310
        $page->openWithoutVerifying();
311
    }
312
313
    /**
314
     * @Given /^I press "New element" link$/
315
     */
316
    public function iPressNewElementLink()
317
    {
318
        $this->defaultPage->getElement('New Element Link')->click();
319
    }
320
321
    /**
322
     * @When /^I press "([^"]*)" button at pagination$/
323
     */
324
    public function iPressButtonAtPagination($button)
325
    {
326
        $this->defaultPage->getElement('Pagination')->clickLink($button);
327
    }
328
329
    /**
330
     * @Then /^I should see pagination with following buttons$/
331
     */
332
    public function iShouldSeePaginationWithFollowingButtons(TableNode $table)
333
    {
334
        $pagination = $this->defaultPage->getElement('Pagination');
335
336
        foreach ($table->getHash() as $buttonRow) {
337
            expect($pagination->hasLink($buttonRow['Button']))->toBe(true);
338
339
            if ($buttonRow['Active'] === 'true') {
340
                expect($pagination->isDisabled($buttonRow['Button']))->toBe(false);
341
            } else {
342
                expect($pagination->isDisabled($buttonRow['Button']))->toBe(true);
343
            }
344
345
            if ($buttonRow['Current'] === 'true') {
346
                expect($pagination->isCurrentPage($buttonRow['Button']))->toBe(true);
347
            } else {
348
                expect($pagination->isCurrentPage($buttonRow['Button']))->toBe(false);
349
            }
350
        }
351
    }
352
353
    /**
354
     * @Then page :page should display OK status
355
     */
356
    public function pageShouldDisplayOKStatus(Page $page)
357
    {
358
        $this->expectPageStatus($page, 200);
359
    }
360
361
    /**
362
     * @Then page :page should display not found exception
363
     */
364
    public function pageShouldDisplayNotFoundException(Page $page)
365
    {
366
        $this->expectPageStatus($page, 404);
367
    }
368
369
    /**
370
     * @Then page :page should throw an error exception
371
     */
372
    public function pageShouldThrowAnErrorExceptionException(Page $page)
373
    {
374
        $this->expectPageStatus($page, 500);
375
    }
376
377
    private function expectPageStatus(Page $page, int $expectedStatus): void
378
    {
379
        $status = $page->getStatusCode();
380
        if ($status !== $expectedStatus) {
381
            throw new Exception(sprintf(
382
                'Invalid status code "%s", expected "%s".',
383
                $status,
384
                $expectedStatus
385
            ));
386
        }
387
    }
388
}
389