From 3aa7377783d893355d8e7aeb2a06fcc2428e40c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Chru=C5=9Bciel?= Date: Thu, 31 Mar 2016 14:59:08 +0200 Subject: [PATCH] [Behat] Introduce current page resolver --- etc/behat/services.xml | 5 ++ .../Page/Admin/Crud/PageWithFormInterface.php | 28 +++++++ .../Behat/Service/CurrentPageResolver.php | 63 +++++++++++++++ .../Service/CurrentPageResolverInterface.php | 29 +++++++ .../spec/Service/CurrentPageResolverSpec.php | 79 +++++++++++++++++++ 5 files changed, 204 insertions(+) create mode 100644 src/Sylius/Behat/Page/Admin/Crud/PageWithFormInterface.php create mode 100644 src/Sylius/Behat/Service/CurrentPageResolver.php create mode 100644 src/Sylius/Behat/Service/CurrentPageResolverInterface.php create mode 100644 src/Sylius/Behat/spec/Service/CurrentPageResolverSpec.php diff --git a/etc/behat/services.xml b/etc/behat/services.xml index b6603f103e..109f7dfa69 100644 --- a/etc/behat/services.xml +++ b/etc/behat/services.xml @@ -26,6 +26,7 @@ Sylius\Behat\Service\ResponseLoader Sylius\Behat\Service\Accessor\NotificationAccessor Sylius\Behat\Service\NotificationChecker + Sylius\Behat\Service\CurrentPageResolver @@ -63,5 +64,9 @@ + + + + diff --git a/src/Sylius/Behat/Page/Admin/Crud/PageWithFormInterface.php b/src/Sylius/Behat/Page/Admin/Crud/PageWithFormInterface.php new file mode 100644 index 0000000000..61d5f056df --- /dev/null +++ b/src/Sylius/Behat/Page/Admin/Crud/PageWithFormInterface.php @@ -0,0 +1,28 @@ + + */ +interface PageWithFormInterface extends PageInterface +{ + /** + * @param string $element + * @param string $message + * + * @return bool + */ + public function checkValidationMessageFor($element, $message); +} diff --git a/src/Sylius/Behat/Service/CurrentPageResolver.php b/src/Sylius/Behat/Service/CurrentPageResolver.php new file mode 100644 index 0000000000..309c1da7e3 --- /dev/null +++ b/src/Sylius/Behat/Service/CurrentPageResolver.php @@ -0,0 +1,63 @@ + + */ +final class CurrentPageResolver implements CurrentPageResolverInterface +{ + /** + * @var Session + */ + private $session; + + /** + * @var UrlMatcherInterface + */ + private $urlMatcher; + + /** + * @param Session $session + * @param UrlMatcherInterface $urlMatcher + */ + public function __construct(Session $session, UrlMatcherInterface $urlMatcher) + { + $this->session = $session; + $this->urlMatcher = $urlMatcher; + } + + /** + * {@inheritdoc} + * + * @throws \LogicException + */ + public function getCurrentPageWithForm(CreatePageInterface $createPage, UpdatePageInterface $updatePage) + { + $routeParameters = $this->urlMatcher->match($this->session->getCurrentUrl()); + + if (false !== strpos($routeParameters['_route'], 'create')) { + return $createPage; + } + + if (false !== strpos($routeParameters['_route'], 'update')) { + return $updatePage; + } + + throw new \LogicException('Route name does not have any of "update" or "create" keyword, so matcher was unable to match proper page.'); + } +} diff --git a/src/Sylius/Behat/Service/CurrentPageResolverInterface.php b/src/Sylius/Behat/Service/CurrentPageResolverInterface.php new file mode 100644 index 0000000000..6f358d20da --- /dev/null +++ b/src/Sylius/Behat/Service/CurrentPageResolverInterface.php @@ -0,0 +1,29 @@ + + */ +interface CurrentPageResolverInterface +{ + /** + * @param CreatePageInterface $createPage + * @param UpdatePageInterface $updatePage + * + * @return PageWithFormInterface + */ + public function getCurrentPageWithForm(CreatePageInterface $createPage, UpdatePageInterface $updatePage); +} diff --git a/src/Sylius/Behat/spec/Service/CurrentPageResolverSpec.php b/src/Sylius/Behat/spec/Service/CurrentPageResolverSpec.php new file mode 100644 index 0000000000..44a21ee8b1 --- /dev/null +++ b/src/Sylius/Behat/spec/Service/CurrentPageResolverSpec.php @@ -0,0 +1,79 @@ + + */ +class CurrentPageResolverSpec extends ObjectBehavior +{ + function let(Session $session, UrlMatcherInterface $urlMatcher) + { + $this->beConstructedWith($session, $urlMatcher); + } + + function it_is_initializable() + { + $this->shouldHaveType('Sylius\Behat\Service\CurrentPageResolver'); + } + + function it_implements_current_page_resolver_interface() + { + $this->shouldImplement(CurrentPageResolverInterface::class); + } + + function it_returns_create_page_if_current_route_name_contains_create_word( + Session $session, + CreatePageInterface $createPage, + UpdatePageInterface $updatePage, + UrlMatcherInterface $urlMatcher + ) { + $session->getCurrentUrl()->willReturn('https://sylius.com/resource/new'); + $urlMatcher->match('https://sylius.com/resource/new')->willReturn(['_route' => 'sylius_resource_create']); + + $this->getCurrentPageWithForm($createPage, $updatePage)->shouldReturn($createPage); + } + + function it_returns_update_page_if_current_route_name_contains_update_word( + Session $session, + CreatePageInterface $createPage, + UpdatePageInterface $updatePage, + UrlMatcherInterface $urlMatcher + ) { + $session->getCurrentUrl()->willReturn('https://sylius.com/resource/edit'); + $urlMatcher->match('https://sylius.com/resource/edit')->willReturn(['_route' => 'sylius_resource_update']); + + $this->getCurrentPageWithForm($createPage, $updatePage)->shouldReturn($updatePage); + } + + function it_throws_an_exception_if_neither_create_nor_update_key_word_has_been_found( + Session $session, + CreatePageInterface $createPage, + UpdatePageInterface $updatePage, + UrlMatcherInterface $urlMatcher + ) { + $session->getCurrentUrl()->willReturn('https://sylius.com/resource/show'); + $urlMatcher->match('https://sylius.com/resource/show')->willReturn(['_route' => 'sylius_resource_show']); + + $this->shouldThrow(\LogicException::class)->during('getCurrentPageWithForm', [$createPage, $updatePage]); + } +}