Passed
Pull Request — master (#32)
by Baptiste
05:20
created

JsonMatcher::root_should_match()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
namespace Behapi\Context;
3
4
use InvalidArgumentException;
5
6
use Behat\Behat\Context\Context;
7
use Behat\Gherkin\Node\PyStringNode;
8
9
use Symfony\Component\PropertyAccess\PropertyAccess;
10
use Symfony\Component\PropertyAccess\PropertyAccessor;
11
12
use Behapi\Tools\HttpHistory;
13
use Behapi\Tools\BehapiFactory;
14
15
class JsonMatcher implements Context
16
{
17
    use ApiTrait;
18
19
    /** @var BehapiFactory */
20
    private $factory;
21
22
    /** @var PropertyAccessor */
23
    private $accessor;
24
25
    public function __construct(HttpHistory $history)
0 ignored issues
show
Unused Code introduced by
The parameter $history is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
26
    {
27
        $this->factory = new BehapiFactory;
28
        $this->accessor = PropertyAccess::createPropertyAccessor();
29
    }
30
31
    /** @Then the root should match: */
32
    public function root_should_match(PyStringNode $pattern)
33
    {
34
        $matcher = $this->factory->createMatcher();
35
36
        if ($matcher->match($this->getJson(), $pattern->getRaw())) {
37
            return;
38
        }
39
40
        throw new InvalidArgumentException(
41
            sprintf(
42
                'The json root does not match with the given pattern (error : %s)',
43
                $matcher->getError()
44
            )
45
        );
46
    }
47
48
    /** @Then in the json, :path should match: */
49 View Code Duplication
    public function path_should_match(string $path, PyStringNode $pattern)
50
    {
51
        $value = $this->getValue($path);
52
        $matcher = $this->factory->createMatcher();
53
54
        $json = json_encode($value);
55
56
        if ($matcher->match($json, $pattern->getRaw())) {
57
            return;
58
        }
59
60
        throw new InvalidArgumentException(
61
            sprintf(
62
                'The json path "%s" does not match with the given pattern (error : %s)',
63
                $path,
64
                $matcher->getError()
65
            )
66
        );
67
    }
68
69
    /** @Then in the json, :path should not match: */
70 View Code Duplication
    public function path_should_not_match(string $path, PyStringNode $pattern)
71
    {
72
        $value = $this->getValue($path);
73
        $matcher = $this->factory->createMatcher();
74
75
        $json = json_encode($value);
76
77
        if (!$matcher->match($json, $pattern->getRaw())) {
78
            return;
79
        }
80
81
        throw new InvalidArgumentException(
82
            sprintf(
83
                'The json path "%s" matches with the given pattern (error : %s)',
84
                $path,
85
                $matcher->getError()
86
            )
87
        );
88
    }
89
90
    private function getJson(): string
91
    {
92
        return json_decode((string) $this->getResponse()->getBody());
93
    }
94
95
    private function getValue(string $path)
96
    {
97
        return $this->accessor->getValue($this->getJson(), $path);
0 ignored issues
show
Documentation introduced by
$this->getJson() is of type string, but the function expects a object|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
    }
99
}
100