Completed
Push — master ( 445306...7875eb )
by Aimeos
02:41
created

StandardTest::testGetById()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 29
rs 8.8571
c 1
b 1
f 0
cc 1
eloc 20
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A StandardTest::testPatchMShopException() 0 14 1
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\Product;
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\Product\Standard( $this->context, $this->view, $templatePaths, 'basket/product' );
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
		$prodId = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' )->getId();
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...
38
		$body = '{"data": {"type": "basket/product", "attributes": {"productid": ' . $prodId . '}}}';
39
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
40
41
		$response = $this->object->post( $request, $this->view->response() );
42
		$result = json_decode( (string) $response->getBody(), true );
43
44
		$this->assertEquals( 1, count( $result['data']['relationships']['basket/product']['data'] ) );
45
46
47
		$body = '{"data": {"type": "basket/product", "id": 0}}';
48
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
49
50
		$response = $this->object->delete( $request, $this->view->response() );
51
		$result = json_decode( (string) $response->getBody(), true );
52
53
		$this->assertEquals( 200, $response->getStatusCode() );
54
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
55
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
56
57
		$this->assertEquals( 1, $result['meta']['total'] );
58
		$this->assertEquals( 'basket', $result['data']['type'] );
59
		$this->assertArrayNotHasKey( 'basket/product', $result['data']['relationships'] );
60
61
		$this->assertArrayNotHasKey( 'errors', $result );
62
	}
63
64
65
	public function testDeleteById()
66
	{
67
		$prodId = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' )->getId();
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...
68
		$body = '{"data": {"type": "basket/product", "attributes": {"productid": ' . $prodId . '}}}';
69
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
70
71
		$response = $this->object->post( $request, $this->view->response() );
72
		$result = json_decode( (string) $response->getBody(), true );
73
74
		$this->assertEquals( 1, count( $result['data']['relationships']['basket/product']['data'] ) );
75
76
77
		$params = array( 'id' => 'default', 'relatedid' => 0 );
78
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
79
		$this->view->addHelper( 'param', $helper );
80
81
		$response = $this->object->delete( $request, $this->view->response() );
82
		$result = json_decode( (string) $response->getBody(), true );
83
84
		$this->assertEquals( 200, $response->getStatusCode() );
85
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
86
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
87
88
		$this->assertEquals( 1, $result['meta']['total'] );
89
		$this->assertEquals( 'basket', $result['data']['type'] );
90
		$this->assertArrayNotHasKey( 'basket/product', $result['data']['relationships'] );
91
92
		$this->assertArrayNotHasKey( 'errors', $result );
93
	}
94
95
96
	public function testDeleteMShopException()
97
	{
98
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
99
100
		$response = $object->delete( $this->view->request(), $this->view->response() );
101
		$result = json_decode( (string) $response->getBody(), true );
102
103
104
		$this->assertEquals( 404, $response->getStatusCode() );
105
		$this->assertArrayHasKey( 'errors', $result );
106
	}
107
108
109
	public function testDeleteException()
110
	{
111
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
112
113
		$response = $object->delete( $this->view->request(), $this->view->response() );
114
		$result = json_decode( (string) $response->getBody(), true );
115
116
117
		$this->assertEquals( 500, $response->getStatusCode() );
118
		$this->assertArrayHasKey( 'errors', $result );
119
	}
120
121
122
	public function testPatch()
123
	{
124
		$prodId = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' )->getId();
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...
125
		$body = '{"data": {"type": "basket/product", "attributes": {"productid": ' . $prodId . '}}}';
126
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
127
128
		$response = $this->object->post( $request, $this->view->response() );
129
		$result = json_decode( (string) $response->getBody(), true );
130
131
		$this->assertEquals( 1, count( $result['data']['relationships']['basket/product']['data'] ) );
132
133
134
		$body = '{"data": {"type": "basket/product", "id": 0, "attributes": {"quantity": 2}}}';
135
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
136
137
		$response = $this->object->patch( $request, $this->view->response() );
138
		$result = json_decode( (string) $response->getBody(), true );
139
140
		$this->assertEquals( 200, $response->getStatusCode() );
141
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
142
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
143
144
		$this->assertEquals( 1, $result['meta']['total'] );
145
		$this->assertEquals( 'basket', $result['data']['type'] );
146
		$this->assertArrayHasKey( 'product', $result['data']['relationships'] );
147
		$this->assertEquals( 1, count( $result['data']['relationships']['basket/product']['data'] ) );
148
		$this->assertEquals( 2, $result['included'][0]['attributes']['order.base.product.quantity'] );
149
150
		$this->assertArrayNotHasKey( 'errors', $result );
151
	}
152
153
154
	public function testPatchMShopException()
