[Behat] Introduce current page resolver

This commit is contained in:
Łukasz Chruściel 2016-03-31 14:59:08 +02:00
parent cd277e85ee
commit 3aa7377783
5 changed files with 204 additions and 0 deletions

View file

@ -26,6 +26,7 @@
<parameter key="sylius.behat.response_loader.class">Sylius\Behat\Service\ResponseLoader</parameter>
<parameter key="sylius.behat.notification_accessor.class">Sylius\Behat\Service\Accessor\NotificationAccessor</parameter>
<parameter key="sylius.behat.notification_checker.class">Sylius\Behat\Service\NotificationChecker</parameter>
<parameter key="sylius.behat.current_page_resolver.class">Sylius\Behat\Service\CurrentPageResolver</parameter>
</parameters>
<services>
<service id="mink.default_session" class="Behat\Mink\Session" factory-service="mink" factory-method="getSession" lazy="true" scope="scenario" public="false" />
@ -63,5 +64,9 @@
<service id="sylius.behat.notification_checker" class="%sylius.behat.notification_checker.class%" scope="scenario" public="false">
<argument type="service" id="sylius.behat.notification_accessor" />
</service>
<service id="sylius.behat.current_page_resolver" class="%sylius.behat.current_page_resolver.class%" scope="scenario" public="false">
<argument type="service" id="mink.default_session" />
<argument type="service" id="router" container="symfony" />
</service>
</services>
</container>

View file

@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Behat\Page\Admin\Crud;
use Sylius\Behat\Page\PageInterface;
/**
* @author Łukasz Chruściel <lukasz.chrusciel@lakion.com>
*/
interface PageWithFormInterface extends PageInterface
{
/**
* @param string $element
* @param string $message
*
* @return bool
*/
public function checkValidationMessageFor($element, $message);
}

View file

@ -0,0 +1,63 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Behat\Service;
use Behat\Mink\Session;
use Sylius\Behat\Page\Admin\Crud\CreatePageInterface;
use Sylius\Behat\Page\Admin\Crud\UpdatePageInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
/**
* @author Łukasz Chruściel <lukasz.chrusciel@lakion.com>
*/
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.');
}
}

View file

@ -0,0 +1,29 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Behat\Service;
use Sylius\Behat\Page\Admin\Crud\CreatePageInterface;
use Sylius\Behat\Page\Admin\Crud\PageWithFormInterface;
use Sylius\Behat\Page\Admin\Crud\UpdatePageInterface;
/**
* @author Łukasz Chruściel <lukasz.chrusciel@lakion.com>
*/
interface CurrentPageResolverInterface
{
/**
* @param CreatePageInterface $createPage
* @param UpdatePageInterface $updatePage
*
* @return PageWithFormInterface
*/
public function getCurrentPageWithForm(CreatePageInterface $createPage, UpdatePageInterface $updatePage);
}

View file

@ -0,0 +1,79 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace spec\Sylius\Behat\Service;
use Behat\Mink\Session;
use PhpSpec\ObjectBehavior;
use Sylius\Behat\Page\Admin\Crud\CreatePageInterface;
use Sylius\Behat\Page\Admin\Crud\UpdatePageInterface;
use Sylius\Behat\Service\CurrentPageResolver;
use Sylius\Behat\Service\CurrentPageResolverInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
/**
* @mixin CurrentPageResolver
*
* @author Łukasz Chruściel <lukasz.chrusciel@lakion.com>
*/
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]);
}
}