Completed
Push — master ( bd4c23...b341c0 )
by Filipe
10:29
created

RouteBuilderSpec::its_a_route_builder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace spec\Slick\Mvc\Http\Router;
4
5
use Aura\Router\Map;
6
use Aura\Router\Route;
7
use Aura\Router\RouterContainer;
8
use PhpSpec\ObjectBehavior;
9
use Slick\Mvc\Exception\RoutesFileNotFoundException;
10
use Slick\Mvc\Exception\RoutesFileParserException;
11
use Slick\Mvc\Http\Router\Builder\FactoryInterface;
12
use Slick\Mvc\Http\Router\RouteBuilder;
13
use Slick\Mvc\Http\Router\RouteBuilderInterface;
14
use Symfony\Component\Yaml\Exception\ParseException;
15
use Symfony\Component\Yaml\Parser;
16
17
class RouteBuilderSpec extends ObjectBehavior
18
{
19
    function let(Parser $parser, FactoryInterface $routeFactory)
20
    {
21
        $file = __DIR__ . '/routes.yml';
22
        $this->beConstructedWith($file, $parser, $routeFactory);
23
    }
24
25
    function it_is_initializable()
26
    {
27
        $this->shouldHaveType(RouteBuilder::class);
28
    }
29
30
    function its_a_route_builder()
31
    {
32
        $this->shouldBeAnInstanceOf(RouteBuilderInterface::class);
33
    }
34
35
    function it_registers_itself_to_a_router_container(
36
        RouterContainer $container
37
    )
38
    {
39
        $this->register($container)->shouldBe($this->getWrappedObject());
40
        $container->setMapBuilder([$this->getWrappedObject(), 'build'])
41
            ->shouldHaveBeenCalled();
42
    }
43
44
    function it_can_parse_yml_files(
45
        Parser $parser,
46
        FactoryInterface $routeFactory,
47
        Map $map
48
    ) {
49
        $file = __DIR__ . '/routes.yml';
50
        $this->beConstructedWith($file, $parser, $routeFactory);
51
        $defaults = [];
52
        $parser->parse(file_get_contents($file))->willReturn($defaults);
53
        $this->build($map);
54
        $parser->parse(file_get_contents($file))->shouldHaveBeenCalled();
55
    }
56
57
    function it_can_set_router_defaults(
58
        Parser $parser,
59
        FactoryInterface $routeFactory
60
    )
61
    {
62
        $map = new Map(new Route());
63
        $file = __DIR__ . '/routes.yml';
64
        $defaults = [
65
            'defaults' => [
66
                'namespace' => 'Controller',
67
                'action' => 'index',
68
                'controller' => 'pages'
69
            ]
70
        ];
71
        $parser->parse(file_get_contents($file))->willReturn($defaults);
72
        $this->beConstructedWith($file, $parser, $routeFactory);
73
        $this->build($map);
74
    }
75
76
    function it_can_parser_routes_from_yml(
77
        Parser $parser,
78
        FactoryInterface $routeFactory,
79
        Map $map
80
    ) {
81
        $file = __DIR__ . '/routes.yml';
82
        $routes = [
83
            'routes' => [
84
                'home' => [
85
                    'allows' => ['GET', 'POST'],
86
                    'path' => '/',
87
                    'defaults' => [
88
                        'action' => 'home'
89
                    ]
90
                ]
91
            ]
92
        ];
93
        $parser->parse(file_get_contents($file))
94
            ->willReturn($routes);
95
        $this->beConstructedWith($file, $parser, $routeFactory);
96
        $this->build($map);
97
98
        $routeFactory->parse('home', $routes['routes']['home'], $map)->shouldHaveBeenCalled();
99
    }
100
101
    function it_throws_exception_when_routes_file_is_not_found(
102
        Parser $parser,
103
        FactoryInterface $routeFactory,
104
        Map $map
105
    ) {
106
        $file = 'some/where/over/the/disk/but/not/found.yml';
107
        $this->beConstructedWith($file, $parser, $routeFactory);
108
        $this->shouldThrow(RoutesFileNotFoundException::class)
109
            ->during('build',  [$map]);
110
    }
111
112
    function it_throws_exception_for_yml_parsing_errors(
113
        Parser $parser,
114
        FactoryInterface $routeFactory,
115
        Map $map
116
    )
117
    {
118
        $file = __DIR__ . '/routes.yml';
119
        $parser->parse(file_get_contents($file))
120
            ->willThrow(new ParseException('test'));
121
        $this->beConstructedWith($file, $parser, $routeFactory);
122
        $this->shouldThrow(RoutesFileParserException::class)
123
            ->during('build',  [$map]);
124
    }
125
}