Passed
Branch develop (2eaa9a)
by Daniel
05:29
created

KernelContext   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 74
rs 10
c 1
b 0
f 0
wmc 6
1
<?php
2
3
use Behat\Behat\Context\Context;
4
use Behat\Symfony2Extension\Context\KernelAwareContext;
5
use PHPUnit\Framework\Assert;
6
use Symfony\Component\HttpKernel\KernelInterface;
7
use Symfony\Component\PropertyAccess\PropertyAccess;
8
9
/**
10
 * Defines application features from the specific context.
11
 */
12
class KernelContext implements Context, KernelAwareContext
13
{
14
    /**
15
     * @var KernelInterface
16
     */
17
    private $kernel;
18
    /**
19
     * @var \Symfony\Component\PropertyAccess\PropertyAccessor
20
     */
21
    private $propertyAccessor;
22
23
    /**
24
     * Initializes context.
25
     *
26
     * Every scenario gets its own context instance.
27
     * You can also pass arbitrary arguments to the
28
     * context constructor through behat.yml.
29
     */
30
    public function __construct()
31
    {
32
        $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
33
    }
34
35
    /**
36
     * @param KernelInterface $kernel
37
     */
38
    public function setKernel(KernelInterface $kernel)
39
    {
40
        $this->kernel = $kernel;
41
    }
42
43
    /**
44
     * @param $path
45
     * @return string
46
     * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
47
     */
48
    private function getPublicPath($path)
49
    {
50
        return $this->kernel->getContainer()->getParameter('kernel.project_dir') . '/public/' . $path;
51
    }
52
53
    /**
54
     * @Then the public file path :path should exist
55
     * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
56
     */
57
    public function filePathExists(string $path)
58
    {
59
        $fullPath = $this->getPublicPath($path);
60
        Assert::assertFileExists($fullPath, 'The file "' . $fullPath . '"" does not exist');
61
    }
62
63
    /**
64
     * @Then the public file path :path should not exist
65
     * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
66
     */
67
    public function filePathDoesNotExists(string $path)
68
    {
69
        $fullPath = $this->getPublicPath($path);
70
        Assert::assertFileNotExists($fullPath, 'The file "' . $fullPath . '"" exists');
71
    }
72
73
    /**
74
     * @Then the service :service should have property :property with a value of :value
75
     * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
76
     * @throws \Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException
77
     * @throws \Symfony\Component\PropertyAccess\Exception\InvalidArgumentException
78
     * @throws \Symfony\Component\PropertyAccess\Exception\AccessException
79
     * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
80
     */
81
    public function assertNodeValueIs(string $service, string $property, string $value)
82
    {
83
        Assert::assertEquals(
84
            $this->propertyAccessor->getValue($this->kernel->getContainer()->get($service), $property),
85
            $value
86
        );
87
    }
88
}
89