SwaggerContext::getClient()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Nicofuma\SwaggerBundle\Tests\Behat\Context;
4
5
use Behat\Mink\Driver\BrowserKitDriver;
6
use Behat\Mink\Exception\UnsupportedDriverActionException;
7
use FR3D\SwaggerAssertions\PhpUnit\SymfonyAssertsTrait;
8
use Nicofuma\SwaggerBundle\Validator\ValidatorMap;
9
use Sanpi\Behatch\Context\BaseContext;
10
11
class SwaggerContext extends BaseContext
12
{
13
    use SymfonyAssertsTrait;
14
15
    /** @var ValidatorMap */
16
    private $map;
17
18
    public function __construct(ValidatorMap $map)
19
    {
20
        $this->map = $map;
21
    }
22
23
    /**
24
     * This method match the response against its Swagger definition.
25
     *
26
     * @throws \Behat\Mink\Exception\ExpectationException
27
     * @throws \Exception
28
     *
29
     * @Then the response should match the Swagger definition
30
     */
31
    public function theResponseMatchSwagger()
32
    {
33
        $response = $this->getResponse();
34
        $request = $this->getRequest();
35
        $schemaManager = $this->map->getValidator($request)->getSchemaManager();
36
37
        $this->assertResponseMatch($response, $schemaManager, $request->getPathInfo(), $request->getMethod());
38
    }
39
40
    /**
41
     * @return \Symfony\Component\BrowserKit\Client
42
     *
43
     * @throws UnsupportedDriverActionException
44
     */
45
    protected function getClient()
46
    {
47
        $driver = $this->getSession()->getDriver();
48
49
        if (!$driver instanceof BrowserKitDriver) {
0 ignored issues
show
Bug introduced by
The class Behat\Mink\Driver\BrowserKitDriver does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
50
            throw new UnsupportedDriverActionException('This step is only supported by the BrowserKitDriver, not the %s one', $driver);
51
        }
52
53
        return $driver->getClient();
54
    }
55
56
    /**
57
     * Get the last response.
58
     *
59
     * @return \Symfony\Component\HttpFoundation\Response
60
     */
61
    protected function getResponse()
62
    {
63
        return $this->getClient()->getResponse();
64
    }
65
66
    /**
67
     * @return \Symfony\Component\HttpFoundation\Request
68
     */
69
    public function getRequest()
70
    {
71
        return $this->getClient()->getRequest();
72
    }
73
}
74