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) |
|
|
|
|
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); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.