Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the Mink service #69

Merged
merged 1 commit into from Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/03_mink_integration.md
Expand Up @@ -29,6 +29,8 @@ default:

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.parameters`** (autowired by `\FriendsOfBehat\SymfonyExtension\Mink\MinkParameters`) - an object
Expand Down
@@ -1,4 +1,4 @@
Feature: Mink integration
Feature: Mink default session and parameters integration

Background:
Given a working Symfony application with SymfonyExtension configured
Expand Down
87 changes: 87 additions & 0 deletions features/mink_integration/mink_service_integration.feature
@@ -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
Expand Up @@ -5,6 +5,7 @@
namespace FriendsOfBehat\SymfonyExtension\Bundle\DependencyInjection;

use Behat\Behat\Context\Context;
use Behat\Mink\Mink;
use Behat\Mink\Session;
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
Expand Down Expand Up @@ -45,10 +46,22 @@ private function registerBehatContainer(ContainerBuilder $container): void

private function provideMinkIntegration(ContainerBuilder $container): void
{
$this->registerMink($container);
$this->registerMinkDefaultSession($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
{
$minkDefaultSessionDefinition = new Definition(Session::class, ['fob_symfony.mink.default_session']);
Expand Down
8 changes: 8 additions & 0 deletions src/ServiceContainer/SymfonyExtension.php
Expand Up @@ -5,6 +5,7 @@
namespace FriendsOfBehat\SymfonyExtension\ServiceContainer;

use Behat\Behat\Context\ServiceContainer\ContextExtension;
use Behat\Mink\Mink;
use Behat\Mink\Session;
use Behat\MinkExtension\ServiceContainer\MinkExtension;
use Behat\Testwork\Environment\ServiceContainer\EnvironmentExtension;
Expand All @@ -16,6 +17,7 @@
use FriendsOfBehat\SymfonyExtension\Listener\KernelOrchestrator;
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Parameter;
Expand Down Expand Up @@ -81,6 +83,7 @@ public function load(ContainerBuilder $container, array $config): void
$this->loadEnvironmentHandler($container);

if ($this->minkExtensionFound) {
$this->loadMink($container);
$this->loadMinkDefaultSession($container);
$this->loadMinkParameters($container);
}
Expand Down Expand Up @@ -143,6 +146,11 @@ private function loadEnvironmentHandler(ContainerBuilder $container): void
$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
{
$minkDefaultSessionDefinition = new Definition(Session::class);
Expand Down