Passed
Push — master ( 8f0a1e...89cf34 )
by Loïc
05:43 queued 11s
created

AbstractCreatePage   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getValidationMessage() 0 13 3
A __construct() 0 3 1
A create() 0 3 1
A getFieldElement() 0 8 3
1
<?php
2
3
/*
4
 * This file is part of the Monofony package.
5
 *
6
 * (c) Monofony
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Monofony\Bridge\Behat\Crud;
13
14
use Behat\Mink\Exception\ElementNotFoundException;
15
use Behat\Mink\Session;
16
use FriendsOfBehat\PageObjectExtension\Page\SymfonyPage;
17
use Symfony\Component\Routing\RouterInterface;
18
19
abstract class AbstractCreatePage extends SymfonyPage implements CreatePageInterface
20
{
21
    public function __construct(Session $session, \ArrayAccess $minkParameters, RouterInterface $router)
22
    {
23
        parent::__construct($session, $minkParameters, $router);
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function create(): void
30
    {
31
        $this->getDocument()->pressButton('Create');
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getValidationMessage(string $element): string
38
    {
39
        $foundElement = $this->getFieldElement($element);
40
        if (null === $foundElement) {
41
            throw new ElementNotFoundException($this->getSession(), 'Field element');
42
        }
43
44
        $validationMessage = $foundElement->find('css', '.sylius-validation-error');
45
        if (null === $validationMessage) {
46
            throw new ElementNotFoundException($this->getSession(), 'Validation message', 'css', '.sylius-validation-error');
47
        }
48
49
        return $validationMessage->getText();
50
    }
51
52
    /**
53
     * @param string $element
54
     *
55
     * @return \Behat\Mink\Element\NodeElement|null
56
     *
57
     * @throws ElementNotFoundException
58
     */
59
    private function getFieldElement($element)
60
    {
61
        $element = $this->getElement($element);
62
        while (null !== $element && !$element->hasClass('field')) {
63
            $element = $element->getParent();
64
        }
65
66
        return $element;
67
    }
68
}
69