1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2016, Cake Development Corporation (http://cakedc.com) |
4
|
|
|
* |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright 2016, Cake Development Corporation (http://cakedc.com) |
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Test\App\Service; |
13
|
|
|
|
14
|
|
|
use Cake\Core\Configure; |
15
|
|
|
use CakeDC\Api\Service\ConfigReader; |
16
|
|
|
use CakeDC\Api\Service\FallbackService; |
17
|
|
|
use CakeDC\Api\Service\Service; |
18
|
|
|
use CakeDC\Api\Service\ServiceRegistry; |
19
|
|
|
use CakeDC\Api\Test\ConfigTrait; |
20
|
|
|
use CakeDC\Api\Test\FixturesTrait; |
21
|
|
|
use Cake\Controller\Controller; |
22
|
|
|
use CakeDC\Api\TestSuite\TestCase; |
23
|
|
|
|
24
|
|
|
class ServiceTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
use ConfigTrait; |
28
|
|
|
use FixturesTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* setUp method |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function setUp() |
36
|
|
|
{ |
37
|
|
|
parent::setUp(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* tearDown method |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function tearDown() |
46
|
|
|
{ |
47
|
|
|
ServiceRegistry::clear(); |
48
|
|
|
parent::tearDown(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Test construct |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
* expectedException \CakeDC\Api\Service\Exception\MissingAdapterException |
56
|
|
|
*/ |
57
|
|
|
public function testConstructWithoutAdapter() |
58
|
|
|
{ |
59
|
|
|
$this->_initializeRequest(); |
60
|
|
|
$this->Service = new FallbackService([ |
|
|
|
|
61
|
|
|
'service' => 'authors', |
62
|
|
|
'request' => $this->request, |
|
|
|
|
63
|
|
|
'response' => $this->response, |
|
|
|
|
64
|
|
|
'baseUrl' => '/authors' |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Test construct |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
public function testConstructWithRendererAsParameter() |
74
|
|
|
{ |
75
|
|
|
$this->_initializeRequest(); |
76
|
|
|
$this->Service = new FallbackService([ |
77
|
|
|
'service' => 'authors', |
78
|
|
|
'request' => $this->request, |
79
|
|
|
'response' => $this->response, |
80
|
|
|
'baseUrl' => '/authors', |
81
|
|
|
'rendererClass' => 'CakeDC/Api.Raw' |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Test load value method |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
* @expectedException \Cake\Routing\Exception\MissingRouteException |
91
|
|
|
*/ |
92
|
|
|
public function testActionNotFound() |
93
|
|
|
{ |
94
|
|
|
$this->_initializeRequest([ |
95
|
|
|
'params' => [ |
96
|
|
|
'service' => 'authors', |
97
|
|
|
] |
98
|
|
|
], 'DELETE'); |
99
|
|
|
$service = $this->request['service']; |
100
|
|
|
$options = [ |
101
|
|
|
'version' => null, |
102
|
|
|
'service' => $service, |
103
|
|
|
'request' => $this->request, |
104
|
|
|
'response' => $this->response, |
105
|
|
|
'baseUrl' => '/authors' |
106
|
|
|
]; |
107
|
|
|
$Service = ServiceRegistry::get($service, $options); |
108
|
|
|
$this->assertTrue($Service instanceof Service); |
109
|
|
|
$this->assertEquals('authors', $Service->name()); |
110
|
|
|
|
111
|
|
|
$this->assertTextEquals('/authors', $Service->baseUrl()); |
112
|
|
|
$action = $Service->buildAction(); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Test load value method |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
public function testActionInitialize() |
121
|
|
|
{ |
122
|
|
|
$this->_initializeRequest([ |
123
|
|
|
'params' => [ |
124
|
|
|
'service' => 'authors', |
125
|
|
|
] |
126
|
|
|
]); |
127
|
|
|
$service = $this->request['service']; |
128
|
|
|
$options = [ |
129
|
|
|
'version' => null, |
130
|
|
|
'service' => $service, |
131
|
|
|
'request' => $this->request, |
132
|
|
|
'response' => $this->response, |
133
|
|
|
'baseUrl' => '/authors' |
134
|
|
|
]; |
135
|
|
|
$Service = ServiceRegistry::get($service, $options); |
136
|
|
|
$this->assertTrue($Service instanceof Service); |
137
|
|
|
$this->assertEquals('authors', $Service->name()); |
138
|
|
|
|
139
|
|
|
$this->assertTextEquals('/authors', $Service->baseUrl()); |
140
|
|
|
$action = $Service->buildAction(); |
141
|
|
|
$this->assertEquals('authors', $action->service()->name()); |
142
|
|
|
$this->assertEquals('authors', $action->table()->table()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Test nested action generation. |
147
|
|
|
* |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
|
public function testNestedActionInitialize() |
151
|
|
|
{ |
152
|
|
|
$this->_initializeRequest([ |
153
|
|
|
'params' => [ |
154
|
|
|
'service' => 'authors', |
155
|
|
|
'pass' => [ |
156
|
|
|
'1', |
157
|
|
|
'articles' |
158
|
|
|
] |
159
|
|
|
] |
160
|
|
|
]); |
161
|
|
|
$service = $this->request['service']; |
162
|
|
|
$options = [ |
163
|
|
|
'version' => null, |
164
|
|
|
'service' => $service, |
165
|
|
|
'request' => $this->request, |
166
|
|
|
'response' => $this->response, |
167
|
|
|
'baseUrl' => '/authors/1/articles', |
168
|
|
|
]; |
169
|
|
|
$Service = ServiceRegistry::get($service, $options); |
170
|
|
|
$this->assertTrue($Service instanceof Service); |
171
|
|
|
$this->assertTextEquals('/authors/1/articles', $Service->baseUrl()); |
172
|
|
|
$action = $Service->buildAction(); |
173
|
|
|
$this->assertEquals('1', $action->parentId()); |
174
|
|
|
$this->assertEquals('author_id', $action->parentIdName()); |
175
|
|
|
$this->assertEquals('articles', $action->service()->name()); |
176
|
|
|
$this->assertEquals('authors', $action->service()->parent()->name()); |
177
|
|
|
$this->assertEquals('articles', $action->table()->table()); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Test nested action generation. |
182
|
|
|
* |
183
|
|
|
* @return void |
184
|
|
|
*/ |
185
|
|
|
public function testInitializeActionStoredAsExistsClass() |
186
|
|
|
{ |
187
|
|
|
$this->_initializeRequest([ |
188
|
|
|
'params' => [ |
189
|
|
|
'service' => 'articles', |
190
|
|
|
'pass' => [ |
191
|
|
|
'tag', |
192
|
|
|
'1' |
193
|
|
|
], |
194
|
|
|
'post' => [ |
195
|
|
|
'tag_id' => 1 |
196
|
|
|
] |
197
|
|
|
] |
198
|
|
|
], 'PUT'); |
199
|
|
|
$service = $this->request['service']; |
200
|
|
|
$options = [ |
201
|
|
|
'version' => null, |
202
|
|
|
'service' => $service, |
203
|
|
|
'request' => $this->request, |
204
|
|
|
'response' => $this->response, |
205
|
|
|
'baseUrl' => '/articles/tag/1', |
206
|
|
|
]; |
207
|
|
|
$Service = ServiceRegistry::get($service, $options); |
208
|
|
|
$this->assertTrue($Service instanceof Service); |
209
|
|
|
$this->assertTextEquals('/articles/tag/1', $Service->baseUrl()); |
210
|
|
|
$action = $Service->buildAction(); |
211
|
|
|
$this->assertEquals('CakeDC\Api\Test\App\Service\Action\ArticlesTagAction', get_class($action)); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Test nested action generation. |
216
|
|
|
* |
217
|
|
|
* @return void |
218
|
|
|
*/ |
219
|
|
|
public function testInitializeActionByServiceConfigMap() |
220
|
|
|
{ |
221
|
|
|
$actionClass = 'CakeDC\Api\Test\App\Service\Action\Author\IndexAction'; |
222
|
|
|
$this->_addSettingByPath('Service.authors.options', [ |
223
|
|
|
'classMap' => [ |
224
|
|
|
'index' => $actionClass |
225
|
|
|
] |
226
|
|
|
]); |
227
|
|
|
$config = require(CONFIG . 'api.php'); |
228
|
|
|
Configure::write($config); |
229
|
|
|
$this->_initializeRequest([ |
230
|
|
|
'params' => [ |
231
|
|
|
'service' => 'authors', |
232
|
|
|
'pass' => [], |
233
|
|
|
] |
234
|
|
|
], 'GET'); |
235
|
|
|
$service = $this->request['service']; |
236
|
|
|
$version = null; |
237
|
|
|
$options = [ |
238
|
|
|
'version' => $version, |
239
|
|
|
'service' => $service, |
240
|
|
|
'request' => $this->request, |
241
|
|
|
'response' => $this->response, |
242
|
|
|
'baseUrl' => '/authors' |
243
|
|
|
]; |
244
|
|
|
$options += (new ConfigReader())->serviceOptions($service, $version); |
245
|
|
|
$Service = ServiceRegistry::get($service, $options); |
246
|
|
|
$this->assertTrue($Service instanceof Service); |
247
|
|
|
$this->assertTextEquals('/authors', $Service->baseUrl()); |
248
|
|
|
$action = $Service->buildAction(); |
249
|
|
|
$this->assertEquals($actionClass, get_class($action)); |
250
|
|
|
$this->assertTextEquals('custom action applied', $action->process()); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: