Completed
Push — master ( 8c9c9d...8ff506 )
by Aimeos
04:08
created

StandardTest::testPost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 14
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\Basket\Coupon;
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\Coupon\Standard( $this->context, $this->view, $templatePaths, 'basket/coupon' );
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
		$this->addProduct( 'CNC' );
38
39
		$body = '{"data": {"type": "basket/coupon", "id": "GHIJ"}}';
40
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
41
42
		$response = $this->object->post( $request, $this->view->response() );
43
		$result = json_decode( (string) $response->getBody(), true );
44
		$this->assertEquals( 1, count( $result['data']['attributes']['coupons'] ) );
45
46
47
		$body = '{"data": {"type": "basket/coupon", "id": "GHIJ"}}';
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( 'coupon', $result['data']['attributes'] );
60
61
		$this->assertArrayNotHasKey( 'errors', $result );
62
	}
63
64
65
	public function testDeleteById()
66
	{
67
		$this->addProduct( 'CNC' );
68
69
		$body = '{"data": {"type": "basket/coupon", "id": "GHIJ"}}';
70
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
71
72
		$response = $this->object->post( $request, $this->view->response() );
73
		$result = json_decode( (string) $response->getBody(), true );
74
75
		$this->assertEquals( 1, count( $result['data']['attributes']['coupons'] ) );
76
77
78
		$params = array( 'id' => 'default', 'relatedid' => 'GHIJ' );
79
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
80
		$this->view->addHelper( 'param', $helper );
81
82
		$response = $this->object->delete( $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( 1, $result['meta']['total'] );
90
		$this->assertEquals( 'basket', $result['data']['type'] );
91
		$this->assertArrayNotHasKey( 'coupon', $result['data']['attributes'] );
92
93
		$this->assertArrayNotHasKey( 'errors', $result );
94
	}
95
96
97
	public function testDeleteMShopException()
98
	{
99
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
100
101
		$response = $object->delete( $this->view->request(), $this->view->response() );
102
		$result = json_decode( (string) $response->getBody(), true );
103
104
105
		$this->assertEquals( 404, $response->getStatusCode() );
106
		$this->assertArrayHasKey( 'errors', $result );
107
	}
108
109
110
	public function testDeleteException()
111
	{
112
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
113
114
		$response = $object->delete( $this->view->request(), $this->view->response() );
115
		$result = json_decode( (string) $response->getBody(), true );
116
117
118
		$this->assertEquals( 500, $response->getStatusCode() );
119
		$this->assertArrayHasKey( 'errors', $result );
120
	}
121
122
123
	public function testPost()
124
	{
125
		$this->addProduct( 'CNC' );
126
127
		$body = '{"data": {"type": "basket/coupon", "id": "GHIJ"}}';
128
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
129
130
		$response = $this->object->post( $request, $this->view->response() );
131
		$result = json_decode( (string) $response->getBody(), true );
132
133
134
		$this->assertEquals( 201, $response->getStatusCode() );
135
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
136
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
137
138
		$this->assertEquals( 1, $result['meta']['total'] );
139
		$this->assertEquals( 'basket', $result['data']['type'] );
140
		$this->assertEquals( 1, count( $result['data']['attributes']['coupons'] ) );
141
		$this->assertEquals( 'GHIJ', $result['data']['attributes']['coupons'][0] );
142
143
		$this->assertArrayNotHasKey( 'errors', $result );
144
	}
145
146
147
	public function testPostMultiple()
148
	{
149
		$this->addProduct( 'CNC' );
150
151
		$body = '{"data": [{
152
			"type": "basket/coupon", "id": "90AB"
153
		}, {
154
			"type": "basket/coupon", "id": "GHIJ"
155
		}]}';
156
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
157
158
		$response = $this->object->post( $request, $this->view->response() );
159
		$result = json_decode( (string) $response->getBody(), true );
160
161
162
		$this->assertEquals( 201, $response->getStatusCode() );
163
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
164
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
165
166
		$this->assertEquals( 1, $result['meta']['total'] );
167
		$this->assertEquals( 'basket', $result['data']['type'] );
168
		$this->assertEquals( 2, count( $result['data']['attributes']['coupons'] ) );
169
		$this->assertEquals( '90AB', $result['data']['attributes']['coupons'][0] );
170
		$this->assertEquals( 'GHIJ', $result['data']['attributes']['coupons'][1] );
171
172
		$this->assertArrayNotHasKey( 'errors', $result );
173
	}
174
175
176
	public function testPostMShopException()
177
	{
178
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
179
180
		$response = $object->post( $this->view->request(), $this->view->response() );
181
		$result = json_decode( (string) $response->getBody(), true );
182
183
184
		$this->assertEquals( 404, $response->getStatusCode() );
185
		$this->assertArrayHasKey( 'errors', $result );
186
	}
187
188
189
	public function testPostException()
190
	{
191
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
192
193
		$response = $object->post( $this->view->request(), $this->view->response() );
194
		$result = json_decode( (string) $response->getBody(), true );
195
196
197
		$this->assertEquals( 500, $response->getStatusCode() );
198
		$this->assertArrayHasKey( 'errors', $result );
199
	}
200
201
202
	protected function addProduct( $code )
203
	{
204
		$prodId = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( $code )->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...
205
206
		$body = '{"data": {"type": "basket/product", "attributes": {"productid": ' . $prodId . '}}}';
207
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
208
209
		$templatePaths = \TestHelperJapi::getTemplatePaths();
210
		$object = new \Aimeos\Client\JsonApi\Basket\Product\Standard( $this->context, $this->view, $templatePaths, 'basket/product' );
211
212
		$object->post( $request, $this->view->response() );
213
	}
214
215
216
	/**
217
	 * Returns a test object with a mocked basket controller
218
	 *
219
	 * @param string $method Basket controller method name to mock
220
	 * @param mixed $result Return value of the mocked method
221
	 */
222
	protected function getObject( $method, $result )
223
	{
224
		$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' )
225
			->setConstructorArgs( [$this->context] )
226
			->setMethods( [$method] )
227
			->getMock();
228
229
		$cntl->expects( $this->once() )->method( $method )->will( $result );
230
231
		\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', $cntl );
232
233
		$templatePaths = \TestHelperJapi::getTemplatePaths();
234
		$object = new \Aimeos\Client\JsonApi\Basket\Coupon\Standard( $this->context, $this->view, $templatePaths, 'basket/coupon' );
235
236
		return $object;
237
	}
238
}