Describe accessing driver's service container
This commit is contained in:
@@ -0,0 +1,105 @@
|
|||||||
|
Feature: Accessing driver's service container
|
||||||
|
|
||||||
|
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:
|
||||||
|
Given the counter service is zeroed
|
||||||
|
When I visit the page "/hello-world"
|
||||||
|
Then the counter service should return 1
|
||||||
|
|
||||||
|
"""
|
||||||
|
And a context file "tests/SomeContext.php" containing:
|
||||||
|
"""
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Tests;
|
||||||
|
|
||||||
|
use App\Counter;
|
||||||
|
use Behat\Behat\Context\Context;
|
||||||
|
use Behat\Mink\Mink;
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||||
|
|
||||||
|
final class SomeContext implements Context {
|
||||||
|
private $mink;
|
||||||
|
private $container;
|
||||||
|
|
||||||
|
public function __construct(Mink $mink, ContainerInterface $container)
|
||||||
|
{
|
||||||
|
$this->mink = $mink;
|
||||||
|
$this->container = $container;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @Given the counter service is zeroed */
|
||||||
|
public function counterServiceIsZeroed(): void
|
||||||
|
{
|
||||||
|
assert(0 === $this->getCounterService()->get());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @When I visit the page :page */
|
||||||
|
public function visitPage(string $page): void
|
||||||
|
{
|
||||||
|
$this->mink->getSession()->visit($page);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @Then the counter service should return :number */
|
||||||
|
public function counterServiceShouldReturn(int $number): void
|
||||||
|
{
|
||||||
|
assert($number === $this->getCounterService()->get());
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getCounterService(): Counter
|
||||||
|
{
|
||||||
|
return $this->container
|
||||||
|
->get('behat.service_container')
|
||||||
|
->get('fob_symfony.driver_kernel')
|
||||||
|
->getContainer()
|
||||||
|
->get('test.service_container')
|
||||||
|
->get('App\Counter')
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
Scenario: Injecting Mink serivce
|
||||||
|
Given a YAML services file containing:
|
||||||
|
"""
|
||||||
|
services:
|
||||||
|
App\Tests\SomeContext:
|
||||||
|
public: true
|
||||||
|
arguments:
|
||||||
|
- '@behat.mink'
|
||||||
|
- '@service_container'
|
||||||
|
"""
|
||||||
|
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
|
||||||
@@ -92,12 +92,13 @@ CON
|
|||||||
$this->thereIsFile('src/Kernel.php', <<<'CON'
|
$this->thereIsFile('src/Kernel.php', <<<'CON'
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace App;
|
namespace App;
|
||||||
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
|
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
|
||||||
use Symfony\Component\Config\Loader\LoaderInterface;
|
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
|
||||||
use Symfony\Component\HttpKernel\Kernel as HttpKernel;
|
use Symfony\Component\HttpKernel\Kernel as HttpKernel;
|
||||||
use Symfony\Component\Routing\RouteCollectionBuilder;
|
use Symfony\Component\Routing\RouteCollectionBuilder;
|
||||||
|
|
||||||
@@ -105,11 +106,6 @@ class Kernel extends HttpKernel
|
|||||||
{
|
{
|
||||||
use MicroKernelTrait;
|
use MicroKernelTrait;
|
||||||
|
|
||||||
public function helloWorld(): Response
|
|
||||||
{
|
|
||||||
return new Response('Hello world!');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function registerBundles(): iterable
|
public function registerBundles(): iterable
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
@@ -125,17 +121,82 @@ class Kernel extends HttpKernel
|
|||||||
'secret' => 'Pigeon',
|
'secret' => 'Pigeon',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$loader->load(__DIR__ . '/../config/default.yaml');
|
||||||
$loader->load(__DIR__ . '/../config/services.yaml');
|
$loader->load(__DIR__ . '/../config/services.yaml');
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function configureRoutes(RouteCollectionBuilder $routes)
|
protected function configureRoutes(RouteCollectionBuilder $routes): void
|
||||||
{
|
{
|
||||||
$routes->add('/hello-world', 'kernel:helloWorld');
|
$routes->add('/hello-world', 'App\Controller:helloWorld');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CON
|
CON
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$this->thereIsFile('src/Controller.php', <<<'CON'
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App;
|
||||||
|
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
final class Controller
|
||||||
|
{
|
||||||
|
private $counter;
|
||||||
|
|
||||||
|
public function __construct(Counter $counter)
|
||||||
|
{
|
||||||
|
$this->counter = $counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function helloWorld(): Response
|
||||||
|
{
|
||||||
|
$this->counter->increase();
|
||||||
|
|
||||||
|
return new Response('Hello world!');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CON
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->thereIsFile('src/Counter.php', <<<'CON'
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App;
|
||||||
|
|
||||||
|
final class Counter
|
||||||
|
{
|
||||||
|
private $counter = 0;
|
||||||
|
|
||||||
|
public function increase(): void
|
||||||
|
{
|
||||||
|
$this->counter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get(): int
|
||||||
|
{
|
||||||
|
return $this->counter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CON
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->thereIsFile('config/default.yaml', <<<'YML'
|
||||||
|
services:
|
||||||
|
App\Controller:
|
||||||
|
arguments:
|
||||||
|
- '@App\Counter'
|
||||||
|
public: true
|
||||||
|
|
||||||
|
App\Counter:
|
||||||
|
public: false
|
||||||
|
YML
|
||||||
|
);
|
||||||
|
|
||||||
$this->thereIsFile('config/services.yaml', '');
|
$this->thereIsFile('config/services.yaml', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user