1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
namespace Aimeos\Client\JsonApi\Basket\Service; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class StandardTest extends \PHPUnit\Framework\TestCase |
13
|
|
|
{ |
14
|
|
|
private $context; |
15
|
|
|
private $object; |
16
|
|
|
private $view; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
protected function setUp() |
20
|
|
|
{ |
21
|
|
|
$this->context = \TestHelperJapi::getContext(); |
22
|
|
|
$templatePaths = \TestHelperJapi::getTemplatePaths(); |
23
|
|
|
$this->view = $this->context->getView(); |
24
|
|
|
|
25
|
|
|
$this->object = new \Aimeos\Client\JsonApi\Basket\Service\Standard( $this->context, $this->view, $templatePaths, 'basket/service' ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
protected function tearDown() |
30
|
|
|
{ |
31
|
|
|
\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', null ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
public function testDelete() |
36
|
|
|
{ |
37
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'service' ); |
38
|
|
|
$servId = $manager->findItem( 'unitcode', [], 'service', 'delivery' )->getId(); |
39
|
|
|
|
40
|
|
|
$body = '{"data": {"type": "basket/service", "id": "delivery", "attributes": {"service.id": ' . $servId . '}}}'; |
41
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
42
|
|
|
|
43
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
44
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
45
|
|
|
|
46
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/service']['data'] ) ); |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
$body = '{"data": {"type": "basket/service", "id": "delivery"}}'; |
50
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
51
|
|
|
|
52
|
|
|
$response = $this->object->delete( $request, $this->view->response() ); |
53
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
54
|
|
|
|
55
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
56
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
57
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
58
|
|
|
|
59
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
60
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
61
|
|
|
$this->assertArrayNotHasKey( 'basket/service', $result['data']['relationships'] ); |
62
|
|
|
|
63
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
public function testDeleteById() |
68
|
|
|
{ |
69
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'service' ); |
70
|
|
|
$servId = $manager->findItem( 'unitcode', [], 'service', 'delivery' )->getId(); |
71
|
|
|
|
72
|
|
|
$body = '{"data": {"type": "basket/service", "id": "delivery", "attributes": {"service.id": ' . $servId . '}}}'; |
73
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
74
|
|
|
|
75
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
76
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
77
|
|
|
|
78
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/service']['data'] ) ); |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
$params = array( 'id' => 'default', 'relatedid' => 'delivery' ); |
82
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
83
|
|
|
$this->view->addHelper( 'param', $helper ); |
84
|
|
|
|
85
|
|
|
$response = $this->object->delete( $request, $this->view->response() ); |
86
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
87
|
|
|
|
88
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
89
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
90
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
91
|
|
|
|
92
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
93
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
94
|
|
|
$this->assertArrayNotHasKey( 'basket/service', $result['data']['relationships'] ); |
95
|
|
|
|
96
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
public function testDeleteMShopException() |
101
|
|
|
{ |
102
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
103
|
|
|
|
104
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
105
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
109
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
public function testDeleteException() |
114
|
|
|
{ |
115
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) ); |
116
|
|
|
|
117
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
118
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
122
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
public function testPost() |
127
|
|
|
{ |
128
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'service' ); |
129
|
|
|
$servId = $manager->findItem( 'unitcode', [], 'service', 'delivery' )->getId(); |
130
|
|
|
|
131
|
|
|
$body = '{"data": {"type": "basket/service", "id": "delivery", "attributes": {"service.id": ' . $servId . '}}}'; |
132
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
133
|
|
|
|
134
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
135
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
136
|
|
|
|
137
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
138
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
139
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
140
|
|
|
|
141
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
142
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
143
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/service']['data'] ) ); |
144
|
|
|
$this->assertEquals( 'unitcode', $result['included'][0]['attributes']['order.base.service.code'] ); |
145
|
|
|
|
146
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
public function testPostMultiple() |
151
|
|
|
{ |
152
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'service' ); |
153
|
|
|
$servId = $manager->findItem( 'unitcode', [], 'service', 'delivery' )->getId(); |
154
|
|
|
$servId2 = $manager->findItem( 'unitpaymentcode', [], 'service', 'payment' )->getId(); |
155
|
|
|
|
156
|
|
|
$body = '{"data": [{ |
157
|
|
|
"type": "basket/service", "id": "delivery", "attributes": {"service.id": ' . $servId . '} |
158
|
|
|
}, { |
159
|
|
|
"type": "basket/service", "id": "payment", "attributes": {"service.id": ' . $servId2 . '} |
160
|
|
|
}]}'; |
161
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
162
|
|
|
|
163
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
164
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
168
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
169
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
170
|
|
|
|
171
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
172
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
173
|
|
|
$this->assertEquals( 2, count( $result['data']['relationships']['basket/service']['data'] ) ); |
174
|
|
|
$this->assertEquals( 'unitcode', $result['included'][0]['attributes']['order.base.service.code'] ); |
175
|
|
|
$this->assertEquals( 'unitpaymentcode', $result['included'][1]['attributes']['order.base.service.code'] ); |
176
|
|
|
|
177
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
public function testPostMShopException() |
182
|
|
|
{ |
183
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
184
|
|
|
|
185
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
186
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
190
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
public function testPostException() |
195
|
|
|
{ |
196
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) ); |
197
|
|
|
|
198
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
199
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
203
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
public function testOptions() |
208
|
|
|
{ |
209
|
|
|
$response = $this->object->options( $this->view->request(), $this->view->response() ); |
210
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
211
|
|
|
|
212
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
213
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
214
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
215
|
|
|
|
216
|
|
|
$this->assertEquals( null, $result['meta']['prefix'] ); |
217
|
|
|
$this->assertEquals( 1, count( $result['meta']['attributes'] ) ); |
218
|
|
|
$this->assertArrayNotHasKey( 'filter', $result['meta'] ); |
219
|
|
|
$this->assertArrayNotHasKey( 'sort', $result['meta'] ); |
220
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Returns a test object with a mocked basket controller |
226
|
|
|
* |
227
|
|
|
* @param string $method Basket controller method name to mock |
228
|
|
|
* @param mixed $result Return value of the mocked method |
229
|
|
|
*/ |
230
|
|
|
protected function getObject( $method, $result ) |
231
|
|
|
{ |
232
|
|
|
$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' ) |
233
|
|
|
->setConstructorArgs( [$this->context] ) |
234
|
|
|
->setMethods( [$method] ) |
235
|
|
|
->getMock(); |
236
|
|
|
|
237
|
|
|
$cntl->expects( $this->once() )->method( $method )->will( $result ); |
238
|
|
|
|
239
|
|
|
\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', $cntl ); |
240
|
|
|
|
241
|
|
|
$templatePaths = \TestHelperJapi::getTemplatePaths(); |
242
|
|
|
$object = new \Aimeos\Client\JsonApi\Basket\Service\Standard( $this->context, $this->view, $templatePaths, 'basket/service' ); |
243
|
|
|
|
244
|
|
|
return $object; |
245
|
|
|
} |
246
|
|
|
} |