| Total Complexity | 53 |
| Total Lines | 287 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AbstractContext often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 24 | abstract class AbstractContext implements BehatContext |
||
| 25 | { |
||
| 26 | /** @var PropertyAccessor */ |
||
| 27 | private $accessor; |
||
| 28 | |||
| 29 | public function __construct() |
||
| 32 | } |
||
| 33 | |||
| 34 | abstract protected function getJson(); |
||
| 35 | |||
| 36 | protected function getValue(string $path) |
||
| 37 | { |
||
| 38 | return $this->accessor->getValue($this->getJson(), $path); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** @Then :path should be accessible in the latest json response */ |
||
| 42 | final public function path_should_be_readable(string $path): void |
||
| 43 | { |
||
| 44 | Assert::true($this->accessor->isReadable($this->getJson(), $path), "The path $path should be a valid path"); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** @Then :path should not exist in the latest json response */ |
||
| 48 | final public function path_should_not_be_readable(string $path): void |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @Then in the json, :path should be equal to :expected |
||
| 55 | * @Then in the json, :path should :not be equal to :expected |
||
| 56 | */ |
||
| 57 | final public function the_json_path_should_be_equal_to(string $path, ?string $not = null, $expected): void |
||
| 58 | { |
||
| 59 | $assert = [Assert::class, $not !== null ? 'notEq' : 'eq']; |
||
| 60 | assert(is_callable($assert)); |
||
| 61 | |||
| 62 | $assert($this->getValue($path), $expected); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** @Then in the json, :path should be: */ |
||
| 66 | final public function the_json_path_should_be_py_string(string $path, PyStringNode $expected): void |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @Then /^in the json, "(?P<path>(?:[^"]|\\")*)" should be (?P<expected>true|false)$/ |
||
| 73 | * @Then /^in the json, "(?P<path>(?:[^"]|\\")*)" should :not be (?P<expected>true|false)$/ |
||
| 74 | */ |
||
| 75 | final public function the_json_path_should_be(string $path, ?string $not = null, string $expected): void |
||
| 76 | { |
||
| 77 | $assert = [Assert::class, $not !== null ? 'notSame' : 'same']; |
||
| 78 | assert(is_callable($assert)); |
||
| 79 | |||
| 80 | $assert($this->getValue($path), 'true' === $expected); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @Then in the json, :path should be null |
||
| 85 | * @Then in the json, :path should :not be null |
||
| 86 | */ |
||
| 87 | final public function the_json_path_should_be_null(string $path, ?string $not = null): void |
||
| 88 | { |
||
| 89 | $assert = [Assert::class, $not !== null ? 'notNull' : 'null']; |
||
| 90 | assert(is_callable($assert)); |
||
| 91 | |||
| 92 | $assert($this->getValue($path)); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @Then in the json, :path should be empty |
||
| 97 | * @Then in the json, :path should :not be empty |
||
| 98 | */ |
||
| 99 | final public function the_json_path_should_be_empty(string $path, ?string $not = null): void |
||
| 100 | { |
||
| 101 | $assert = [Assert::class, $not !== null ? 'notEmpty' : 'isEmpty']; |
||
| 102 | assert(is_callable($assert)); |
||
| 103 | |||
| 104 | $assert($this->getValue($path)); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @Then in the json, :path should contain :expected |
||
| 109 | * @Then in the json, :path should :not contain :expected |
||
| 110 | */ |
||
| 111 | final public function the_json_path_contains(string $path, ?string $not = null, $expected): void |
||
| 112 | { |
||
| 113 | $assert = [Assert::class, $not !== null ? 'notContains' : 'contains']; |
||
| 114 | assert(is_callable($assert)); |
||
| 115 | |||
| 116 | $assert($this->getValue($path), $expected); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** @Then in the json, :path collection should contain an element with :value equal to :expected */ |
||
| 120 | final public function the_json_path_collection_contains(string $path, string $value, $expected): void |
||
| 121 | { |
||
| 122 | $collection = $this->accessor->getValue($this->getJson(), $path); |
||
| 123 | Assert::isIterable($collection); |
||
| 124 | |||
| 125 | foreach ($collection as $element) { |
||
| 126 | if ($expected === $this->accessor->getValue($element, $value)) { |
||
| 127 | return; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | throw new InvalidArgumentException("$path collection does not contain an element with $value equal to $expected"); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** @Then in the json, :path should be a valid date(time) */ |
||
| 135 | final public function the_json_path_should_be_a_valid_date(string $path): void |
||
| 136 | { |
||
| 137 | try { |
||
| 138 | new DateTime($this->getValue($path)); |
||
| 139 | } catch (Throwable $t) { |
||
| 140 | throw new InvalidArgumentException("$path does not contain a valid date"); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | /** @Then in the json, :path should be greater than :expected */ |
||
| 145 | final public function the_json_path_should_be_greater_than(string $path, int $expected): void |
||
| 148 | } |
||
| 149 | |||
| 150 | /** @Then in the json, :path should be greater than or equal to :expected */ |
||
| 151 | final public function the_json_path_should_be_greater_or_equal_than(string $path, int $expected): void |
||
| 152 | { |
||
| 153 | Assert::greaterThanEq($this->getValue($path), $expected); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** @Then in the json, :path should be less than :expected */ |
||
| 157 | final public function the_json_path_should_be_less_than(string $path, int $expected): void |
||
| 160 | } |
||
| 161 | |||
| 162 | /** @Then in the json, :path should be less than or equal to :expected */ |
||
| 163 | final public function the_json_path_should_be_less_or_equal_than(string $path, int $expected): void |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @Then in the json, the root should be an array |
||
| 170 | * @Then in the json, :path should be an array |
||
| 171 | */ |
||
| 172 | final public function should_be_an_array(?string $path = null): void |
||
| 173 | { |
||
| 174 | Assert::isArray($path === null ? $this->getJson() : $this->getValue($path)); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @Then in the json, the root collection should have at :limit :count element(s) |
||
| 179 | * @Then in the json, :path collection should have at :limit :count element(s) |
||
| 180 | * |
||
| 181 | * @Then in the json, the root collection should have :count element(s) |
||
| 182 | * @Then in the json, :path collection should have :count element(s) |
||
| 183 | */ |
||
| 184 | final public function the_json_collection_should_have_at_least_elements(?string $path = null, ?string $limit = null, int $count): void |
||
| 185 | { |
||
| 186 | $value = $path === null ? $this->getJson() : $this->getValue($path); |
||
| 187 | |||
| 188 | $assert = [Assert::class, 'count']; |
||
| 189 | |||
| 190 | if ($limit !== null) { |
||
| 191 | $assert[1] = $limit === 'most' ? 'maxCount' : 'minCount'; |
||
| 192 | } |
||
| 193 | |||
| 194 | assert(is_callable($assert)); |
||
| 195 | |||
| 196 | Assert::isCountable($value); |
||
| 197 | $assert($value, $count); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** @Then in the json, :path should match :pattern */ |
||
| 201 | final public function the_json_path_should_match(string $path, string $pattern): void |
||
| 202 | { |
||
| 203 | Assert::regex($this->getValue($path), $pattern); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @Then in the json, :path should not match :pattern |
||
| 208 | * |
||
| 209 | * ----- |
||
| 210 | * |
||
| 211 | * Note :: The body of this assertion should be replaced by a |
||
| 212 | * `Assert::notRegex` as soon as the Assert's PR |
||
| 213 | * https://github.com/webmozart/assert/pull/58 is merged and released. |
||
| 214 | */ |
||
| 215 | final public function the_json_path_should_not_match(string $path, string $pattern): void |
||
| 216 | { |
||
| 217 | if (!preg_match($pattern, $this->getValue($path), $matches, PREG_OFFSET_CAPTURE)) { |
||
| 218 | // it's all good, it is supposed not to match. :} |
||
| 219 | return; |
||
| 220 | } |
||
| 221 | |||
| 222 | throw new InvalidArgumentException("The value matches {$pattern} at offset {$matches[0][1]}"); |
||
| 223 | } |
||
| 224 | |||
| 225 | /** @Then in the json, :path should be a valid json encoded string */ |
||
| 226 | final public function the_json_path_should_be_a_valid_json_encoded_string(string $path): void |
||
| 227 | { |
||
| 228 | $value = json_decode($this->getValue($path)); |
||
| 229 | |||
| 230 | Assert::notNull($value); |
||
| 231 | Assert::same(json_last_error(), JSON_ERROR_NONE); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @Then /^in the json, all the elements in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should have a(?:n?) "(?P<property>(?:[^"]|\\")*)" property$/ |
||
| 236 | * @Then /^in the json, all the elements in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) have a(?:n?) "(?P<property>(?:[^"]|\\")*)" property$/ |
||
| 237 | **/ |
||
| 238 | final public function the_json_path_elements_in_collection_should_have_a_property(string $path, ?string $not = null, string $property): void |
||
| 239 | { |
||
| 240 | $assert = [Assert::class, $not !== null ? 'allPropertyNotExists' : 'allPropertyExists']; |
||
| 241 | assert(is_callable($assert)); |
||
| 242 | |||
| 243 | $assert(empty($path) ? $this->getJson() : $this->getValue($path), $property); |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should be equal to "(?P<expected>(?:[^"]|\\")*)"$/ |
||
| 248 | * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) be equal to "(?P<expected>(?:[^"]|\\")*)"$/ |
||
| 249 | */ |
||
| 250 | final public function the_json_each_elements_in_collection_should_be_equal_to(string $path, string $property, ?string $not = null, string $expected): void |
||
| 251 | { |
||
| 252 | $values = empty($path) ? $this->getJson() : $this->getValue($path); |
||
| 253 | Assert::isIterable($values); |
||
| 254 | |||
| 255 | $assert = [Assert::class, $not !== null ? 'notSame' : 'same']; |
||
| 256 | assert(is_callable($assert)); |
||
| 257 | |||
| 258 | foreach ($values as $element) { |
||
| 259 | $assert($this->accessor->getValue($element, $property), $expected); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should be (?P<expected>true|false)$/ |
||
| 265 | * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) be (?P<expected>true|false)$/ |
||
| 266 | */ |
||
| 267 | final public function the_json_each_elements_in_collection_should_be_bool(string $path, string $property, ?string $not = null, string $expected): void |
||
| 268 | { |
||
| 269 | $values = empty($path) ? $this->getJson() : $this->getValue($path); |
||
| 270 | Assert::isIterable($values); |
||
| 271 | |||
| 272 | $assert = [Assert::class, $not !== null ? 'notSame' : 'same']; |
||
| 273 | assert(is_callable($assert)); |
||
| 274 | |||
| 275 | foreach ($values as $element) { |
||
| 276 | $assert($this->accessor->getValue($element, $property), $expected === 'true'); |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should be equal to (?P<expected>[0-9]+)$/ |
||
| 282 | * @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]+)$/ |
||
| 283 | */ |
||
| 284 | final public function the_json_each_elements_in_collection_should_be_equal_to_int(string $path, string $property, ?string $not = null, int $expected): void |
||
| 285 | { |
||
| 286 | $values = empty($path) ? $this->getJson() : $this->getValue($path); |
||
| 287 | Assert::isIterable($values); |
||
| 288 | |||
| 289 | $assert = [Assert::class, $not !== null ? 'notSame' : 'same']; |
||
| 290 | assert(is_callable($assert)); |
||
| 291 | |||
| 292 | foreach ($values as $element) { |
||
| 293 | $assert($this->accessor->getValue($element, $property), $expected); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should contain "(?P<expected>(?:[^"]|\\")*)"$/ |
||
| 299 | * @Then /^in the json, each "(?P<property>(?:[^"]|\\")*)" property in the (?:root|\"(?P<path>(?:[^"]|\\")*)\") collection should (?P<not>not) contain "(?P<expected>(?:[^"]|\\")*)"$/ |
||
| 300 | **/ |
||
| 301 | final public function the_json_each_elements_in_collection_should_contain(string $path, string $property, ?string $not = null, string $expected): void |
||
| 311 | } |
||
| 312 | } |
||
| 313 | } |
||
| 314 |