KernelOrchestrator::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FriendsOfBehat\SymfonyExtension\Listener;
6
7
use Behat\Behat\EventDispatcher\Event\ExampleTested;
8
use Behat\Behat\EventDispatcher\Event\ScenarioTested;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
use Symfony\Component\HttpKernel\KernelInterface;
12
13
final class KernelOrchestrator implements EventSubscriberInterface
14
{
15
    /** @var KernelInterface */
16
    private $symfonyKernel;
17
18
    /** @var ContainerInterface */
19
    private $behatContainer;
20
21
    public function __construct(KernelInterface $symfonyKernel, ContainerInterface $behatContainer)
22
    {
23
        $this->symfonyKernel = $symfonyKernel;
24
        $this->behatContainer = $behatContainer;
25
    }
26
27
    public static function getSubscribedEvents(): array
28
    {
29
        return [
30
            ScenarioTested::BEFORE => ['setUp', 15],
31
            ExampleTested::BEFORE => ['setUp', 15],
32
            ScenarioTested::AFTER => ['tearDown', -15],
33
            ExampleTested::AFTER => ['tearDown', -15],
34
        ];
35
    }
36
37
    public function setUp(): void
38
    {
39
        /** @psalm-suppress InvalidArgument Psalm complains that ContainerInterface does not match object|null */
40
        $this->symfonyKernel->getContainer()->set('behat.service_container', $this->behatContainer);
41
    }
42
43
    public function tearDown(): void
44
    {
45
        $this->symfonyKernel->getContainer()->set('behat.service_container', null);
46
        $this->symfonyKernel->shutdown();
47
        $this->symfonyKernel->boot();
48
    }
49
}
50