Skip to content

Commit 634bed0

Browse files
change signature of setRouteCollections (#24)
* change signature of setRouteCollections * Added UrlGeneratorTest with some basic checks Co-authored-by: Christoph Kluge <[email protected]>
1 parent e2980b6 commit 634bed0

File tree

2 files changed

+116
-2
lines changed

2 files changed

+116
-2
lines changed

src/Routing/UrlGenerator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public function version($version)
4141
/**
4242
* Set the route collection instance.
4343
*
44-
* @param array $collections
44+
* @param mixed $collections
4545
*/
46-
public function setRouteCollections(array $collections)
46+
public function setRouteCollections(mixed $collections)
4747
{
4848
$this->collections = $collections;
4949
}

tests/Routing/UrlGeneratorTest.php

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace Dingo\Api\Tests\Routing;
4+
5+
use Dingo\Api\Routing\Router;
6+
use Dingo\Api\Routing\UrlGenerator;
7+
use Dingo\Api\Tests\Stubs\RoutingAdapterStub;
8+
use Dingo\Api\Tests\Stubs\RoutingControllerStub;
9+
use Illuminate\Container\Container;
10+
11+
class UrlGeneratorTest extends Adapter\BaseAdapterTest
12+
{
13+
public function getAdapterInstance()
14+
{
15+
return $this->container->make(RoutingAdapterStub::class);
16+
}
17+
18+
public function getContainerInstance()
19+
{
20+
return new Container;
21+
}
22+
23+
public function testRouteGenerationSimple()
24+
{
25+
$this->router = new Router($this->adapter, $this->exception, $this->container, null, null);
26+
$this->router->version('v1', function (Router $router) {
27+
$router->any('foo', function () {
28+
})->name('my.route');
29+
});
30+
31+
$generator = new UrlGenerator($this->createRequest('/foo', 'GET'));
32+
$generator->setRouteCollections($this->router->getRoutes());
33+
34+
$test = $generator->version('v1')->route('my.route');
35+
$this->assertSame('http://localhost/foo', $test);
36+
}
37+
38+
public function testRouteGenerationWithActionAs()
39+
{
40+
$this->router = new Router($this->adapter, $this->exception, $this->container, null, null);
41+
$this->router->version('v1', function (Router $router) {
42+
$router->any('foo', [
43+
'as' => 'my.route',
44+
'uses' => RoutingControllerStub::class.'@index',
45+
]);
46+
});
47+
48+
$generator = new UrlGenerator($this->createRequest('/foo', 'GET'));
49+
$generator->setRouteCollections($this->router->getRoutes());
50+
51+
$test = $generator->version('v1')->route('my.route');
52+
$this->assertSame('http://localhost/foo', $test);
53+
}
54+
55+
public function testRouteGenerationWithDomain()
56+
{
57+
$this->router = new Router($this->adapter, $this->exception, $this->container, 'dingo.dev', null);
58+
$this->router->version('v1', function (Router $router) {
59+
$router->any('foo', function () {
60+
})->name('my.route');
61+
});
62+
63+
$generator = new UrlGenerator($this->createRequest('/foo', 'GET'));
64+
$generator->setRouteCollections($this->router->getRoutes());
65+
66+
$test = $generator->version('v1')->route('my.route');
67+
$this->assertSame('http://dingo.dev/foo', $test);
68+
}
69+
70+
public function testRouteGenerationWithPrefix()
71+
{
72+
$this->router = new Router($this->adapter, $this->exception, $this->container, 'dingo.dev', 'api');
73+
$this->router->version('v1', function (Router $router) {
74+
$router->any('foo', function () {
75+
})->name('my.route');
76+
});
77+
78+
$generator = new UrlGenerator($this->createRequest('/foo', 'GET'));
79+
$generator->setRouteCollections($this->router->getRoutes());
80+
81+
$test = $generator->version('v1')->route('my.route');
82+
$this->assertSame('http://dingo.dev/api/foo', $test);
83+
}
84+
85+
public function testRouteGenerationWithNamedParameters()
86+
{
87+
$this->router = new Router($this->adapter, $this->exception, $this->container, 'dingo.dev', 'api');
88+
$this->router->version('v1', function (Router $router) {
89+
$router->any('foo/{bar}', function () {
90+
})->name('my.route');
91+
});
92+
93+
$generator = new UrlGenerator($this->createRequest('/foo', 'GET'));
94+
$generator->setRouteCollections($this->router->getRoutes());
95+
96+
$test = $generator->version('v1')->route('my.route', ['bar' => 'xyz']);
97+
$this->assertSame('http://dingo.dev/api/foo/xyz', $test);
98+
}
99+
100+
public function testRouteGenerationWithIndexedParameters()
101+
{
102+
$this->router = new Router($this->adapter, $this->exception, $this->container, 'dingo.dev', 'api');
103+
$this->router->version('v1', function (Router $router) {
104+
$router->any('foo/{bar}', function () {
105+
})->name('my.route');
106+
});
107+
108+
$generator = new UrlGenerator($this->createRequest('/foo', 'GET'));
109+
$generator->setRouteCollections($this->router->getRoutes());
110+
111+
$test = $generator->version('v1')->route('my.route', ['xyz']);
112+
$this->assertSame('http://dingo.dev/api/foo/xyz', $test);
113+
}
114+
}

0 commit comments

Comments
 (0)