Completed
Push — remove-getResourceName ( dd7dce )
by Kamil
18:43
created

UpdatePage::getResourceName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Behat\Page\Admin\Crud;
13
14
use Behat\Mink\Exception\ElementNotFoundException;
15
use Behat\Mink\Session;
16
use Sylius\Behat\Page\SymfonyPage;
17
use Sylius\Component\Core\Formatter\StringInflector;
18
use Symfony\Component\Routing\RouterInterface;
19
20
/**
21
 * @author Arkadiusz Krakowiak <[email protected]>
22
 */
23
class UpdatePage extends SymfonyPage implements UpdatePageInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    private $resourceName;
29
30
    /**
31
     * @param Session $session
32
     * @param array $parameters
33
     * @param RouterInterface $router
34
     * @param string $resourceName
35
     */
36
    public function __construct(Session $session, array $parameters, RouterInterface $router, $resourceName)
37
    {
38
        parent::__construct($session, $parameters, $router);
39
40
        $this->resourceName = strtolower($resourceName);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function saveChanges()
47
    {
48
        $this->getDocument()->pressButton('sylius_save_changes_button');
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getValidationMessage($element)
55
    {
56
        $foundElement = $this->getFieldElement($element);
57
        if (null === $foundElement) {
58
            throw new ElementNotFoundException($this->getSession(), 'Field element');
59
        }
60
61
        $validationMessage = $foundElement->find('css', '.sylius-validation-error');
62
        if (null === $validationMessage) {
63
            throw new ElementNotFoundException($this->getSession(), 'Validation message', 'css', '.sylius-validation-error');
64
        }
65
66
        return $validationMessage->getText();
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function hasResourceValues(array $parameters)
73
    {
74
        foreach ($parameters as $element => $value) {
75
            if ($this->getElement($element)->getValue() !== (string) $value) {
76
                return false;
77
            }
78
        }
79
80
        return true;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getRouteName()
87
    {
88
        return sprintf('sylius_admin_%s_update', $this->resourceName);
89
    }
90
91
    /**
92
     * @param string $element
93
     *
94
     * @return \Behat\Mink\Element\NodeElement|null
95
     *
96
     * @throws ElementNotFoundException
97
     */
98
    private function getFieldElement($element)
99
    {
100
        $element = $this->getElement(StringInflector::nameToCode($element));
101
        while (null !== $element && !($element->hasClass('field'))) {
102
            $element = $element->getParent();
103
        }
104
105
        return $element;
106
    }
107
}
108