mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-14 17:10:53 +00:00
[Behat] Added CookieSetter
This commit is contained in:
parent
1d3cfc400c
commit
9224604dcc
4 changed files with 171 additions and 0 deletions
|
|
@ -17,12 +17,18 @@
|
|||
<import resource="services/pages.xml"/>
|
||||
</imports>
|
||||
<parameters>
|
||||
<parameter key="sylius.behat.cookie_setter.class">Sylius\Behat\CookieSetter</parameter>
|
||||
<parameter key="sylius.behat.page.class">Sylius\Behat\Page\Page</parameter>
|
||||
<parameter key="sylius.behat.symfony_page.class">Sylius\Behat\Page\SymfonyPage</parameter>
|
||||
</parameters>
|
||||
<services>
|
||||
<service id="mink.default_session" class="Behat\Mink\Session" factory-service="mink" factory-method="getSession" lazy="true" scope="scenario" public="false" />
|
||||
|
||||
<service id="sylius.behat.cookie_setter" class="%sylius.behat.cookie_setter.class%" scope="scenario" public="false">
|
||||
<argument type="service" id="mink.default_session" />
|
||||
<argument>%mink.parameters%</argument>
|
||||
</service>
|
||||
|
||||
<service id="sylius.behat.page" class="%sylius.behat.page.class%" abstract="true" scope="scenario" public="false">
|
||||
<argument type="service" id="mink.default_session" />
|
||||
<argument>%mink.parameters%</argument>
|
||||
|
|
|
|||
64
src/Sylius/Behat/CookieSetter.php
Normal file
64
src/Sylius/Behat/CookieSetter.php
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
<?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;
|
||||
|
||||
use Behat\Mink\Session;
|
||||
use Behat\Symfony2Extension\Driver\KernelDriver;
|
||||
|
||||
/**
|
||||
* @author Kamil Kokot <kamil.kokot@lakion.com>
|
||||
*/
|
||||
final class CookieSetter implements CookieSetterInterface
|
||||
{
|
||||
/**
|
||||
* @var Session
|
||||
*/
|
||||
private $minkSession;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $minkParameters;
|
||||
|
||||
/**
|
||||
* @param Session $minkSession
|
||||
* @param array $minkParameters
|
||||
*/
|
||||
public function __construct(Session $minkSession, array $minkParameters)
|
||||
{
|
||||
$this->minkSession = $minkSession;
|
||||
$this->minkParameters = $minkParameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setCookie($name, $value)
|
||||
{
|
||||
$this->prepareMinkSessionIfNeeded();
|
||||
|
||||
$this->minkSession->setCookie($name, $value);
|
||||
}
|
||||
|
||||
private function prepareMinkSessionIfNeeded()
|
||||
{
|
||||
if ($this->minkSession->getDriver() instanceof KernelDriver) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (false !== strpos($this->minkSession->getCurrentUrl(), $this->minkParameters['base_url'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->minkSession->visit(rtrim($this->minkParameters['base_url'], '/') . '/');
|
||||
}
|
||||
}
|
||||
24
src/Sylius/Behat/CookieSetterInterface.php
Normal file
24
src/Sylius/Behat/CookieSetterInterface.php
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* @author Kamil Kokot <kamil.kokot@lakion.com>
|
||||
*/
|
||||
interface CookieSetterInterface
|
||||
{
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
*/
|
||||
public function setCookie($name, $value);
|
||||
}
|
||||
77
src/Sylius/Behat/spec/CookieSetterSpec.php
Normal file
77
src/Sylius/Behat/spec/CookieSetterSpec.php
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
<?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;
|
||||
|
||||
use Behat\Mink\Driver\DriverInterface;
|
||||
use Behat\Mink\Session;
|
||||
use Behat\Symfony2Extension\Driver\KernelDriver;
|
||||
use PhpSpec\ObjectBehavior;
|
||||
use Prophecy\Argument;
|
||||
use Sylius\Behat\CookieSetter;
|
||||
use Sylius\Behat\CookieSetterInterface;
|
||||
|
||||
/**
|
||||
* @mixin CookieSetter
|
||||
*
|
||||
* @author Kamil Kokot <kamil.kokot@lakion.com>
|
||||
*/
|
||||
class CookieSetterSpec extends ObjectBehavior
|
||||
{
|
||||
function let(Session $minkSession)
|
||||
{
|
||||
$this->beConstructedWith($minkSession, ['base_url' => 'http://localhost:8080/']);
|
||||
}
|
||||
|
||||
function it_is_initializable()
|
||||
{
|
||||
$this->shouldHaveType('Sylius\Behat\CookieSetter');
|
||||
}
|
||||
|
||||
function it_implements_cookie_setter_interface()
|
||||
{
|
||||
$this->shouldImplement(CookieSetterInterface::class);
|
||||
}
|
||||
|
||||
function it_just_sets_a_cookie_if_using_kernel_driver(Session $minkSession, KernelDriver $kernelDriver)
|
||||
{
|
||||
$minkSession->getDriver()->willReturn($kernelDriver);
|
||||
|
||||
$minkSession->setCookie('abc', 'def')->shouldBeCalled();
|
||||
|
||||
$this->setCookie('abc', 'def');
|
||||
}
|
||||
|
||||
function it_sets_a_cookie_if_not_using_kernel_driver_and_driver_is_currently_at_base_url(
|
||||
Session $minkSession,
|
||||
DriverInterface $driver
|
||||
) {
|
||||
$minkSession->getDriver()->willReturn($driver);
|
||||
$minkSession->getCurrentUrl()->willReturn('http://localhost:8080/random/site');
|
||||
|
||||
$minkSession->setCookie('abc', 'def')->shouldBeCalled();
|
||||
|
||||
$this->setCookie('abc', 'def');
|
||||
}
|
||||
|
||||
function it_loads_base_url_and_sets_a_cookie_if_not_using_kernel_driver_and_driver_is_currently_outside_base_url(
|
||||
Session $minkSession,
|
||||
DriverInterface $driver
|
||||
) {
|
||||
$minkSession->getDriver()->willReturn($driver);
|
||||
$minkSession->getCurrentUrl()->willReturn('http://sylius.org');
|
||||
|
||||
$minkSession->visit('http://localhost:8080/')->shouldBeCalled();
|
||||
$minkSession->setCookie('abc', 'def')->shouldBeCalled();
|
||||
|
||||
$this->setCookie('abc', 'def');
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue