Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like RequestSpec 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 RequestSpec, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class RequestSpec extends ObjectBehavior |
||
| 16 | { |
||
| 17 | function it_is_initializable() |
||
| 18 | { |
||
| 19 | $this->shouldHaveType(Request::class); |
||
| 20 | } |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Retrieves the HTTP protocol version as a string. |
||
| 24 | */ |
||
| 25 | function it_returns_its_protocol_version_as_a_string() |
||
| 26 | { |
||
| 27 | $this->getProtocolVersion()->shouldHaveType("string"); |
||
| 28 | } |
||
| 29 | |||
| 30 | |||
| 31 | /** |
||
| 32 | * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0"). |
||
| 33 | */ |
||
| 34 | function it_returns_a_string_containing_only_the_http_version() |
||
| 35 | { |
||
| 36 | $this->getProtocolVersion()->shouldReturn("1.1"); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Return an instance with the specified HTTP protocol version. |
||
| 41 | * |
||
| 42 | * The version string MUST contain only the HTTP version number (e.g., |
||
| 43 | * "1.1", "1.0"). |
||
| 44 | * |
||
| 45 | * This method MUST be implemented in such a way as to retain the |
||
| 46 | * immutability of the message, and MUST return an instance that has the |
||
| 47 | * new protocol version. |
||
| 48 | */ |
||
| 49 | function it_returns_a_new_request_with_a_protocol_update() |
||
| 50 | { |
||
| 51 | $request = $this->withProtocolVersion("1.0"); |
||
| 52 | |||
| 53 | $request->shouldHaveType(Request::class); |
||
| 54 | $request->getProtocolVersion()->shouldReturn("1.0"); |
||
| 55 | } |
||
| 56 | |||
| 57 | function it_retains_the_original_protocol_version_when_a_new_protocol_version_is_set() |
||
| 58 | { |
||
| 59 | $request = $this->withProtocolVersion("1.0"); |
||
| 60 | |||
| 61 | $request->shouldHaveType(Request::class); |
||
| 62 | $request->getProtocolVersion()->shouldReturn("1.0"); |
||
| 63 | |||
| 64 | $this->getProtocolVersion()->shouldReturn("1.1"); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Returns an associative array of the message's headers. Each key MUST be a |
||
| 69 | * header name, and each value MUST be an array of strings for that header. |
||
| 70 | * |
||
| 71 | * The keys represent the header name as it will be sent over the wire, and |
||
| 72 | * each value is an array of strings associated with the header. |
||
| 73 | */ |
||
| 74 | function it_returns_its_headers_as_an_associative_array_of_arrays() |
||
| 75 | { |
||
| 76 | $headers = $this->getHeaders(); |
||
| 77 | |||
| 78 | $headers->shouldBeArray(); |
||
| 79 | |||
| 80 | if (!empty($headers)) { |
||
| 81 | foreach ($headers as $header => $values) { |
||
| 82 | $header->shouldHaveType("string"); |
||
| 83 | $values->shouldBeArray(); |
||
| 84 | |||
| 85 | if (!empty($values)) { |
||
| 86 | foreach ($values as $value) { |
||
| 87 | $value->shouldHaveType("string"); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * While header names are not case-sensitive, getHeaders() will preserve the |
||
| 96 | * exact case in which headers were originally specified. |
||
| 97 | */ |
||
| 98 | function it_preserves_the_case_of_returned_headers() |
||
| 99 | { |
||
| 100 | $spec_header = array( |
||
| 101 | "fOo" => array( |
||
| 102 | "baR" |
||
| 103 | ) |
||
| 104 | ); |
||
| 105 | |||
| 106 | //TODO: Construct with new header |
||
| 107 | |||
| 108 | $this->getHeaders()->shouldReturn($spec_header); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Returns true if any header names match the given header |
||
| 113 | */ |
||
| 114 | function it_reports_that_it_has_a_header() |
||
| 115 | { |
||
| 116 | $this->hasHeader("foo")->shouldBe(true); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Checks if a header exists by the given case-insensitive name. |
||
| 121 | */ |
||
| 122 | function it_determines_the_existence_of_a_header_in_a_case_insensitive_way() |
||
| 123 | { |
||
| 124 | $this->hasHeader("FOO")->shouldBe(true); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Returns false if no matching header name is found in the message. |
||
| 129 | */ |
||
| 130 | function it_reports_that_it_does_not_have_a_header() |
||
| 131 | { |
||
| 132 | $this->hasHeader("bar")->shouldBe(false); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Returns an array of string values as provided for the given header. |
||
| 137 | */ |
||
| 138 | function it_returns_an_existing_header_as_an_array_of_strings() |
||
| 139 | { |
||
| 140 | $spec_header = array( |
||
| 141 | "fOo" => array( |
||
| 142 | "baR" |
||
| 143 | ) |
||
| 144 | ); |
||
| 145 | |||
| 146 | $this->getHeader("foo")->shouldReturn($spec_header); |
||
| 147 | |||
| 148 | $header = $this->getHeader("foo"); |
||
| 149 | |||
| 150 | $header->shouldBeArray(); |
||
| 151 | |||
| 152 | foreach ($header as $value) { |
||
| 153 | $value->shouldHaveType("string"); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | function it_returns_an_existing_header_in_a_case_insensitive_way() |
||
| 158 | { |
||
| 159 | $spec_header = array( |
||
| 160 | "fOo" => array( |
||
| 161 | "baR" |
||
| 162 | ) |
||
| 163 | ); |
||
| 164 | |||
| 165 | $this->getHeader("FOO")->shouldReturn($spec_header); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * If the header does not appear in the message, this method MUST return an |
||
| 170 | * empty array. |
||
| 171 | */ |
||
| 172 | function it_returns_an_empty_array_when_a_header_does_not_exist() |
||
| 173 | { |
||
| 174 | $header = $this->getHeader("bar"); |
||
| 175 | |||
| 176 | $header->shouldBeArray(); |
||
| 177 | $header->shouldHaveCount(0); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Retrieves a comma-separated string of the values for a single header. |
||
| 182 | */ |
||
| 183 | function it_returns_a_string_of_values_for_an_existing_header() |
||
| 184 | { |
||
| 185 | $this->getHeaderLine("foo")->shouldBe("bar,baz"); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * This method returns all of the header values of the given |
||
| 190 | * case-insensitive header name as a string concatenated together using |
||
| 191 | * a comma. |
||
| 192 | */ |
||
| 193 | function it_returns_a_string_of_values_for_an_existing_header_in_a_case_insensitive_way() |
||
| 194 | { |
||
| 195 | $this->getHeaderLine("FOO")->shouldBe("bar,baz"); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * If the header does not appear in the message, this method MUST return |
||
| 200 | * an empty string. |
||
| 201 | */ |
||
| 202 | function it_returns_an_empty_string_for_a_header_that_does_not_exist() |
||
| 203 | { |
||
| 204 | $this->getHeaderLine("bar")->shouldBe(""); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Return an instance with the provided value replacing the specified header. |
||
| 209 | */ |
||
| 210 | function it_returns_a_new_request_with_a_header_update_with_a_single_value() |
||
| 211 | { |
||
| 212 | $header = "foo"; |
||
| 213 | $value = "qux"; |
||
| 214 | |||
| 215 | $request = $this->withHeader($header, $value); |
||
| 216 | |||
| 217 | $request->getHeaderLine($header)->shouldBe($value); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * While header names are case-insensitive, the casing of the header will |
||
| 222 | * be preserved by this function, and returned from getHeaders(). |
||
| 223 | */ |
||
| 224 | function it_returns_a_new_request_with_a_header_update_in_a_case_insensitive_way() |
||
| 225 | { |
||
| 226 | $header = "FOO"; |
||
| 227 | $value = "qux"; |
||
| 228 | |||
| 229 | $request = $this->withHeader($header, $value); |
||
| 230 | |||
| 231 | $request->getHeaderLine("foo")->shouldBe($value); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Return an instance with the provided value replacing the specified header. |
||
| 236 | */ |
||
| 237 | function it_returns_a_new_request_with_a_header_update_with_an_array_of_values() |
||
| 238 | { |
||
| 239 | $header = "foo"; |
||
| 240 | $values = array("qux", "quux"); |
||
| 241 | |||
| 242 | $request = $this->withHeader($header, $values); |
||
| 243 | |||
| 244 | $request->getHeaderLine($header)->shouldBe("qux,quux"); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * This method MUST be implemented in such a way as to retain the |
||
| 249 | * immutability of the message, and MUST return an instance that has the |
||
| 250 | * new and/or updated header and value. |
||
| 251 | */ |
||
| 252 | function it_retains_its_original_header_when_it_replaces_a_header() |
||
| 253 | { |
||
| 254 | $spec_header = array( |
||
| 255 | "fOo" => array( |
||
| 256 | "baR" |
||
| 257 | ) |
||
| 258 | ); |
||
| 259 | |||
| 260 | $header = "foo"; |
||
| 261 | $values = array("qux", "quux"); |
||
| 262 | |||
| 263 | $request = $this->withHeader($header, $values); |
||
| 264 | |||
| 265 | $request->getHeaderLine($header)->shouldBe("qux,quux"); |
||
| 266 | |||
| 267 | $this->getHeaders()->shouldReturn($spec_header); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Throws \InvalidArgumentException for invalid header names or values. |
||
| 272 | */ |
||
| 273 | function it_throws_an_exception_with_a_header_update_with_invalid_header_name() |
||
| 274 | { |
||
| 275 | $header = array(); |
||
| 276 | $value = "qux"; |
||
| 277 | |||
| 278 | $arguments = array($header, $value); |
||
| 279 | |||
| 280 | $this->shouldThrow('\InvalidArgumentException')->during("withHeader", $arguments); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Throws \InvalidArgumentException for invalid header names or values. |
||
| 285 | */ |
||
| 286 | function it_throws_an_exception_with_a_header_update_with_invalid_value() |
||
| 287 | { |
||
| 288 | $header = "foo"; |
||
| 289 | $value = array(); |
||
| 290 | |||
| 291 | $arguments = array($header, $value); |
||
| 292 | |||
| 293 | $this->shouldThrow('\InvalidArgumentException')->during("withHeader", $arguments); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Existing values for the specified header will be maintained. The new |
||
| 298 | * value(s) will be appended to the existing list. |
||
| 299 | */ |
||
| 300 | function it_returns_a_new_request_with_a_header_added_with_a_single_value() |
||
| 301 | { |
||
| 302 | $header = "foo"; |
||
| 303 | $value = "qux"; |
||
| 304 | |||
| 305 | $request = $this->withAddedHeader($header, $value); |
||
| 306 | |||
| 307 | $request->getHeaderLine($header)->shouldBe("bar,baz,qux"); |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * While header names are case-insensitive, the casing of the header will |
||
| 312 | * be preserved by this function, and returned from getHeaders(). |
||
| 313 | */ |
||
| 314 | function it_returns_a_new_request_with_a_header_added_in_a_case_insensitive_way() |
||
| 315 | { |
||
| 316 | $header = "FOO"; |
||
| 317 | $value = "qux"; |
||
| 318 | |||
| 319 | $request = $this->withAddedHeader($header, $value); |
||
| 320 | |||
| 321 | $request->getHeaderLine("foo")->shouldBe("bar,baz,qux"); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Existing values for the specified header will be maintained. The new |
||
| 326 | * value(s) will be appended to the existing list. |
||
| 327 | */ |
||
| 328 | function it_returns_a_new_request_with_a_header_added_with_an_array_of_values() |
||
| 329 | { |
||
| 330 | $header = "foo"; |
||
| 331 | $values = array("qux", "quux"); |
||
| 332 | |||
| 333 | $request = $this->withAddedHeader($header, $values); |
||
| 334 | |||
| 335 | $request->getHeaderLine($header)->shouldBe("bar,baz,qux,quux"); |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * If the header did not exist previously, it will be added. |
||
| 340 | */ |
||
| 341 | function it_returns_a_new_request_with_a_header_created_given_a_nonexistent_header() |
||
| 342 | { |
||
| 343 | $header = "bar"; |
||
| 344 | $values = array("qux", "quux"); |
||
| 345 | |||
| 346 | $request = $this->withAddedHeader($header, $values); |
||
| 347 | |||
| 348 | $request->getHeaderLine($header)->shouldBe("qux,quux"); |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * This method MUST be implemented in such a way as to retain the |
||
| 353 | * immutability of the message, and MUST return an instance that has the |
||
| 354 | * new and/or updated header and value. |
||
| 355 | */ |
||
| 356 | function it_retains_its_original_header_when_adding_a_header() |
||
| 357 | { |
||
| 358 | $header = "foo"; |
||
| 359 | $value = "qux"; |
||
| 360 | |||
| 361 | $request = $this->withAddedHeader($header, $value); |
||
| 362 | |||
| 363 | $request->getHeaderLine($header)->shouldReturn("bar,baz,qux"); |
||
| 364 | $this->getHeaderLine($header)->shouldReturn("bar,baz"); |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Throws \InvalidArgumentException for invalid header names or values. |
||
| 369 | */ |
||
| 370 | function it_throws_an_exception_with_a_header_added_with_invalid_header_name() |
||
| 371 | { |
||
| 372 | $header = array(); |
||
| 373 | $value = "qux"; |
||
| 374 | |||
| 375 | $arguments = array($header, $value); |
||
| 376 | |||
| 377 | $this->shouldThrow('\InvalidArgumentException')->during("withAddedHeader", $arguments); |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Throws \InvalidArgumentException for invalid header names or values. |
||
| 382 | */ |
||
| 383 | function it_throws_an_exception_with_a_header_added_with_invalid_value() |
||
| 384 | { |
||
| 385 | $header = "foo"; |
||
| 386 | $value = array(); |
||
| 387 | |||
| 388 | $arguments = array($header, $value); |
||
| 389 | |||
| 390 | $this->shouldThrow('\InvalidArgumentException')->during("withAddedHeader", $arguments); |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Return an instance without the specified header. |
||
| 395 | */ |
||
| 396 | function it_returns_a_new_request_without_a_header() |
||
| 397 | { |
||
| 398 | $request = $this->withoutHeader("foo"); |
||
| 399 | |||
| 400 | $request->getHeaderLine("foo")->shouldBe(""); |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Header resolution MUST be done without case-sensitivity. |
||
| 405 | */ |
||
| 406 | function it_returns_a_new_request_without_a_header_in_a_case_insensitive_way() |
||
| 407 | { |
||
| 408 | $request = $this->withoutHeader("FOO"); |
||
| 409 | |||
| 410 | $request->getHeaderLine("foo")->shouldBe(""); |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * This method MUST be implemented in such a way as to retain the |
||
| 415 | * immutability of the message, and MUST return an instance that removes |
||
| 416 | * the named header. |
||
| 417 | */ |
||
| 418 | function it_retains_its_header_when_returning_a_new_request_without_a_header() |
||
| 419 | { |
||
| 420 | $request = $this->withoutHeader("FOO"); |
||
| 421 | |||
| 422 | $request->getHeaderLine("foo")->shouldBe(""); |
||
| 423 | |||
| 424 | $this->getHeaderLine("foo")->shouldReturn("bar,baz"); |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Gets the body of the message. |
||
| 429 | */ |
||
| 430 | function it_returns_the_body_of_its_message_as_a_stream() |
||
| 431 | { |
||
| 432 | $this->getBody()->shouldBeAnInstanceOf("StreamInterface"); |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Return an instance with the specified message body. |
||
| 437 | * |
||
| 438 | * @param \PhpSpec\Wrapper\Collaborator|StreamInterface $body |
||
| 439 | */ |
||
| 440 | function it_returns_a_new_request_with_a_body(StreamInterface $body) |
||
| 441 | { |
||
| 442 | $body->getContents()->willReturn("I'm a stream!"); |
||
| 443 | |||
| 444 | $request = $this->withBody($body); |
||
| 445 | |||
| 446 | $request->getBody()->getContents()->shouldBe("I'm a stream!"); |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * This method MUST be implemented in such a way as to retain the |
||
| 451 | * immutability of the message, and MUST return a new instance that has the |
||
| 452 | * new body stream. |
||
| 453 | * |
||
| 454 | * @param \PhpSpec\Wrapper\Collaborator|StreamInterface $old_body |
||
| 455 | * @param \PhpSpec\Wrapper\Collaborator|StreamInterface $new_body |
||
| 456 | */ |
||
| 457 | function it_retains_its_body_when_returning_a_new_request_with_a_body(StreamInterface $old_body, |
||
| 458 | StreamInterface $new_body) |
||
| 459 | { |
||
| 460 | $old_body->getContents()->willReturn("I'm the original stream!"); |
||
| 461 | $new_body->getContents()->willReturn("I'm the new stream!"); |
||
| 462 | |||
| 463 | $request = $this->withBody($new_body); |
||
| 464 | |||
| 465 | $request->getBody()->getContents()->shouldReturn("I'm the new stream!"); |
||
| 466 | |||
| 467 | $this->getBody()->getContents()->shouldReturn("I'm the original stream!"); |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * The body MUST be a StreamInterface object. |
||
| 472 | * |
||
| 473 | * Throws \InvalidArgumentException When the body is not valid. |
||
| 474 | */ |
||
| 475 | function it_throws_an_exception_with_an_invalid_body() |
||
| 476 | { |
||
| 477 | $body = "I'm not a stream!"; |
||
| 478 | |||
| 479 | $argument = array($body); |
||
| 480 | |||
| 481 | $this->shouldThrow('\InvalidArgumentException')->during("withBody", $argument); |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Retrieves the message's request target. |
||
| 486 | * |
||
| 487 | * Retrieves the message's request-target either as it will appear (for |
||
| 488 | * clients), as it appeared at request (for servers), or as it was |
||
| 489 | * specified for the instance (see withRequestTarget()). |
||
| 490 | * |
||
| 491 | * In most cases, this will be the origin-form of the composed URI, |
||
| 492 | * unless a value was provided to the concrete implementation (see |
||
| 493 | * withRequestTarget() below). |
||
| 494 | */ |
||
| 495 | function it_returns_the_request_target_from_a_uri_path() |
||
| 496 | { |
||
| 497 | $this->getRequestTarget()->shouldReturn("/baz/qux.html"); |
||
| 498 | } |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Retrieves the message's request target. |
||
| 502 | * |
||
| 503 | * Retrieves the message's request-target either as it will appear (for |
||
| 504 | * clients), as it appeared at request (for servers), or as it was |
||
| 505 | * specified for the instance (see withRequestTarget()). |
||
| 506 | * |
||
| 507 | * In most cases, this will be the origin-form of the composed URI, |
||
| 508 | * unless a value was provided to the concrete implementation (see |
||
| 509 | * withRequestTarget() below). |
||
| 510 | */ |
||
| 511 | function it_returns_the_request_target_explicitly_set() |
||
| 512 | { |
||
| 513 | $this->getRequestTarget()->shouldReturn("*"); |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * If no URI is available, and no request-target has been specifically |
||
| 518 | * provided, this method MUST return the string "/". |
||
| 519 | */ |
||
| 520 | function it_returns_an_empty_path_when_no_request_target_and_no_uri_exists() |
||
| 521 | { |
||
| 522 | $this->getRequestTarget()->shouldReturn("/"); |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Return an instance with the specific request-target. |
||
| 527 | */ |
||
| 528 | function it_returns_a_new_request_with_a_request_target() |
||
| 529 | { |
||
| 530 | $target = "https://www.foo.bar/baz/qux.html"; |
||
| 531 | |||
| 532 | $request = $this->withRequestTarget($target); |
||
| 533 | |||
| 534 | $request->getRequestTarget()->shouldReturn($target); |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * This method MUST be implemented in such a way as to retain the |
||
| 539 | * immutability of the message, and MUST return an instance that has the |
||
| 540 | * changed request target. |
||
| 541 | */ |
||
| 542 | function it_retains_its_request_target_when_creating_a_request_with_a_new_request_target() |
||
| 543 | { |
||
| 544 | $target = "https://www.foo.bar/baz/qux.html"; |
||
| 545 | |||
| 546 | $request = $this->withRequestTarget($target); |
||
| 547 | |||
| 548 | $request->getRequestTarget()->shouldReturn($target); |
||
| 549 | $this->getRequestTarget()->shouldReturn("/baz/qux.html"); |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * $requestTarget parameter type may be mixed. |
||
| 554 | */ |
||
| 555 | function it_returns_a_new_request_with_an_empty_request_target_with_a_null_request_target() |
||
| 556 | { |
||
| 557 | $target = null; |
||
| 558 | |||
| 559 | $request = $this->withRequestTarget($target); |
||
| 560 | |||
| 561 | $request->getRequestTarget()->shouldReturn("/"); |
||
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Retrieves the HTTP method of the request. |
||
| 566 | */ |
||
| 567 | function it_returns_its_method() |
||
| 568 | { |
||
| 569 | $this->getMethod()->shouldReturn("HEAD"); |
||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Return an instance with the provided HTTP method. |
||
| 574 | * |
||
| 575 | * While HTTP method names are typically all uppercase characters, HTTP |
||
| 576 | * method names are case-sensitive and thus implementations SHOULD NOT |
||
| 577 | * modify the given string. |
||
| 578 | */ |
||
| 579 | function it_returns_a_new_request_with_a_method() |
||
| 580 | { |
||
| 581 | $method = "GET"; |
||
| 582 | |||
| 583 | $request = $this->withMethod($method); |
||
| 584 | |||
| 585 | $request->getMethod()->shouldReturn($method); |
||
| 586 | } |
||
| 587 | |||
| 588 | /** |
||
| 589 | * This method MUST be implemented in such a way as to retain the |
||
| 590 | * immutability of the message, and MUST return an instance that has the |
||
| 591 | * changed request method. |
||
| 592 | */ |
||
| 593 | function it_retains_its_method_when_creating_a_request_with_a_new_method() |
||
| 594 | { |
||
| 595 | $method = "GET"; |
||
| 596 | |||
| 597 | $request = $this->withMethod($method); |
||
| 598 | |||
| 599 | $request->getMethod()->shouldReturn($method); |
||
| 600 | $this->getMethod()->shouldReturn("HEAD"); |
||
| 601 | } |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Throws \InvalidArgumentException for invalid HTTP methods. |
||
| 605 | */ |
||
| 606 | function it_throws_an_exception_with_an_invalid_method() |
||
| 607 | { |
||
| 608 | $method = "FOO"; |
||
| 609 | |||
| 610 | $arguments = array($method); |
||
| 611 | |||
| 612 | $this->shouldThrow('\InvalidArgumentException')->during("withMethod", $arguments); |
||
| 613 | } |
||
| 614 | |||
| 615 | /** |
||
| 616 | * This method MUST return a UriInterface instance. |
||
| 617 | */ |
||
| 618 | function it_returns_its_uri() |
||
| 619 | { |
||
| 620 | $this->getUri()->shouldReturnAnInstanceOf("UriInterface"); |
||
| 621 | } |
||
| 622 | |||
| 623 | /** |
||
| 624 | * This method MUST update the Host header of the returned request by |
||
| 625 | * default if the URI contains a host component. |
||
| 626 | * |
||
| 627 | * @param \PhpSpec\Wrapper\Collaborator|UriInterface $uri |
||
| 628 | */ |
||
| 629 | function it_returns_a_new_request_with_a_uri_with_host_replacement(UriInterface $uri) |
||
| 630 | { |
||
| 631 | $host = "www.baz.qux"; |
||
| 632 | |||
| 633 | $uri->getHost()->willReturn($host); |
||
| 634 | |||
| 635 | $request = $this->withUri($uri); |
||
| 636 | |||
| 637 | $request->getUri()->getHost()->shouldReturn($host); |
||
| 638 | $request->getHeaderLine("host")->shouldReturn($host); |
||
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * You can opt-in to preserving the original state of the Host header by |
||
| 643 | * setting `$preserveHost` to `true`. |
||
| 644 | * |
||
| 645 | * When `$preserveHost` is set to `true`, this method interacts with the |
||
| 646 | * Host header in the following ways: |
||
| 647 | * |
||
| 648 | * - If a Host header is present and non-empty, this method MUST NOT update |
||
| 649 | * the Host header in the returned request. |
||
| 650 | * |
||
| 651 | * @param \PhpSpec\Wrapper\Collaborator|UriInterface $uri |
||
| 652 | */ |
||
| 653 | function it_returns_a_new_request_with_a_uri_without_host_replacement(UriInterface $uri) |
||
| 654 | { |
||
| 655 | $old_host = $this->getHeaderLine("host"); |
||
| 656 | $new_host = "www.baz.qux"; |
||
| 657 | |||
| 658 | $uri->getHost()->willReturn($new_host); |
||
| 659 | |||
| 660 | $request = $this->withUri($uri, true); |
||
| 661 | |||
| 662 | $request->getHeaderLine("host")->shouldReturn($old_host); |
||
| 663 | $request->getUri()->getHost()->shouldReturn($new_host); |
||
| 664 | } |
||
| 665 | |||
| 666 | /** |
||
| 667 | * When `$preserveHost` is set to `true`, this method interacts with the |
||
| 668 | * Host header in the following ways: |
||
| 669 | * |
||
| 670 | * - If the Host header is missing or empty, and the new URI contains |
||
| 671 | * a host component, this method MUST update the Host header in the returned |
||
| 672 | * request. |
||
| 673 | * |
||
| 674 | * @param \PhpSpec\Wrapper\Collaborator|UriInterface $uri |
||
| 675 | */ |
||
| 676 | function it_returns_a_new_request_with_a_host_when_no_host_set(UriInterface $uri) |
||
| 677 | { |
||
| 678 | $host = "www.baz.qux"; |
||
| 679 | |||
| 680 | $uri->getHost()->willReturn($host); |
||
| 681 | |||
| 682 | $no_host = $this->withoutHeader("host"); |
||
| 683 | |||
| 684 | $request = $no_host->withUri($uri, true); |
||
| 685 | |||
| 686 | $request->getHeaderLine("host")->shouldReturn($host); |
||
| 687 | } |
||
| 688 | |||
| 689 | /** |
||
| 690 | * When `$preserveHost` is set to `true`, this method interacts with the |
||
| 691 | * Host header in the following ways: |
||
| 692 | * |
||
| 693 | * - If the Host header is missing or empty, and the new URI contains |
||
| 694 | * a host component, this method MUST update the Host header in the returned |
||
| 695 | * request. |
||
| 696 | * |
||
| 697 | * @param \PhpSpec\Wrapper\Collaborator|UriInterface $uri |
||
| 698 | */ |
||
| 699 | function it_returns_a_new_request_with_a_host_when_empty_host_set(UriInterface $uri) |
||
| 700 | { |
||
| 701 | $host = "www.baz.qux"; |
||
| 702 | |||
| 703 | $uri->getHost()->willReturn($host); |
||
| 704 | |||
| 705 | $empty_host = $this->withHeader("host",""); |
||
| 706 | |||
| 707 | $request = $empty_host->withUri($uri, true); |
||
| 708 | |||
| 709 | $request->getHeaderLine("host")->shouldReturn($host); |
||
| 710 | } |
||
| 711 | |||
| 712 | /** |
||
| 713 | * When `$preserveHost` is set to `true`, this method interacts with the |
||
| 714 | * Host header in the following ways: |
||
| 715 | * |
||
| 716 | * - If the Host header is missing or empty, and the new URI does not contain a |
||
| 717 | * host component, this method MUST NOT update the Host header in the returned |
||
| 718 | * request. |
||
| 719 | * |
||
| 720 | * @param \PhpSpec\Wrapper\Collaborator|UriInterface $uri |
||
| 721 | */ |
||
| 722 | function it_preserves_its_original_empty_host_with_a_hostless_uri(UriInterface $uri) |
||
| 723 | { |
||
| 724 | $uri->getHost()->willReturn(null); |
||
| 725 | |||
| 726 | $empty_host = $this->withHeader("host",""); |
||
| 727 | |||
| 728 | $request = $empty_host->withUri($uri); |
||
| 729 | |||
| 730 | $request->getHeaderLine("host")->shouldReturn(""); |
||
| 731 | } |
||
| 732 | |||
| 733 | /** |
||
| 734 | * This method MUST be implemented in such a way as to retain the |
||
| 735 | * immutability of the message, and MUST return an instance that has the |
||
| 736 | * new UriInterface instance. |
||
| 737 | * |
||
| 738 | * @param \PhpSpec\Wrapper\Collaborator|UriInterface $old_uri |
||
| 739 | * @param \PhpSpec\Wrapper\Collaborator|UriInterface $new_uri |
||
| 740 | */ |
||
| 741 | function it_retains_its_uri_when_returning_a_new_request_with_a_uri(UriInterface $old_uri, |
||
| 742 | UriInterface $new_uri) |
||
| 743 | { |
||
| 744 | $old_uri->getHost()->willReturn("www.foo.bar"); |
||
| 745 | $new_uri->getHost()->willReturn("www.baz.qux"); |
||
| 746 | |||
| 747 | $request = $this->withUri($new_uri); |
||
| 748 | |||
| 749 | $request->getUri()->getHost()->shouldReturn("www.baz.qux"); |
||
| 750 | $this->getUri()->getHost()->shouldReturn("www.foo.bar"); |
||
| 751 | } |
||
| 752 | } |
||
| 753 |