Skip to content

Commit e376880

Browse files
committed
Working on more test coverage for routing adapters.
Signed-off-by: Jason Lewis <[email protected]>
1 parent 35718bf commit e376880

File tree

3 files changed

+229
-176
lines changed

3 files changed

+229
-176
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
<?php
2+
3+
namespace Dingo\Api\Tests\Routing\Adapter;
4+
5+
use Mockery as m;
6+
use Dingo\Api\Http;
7+
use Dingo\Api\Routing\Router;
8+
use PHPUnit_Framework_TestCase;
9+
use Illuminate\Events\Dispatcher;
10+
use Illuminate\Container\Container;
11+
use Dingo\Api\Routing\Adapter\Laravel;
12+
use Dingo\Api\Tests\Stubs\MiddlewareStub;
13+
use Illuminate\Routing\Router as IlluminateRouter;
14+
15+
abstract class BaseAdapterTest extends PHPUnit_Framework_TestCase
16+
{
17+
public function setUp()
18+
{
19+
$this->container = new Container;
20+
$this->container['api.auth'] = new MiddlewareStub;
21+
$this->container['api.limiting'] = new MiddlewareStub;
22+
23+
$class = $this->getAdapterClassName();
24+
25+
$this->adapter = new $class(new IlluminateRouter(new Dispatcher($this->container), $this->container));
26+
$this->exception = m::mock('Dingo\Api\Exception\Handler');
27+
$this->router = new Router($this->adapter, new Http\Parser\Accept('api', 'v1', 'json'), $this->exception, $this->container);
28+
29+
Http\Response::setFormatters(['json' => new Http\Response\Format\Json]);
30+
}
31+
32+
public function tearDown()
33+
{
34+
m::close();
35+
}
36+
37+
abstract public function getAdapterClassName();
38+
39+
protected function createRequest($uri, $method, array $headers = [])
40+
{
41+
$request = Http\Request::create($uri, $method);
42+
43+
foreach ($headers as $key => $value) {
44+
$request->headers->set($key, $value);
45+
}
46+
47+
return $this->container['request'] = $request;
48+
}
49+
50+
public function testBasicRouteVersions()
51+
{
52+
$this->router->version('v1', function () {
53+
$this->router->get('foo', function () {
54+
return 'foo';
55+
});
56+
$this->router->post('foo', function () {
57+
return 'posted';
58+
});
59+
$this->router->patch('foo', function () {
60+
return 'patched';
61+
});
62+
$this->router->delete('foo', function () {
63+
return 'deleted';
64+
});
65+
$this->router->put('foo', function () {
66+
return 'put';
67+
});
68+
$this->router->options('foo', function () {
69+
return 'options';
70+
});
71+
});
72+
73+
$this->router->group(['version' => 'v2'], function () {
74+
$this->router->get('foo', ['version' => 'v3', function () {
75+
return 'bar';
76+
}]);
77+
});
78+
79+
$this->assertArrayHasKey('v1', $this->router->getRoutes(), 'No routes were registered for version 1.');
80+
$this->assertArrayHasKey('v2', $this->router->getRoutes(), 'No routes were registered for version 2.');
81+
$this->assertArrayHasKey('v3', $this->router->getRoutes(), 'No routes were registered for version 3.');
82+
83+
$request = $this->createRequest('foo', 'GET', ['accept' => 'application/vnd.api.v1+json']);
84+
$this->assertEquals('foo', $this->router->dispatch($request)->getContent());
85+
86+
$request = $this->createRequest('foo', 'GET', ['accept' => 'application/vnd.api.v2+json']);
87+
$this->assertEquals('bar', $this->router->dispatch($request)->getContent());
88+
89+
$request = $this->createRequest('foo', 'GET', ['accept' => 'application/vnd.api.v3+json']);
90+
$this->assertEquals('bar', $this->router->dispatch($request)->getContent());
91+
92+
$request = $this->createRequest('foo', 'POST', ['accept' => 'application/vnd.api.v1+json']);
93+
$this->assertEquals('posted', $this->router->dispatch($request)->getContent());
94+
95+
$request = $this->createRequest('foo', 'PATCH', ['accept' => 'application/vnd.api.v1+json']);
96+
$this->assertEquals('patched', $this->router->dispatch($request)->getContent());
97+
98+
$request = $this->createRequest('foo', 'DELETE', ['accept' => 'application/vnd.api.v1+json']);
99+
$this->assertEquals('deleted', $this->router->dispatch($request)->getContent());
100+
101+
$request = $this->createRequest('foo', 'PUT', ['accept' => 'application/vnd.api.v1+json']);
102+
$this->assertEquals('put', $this->router->dispatch($request)->getContent());
103+
104+
$request = $this->createRequest('foo', 'options', ['accept' => 'application/vnd.api.v1+json']);
105+
$this->assertEquals('options', $this->router->dispatch($request)->getContent());
106+
}
107+
108+
public function testAdapterDispatchesRequestsThroughRouter()
109+
{
110+
$this->container['request'] = Http\Request::create('foo', 'GET');
111+
112+
$this->router->version('v1', function () {
113+
$this->router->get('foo', function () {
114+
return 'foo';
115+
});
116+
});
117+
118+
$response = $this->router->dispatch($this->container['request']);
119+
120+
$this->assertEquals('foo', $response->getContent());
121+
}
122+
123+
public function testRoutesWithPrefix()
124+
{
125+
$this->router->version('v1', ['prefix' => 'foo/bar'], function () {
126+
$this->router->get('foo', function () {
127+
return 'foo';
128+
});
129+
});
130+
131+
$this->router->version('v2', ['prefix' => 'foo/bar'], function () {
132+
$this->router->get('foo', function () {
133+
return 'bar';
134+
});
135+
});
136+
137+
$request = $this->createRequest('foo/bar/foo', 'GET', ['accept' => 'application/vnd.api.v2+json']);
138+
$this->assertEquals('bar', $this->router->dispatch($request)->getContent(), 'Router could not dispatch prefixed routes.');
139+
}
140+
141+
public function testRoutesWithDomains()
142+
{
143+
$this->router->version('v1', ['domain' => 'foo.bar'], function () {
144+
$this->router->get('foo', function () {
145+
return 'foo';
146+
});
147+
});
148+
149+
$this->router->version('v2', ['domain' => 'foo.bar'], function () {
150+
$this->router->get('foo', function () {
151+
return 'bar';
152+
});
153+
});
154+
155+
$request = $this->createRequest('http://foo.bar/foo', 'GET', ['accept' => 'application/vnd.api.v2+json']);
156+
$this->assertEquals('bar', $this->router->dispatch($request)->getContent(), 'Router could not dispatch domain routes.');
157+
}
158+
159+
public function testPointReleaseVersions()
160+
{
161+
$this->router->version('v1.1', function () {
162+
$this->router->get('foo', function () {
163+
return 'foo';
164+
});
165+
});
166+
167+
$this->router->version('v2.0.1', function () {
168+
$this->router->get('bar', function () {
169+
return 'bar';
170+
});
171+
});
172+
173+
$request = $this->createRequest('foo', 'GET', ['accept' => 'application/vnd.api.v1.1+json']);
174+
$this->assertEquals('foo', $this->router->dispatch($request)->getContent(), 'Router does not support point release versions.');
175+
176+
$request = $this->createRequest('bar', 'GET', ['accept' => 'application/vnd.api.v2.0.1+json']);
177+
$this->assertEquals('bar', $this->router->dispatch($request)->getContent(), 'Router does not support point release versions.');
178+
}
179+
180+
public function testRoutingControllers()
181+
{
182+
$this->router->version('v1', ['namespace' => 'Dingo\Api\Tests\Stubs'], function () {
183+
$this->router->controllers([
184+
'bar' => 'RoutingControllerStub'
185+
]);
186+
});
187+
188+
$request = $this->createRequest('bar/index', 'GET', ['accept' => 'application/vnd.api.v1+json']);
189+
190+
$this->assertEquals('foo', $this->router->dispatch($request)->getContent(), 'Router did not register controller correctly.');
191+
192+
$this->router->version('v2', function () {
193+
$this->router->controllers([
194+
'bar' => 'Dingo\Api\Tests\Stubs\RoutingControllerStub'
195+
]);
196+
});
197+
198+
$request = $this->createRequest('bar/index', 'GET', ['accept' => 'application/vnd.api.v1+json']);
199+
200+
$this->assertEquals('foo', $this->router->dispatch($request)->getContent(), 'Router did not register controller correctly.');
201+
}
202+
203+
public function testRoutingResources()
204+
{
205+
$this->router->version('v1', ['namespace' => 'Dingo\Api\Tests\Stubs'], function () {
206+
$this->router->resources([
207+
'bar' => ['RoutingControllerStub', ['only' => ['index']]]
208+
]);
209+
});
210+
211+
$request = $this->createRequest('bar', 'GET', ['accept' => 'application/vnd.api.v1+json']);
212+
213+
$this->assertEquals('foo', $this->router->dispatch($request)->getContent(), 'Router did not register controller correctly.');
214+
}
215+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Dingo\Api\Tests\Routing\Adapter;
4+
5+
class LaravelTest extends BaseAdapterTest
6+
{
7+
public function getAdapterClassName()
8+
{
9+
return 'Dingo\Api\Routing\Adapter\Laravel';
10+
}
11+
}

0 commit comments

Comments
 (0)