155
	{
156
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
157
158
		$body = '{"data": {"attributes": []}}';
159
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
160
161
		$response = $object->patch( $request, $this->view->response() );
162
		$result = json_decode( (string) $response->getBody(), true );
163
164
165
		$this->assertEquals( 404, $response->getStatusCode() );
166
		$this->assertArrayHasKey( 'errors', $result );
167
	}
168
169
170
	public function testPatchException()
171
	{
172
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
173
174
		$body = '{"data": {"attributes": []}}';
175
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
176
177
		$response = $object->patch( $request, $this->view->response() );
178
		$result = json_decode( (string) $response->getBody(), true );
179
180
181
		$this->assertEquals( 500, $response->getStatusCode() );
182
		$this->assertArrayHasKey( 'errors', $result );
183
	}
184
185
186
	public function testPost()
187
	{
188
		$prodId = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' )->getId();
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...
189
		$body = '{"data": {"type": "basket/product", "attributes": {"productid": ' . $prodId . '}}}';
190
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
191
192
		$response = $this->object->post( $request, $this->view->response() );
193
		$result = json_decode( (string) $response->getBody(), true );
194
195
196
		$this->assertEquals( 201, $response->getStatusCode() );
197
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
198
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
199
200
		$this->assertEquals( 1, $result['meta']['total'] );
201
		$this->assertEquals( 'basket', $result['data']['type'] );
202
		$this->assertEquals( 1, count( $result['data']['relationships']['basket/product']['data'] ) );
203
		$this->assertEquals( $prodId, $result['included'][0]['attributes']['order.base.product.productid'] );
204
205
		$this->assertArrayNotHasKey( 'errors', $result );
206
	}
207
208
209
	public function testPostMultiple()
210
	{
211
		$prodId = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' )->getId();
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...
212
		$prodId2 = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNE' )->getId();
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...
213
214
		$body = '{"data": [{
215
			"type": "basket/product", "attributes": {"productid": ' . $prodId . '}
216
		}, {
217
			"type": "basket/product", "attributes": {"productid": ' . $prodId2 . '}
218
		}]}';
219
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
220
221
		$response = $this->object->post( $request, $this->view->response() );
222
		$result = json_decode( (string) $response->getBody(), true );
223
224
225
		$this->assertEquals( 201, $response->getStatusCode() );
226
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
227
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
228
229
		$this->assertEquals( 1, $result['meta']['total'] );
230
		$this->assertEquals( 'basket', $result['data']['type'] );
231
		$this->assertEquals( 2, count( $result['data']['relationships']['basket/product']['data'] ) );
232
		$this->assertEquals( $prodId, $result['included'][0]['attributes']['order.base.product.productid'] );
233
		$this->assertEquals( $prodId2, $result['included'][1]['attributes']['order.base.product.productid'] );
234
235
		$this->assertArrayNotHasKey( 'errors', $result );
236
	}
237
238
239
	public function testPostMShopException()
240
	{
241
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
242
243
		$response = $object->post( $this->view->request(), $this->view->response() );
244
		$result = json_decode( (string) $response->getBody(), true );
245
246
247
		$this->assertEquals( 404, $response->getStatusCode() );
248
		$this->assertArrayHasKey( 'errors', $result );
249
	}
250
251
252
	public function testPostException()
253
	{
254
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
255
256
		$response = $object->post( $this->view->request(), $this->view->response() );
257
		$result = json_decode( (string) $response->getBody(), true );
258
259
260
		$this->assertEquals( 500, $response->getStatusCode() );
261
		$this->assertArrayHasKey( 'errors', $result );
262
	}
263
264
265
	/**
266
	 * Returns a stored product item from the order
267
	 *
268
	 * @return \Aimeos\MShop\Order\Item\Base\Product\Iface Ordered product item
269
	 */
270
	protected function getOrderProductItem()
271
	{
272
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'order/base/product' );
273
274
		$search = $manager->createSearch();
275
		$search->setSlice( 0, 1 );
276
277
		$items = $manager->searchItems( $search );
278
279
		if( ( $item = reset( $items ) ) === false ) {
280
			throw new \Exception( 'No order/base/product item found' );
281
		}
282
283
		return $item;
284
	}
285
286
287
288
	/**
289
	 * Returns a test object with a mocked basket controller
290
	 *
291
	 * @param string $method Basket controller method name to mock
292
	 * @param mixed $result Return value of the mocked method
293
	 */
294
	protected function getObject( $method, $result )
295
	{
296
		$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' )
297
			->setConstructorArgs( [$this->context] )
298
			->setMethods( [$method] )
299
			->getMock();
300
301
		$cntl->expects( $this->once() )->method( $method )->will( $result );
302
303
		\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', $cntl );
304
305
		$templatePaths = \TestHelperJapi::getTemplatePaths();
306
		$object = new \Aimeos\Client\JsonApi\Basket\Product\Standard( $this->context, $this->view, $templatePaths, 'basket/product' );
307
308
		return $object;
309
	}
310
}