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:
| 1 | <?php |
||
| 26 | class ContextSpec extends ObjectBehavior |
||
| 27 | { |
||
| 28 | |||
| 29 | function let(ServerRequestInterface $request, UriGeneratorInterface $uriGenerator) |
||
| 30 | { |
||
| 31 | $route = (new Route())->attributes(['foo' =>'bar', 'bar' => 'baz']); |
||
| 32 | $this->beConstructedWith($request, $route, $uriGenerator); |
||
| 33 | } |
||
| 34 | |||
| 35 | function its_a_controller_context() |
||
| 36 | { |
||
| 37 | $this->shouldBeAnInstanceOf(ControllerContextInterface::class); |
||
| 38 | } |
||
| 39 | |||
| 40 | function it_is_initializable() |
||
| 41 | { |
||
| 42 | $this->shouldHaveType(Context::class); |
||
| 43 | } |
||
| 44 | |||
| 45 | function it_can_have_a_list_of_query_params( |
||
| 46 | ServerRequestInterface $request |
||
| 47 | ) |
||
| 48 | { |
||
| 49 | $data = ['foo' =>'bar', 'bar' => 'baz']; |
||
| 50 | $request->getQueryParams()->willReturn($data); |
||
| 51 | $this->queryParam()->shouldBe($data); |
||
| 52 | } |
||
| 53 | |||
| 54 | function it_can_retrieve_a_single_query_param_value( |
||
| 55 | ServerRequestInterface $request |
||
| 56 | ) |
||
| 57 | { |
||
| 58 | $data = ['foo' =>'bar', 'bar' => 'baz']; |
||
| 59 | $request->getQueryParams()->willReturn($data); |
||
| 60 | $this->queryParam('foo')->shouldBe('bar'); |
||
| 61 | } |
||
| 62 | |||
| 63 | function it_can_have_a_list_of_post_parameters( |
||
| 64 | ServerRequestInterface $request |
||
| 65 | ) |
||
| 66 | { |
||
| 67 | $data = ['foo' =>'bar', 'bar' => 'baz']; |
||
| 68 | $request->getParsedBody()->willReturn($data); |
||
| 69 | $this->postParam()->shouldBe($data); |
||
| 70 | } |
||
| 71 | |||
| 72 | function it_will_return_a_default_value_for_missing_parameters( |
||
| 73 | ServerRequestInterface $request |
||
| 74 | ) |
||
| 75 | { |
||
| 76 | $data = ['foo' =>'bar', 'bar' => 'baz']; |
||
| 77 | $request->getParsedBody()->willReturn($data); |
||
| 78 | $this->postParam('baz', 'foo')->shouldBe('foo'); |
||
| 79 | } |
||
| 80 | |||
| 81 | function it_can_retrieve_a_route_parameter() |
||
| 82 | { |
||
| 83 | $this->routeParam('foo')->shouldBe('bar'); |
||
| 84 | } |
||
| 85 | |||
| 86 | function it_has_a_server_request(ServerRequestInterface $request) |
||
| 87 | { |
||
| 88 | $this->request()->shouldBe($request); |
||
| 89 | } |
||
| 90 | |||
| 91 | function it_can_check_if_current_request_has_a_given_method( |
||
| 92 | ServerRequestInterface $request |
||
| 93 | ) |
||
| 94 | { |
||
| 95 | $request->getMethod()->willReturn('PUT'); |
||
| 96 | $this->requestIs('put')->shouldBe(true); |
||
| 97 | } |
||
| 98 | |||
| 99 | function it_can_be_set_with_a_response(ResponseInterface $response) |
||
| 100 | { |
||
| 101 | $this->setResponse($response)->shouldBe($this->getWrappedObject()); |
||
| 102 | $this->response()->shouldBe($response); |
||
| 103 | } |
||
| 104 | |||
| 105 | function it_can_set_a_redirect_response(UriGeneratorInterface $uriGenerator) |
||
| 106 | { |
||
| 107 | $uriGenerator->generate('/some/page', ['foo' => 'bar']) |
||
| 108 | ->shouldBeCalled() |
||
| 109 | ->willReturn(new Uri('http://example.com/some/page?foo=bar')); |
||
| 110 | |||
| 111 | $this->redirect('/some/page', ['foo' => 'bar']); |
||
| 112 | $response = $this->response(); |
||
| 113 | $response->shouldBeAnInstanceOf(ResponseInterface::class); |
||
| 114 | $response->getStatusCode()->shouldBe(302); |
||
| 115 | $response->getHeaderLine('Location')->shouldBe('http://example.com/some/page?foo=bar'); |
||
| 116 | $this->handlesResponse()->shouldBe(true); |
||
| 117 | } |
||
| 118 | |||
| 119 | function it_can_set_the_template_to_be_used(ServerRequestInterface $request) |
||
| 120 | { |
||
| 121 | $request->withAttribute('template', 'some/path') |
||
| 122 | ->shouldBeCalled() |
||
| 123 | ->willReturn($request); |
||
| 124 | $this->useTemplate('some/path')->shouldBe($this->getWrappedObject()); |
||
| 125 | } |
||
| 126 | |||
| 127 | function it_can_change_the_request(ServerRequestInterface $otherRequest, ServerRequestInterface $request) |
||
| 128 | { |
||
| 129 | $this->changeRequest($otherRequest)->shouldBe($this->getWrappedObject()); |
||
| 130 | $this->request()->shouldBe($otherRequest); |
||
| 131 | $this->request()->shouldNotBe($request); |
||
| 132 | } |
||
| 133 | } |