Completed
Push — master ( c4d705...5571d6 )
by Filipe
03:31
created

RouteBuilderSpec   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 0
loc 102
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of slick/web_stack package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace spec\Slick\WebStack\Router;
11
12
use Aura\Router\Map;
13
use Aura\Router\Route;
14
use Aura\Router\RouterContainer;
15
use Prophecy\Argument;
16
use Slick\WebStack\Exception\RoutesFileNotFoundException;
17
use Slick\WebStack\Exception\RoutesFileParserException;
18
use Slick\WebStack\Router\Builder\FactoryInterface;
19
use Slick\WebStack\Router\RouteBuilder;
20
use PhpSpec\ObjectBehavior;
21
use Slick\WebStack\Router\RouteBuilderInterface;
22
use Symfony\Component\Yaml\Exception\ParseException;
23
use Symfony\Component\Yaml\Parser;
24
25
/**
26
 * RouteBuilderSpec specs
27
 *
28
 * @package spec\Slick\WebStack\Router
29
 */
30
class RouteBuilderSpec extends ObjectBehavior
31
{
32
    function let(Parser $parser, FactoryInterface $routeFactory)
33
    {
34
        $file = __DIR__ . '/routes.yml';
35
        $this->beConstructedWith($file, $parser, $routeFactory);
36
    }
37
38
    function it_is_initializable()
39
    {
40
        $this->shouldHaveType(RouteBuilder::class);
41
    }
42
43
    function its_a_route_builder()
44
    {
45
        $this->shouldBeAnInstanceOf(RouteBuilderInterface::class);
46
    }
47
48
    function it_registers_itself_to_a_router_container(
49
        RouterContainer $container
50
    ) {
51
        $this->register($container)->shouldBe($this->getWrappedObject());
52
        $container->setMapBuilder([$this->getWrappedObject(), 'build'])
53
            ->shouldHaveBeenCalled();
54
    }
55
56
    function it_can_parse_yml_files(
57
        Parser $parser,
58
        FactoryInterface $routeFactory,
59
        Map $map
60
    ) {
61
        $file = __DIR__ . '/routes.yml';
62
        $this->beConstructedWith($file, $parser, $routeFactory);
63
        $defaults = [];
64
        $parser->parse(file_get_contents($file))->willReturn($defaults);
65
        $this->build($map);
66
        $parser->parse(file_get_contents($file))->shouldHaveBeenCalled();
67
    }
68
69
    function it_can_set_router_defaults(
70
        Parser $parser,
71
        FactoryInterface $routeFactory
72
    ) {
73
        $map = new Map(new Route());
74
        $file = __DIR__ . '/routes.yml';
75
        $defaults = [
76
            'defaults' => [
77
                'namespace' => 'Controller',
78
                'action' => 'index',
79
                'controller' => 'pages'
80
            ]
81
        ];
82
        $parser->parse(file_get_contents($file))->willReturn($defaults);
83
        $this->beConstructedWith($file, $parser, $routeFactory);
84
        $this->build($map);
85
    }
86
87
    function it_can_parser_routes_from_yml(
88
        Parser $parser,
89
        FactoryInterface $routeFactory,
90
        Map $map
91
    ) {
92
        $file = __DIR__ . '/routes.yml';
93
        $routes = [
94
            'routes' => [
95
                'home' => [
96
                    'allows' => ['GET', 'POST'],
97
                    'path' => '/',
98
                    'defaults' => [
99
                        'action' => 'home'
100
                    ]
101
                ]
102
            ]
103
        ];
104
        $parser->parse(file_get_contents($file))
105
            ->willReturn($routes);
106
        $this->beConstructedWith($file, $parser, $routeFactory);
107
        $this->build($map);
108
        $routeFactory->parse('home', $routes['routes']['home'], $map)->shouldHaveBeenCalled();
109
    }
110
111
    function it_throws_exception_when_routes_file_is_not_found(
112
        Parser $parser,
113
        FactoryInterface $routeFactory,
114
        Map $map
115
    ) {
116
        $file = 'some/where/over/the/disk/but/not/found.yml';
117
        $this->beConstructedWith($file, $parser, $routeFactory);
118
        $this->shouldThrow(RoutesFileNotFoundException::class)
119
            ->during('build',  [$map]);
120
    }
121
122
    function it_throws_exception_for_yml_parsing_errors(
123
        Parser $parser,
124
        FactoryInterface $routeFactory,
125
        Map $map
126
    ) {
127
        $file = __DIR__ . '/routes.yml';
128
        $parser->parse(file_get_contents($file))
129
            ->willThrow(new ParseException('test'));
130
        $this->beConstructedWith($file, $parser, $routeFactory);
131
        $this->shouldThrow(RoutesFileParserException::class)
132
            ->during('build',  [$map]);
133
    }
134
135
    function it_reads_multiple_definition_files(FactoryInterface $routeFactory, Map $map)
136
    {
137
        $file = __DIR__ . '/routes.yml';
138
        $this->beConstructedWith($file, new Parser(), $routeFactory);
139
        $this->build($map);
140
        $routeFactory->parse('articles:article.read', Argument::type('array'), $map)->shouldHaveBeenCalled();
141
    }
142
}
143