Completed
Push — master ( c0f250...321e24 )
by Filipe
15:10
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

9 Methods

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