1 | <?php |
||
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) |
||
143 |