|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace FriendsOfBehat\PageObjectExtension\Page; |
| 6 | + |
| 7 | +use Behat\Mink\Session; |
| 8 | +use Symfony\Component\Routing\RouterInterface; |
| 9 | + |
| 10 | +abstract class SymfonyPage extends Page implements SymfonyPageInterface |
| 11 | +{ |
| 12 | + /** @var RouterInterface */ |
| 13 | + protected $router; |
| 14 | + |
| 15 | + /** @var array */ |
| 16 | + protected static $additionalParameters = ['_locale' => 'en_US']; |
| 17 | + |
| 18 | + public function __construct(Session $session, array $parameters, RouterInterface $router) |
| 19 | + { |
| 20 | + parent::__construct($session, $parameters); |
| 21 | + |
| 22 | + $this->router = $router; |
| 23 | + } |
| 24 | + |
| 25 | + abstract public function getRouteName(): string; |
| 26 | + |
| 27 | + /** |
| 28 | + * @throws UnexpectedPageException |
| 29 | + */ |
| 30 | + public function verifyRoute(array $requiredUrlParameters = []): void |
| 31 | + { |
| 32 | + $url = $this->getDriver()->getCurrentUrl(); |
| 33 | + $path = parse_url($url)['path']; |
| 34 | + |
| 35 | + $path = preg_replace('#^/app(_dev|_test|_test_cached)?\.php/#', '/', $path); |
| 36 | + $matchedRoute = $this->router->match($path); |
| 37 | + |
| 38 | + $this->verifyRouteName($matchedRoute, $url); |
| 39 | + $this->verifyRouteParameters($requiredUrlParameters, $matchedRoute); |
| 40 | + } |
| 41 | + |
| 42 | + final protected function makePathAbsolute(string $path): string |
| 43 | + { |
| 44 | + $baseUrl = rtrim($this->getParameter('base_url'), '/') . '/'; |
| 45 | + |
| 46 | + return 0 !== strpos($path, 'http') ? $baseUrl . ltrim($path, '/') : $path; |
| 47 | + } |
| 48 | + |
| 49 | + protected function getUrl(array $urlParameters = []): string |
| 50 | + { |
| 51 | + $path = $this->router->generate($this->getRouteName(), $urlParameters + static::$additionalParameters); |
| 52 | + |
| 53 | + $replace = []; |
| 54 | + foreach (static::$additionalParameters as $key => $value) { |
| 55 | + $replace[sprintf('&%s=%s', $key, $value)] = ''; |
| 56 | + $replace[sprintf('?%s=%s&', $key, $value)] = '?'; |
| 57 | + $replace[sprintf('?%s=%s', $key, $value)] = ''; |
| 58 | + } |
| 59 | + |
| 60 | + $path = str_replace(array_keys($replace), array_values($replace), $path); |
| 61 | + |
| 62 | + return $this->makePathAbsolute($path); |
| 63 | + } |
| 64 | + |
| 65 | + protected function verifyUrl(array $urlParameters = []): void |
| 66 | + { |
| 67 | + $url = $this->getDriver()->getCurrentUrl(); |
| 68 | + $path = parse_url($url)['path']; |
| 69 | + |
| 70 | + $path = preg_replace('#^/app(_dev|_test|_test_cached)?\.php/#', '/', $path); |
| 71 | + $matchedRoute = $this->router->match($path); |
| 72 | + |
| 73 | + if (isset($matchedRoute['_locale'])) { |
| 74 | + $urlParameters += ['_locale' => $matchedRoute['_locale']]; |
| 75 | + } |
| 76 | + |
| 77 | + parent::verifyUrl($urlParameters); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @throws UnexpectedPageException |
| 82 | + */ |
| 83 | + private function verifyRouteName(array $matchedRoute, string $url): void |
| 84 | + { |
| 85 | + if ($matchedRoute['_route'] !== $this->getRouteName()) { |
| 86 | + throw new UnexpectedPageException( |
| 87 | + sprintf( |
| 88 | + "Matched route '%s' does not match the expected route '%s' for URL '%s'", |
| 89 | + $matchedRoute['_route'], |
| 90 | + $this->getRouteName(), |
| 91 | + $url |
| 92 | + ) |
| 93 | + ); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @throws UnexpectedPageException |
| 99 | + */ |
| 100 | + private function verifyRouteParameters(array $requiredUrlParameters, array $matchedRoute): void |
| 101 | + { |
| 102 | + foreach ($requiredUrlParameters as $key => $value) { |
| 103 | + if (!isset($matchedRoute[$key]) || $matchedRoute[$key] !== $value) { |
| 104 | + throw new UnexpectedPageException( |
| 105 | + sprintf( |
| 106 | + "Matched route does not match the expected parameter '%s'='%s' (%s found)", |
| 107 | + $key, |
| 108 | + $value, |
| 109 | + $matchedRoute[$key] ?? 'null' |
| 110 | + ) |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments