php-kitchen /
code-specs
| 1 | <?php |
||
| 2 | |||
| 3 | namespace PHPKitchen\CodeSpecs\Expectation\Matcher; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * ArrayMatcher is designed to check given array matches expectation. |
||
| 7 | * |
||
| 8 | * @package PHPKitchen\CodeSpecs\Expectation |
||
| 9 | * @author Dima 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 | |||
| 19 | return $this; |
||
| 20 | } |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @return $this |
||
| 24 | */ |
||
| 25 | /** |
||
| 26 | * @return $this |
||
| 27 | */ |
||
| 28 | public function doesNotHaveKey($key) { |
||
| 29 | $this->startStep('does not have key "' . $key . '"') |
||
| 30 | ->assertArrayNotHasKey($key); |
||
| 31 | |||
| 32 | return $this; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @return $this |
||
| 37 | */ |
||
| 38 | public function hasSubset($subset, $subsetName = '') { |
||
| 39 | $stepName = $subsetName ? "has subset \"{$subsetName}\"" : 'has expected subset'; |
||
| 40 | $this->startStep($stepName) |
||
| 41 | ->assertArraySubset($subset, false); |
||
| 42 | |||
| 43 | return $this; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * In addition of verification that the array has subset check for object identity in subset and actual array. |
||
| 48 | * |
||
| 49 | * @param array|\ArrayAccess $subset |
||
| 50 | * |
||
| 51 | * @return $this |
||
| 52 | */ |
||
| 53 | /** |
||
| 54 | * @return $this |
||
| 55 | */ |
||
| 56 | public function hasExactlyTheSameSubset($subset, $subsetName = '') { |
||
| 57 | $stepName = $subsetName ? "has exactly the same subset \"{$subsetName}\"" : 'has exactly the same expected subset'; |
||
| 58 | |||
| 59 | $this->startStep($stepName) |
||
| 60 | ->assertArraySubset($subset, true); |
||
| 61 | |||
| 62 | return $this; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return $this |
||
| 67 | */ |
||
| 68 | public function hasSameSizeAs($expected, $expectedValueName = '') { |
||
| 69 | $stepName = $expectedValueName ? "has same size as {$expectedValueName}" : "has same size as expected"; |
||
| 70 | |||
| 71 | $this->startStep($stepName) |
||
| 72 | ->assertSameSize($expected); |
||
| 73 | |||
| 74 | return $this; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @return $this |
||
| 79 | */ |
||
| 80 | public function doesNotHaveSameSizeAs($expected, $expectedValueName = '') { |
||
| 81 | $stepName = $expectedValueName ? "does not have same size as {$expectedValueName}" : "does not have same size as expected"; |
||
| 82 | |||
| 83 | $this->startStep($stepName) |
||
| 84 | ->assertNotSameSize($expected); |
||
| 85 | |||
| 86 | return $this; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return $this |
||
| 91 | */ |
||
| 92 | public function contains($needle, $needleName = '') { |
||
| 93 | $stepName = $needleName ? "contains {$needleName}" : "contains expected needle"; |
||
| 94 | |||
| 95 | $this->startStep($stepName) |
||
| 96 | ->assertContains($needle); |
||
| 97 | |||
| 98 | return $this; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return $this |
||
| 103 | */ |
||
| 104 | public function doesNotContain($needle, $needleName = '') { |
||
| 105 | $stepName = $needleName ? "does not contain {$needleName}" : "does not contain expected needle"; |
||
| 106 | |||
| 107 | $this->startStep($stepName) |
||
| 108 | ->assertNotContains($needle); |
||
| 109 | |||
| 110 | return $this; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return $this |
||
| 115 | */ |
||
| 116 | public function containsOnlyValuesOfType($type) { |
||
| 117 | $this->startStep('contains only values of type "' . $type . '"') |
||
| 118 | ->assertContainsOnly($type); |
||
| 119 | |||
| 120 | return $this; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return $this |
||
| 125 | */ |
||
| 126 | public function containsOnlyValuesOfNativeType($type) { |
||
| 127 | $this->startStep('contains only values of native type "' . $type . '"') |
||
| 128 | ->assertContainsOnly($type, true); |
||
| 129 | |||
| 130 | return $this; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return $this |
||
| 135 | */ |
||
| 136 | public function containsOnlyInstancesOf($class) { |
||
| 137 | $this->startStep('contains only instances of "' . $class . '"') |
||
| 138 | ->assertContainsOnlyInstancesOf($class); |
||
| 139 | |||
| 140 | return $this; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return $this |
||
| 145 | */ |
||
| 146 | public function doesNotContainOnlyValuesOfType($type) { |
||
| 147 | $this->startStep('does not contain only values of type "' . $type . '"') |
||
| 148 | ->assertNotContainsOnly($type, null); |
||
| 149 | |||
| 150 | return $this; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return $this |
||
| 155 | */ |
||
| 156 | public function doesNotContainOnlyValuesOfNativeType($type) { |
||
| 157 | $this->startStep('does not contain only values of native type "' . $type . '"') |
||
| 158 | ->assertNotContainsOnly($type, true); |
||
| 159 | |||
| 160 | return $this; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return $this |
||
| 165 | */ |
||
| 166 | public function countIsEqualToCountOf($countOrCountable, $expectedValueName = '') { |
||
| 167 | $stepName = $expectedValueName ? "has count equal to count of {$expectedValueName}" : "has count equal to count of expected"; |
||
| 168 | |||
| 169 | $this->startStep($stepName) |
||
| 170 | ->assertCount($this->convertToCount($countOrCountable)); |
||
| 171 | |||
| 172 | return $this; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return $this |
||
| 177 | */ |
||
| 178 | public function countIsNotEqualToCountOf($countOrCountable, $expectedValueName = '') { |
||
| 179 | $stepName = $expectedValueName ? "does not have count equal count of {$expectedValueName}" : "does not have count equal count of expected"; |
||
| 180 | |||
| 181 | $this->startStep($stepName) |
||
| 182 | ->assertNotCount($this->convertToCount($countOrCountable)); |
||
| 183 | |||
| 184 | return $this; |
||
| 185 | } |
||
| 186 | |||
| 187 | protected function convertToCount($value) { |
||
| 188 | if (is_array($value) || $value instanceof \Countable || $value instanceof \Traversable) { |
||
| 189 | $count = count($value); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 190 | } else { |
||
| 191 | $count = $value; |
||
| 192 | } |
||
| 193 | |||
| 194 | return $count; |
||
| 195 | } |
||
| 196 | } |