Completed
Push — master ( d89dee...9ec67f )
by Aimeos
02:26
created

StandardTest::testPostControllerException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 14
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
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\Customer\Address;
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\Customer\Address\Standard( $this->context, $this->view, $templatePaths, 'customer/address' );
26
	}
27
28
29
	protected function tearDown()
30
	{
31
		\Aimeos\Controller\Frontend\Customer\Factory::injectController( '\Aimeos\Controller\Frontend\Customer\Standard', null );
32
	}
33
34
35
	public function testDelete()
36
	{
37
		$custManager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' );
38
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer/address' );
39
40
		$userId = $custManager->findItem( 'UTC001' )->getId();
41
		$this->context->setUserId( $userId );
42
		$item = $manager->createItem()->setParentId( $userId );
43
		$manager->saveItem( $item );
44
45
46
		$params = array( 'id' => $userId );
47
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
48
		$this->view->addHelper( 'param', $helper );
49
50
		$body = '{"data": {"type": "customer/address", "id": ' . $item->getId() . '}}';
51
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
52
53
54
		$response = $this->object->delete( $request, $this->view->response() );
55
		$result = json_decode( (string) $response->getBody(), true );
56
57
		$this->assertEquals( 200, $response->getStatusCode() );
58
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
59
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
60
61
		$this->assertEquals( 0, $result['meta']['total'] );
62
		$this->assertArrayNotHasKey( 'errors', $result );
63
	}
64
65
66
	public function testDeleteById()
67
	{
68
		$custManager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' );
69
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer/address' );
70
71
		$userId = $custManager->findItem( 'UTC001' )->getId();
72
		$this->context->setUserId( $userId );
73
		$item = $manager->createItem()->setParentId( $userId );
74
		$manager->saveItem( $item );
75
76
77
		$params = array( 'id' => $userId, 'relatedid' => $item->getId() );
78
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
79
		$this->view->addHelper( 'param', $helper );
80
81
82
		$response = $this->object->delete( $this->view->request(), $this->view->response() );
83
		$result = json_decode( (string) $response->getBody(), true );
84
85
		$this->assertEquals( 200, $response->getStatusCode() );
86
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
87
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
88
89
		$this->assertEquals( 0, $result['meta']['total'] );
90
		$this->assertArrayNotHasKey( 'errors', $result );
91
	}
92
93
94
	public function testDeleteControllerException()
95
	{
96
		$object = $this->getObject( 'deleteAddressItem', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
97
98
		$params = array( 'relatedid' => -1 );
99
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
100
		$this->view->addHelper( 'param', $helper );
101
102
		$response = $object->delete( $this->view->request(), $this->view->response() );
103
		$result = json_decode( (string) $response->getBody(), true );
104
105
106
		$this->assertEquals( 403, $response->getStatusCode() );
107
		$this->assertArrayHasKey( 'errors', $result );
108
	}
109
110
111
	public function testDeleteMShopException()
112
	{
113
		$object = $this->getObject( 'deleteAddressItem', $this->throwException( new \Aimeos\MShop\Exception() ) );
114
115
		$params = array( 'relatedid' => -1 );
116
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
117
		$this->view->addHelper( 'param', $helper );
118
119
		$response = $object->delete( $this->view->request(), $this->view->response() );
120
		$result = json_decode( (string) $response->getBody(), true );
121
122
123
		$this->assertEquals( 404, $response->getStatusCode() );
124
		$this->assertArrayHasKey( 'errors', $result );
125
	}
126
127
128
	public function testDeleteException()
129
	{
130
		$object = $this->getObject( 'deleteAddressItem', $this->throwException( new \Exception() ) );
131
132
		$params = array( 'relatedid' => -1 );
133
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
134
		$this->view->addHelper( 'param', $helper );
135
136
		$response = $object->delete( $this->view->request(), $this->view->response() );
137
		$result = json_decode( (string) $response->getBody(), true );
138
139
140
		$this->assertEquals( 500, $response->getStatusCode() );
141
		$this->assertArrayHasKey( 'errors', $result );
142
	}
143
144
145
	public function testGet()
146
	{
147
		$user = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' )->findItem( 'UTC001' );
1 ignored issue
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MShop\Common\Manager\Iface as the method findItem() does only exist in the following implementations of said interface: Aimeos\MShop\Attribute\Manager\Lists\Type\Standard, Aimeos\MShop\Attribute\Manager\Standard, Aimeos\MShop\Attribute\Manager\Type\Standard, Aimeos\MShop\Catalog\Manager\Decorator\Base, Aimeos\MShop\Catalog\Manager\Decorator\Sitecheck, Aimeos\MShop\Catalog\Manager\Lists\Type\Standard, Aimeos\MShop\Catalog\Manager\Standard, Aimeos\MShop\Common\Manager\Decorator\Base, Aimeos\MShop\Common\Manager\Decorator\Changelog, Aimeos\MShop\Common\Manager\Decorator\Sitecheck, Aimeos\MShop\Common\Manager\Type\Base, Aimeos\MShop\Coupon\Manager\Code\Standard, Aimeos\MShop\Customer\Manager\Base, Aimeos\MShop\Customer\Manager\Group\Standard, Aimeos\MShop\Customer\Manager\Lists\Type\Standard, Aimeos\MShop\Customer\Manager\Standard, Aimeos\MShop\Locale\Manager\Site\Standard, Aimeos\MShop\Media\Manager\Lists\Type\Standard, Aimeos\MShop\Media\Manager\Type\Standard, Aimeos\MShop\Plugin\Manager\Type\Standard, Aimeos\MShop\Price\Manager\Lists\Type\Standard, Aimeos\MShop\Price\Manager\Type\Standard, Aimeos\MShop\Product\Manager\Lists\Type\Standard, Aimeos\MShop\Product\Man...\Property\Type\Standard, Aimeos\MShop\Product\Manager\Standard, Aimeos\MShop\Product\Manager\Type\Standard, Aimeos\MShop\Service\Manager\Lists\Type\Standard, Aimeos\MShop\Service\Manager\Standard, Aimeos\MShop\Service\Manager\Type\Standard, Aimeos\MShop\Stock\Manager\Standard, Aimeos\MShop\Stock\Manager\Type\Standard, Aimeos\MShop\Supplier\Manager\Lists\Type\Standard, Aimeos\MShop\Supplier\Manager\Standard, Aimeos\MShop\Tag\Manager\Type\Standard, Aimeos\MShop\Text\Manager\Lists\Type\Standard, Aimeos\MShop\Text\Manager\Type\Standard.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
148
		$this->context->setUserId( $user->getId() );
149
150
		$params = array( 'id' => $user->getId() );
151
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
152
		$this->view->addHelper( 'param', $helper );
153
154
155
		$response = $this->object->get( $this->view->request(), $this->view->response() );
156
		$result = json_decode( (string) $response->getBody(), true );
157
158
		$this->assertEquals( 200, $response->getStatusCode() );
159
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
160
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
161
162
		$this->assertEquals( 1, $result['meta']['total'] );
163
		$this->assertEquals( 'customer/address', $result['data'][0]['type'] );
164
		$this->assertNotNull( $result['data'][0]['id'] );
165
		$this->assertGreaterThan( 27, count( $result['data'][0]['attributes'] ) );
166
167
		$this->assertArrayNotHasKey( 'errors', $result );
168
	}
169
170
171
	public function testGetById()
172
	{
173
		$user = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' )->findItem( 'UTC001', ['customer/address'] );
1 ignored issue
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MShop\Common\Manager\Iface as the method findItem() does only exist in the following implementations of said interface: Aimeos\MShop\Attribute\Manager\Lists\Type\Standard, Aimeos\MShop\Attribute\Manager\Standard, Aimeos\MShop\Attribute\Manager\Type\Standard, Aimeos\MShop\Catalog\Manager\Decorator\Base, Aimeos\MShop\Catalog\Manager\Decorator\Sitecheck, Aimeos\MShop\Catalog\Manager\Lists\Type\Standard, Aimeos\MShop\Catalog\Manager\Standard, Aimeos\MShop\Common\Manager\Decorator\Base, Aimeos\MShop\Common\Manager\Decorator\Changelog, Aimeos\MShop\Common\Manager\Decorator\Sitecheck, Aimeos\MShop\Common\Manager\Type\Base, Aimeos\MShop\Coupon\Manager\Code\Standard, Aimeos\MShop\Customer\Manager\Base, Aimeos\MShop\Customer\Manager\Group\Standard, Aimeos\MShop\Customer\Manager\Lists\Type\Standard, Aimeos\MShop\Customer\Manager\Standard, Aimeos\MShop\Locale\Manager\Site\Standard, Aimeos\MShop\Media\Manager\Lists\Type\Standard, Aimeos\MShop\Media\Manager\Type\Standard, Aimeos\MShop\Plugin\Manager\Type\Standard, Aimeos\MShop\Price\Manager\Lists\Type\Standard, Aimeos\MShop\Price\Manager\Type\Standard, Aimeos\MShop\Product\Manager\Lists\Type\Standard, Aimeos\MShop\Product\Man...\Property\Type\Standard, Aimeos\MShop\Product\Manager\Standard, Aimeos\MShop\Product\Manager\Type\Standard, Aimeos\MShop\Service\Manager\Lists\Type\Standard, Aimeos\MShop\Service\Manager\Standard, Aimeos\MShop\Service\Manager\Type\Standard, Aimeos\MShop\Stock\Manager\Standard, Aimeos\MShop\Stock\Manager\Type\Standard, Aimeos\MShop\Supplier\Manager\Lists\Type\Standard, Aimeos\MShop\Supplier\Manager\Standard, Aimeos\MShop\Tag\Manager\Type\Standard, Aimeos\MShop\Text\Manager\Lists\Type\Standard, Aimeos\MShop\Text\Manager\Type\Standard.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
174
		$this->context->setUserId( $user->getId() );
175
		$addrItems = $user->getAddressItems();
176
177
		if( ( $addrItem = reset( $addrItems ) ) === false ) {
178
			throw new \RuntimeException( 'No address item found for "UTC001"' );
179
		}
180
181
		$params = array(
182
			'id' => $user->getId(),
183
			'related' => 'address',
184
			'relatedid' => $addrItem->getId(),
185
			'fields' => array( 'customer/address' => 'customer.address.id,customer.address.firstname' ),
186
		);
187
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
188
		$this->view->addHelper( 'param', $helper );
189
190
191
		$response = $this->object->get( $this->view->request(), $this->view->response() );
192
		$result = json_decode( (string) $response->getBody(), true );
193
194
		$this->assertEquals( 200, $response->getStatusCode() );
195
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
196
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
197
198
		$this->assertEquals( 1, $result['meta']['total'] );
199
		$this->assertEquals( 'customer/address', $result['data']['type'] );
200
		$this->assertEquals( 2, count( $result['data']['attributes'] ) );
201
		$this->assertNotNull( $result['data']['id'] );
202
203
		$this->assertArrayNotHasKey( 'errors', $result );
204
	}
205
206
207
	public function testGetControllerException()
208
	{
209
		$object = $this->getObject( 'getAddressItem', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
210
211
		$params = array( 'relatedid' => -1 );
212
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
213
		$this->view->addHelper( 'param', $helper );
214
215
		$response = $object->get( $this->view->request(), $this->view->response() );
216
		$result = json_decode( (string) $response->getBody(), true );
217
218
219
		$this->assertEquals( 403, $response->getStatusCode() );
220
		$this->assertArrayHasKey( 'errors', $result );
221
	}
222
223
224
	public function testGetMShopException()
225
	{
226
		$object = $this->getObject( 'getAddressItem', $this->throwException( new \Aimeos\MShop\Exception() ) );
227
228
		$params = array( 'relatedid' => -1 );
229
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
230
		$this->view->addHelper( 'param', $helper );
231
232
		$response = $object->get( $this->view->request(), $this->view->response() );
233
		$result = json_decode( (string) $response->getBody(), true );
234
235
236
		$this->assertEquals( 404, $response->getStatusCode() );
237
		$this->assertArrayHasKey( 'errors', $result );
238
	}
239
240
241
	public function testGetException()
242
	{
243
		$object = $this->getObject( 'getAddressItem', $this->throwException( new \Exception() ) );
244
245
		$params = array( 'relatedid' => -1 );
246
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
247
		$this->view->addHelper( 'param', $helper );
248
249
		$response = $object->get( $this->view->request(), $this->view->response() );
250
		$result = json_decode( (string) $response->getBody(), true );
251
252
253
		$this->assertEquals( 500, $response->getStatusCode() );
254
		$this->assertArrayHasKey( 'errors', $result );
255
	}
256
257
258
	public function testPatch()
259
	{
260
		$custManager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' );
261
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer/address' );
262
263
		$customerId = $custManager->findItem( 'UTC001' )->getId();
264
		$item = $manager->createItem()->setParentId( $customerId );
265
		$manager->saveItem( $item );
266
267
		$this->context->setUserId( $customerId );
268
269
		$params = array( 'id' => $item->getId(), 'relatedid' => $item->getId() );
270
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
271
		$this->view->addHelper( 'param', $helper );
272
273
		$body = '{"data": {"attributes": {"customer.address.lastname": "test"}}}	';
274
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
275
276
		$response = $this->object->patch( $request, $this->view->response() );
277
		$result = json_decode( (string) $response->getBody(), true );
278
279
		$manager->deleteItem( $item->getId() );
280
281
282
		$this->assertEquals( 200, $response->getStatusCode() );
283
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
284
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
285
286
		$this->assertEquals( 1, $result['meta']['total'] );
287
		$this->assertEquals( 'customer/address', $result['data']['type'] );
288
		$this->assertGreaterThan( 27, count( $result['data']['attributes'] ) );
289
		$this->assertEquals( 'test', $result['data']['attributes']['customer.address.lastname'] );
290
291
		$this->assertArrayNotHasKey( 'errors', $result );
292
	}
293
294
295
	public function testPatchControllerException()
296
	{
297
		$object = $this->getObject( 'editAddressItem', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
298
299
		$body = '{"data": {"attributes": []}}';
300
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
301
302
		$response = $object->patch( $request, $this->view->response() );
303
		$result = json_decode( (string) $response->getBody(), true );
304
305
306
		$this->assertEquals( 403, $response->getStatusCode() );
307
		$this->assertArrayHasKey( 'errors', $result );
308
	}
309
310
311
	public function testPatchMShopException()
312
	{
313
		$object = $this->getObject( 'editAddressItem', $this->throwException( new \Aimeos\MShop\Exception() ) );
314
315
		$body = '{"data": {"attributes": []}}';
316
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
317
318
		$response = $object->patch( $request, $this->view->response() );
319
		$result = json_decode( (string) $response->getBody(), true );
320
321
322
		$this->assertEquals( 404, $response->getStatusCode() );
323
		$this->assertArrayHasKey( 'errors', $result );
324
	}
325
326
327
	public function testPatchException()
328
	{
329
		$object = $this->getObject( 'editAddressItem', $this->throwException( new \Exception() ) );
330
331
		$body = '{"data": {"attributes": []}}';
332
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
333
334
		$response = $object->patch( $request, $this->view->response() );
335
		$result = json_decode( (string) $response->getBody(), true );
336
337
338
		$this->assertEquals( 500, $response->getStatusCode() );
339
		$this->assertArrayHasKey( 'errors', $result );
340
	}
341
342
343
	public function testPost()
344
	{
345
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' );
346
		$userId = $manager->findItem( 'UTC001' )->getId();
347
		$this->context->setUserId( $userId );
348
349
		$params = array( 'id' => $userId );
350
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
351
		$this->view->addHelper( 'param', $helper );
352
353
		$body = '{"data": {"type": "customer/address", "attributes": {"customer.address.firstname": "test"}}}';
354
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
355
356
		$response = $this->object->post( $request, $this->view->response() );
357
		$result = json_decode( (string) $response->getBody(), true );
358
359
360
		$this->assertEquals( 201, $response->getStatusCode() );
361
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
362
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
363
364
		$this->assertEquals( 1, $result['meta']['total'] );
365
		$this->assertEquals( 'customer/address', $result['data'][0]['type'] );
366
		$this->assertEquals( 'test', $result['data'][0]['attributes']['customer.address.firstname'] );
367
368
		$this->assertArrayNotHasKey( 'errors', $result );
369
370
371
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer/address' );
372
		$manager->deleteItem( $result['data'][0]['id'] );
373
	}
374
375
376
	public function testPostMultiple()
377
	{
378
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' );
379
		$userId = $manager->findItem( 'UTC001' )->getId();
380
		$this->context->setUserId( $userId );
381
382
		$params = array( 'id' => $userId );
383
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
384
		$this->view->addHelper( 'param', $helper );
385
386
		$body = '{"data": [{
387
			"type": "customer/address", "attributes": {"customer.address.firstname": "test"}
388
		}, {
389
			"type": "customer/address", "attributes": {"customer.address.lastname": "test"}
390
		}]}';
391
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
392
393
		$response = $this->object->post( $request, $this->view->response() );
394
		$result = json_decode( (string) $response->getBody(), true );
395
396
397
		$this->assertEquals( 201, $response->getStatusCode() );
398
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
399
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
400
401
		$this->assertEquals( 2, $result['meta']['total'] );
402
		$this->assertNotNull( $result['data'][0]['id'] );
403
		$this->assertEquals( 'customer/address', $result['data'][0]['type'] );
404
		$this->assertEquals( 'test', $result['data'][0]['attributes']['customer.address.firstname'] );
405
		$this->assertEquals( 'test', $result['data'][1]['attributes']['customer.address.lastname'] );
406
407
		$this->assertArrayNotHasKey( 'errors', $result );
408
409
410
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer/address' );
411
		$manager->deleteItems( [$result['data'][0]['id'], $result['data'][1]['id']] );
412
	}
