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 |
||
| 27 | class RouterMiddlewareSpec extends ObjectBehavior |
||
| 28 | { |
||
| 29 | |||
| 30 | function let( |
||
| 31 | RouterContainer $routerContainer, |
||
| 32 | ServerRequestInterface $request, |
||
| 33 | RequestHandlerInterface $handler, |
||
| 34 | ResponseInterface $response, |
||
| 35 | Matcher $matcher |
||
| 36 | ) |
||
| 37 | { |
||
| 38 | $routerContainer->getMatcher()->willReturn($matcher); |
||
| 39 | $handler->handle($request)->willReturn($response); |
||
| 40 | $this->beConstructedWith($routerContainer); |
||
| 41 | } |
||
| 42 | |||
| 43 | function its_a_middleware() |
||
| 44 | { |
||
| 45 | $this->shouldBeAnInstanceOf(MiddlewareInterface::class); |
||
| 46 | } |
||
| 47 | |||
| 48 | function it_is_initializable_with_a_router_container() |
||
| 49 | { |
||
| 50 | $this->shouldHaveType(RouterMiddleware::class); |
||
| 51 | } |
||
| 52 | |||
| 53 | function it_matches_the_request_to_its_routes_container( |
||
| 54 | ServerRequestInterface $request, |
||
| 55 | RequestHandlerInterface $handler, |
||
| 56 | ResponseInterface $response, |
||
| 57 | Matcher $matcher, |
||
| 58 | Route $route |
||
| 59 | ) |
||
| 60 | { |
||
| 61 | |||
| 62 | $matcher->match($request)->shouldBeCalled()->willReturn($route); |
||
| 63 | |||
| 64 | $request->withAttribute('route', $route)->shouldBeCalled()->willReturn($request); |
||
| 65 | |||
| 66 | $this->process($request, $handler)->shouldBe($response); |
||
| 67 | } |
||
| 68 | |||
| 69 | function it_returns_a_405_response_if_method_not_allowed( |
||
| 70 | ServerRequestInterface $request, |
||
| 71 | RequestHandlerInterface $handler, |
||
| 72 | Matcher $matcher |
||
| 73 | ) { |
||
| 74 | $matcher->match($request)->willReturn(null); |
||
| 75 | |||
| 76 | $failRoute = new Route(); |
||
| 77 | $failRoute->allows(['POST']); |
||
| 78 | $failRoute->failedRule('Aura\Router\Rule\Allows'); |
||
| 79 | |||
| 80 | $matcher->getFailedRoute()->willReturn($failRoute); |
||
| 81 | |||
| 82 | $response = $this->process($request, $handler); |
||
| 83 | $response->getStatusCode()->shouldBe(405); |
||
| 84 | $response->getHeaderLine('Allow')->shouldBe('POST'); |
||
| 85 | } |
||
| 86 | |||
| 87 | function it_returns_a_406_response_if_request_not_accepted( |
||
| 88 | ServerRequestInterface $request, |
||
| 89 | RequestHandlerInterface $handler, |
||
| 90 | Matcher $matcher |
||
| 91 | ) { |
||
| 92 | $matcher->match($request)->willReturn(null); |
||
| 93 | |||
| 94 | $failRoute = new Route(); |
||
| 95 | $failRoute->failedRule('Aura\Router\Rule\Accepts'); |
||
| 96 | $matcher->getFailedRoute()->willReturn($failRoute); |
||
| 97 | |||
| 98 | $response = $this->process($request, $handler); |
||
| 99 | $response->getStatusCode()->shouldBe(406); |
||
| 100 | } |
||
| 101 | |||
| 102 | function it_returns_a_404_response_for_all_other_failed_routes( |
||
| 103 | ServerRequestInterface $request, |
||
| 104 | RequestHandlerInterface $handler, |
||
| 105 | Matcher $matcher |
||
| 106 | ) { |
||
| 107 | $matcher->match($request)->willReturn(null); |
||
| 108 | |||
| 109 | $failRoute = new Route(); |
||
| 110 | $failRoute->failedRule('other'); |
||
| 111 | $matcher->getFailedRoute()->willReturn($failRoute); |
||
| 112 | |||
| 113 | $response = $this->process($request, $handler); |
||
| 114 | $response->getStatusCode()->shouldBe(404); |
||
| 115 | } |
||
| 116 | } |
||
| 117 |