1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPKitchen\CodeSpecsCore\Expectation\Matcher; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* ArrayMatcher is designed to check given array matches expectation. |
7
|
|
|
* |
8
|
|
|
* @package PHPKitchen\CodeSpecsCore\Expectation |
9
|
|
|
* @author Dmitry Kolodko <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class ArrayMatcher extends ValueMatcher { |
12
|
|
|
/** |
13
|
|
|
* @return $this |
14
|
|
|
*/ |
15
|
|
|
public function hasKey($key) { |
16
|
|
|
$this->startStep('has key "' . $key . '"') |
17
|
|
|
->assertArrayHasKey($key); |
18
|
|
|
return $this; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return $this |
23
|
|
|
*/ |
24
|
|
|
/** |
25
|
|
|
* @return $this |
26
|
|
|
*/ |
27
|
|
|
public function doesNotHaveKey($key) { |
28
|
|
|
$this->startStep('does not have key "' . $key . '"') |
29
|
|
|
->assertArrayNotHasKey($key); |
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return $this |
35
|
|
|
*/ |
36
|
|
|
public function hasSubset($subset, $subsetName = '') { |
37
|
|
|
$stepName = $subsetName ? "has subset \"{$subsetName}\"" : 'has expected subset'; |
38
|
|
|
$this->startStep($stepName) |
39
|
|
|
->assertArraySubset($subset, false); |
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* In addition of verification that the array has subset check for object identity in subset and actual array. |
45
|
|
|
* |
46
|
|
|
* @param array|\ArrayAccess $subset |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
|
|
/** |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function hasExactlyTheSameSubset($subset, $subsetName = '') { |
53
|
|
|
$stepName = $subsetName ? "has exactly the same subset \"{$subsetName}\"" : 'has exactly the same expected subset'; |
54
|
|
|
|
55
|
|
|
$this->startStep($stepName) |
56
|
|
|
->assertArraySubset($subset, true); |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function hasSameSizeAs($expected, $expectedValueName = '') { |
64
|
|
|
$stepName = $expectedValueName ? "has same size as {$expectedValueName}" : "has same size as expected"; |
65
|
|
|
|
66
|
|
|
$this->startStep($stepName) |
67
|
|
|
->assertSameSize($expected); |
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
|
public function doesNotHaveSameSizeAs($expected, $expectedValueName = '') { |
75
|
|
|
$stepName = $expectedValueName ? "does not have same size as {$expectedValueName}" : "does not have same size as expected"; |
76
|
|
|
|
77
|
|
|
$this->startStep($stepName) |
78
|
|
|
->assertNotSameSize($expected); |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function contains($needle, $needleName = '') { |
86
|
|
|
$stepName = $needleName ? "contains {$needleName}" : "contains expected needle"; |
87
|
|
|
|
88
|
|
|
$this->startStep($stepName) |
89
|
|
|
->assertContains($needle); |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function doesNotContain($needle, $needleName = '') { |
97
|
|
|
$stepName = $needleName ? "does not contain {$needleName}" : "does not contain expected needle"; |
98
|
|
|
|
99
|
|
|
$this->startStep($stepName) |
100
|
|
|
->assertNotContains($needle); |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function containsOnlyValuesOfType($type) { |
108
|
|
|
$this->startStep('contains only values of type "' . $type . '"') |
109
|
|
|
->assertContainsOnly($type); |
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
|
|
public function containsOnlyValuesOfNativeType($type) { |
117
|
|
|
$this->startStep('contains only values of native type "' . $type . '"') |
118
|
|
|
->assertContainsOnly($type, true); |
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
|
|
public function containsOnlyInstancesOf($class) { |
126
|
|
|
$this->startStep('contains only instances of "' . $class . '"') |
127
|
|
|
->assertContainsOnlyInstancesOf($class); |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function doesNotContainOnlyValuesOfType($type) { |
135
|
|
|
$this->startStep('does not contain only values of type "' . $type . '"') |
136
|
|
|
->assertNotContainsOnly($type, null); |
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function doesNotContainOnlyValuesOfNativeType($type) { |
144
|
|
|
$this->startStep('does not contain only values of native type "' . $type . '"') |
145
|
|
|
->assertNotContainsOnly($type, true); |
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
|
View Code Duplication |
public function countIsEqualToCountOf($countOrCountable, $expectedValueName = '') { |
|
|
|
|
153
|
|
|
$stepName = $expectedValueName ? "has count equal to count of {$expectedValueName}" : "has count equal to count of expected"; |
154
|
|
|
|
155
|
|
|
$this->startStep($stepName) |
156
|
|
|
->assertCount($this->convertToCount($countOrCountable)); |
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return $this |
162
|
|
|
*/ |
163
|
|
View Code Duplication |
public function countIsNotEqualToCountOf($countOrCountable, $expectedValueName = '') { |
|
|
|
|
164
|
|
|
$stepName = $expectedValueName ? "does not have count equal count of {$expectedValueName}" : "does not have count equal count of expected"; |
165
|
|
|
|
166
|
|
|
$this->startStep($stepName) |
167
|
|
|
->assertNotCount($this->convertToCount($countOrCountable)); |
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
protected function convertToCount($value) { |
172
|
|
|
if (is_array($value) || $value instanceof \Countable || $value instanceof \Traversable) { |
173
|
|
|
$count = count($value); |
174
|
|
|
} else { |
175
|
|
|
$count = $value; |
176
|
|
|
} |
177
|
|
|
return $count; |
178
|
|
|
} |
179
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.