|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
namespace Behapi\Json; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
|
5
|
|
|
|
|
6
|
|
|
use Behapi\Assert\Assert; |
|
7
|
|
|
|
|
8
|
|
|
trait EachInCollectionTrait |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var PropertyAccessor */ |
|
11
|
|
|
private $accessor; |
|
12
|
|
|
|
|
13
|
|
|
abstract protected function getValue(?string $path); |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @Then /^in the json, each elements in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should have a(?:n?) "(?P<property>(?:[^"]|\\")*)" property$/ |
|
17
|
|
|
* @Then /^in the json, each elements in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) have a(?:n?) "(?P<property>(?:[^"]|\\")*)" property$/ |
|
18
|
|
|
**/ |
|
19
|
|
|
final public function the_json_path_elements_in_collection_should_have_a_property(string $path, ?string $not = null, string $property): void |
|
20
|
|
|
{ |
|
21
|
|
|
$assert = Assert::that($this->getValue(empty($path) ? null : $path)); |
|
22
|
|
|
|
|
23
|
|
|
if ($not !== null) { |
|
24
|
|
|
$assert = $assert->not(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$assert |
|
28
|
|
|
->all() |
|
29
|
|
|
->propertyExists($property) |
|
30
|
|
|
; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should be equal to "(?P<expected>(?:[^"]|\\")*)"$/ |
|
35
|
|
|
* @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) be equal to "(?P<expected>(?:[^"]|\\")*)"$/ |
|
36
|
|
|
*/ |
|
37
|
|
|
final public function the_json_each_elements_in_collection_should_be_equal_to(string $path, string $property, ?string $not = null, string $expected): void |
|
38
|
|
|
{ |
|
39
|
|
|
$values = $this->getValue(empty($path) ? null : $path); |
|
40
|
|
|
|
|
41
|
|
|
Assert::that($values) |
|
42
|
|
|
->isTraversable() |
|
43
|
|
|
; |
|
44
|
|
|
|
|
45
|
|
|
$values = array_map(function ($value) use ($property) { return $this->accessor->getValue($value, $property); }, $values); |
|
46
|
|
|
|
|
47
|
|
|
$assert = Assert::that($values) |
|
48
|
|
|
->all() |
|
49
|
|
|
; |
|
50
|
|
|
|
|
51
|
|
|
if ($not !== null) { |
|
52
|
|
|
$assert = $assert->not(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$assert->same($expected); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should be (?P<expected>true|false)$/ |
|
60
|
|
|
* @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) be (?P<expected>true|false)$/ |
|
61
|
|
|
*/ |
|
62
|
|
|
final public function the_json_each_elements_in_collection_should_be_bool(string $path, string $property, ?string $not = null, string $expected): void |
|
63
|
|
|
{ |
|
64
|
|
|
$values = $this->getValue(empty($path) ? null : $path); |
|
65
|
|
|
|
|
66
|
|
|
Assert::that($values) |
|
67
|
|
|
->isTraversable() |
|
68
|
|
|
; |
|
69
|
|
|
|
|
70
|
|
|
$values = array_map(function ($value) use ($property) { return $this->accessor->getValue($value, $property); }, $values); |
|
71
|
|
|
|
|
72
|
|
|
$assert = Assert::that($values) |
|
73
|
|
|
->all() |
|
74
|
|
|
->boolean() |
|
75
|
|
|
; |
|
76
|
|
|
|
|
77
|
|
|
if ($not !== null) { |
|
78
|
|
|
$assert = $assert->not(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$assert->same($expected === 'true'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should be equal to (?P<expected>[0-9]+)$/ |
|
86
|
|
|
* @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) be equal to (?P<expected>[0-9]+)$/ |
|
87
|
|
|
*/ |
|
88
|
|
|
final public function the_json_each_elements_in_collection_should_be_equal_to_int(string $path, string $property, ?string $not = null, int $expected): void |
|
89
|
|
|
{ |
|
90
|
|
|
$values = $this->getValue(empty($path) ? null : $path); |
|
91
|
|
|
|
|
92
|
|
|
Assert::that($values) |
|
93
|
|
|
->isTraversable() |
|
94
|
|
|
; |
|
95
|
|
|
|
|
96
|
|
|
$values = array_map(function ($value) use ($property) { return $this->accessor->getValue($value, $property); }, $values); |
|
97
|
|
|
|
|
98
|
|
|
$assert = Assert::that($values) |
|
99
|
|
|
->all() |
|
100
|
|
|
->integer() |
|
101
|
|
|
; |
|
102
|
|
|
|
|
103
|
|
|
if ($not !== null) { |
|
104
|
|
|
$assert = $assert->not(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$assert->same($expected); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should contain "(?P<expected>(?:[^"]|\\")*)"$/ |
|
112
|
|
|
* @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) contain "(?P<expected>(?:[^"]|\\")*)"$/ |
|
113
|
|
|
**/ |
|
114
|
|
|
final public function the_json_each_elements_in_collection_should_contain(string $path, string $property, ?string $not = null, string $expected): void |
|
115
|
|
|
{ |
|
116
|
|
|
$values = $this->getValue(empty($path) ? null : $path); |
|
117
|
|
|
|
|
118
|
|
|
Assert::that($values) |
|
119
|
|
|
->isTraversable() |
|
120
|
|
|
; |
|
121
|
|
|
|
|
122
|
|
|
$values = array_map(function ($value) use ($property) { return $this->accessor->getValue($value, $property); }, $values); |
|
123
|
|
|
|
|
124
|
|
|
$assert = Assert::that($values) |
|
125
|
|
|
->all() |
|
126
|
|
|
; |
|
127
|
|
|
|
|
128
|
|
|
if ($not !== null) { |
|
129
|
|
|
$assert = $assert->not(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$assert->contains($expected); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|