feature #69 Expose the Mink service (pamil)
This PR was merged into the 2.1-dev branch.
Discussion
----------
Closes #61.
Commits
-------
d61600f563 Expose the Mink service
This commit is contained in:
@@ -29,6 +29,8 @@ default:
|
|||||||
|
|
||||||
This integration provides two services to use inside Symfony container:
|
This integration provides two services to use inside Symfony container:
|
||||||
|
|
||||||
|
* **`behat.mink`** (autowired by `\Behat\Mink\Mink`) - the Mink service
|
||||||
|
|
||||||
* **`behat.mink.default_session`** (autowired by `\Behat\Mink\Session`) - the default Mink session for the current scenario
|
* **`behat.mink.default_session`** (autowired by `\Behat\Mink\Session`) - the default Mink session for the current scenario
|
||||||
|
|
||||||
* **`behat.mink.parameters`** (autowired by `\FriendsOfBehat\SymfonyExtension\Mink\MinkParameters`) - an object
|
* **`behat.mink.parameters`** (autowired by `\FriendsOfBehat\SymfonyExtension\Mink\MinkParameters`) - an object
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
Feature: Mink integration
|
Feature: Mink default session and parameters integration
|
||||||
|
|
||||||
Background:
|
Background:
|
||||||
Given a working Symfony application with SymfonyExtension configured
|
Given a working Symfony application with SymfonyExtension configured
|
||||||
87
features/mink_integration/mink_service_integration.feature
Normal file
87
features/mink_integration/mink_service_integration.feature
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
Feature: Mink service integration
|
||||||
|
|
||||||
|
Background:
|
||||||
|
Given a working Symfony application with SymfonyExtension configured
|
||||||
|
And a Behat configuration containing:
|
||||||
|
"""
|
||||||
|
default:
|
||||||
|
extensions:
|
||||||
|
Behat\MinkExtension:
|
||||||
|
base_url: "http://localhost:8080/"
|
||||||
|
default_session: symfony
|
||||||
|
sessions:
|
||||||
|
symfony:
|
||||||
|
symfony: ~
|
||||||
|
suites:
|
||||||
|
default:
|
||||||
|
contexts:
|
||||||
|
- App\Tests\SomeContext
|
||||||
|
"""
|
||||||
|
And a feature file containing:
|
||||||
|
"""
|
||||||
|
Feature:
|
||||||
|
Scenario:
|
||||||
|
When I visit the page "/hello-world"
|
||||||
|
Then I should see "Hello world!" on the page
|
||||||
|
|
||||||
|
# Doubling the scenario to account for some weird error connected to Mink's session
|
||||||
|
Scenario:
|
||||||
|
When I visit the page "/hello-world"
|
||||||
|
Then I should see "Hello world!" on the page
|
||||||
|
"""
|
||||||
|
And a context file "tests/SomeContext.php" containing:
|
||||||
|
"""
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Tests;
|
||||||
|
|
||||||
|
use Behat\Behat\Context\Context;
|
||||||
|
use Behat\Mink\Mink;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
|
final class SomeContext implements Context {
|
||||||
|
private $mink;
|
||||||
|
|
||||||
|
public function __construct(Mink $mink)
|
||||||
|
{
|
||||||
|
$this->mink = $mink;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @When I visit the page :page */
|
||||||
|
public function visitPage(string $page): void
|
||||||
|
{
|
||||||
|
$this->mink->getSession()->visit($page);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @Then I should see :content on the page */
|
||||||
|
public function shouldSeeContentOnPage(string $content): void
|
||||||
|
{
|
||||||
|
assert(false !== strpos($this->mink->getSession()->getPage()->getContent(), $content));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
Scenario: Injecting Mink serivce
|
||||||
|
Given a YAML services file containing:
|
||||||
|
"""
|
||||||
|
services:
|
||||||
|
App\Tests\SomeContext:
|
||||||
|
public: true
|
||||||
|
arguments:
|
||||||
|
- '@behat.mink'
|
||||||
|
"""
|
||||||
|
When I run Behat
|
||||||
|
Then it should pass
|
||||||
|
|
||||||
|
Scenario: Autowiring and autoconfiguring Mink service
|
||||||
|
Given a YAML services file containing:
|
||||||
|
"""
|
||||||
|
services:
|
||||||
|
_defaults:
|
||||||
|
autowire: true
|
||||||
|
autoconfigure: true
|
||||||
|
|
||||||
|
App\Tests\SomeContext: ~
|
||||||
|
"""
|
||||||
|
When I run Behat
|
||||||
|
Then it should pass
|
||||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace FriendsOfBehat\SymfonyExtension\Bundle\DependencyInjection;
|
namespace FriendsOfBehat\SymfonyExtension\Bundle\DependencyInjection;
|
||||||
|
|
||||||
use Behat\Behat\Context\Context;
|
use Behat\Behat\Context\Context;
|
||||||
|
use Behat\Mink\Mink;
|
||||||
use Behat\Mink\Session;
|
use Behat\Mink\Session;
|
||||||
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
|
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
|
||||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||||
@@ -45,10 +46,22 @@ final class FriendsOfBehatSymfonyExtensionExtension extends Extension implements
|
|||||||
|
|
||||||
private function provideMinkIntegration(ContainerBuilder $container): void
|
private function provideMinkIntegration(ContainerBuilder $container): void
|
||||||
{
|
{
|
||||||
|
$this->registerMink($container);
|
||||||
$this->registerMinkDefaultSession($container);
|
$this->registerMinkDefaultSession($container);
|
||||||
$this->registerMinkParameters($container);
|
$this->registerMinkParameters($container);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function registerMink(ContainerBuilder $container): void
|
||||||
|
{
|
||||||
|
$minkDefaultSessionDefinition = new Definition(Mink::class, ['fob_symfony.mink']);
|
||||||
|
$minkDefaultSessionDefinition->setPublic(true);
|
||||||
|
$minkDefaultSessionDefinition->setLazy(true);
|
||||||
|
$minkDefaultSessionDefinition->setFactory([new Reference('behat.service_container'), 'get']);
|
||||||
|
|
||||||
|
$container->setDefinition('behat.mink', $minkDefaultSessionDefinition);
|
||||||
|
$container->setAlias(Mink::class, 'behat.mink');
|
||||||
|
}
|
||||||
|
|
||||||
private function registerMinkDefaultSession(ContainerBuilder $container): void
|
private function registerMinkDefaultSession(ContainerBuilder $container): void
|
||||||
{
|
{
|
||||||
$minkDefaultSessionDefinition = new Definition(Session::class, ['fob_symfony.mink.default_session']);
|
$minkDefaultSessionDefinition = new Definition(Session::class, ['fob_symfony.mink.default_session']);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace FriendsOfBehat\SymfonyExtension\ServiceContainer;
|
namespace FriendsOfBehat\SymfonyExtension\ServiceContainer;
|
||||||
|
|
||||||
use Behat\Behat\Context\ServiceContainer\ContextExtension;
|
use Behat\Behat\Context\ServiceContainer\ContextExtension;
|
||||||
|
use Behat\Mink\Mink;
|
||||||
use Behat\Mink\Session;
|
use Behat\Mink\Session;
|
||||||
use Behat\MinkExtension\ServiceContainer\MinkExtension;
|
use Behat\MinkExtension\ServiceContainer\MinkExtension;
|
||||||
use Behat\Testwork\Environment\ServiceContainer\EnvironmentExtension;
|
use Behat\Testwork\Environment\ServiceContainer\EnvironmentExtension;
|
||||||
@@ -16,6 +17,7 @@ use FriendsOfBehat\SymfonyExtension\Driver\Factory\SymfonyDriverFactory;
|
|||||||
use FriendsOfBehat\SymfonyExtension\Listener\KernelOrchestrator;
|
use FriendsOfBehat\SymfonyExtension\Listener\KernelOrchestrator;
|
||||||
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
|
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
|
||||||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
|
||||||
|
use Symfony\Component\DependencyInjection\Alias;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
use Symfony\Component\DependencyInjection\Definition;
|
use Symfony\Component\DependencyInjection\Definition;
|
||||||
use Symfony\Component\DependencyInjection\Parameter;
|
use Symfony\Component\DependencyInjection\Parameter;
|
||||||
@@ -81,6 +83,7 @@ final class SymfonyExtension implements Extension
|
|||||||
$this->loadEnvironmentHandler($container);
|
$this->loadEnvironmentHandler($container);
|
||||||
|
|
||||||
if ($this->minkExtensionFound) {
|
if ($this->minkExtensionFound) {
|
||||||
|
$this->loadMink($container);
|
||||||
$this->loadMinkDefaultSession($container);
|
$this->loadMinkDefaultSession($container);
|
||||||
$this->loadMinkParameters($container);
|
$this->loadMinkParameters($container);
|
||||||
}
|
}
|
||||||
@@ -143,6 +146,11 @@ final class SymfonyExtension implements Extension
|
|||||||
$container->setDefinition('fob_symfony.environment_handler.context_service', $definition);
|
$container->setDefinition('fob_symfony.environment_handler.context_service', $definition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function loadMink(ContainerBuilder $container): void
|
||||||
|
{
|
||||||
|
$container->setAlias('fob_symfony.mink', (new Alias('mink'))->setPublic(true));
|
||||||
|
}
|
||||||
|
|
||||||
private function loadMinkDefaultSession(ContainerBuilder $container): void
|
private function loadMinkDefaultSession(ContainerBuilder $container): void
|
||||||
{
|
{
|
||||||
$minkDefaultSessionDefinition = new Definition(Session::class);
|
$minkDefaultSessionDefinition = new Definition(Session::class);
|
||||||
|
|||||||
Reference in New Issue
Block a user