Test Mink integration

This commit is contained in:
Kamil Kokot
2019-01-09 23:47:24 +01:00
parent c8b0cbb205
commit 6061eaaf62
3 changed files with 133 additions and 29 deletions

View File

@@ -0,0 +1,107 @@
Feature: Mink 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
And the base url from Mink parameters should be "http://localhost:8080/"
# 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 the base url from Mink parameters should be "http://localhost:8080/"
"""
And a context file "tests/SomeContext.php" containing:
"""
<?php
namespace App\Tests;
use Behat\Behat\Context\Context;
use Behat\Mink\Session;
use Psr\Container\ContainerInterface;
final class SomeContext implements Context {
private $session;
private $parameters;
public function __construct(Session $session, $minkParameters)
{
if (!is_array($minkParameters) && !$minkParameters instanceof \ArrayAccess) {
throw new \InvalidArgumentException(sprintf(
'"$parameters" passed to "%s" has to be an array or implement "%s".',
self::class,
\ArrayAccess::class
));
}
$this->session = $session;
$this->parameters = $minkParameters;
}
/** @When I visit the page :page */
public function visitPage(string $page): void
{
$this->session->visit($page);
}
/** @Then I should see :content on the page */
public function shouldSeeContentOnPage(string $content): void
{
assert(false !== strpos($this->session->getPage()->getContent(), $content));
}
/** @Then the base url from Mink parameters should be :expected */
public function baseUrlShouldBe(string $expected): void
{
assert(isset($this->parameters['base_url']));
assert($this->parameters['base_url'] === $expected);
}
}
"""
Scenario: Injecting Mink session and Mink parameters
Given a YAML services file containing:
"""
services:
App\Tests\SomeContext:
public: true
arguments:
- '@behat.mink.default_session'
- '@behat.mink.parameters'
"""
When I run Behat
Then it should pass
Scenario: Autowiring and autoconfiguring Mink session and Mink parameters
Given a YAML services file containing:
"""
services:
_defaults:
autowire: true
autoconfigure: true
App\Tests\SomeContext: ~
"""
When I run Behat
Then it should pass

View File

@@ -24,6 +24,9 @@ final class FriendsOfBehatSymfonyExtensionExtension extends Extension implements
$container $container
->registerForAutoconfiguration(Context::class) ->registerForAutoconfiguration(Context::class)
->addTag('fob.context') ->addTag('fob.context')
->setBindings([
'$minkParameters' => new Reference('behat.mink.parameters'),
])
; ;
} }

View File

@@ -91,13 +91,23 @@ CON
namespace App; namespace App;
use Symfony\Component\HttpKernel\Kernel as HttpKernel; use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel as HttpKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
class Kernel extends HttpKernel class Kernel extends HttpKernel
{ {
public function registerBundles() use MicroKernelTrait;
public function helloWorld(): Response
{
return new Response('Hello world!');
}
public function registerBundles(): iterable
{ {
return [ return [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(), new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
@@ -105,17 +115,20 @@ class Kernel extends HttpKernel
]; ];
} }
public function registerContainerConfiguration(LoaderInterface $loader) protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{ {
$loader->load(function (ContainerBuilder $container): void {
$container->loadFromExtension('framework', [ $container->loadFromExtension('framework', [
'test' => $this->getEnvironment() === 'test', 'test' => $this->getEnvironment() === 'test',
'secret' => 'Pigeon', 'secret' => 'Pigeon',
]); ]);
});
$loader->load(__DIR__ . '/../config/services.yaml'); $loader->load(__DIR__ . '/../config/services.yaml');
} }
protected function configureRoutes(RouteCollectionBuilder $routes)
{
$routes->add('/hello-world', 'kernel::helloWorld');
}
} }
CON CON
); );
@@ -151,25 +164,6 @@ CON
self::$filesystem->dumpFile($mainConfigFile, Yaml::dump($mainBehatConfiguration)); self::$filesystem->dumpFile($mainConfigFile, Yaml::dump($mainBehatConfiguration));
} }
/**
* @Given /^a Behat configuration with the minimal working configuration for MinkExtension$/
*/
public function thereIsConfigurationWithMinimalWorkingConfigurationForMinkExtension(): void
{
$this->thereIsConfiguration(<<<'CON'
default:
extensions:
Behat\MinkExtension:
base_url: "http://localhost:8080/"
default_session: symfony
sessions:
symfony:
symfony: ~
CON
);
}
/** /**
* @Given /^a (?:.+ |)file "([^"]+)" containing(?: "([^"]+)"|:)$/ * @Given /^a (?:.+ |)file "([^"]+)" containing(?: "([^"]+)"|:)$/
*/ */