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