Passed
Branch master (e8fd46)
by Alexey
02:50
created

RouteMatcherSpec   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 45.9 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 0
Metric Value
dl 28
loc 61
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 10

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace spec\Venta\Routing;
4
5
use FastRoute\Dispatcher;
6
use PhpSpec\ObjectBehavior;
7
use Prophecy\Argument;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Message\UriInterface;
10
use Venta\Contracts\Routing\FastrouteDispatcherFactory;
11
use Venta\Contracts\Routing\Route;
12
use Venta\Contracts\Routing\RouteCollection;
13
use Venta\Contracts\Routing\RouteParser;
14
use Venta\Routing\Exception\NotAllowedException;
15
use Venta\Routing\Exception\NotFoundException;
16
17
class RouteMatcherSpec extends ObjectBehavior
18
{
19
20
    function let(RouteParser $parser, FastrouteDispatcherFactory $factory, Route $route, Dispatcher $dispatcher)
21
    {
22
        $parser->parse(Argument::containing($route->getWrappedObject()))->willReturn(['parsed']);
23
        $factory->create(['parsed'])->willReturn($dispatcher);
24
        $this->beConstructedWith($parser, $factory);
25
    }
26
27
    function it_is_initializable()
28
    {
29
        $this->shouldImplement(\Venta\Contracts\Routing\RouteMatcher::class);
30
    }
31
32
    function it_matches_route(
33
        ServerRequestInterface $request,
34
        RouteCollection $routeCollection,
35
        Route $route,
36
        UriInterface $uri,
37
        Dispatcher $dispatcher
38
    ) {
39
        $routeCollection->getRoutes()->willReturn([$route]);
40
        $request->getUri()->willReturn($uri);
41
        $uri->getPath()->willReturn('/url');
42
        $request->getMethod()->willReturn('GET');
43
        $dispatcher->dispatch('GET', '/url')->willReturn([Dispatcher::FOUND, $route, ['vars']]);
44
        $route->withVariables(['vars'])->willReturn($route);
45
        $this->match($request, $routeCollection)->shouldBe($route);
46
    }
47
48
    function it_throws_not_allowed_exception(
49
        ServerRequestInterface $request,
50
        RouteCollection $routeCollection,
51
        Route $route,
52
        UriInterface $uri,
53
        Dispatcher $dispatcher
54
    ) {
55
        $routeCollection->getRoutes()->willReturn([$route]);
56
        $request->getUri()->willReturn($uri);
57
        $uri->getPath()->willReturn('/url');
58
        $request->getMethod()->willReturn('GET');
59
        $dispatcher->dispatch('GET', '/url')->willReturn([Dispatcher::METHOD_NOT_ALLOWED, ['POST']]);
60
        $this->shouldThrow(NotAllowedException::class)->match($request, $routeCollection);
61
    }
62
63
    function it_throws_not_found_exception(
64
        ServerRequestInterface $request,
65
        RouteCollection $routeCollection,
66
        Route $route,
67
        UriInterface $uri,
68
        Dispatcher $dispatcher
69
    ) {
70
        $routeCollection->getRoutes()->willReturn([$route]);
71
        $request->getUri()->willReturn($uri);
72
        $uri->getPath()->willReturn('/url');
73
        $request->getMethod()->willReturn('GET');
74
        $dispatcher->dispatch('GET', '/url')->willReturn([Dispatcher::NOT_FOUND]);
75
        $this->shouldThrow(NotFoundException::class)->match($request, $routeCollection);
76
    }
77
}
78