413
414
415
	public function testPostControllerException()
416
	{
417
		$object = $this->getObject( 'addAddressItem', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
418
419
		$body = '{"data": {"attributes": []}}';
420
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
421
422
		$response = $object->post( $request, $this->view->response() );
423
		$result = json_decode( (string) $response->getBody(), true );
424
425
426
		$this->assertEquals( 403, $response->getStatusCode() );
427
		$this->assertArrayHasKey( 'errors', $result );
428
	}
429
430
431
	public function testPostMShopException()
432
	{
433
		$object = $this->getObject( 'addAddressItem', $this->throwException( new \Aimeos\MShop\Exception() ) );
434
435
		$body = '{"data": {"attributes": []}}';
436
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
437
438
		$response = $object->post( $request, $this->view->response() );
439
		$result = json_decode( (string) $response->getBody(), true );
440
441
442
		$this->assertEquals( 404, $response->getStatusCode() );
443
		$this->assertArrayHasKey( 'errors', $result );
444
	}
445
446
447
	public function testPostException()
448
	{
449
		$object = $this->getObject( 'addAddressItem', $this->throwException( new \Exception() ) );
450
451
		$body = '{"data": {"attributes": []}}';
452
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
453
454
		$response = $object->post( $request, $this->view->response() );
455
		$result = json_decode( (string) $response->getBody(), true );
456
457
458
		$this->assertEquals( 500, $response->getStatusCode() );
459
		$this->assertArrayHasKey( 'errors', $result );
460
	}
461
462
463
	/**
464
	 * Returns a test object with a mocked customer controller
465
	 *
466
	 * @param string $method Customer controller method name to mock
467
	 * @param mixed $result Return value of the mocked method
468
	 */
469
	protected function getObject( $method, $result )
470
	{
471
		$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Customer\Standard' )
472
			->setConstructorArgs( [$this->context] )
473
			->setMethods( [$method] )
474
			->getMock();
475
476
		$cntl->expects( $this->once() )->method( $method )->will( $result );
477
478
		\Aimeos\Controller\Frontend\Customer\Factory::injectController( '\Aimeos\Controller\Frontend\Customer\Standard', $cntl );
479
480
		$templatePaths = \TestHelperJapi::getTemplatePaths();
481
		$object = new \Aimeos\Client\JsonApi\Customer\Address\Standard( $this->context, $this->view, $templatePaths, 'customer/address' );
482
483
		return $object;
484
	}
485
}