Completed
Push — utc-datetime-storing ( 6fbf75...e1fcef )
by Kamil
25:51
created

SymfonyPage::verifyRouteParameters()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 2
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
declare(strict_types=1);
13
14
namespace Sylius\Behat\Page;
15
16
use Behat\Mink\Element\NodeElement;
17
use Behat\Mink\Session;
18
use Symfony\Component\Routing\RouterInterface;
19
20
/**
21
 * @author Arkadiusz Krakowiak <[email protected]>
22
 */
23
abstract class SymfonyPage extends Page implements SymfonyPageInterface
0 ignored issues
show
Coding Style introduced by
SymfonyPage does not seem to conform to the naming convention (^Abstract).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
24
{
25
    /**
26
     * @var RouterInterface
27
     */
28
    protected $router;
29
30
    /**
31
     * @var array
32
     */
33
    protected static $additionalParameters = ['_locale' => 'en_US'];
34
35
    /**
36
     * @param Session $session
37
     * @param array $parameters
38
     * @param RouterInterface $router
39
     */
40
    public function __construct(Session $session, array $parameters, RouterInterface $router)
41
    {
42
        parent::__construct($session, $parameters);
43
44
        $this->router = $router;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    abstract public function getRouteName();
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function getUrl(array $urlParameters = [])
56
    {
57
        $path = $this->router->generate($this->getRouteName(), $urlParameters + static::$additionalParameters);
58
59
        $replace = [];
60
        foreach (static::$additionalParameters as $key => $value) {
61
            $replace[sprintf('&%s=%s', $key, $value)] = '';
62
            $replace[sprintf('?%s=%s&', $key, $value)] = '?';
63
            $replace[sprintf('?%s=%s', $key, $value)] = '';
64
        }
65
66
        $path = str_replace(array_keys($replace), array_values($replace), $path);
67
68
        return $this->makePathAbsolute($path);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    protected function verifyUrl(array $urlParameters = [])
75
    {
76
        $url = $this->getDriver()->getCurrentUrl();
77
        $path = parse_url($url)['path'];
78
79
        $path = preg_replace('#^/app(_dev|_test|_test_cached)?\.php/#', '/', $path);
80
        $matchedRoute = $this->router->match($path);
81
82
        if (isset($matchedRoute['_locale'])) {
83
            $urlParameters += ['_locale' => $matchedRoute['_locale']];
84
        }
85
86
        parent::verifyUrl($urlParameters);
87
    }
88
89
    /**
90
     * @param array $requiredUrlParameters
91
     *
92
     * @throws UnexpectedPageException
93
     */
94
    public function verifyRoute(array $requiredUrlParameters = [])
95
    {
96
        $url = $this->getDriver()->getCurrentUrl();
97
        $path = parse_url($url)['path'];
98
99
        $path = preg_replace('#^/app(_dev|_test|_test_cached)?\.php/#', '/', $path);
100
        $matchedRoute = $this->router->match($path);
101
102
        $this->verifyRouteName($matchedRoute, $url);
103
        $this->verifyRouteParameters($requiredUrlParameters, $matchedRoute);
104
    }
105
106
107
    /**
108
     * @param array $matchedRoute
109
     * @param string $url
110
     *
111
     * @throws UnexpectedPageException
112
     */
113
    private function verifyRouteName(array $matchedRoute, string $url)
114
    {
115
        if ($matchedRoute['_route'] !== $this->getRouteName()) {
116
            throw new UnexpectedPageException(
117
                sprintf(
118
                    "Matched route '%s' does not match the expected route '%s' for URL '%s'",
119
                    $matchedRoute['_route'],
120
                    $this->getRouteName(),
121
                    $url
122
                )
123
            );
124
        }
125
    }
126
127
    /**
128
     * @param array $requiredUrlParameters
129
     * @param array $matchedRoute
130
     *
131
     * @throws UnexpectedPageException
132
     */
133
    private function verifyRouteParameters(array $requiredUrlParameters, array $matchedRoute)
134
    {
135
        foreach ($requiredUrlParameters as $key => $value) {
136
            if (!isset($matchedRoute[$key]) || $matchedRoute[$key] !== $value) {
137
                throw new UnexpectedPageException(
138
                    sprintf(
139
                        "Matched route does not match the expected parameter '%s'='%s' (%s found)",
140
                        $key,
141
                        $value,
142
                        $matchedRoute[$key] ?? 'null'
143
                    )
144
                );
145
            }
146
        }
147
    }
148
149
    /**
150
     * @param NodeElement $modalContainer
151
     * @param string $appearClass
152
     *
153
     * @todo it really shouldn't be here :)
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
154
     */
155
    protected function waitForModalToAppear(NodeElement $modalContainer, $appearClass = 'in')
156
    {
157
        $this->getDocument()->waitFor(1, function () use ($modalContainer, $appearClass) {
158
            return false !== strpos($modalContainer->getAttribute('class'), $appearClass);
159
        });
160
    }
161
162
    /**
163
     * @param string $path
164
     *
165
     * @return string
166
     */
167
    final protected function makePathAbsolute($path)
168
    {
169
        $baseUrl = rtrim($this->getParameter('base_url'), '/').'/';
170
171
        return 0 !== strpos($path, 'http') ? $baseUrl.ltrim($path, '/') : $path;
172
    }
173
}
174