Passed
Push — master ( c2722c...e0f9fd )
by Aimeos
04:23
created

PayPalExpressTest::testUpdatePush()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 58
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 38
nc 6
nop 0
dl 0
loc 58
rs 9.0008
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 */
8
9
10
namespace Aimeos\MShop\Service\Provider\Payment;
11
12
13
class PayPalExpressTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $context;
16
	private $object;
17
	private $serviceItem;
18
	private $orderMock;
19
	private $order;
20
21
22
	protected function setUp()
23
	{
24
		$this->context = \TestHelperMShop::getContext();
25
		$serviceManager = \Aimeos\MShop\Service\Manager\Factory::create( $this->context );
26
27
		$search = $serviceManager->createSearch();
28
		$search->setConditions( $search->compare( '==', 'service.code', 'paypalexpress' ) );
29
30
		$serviceItems = $serviceManager->searchItems( $search );
31
32
		if( ( $this->serviceItem = reset( $serviceItems ) ) === false ) {
33
			throw new \RuntimeException( 'No paypalexpress service item available' );
34
		}
35
36
		$this->object = $this->getMockBuilder( \Aimeos\MShop\Service\Provider\Payment\PayPalExpress::class )
37
			->setConstructorArgs( [$this->context, $this->serviceItem] )
38
			->setMethods( ['send'] )
39
			->getMock();
40
41
42
		$orderManager = \Aimeos\MShop\Order\Manager\Factory::create( $this->context );
43
44
		$search = $orderManager->createSearch();
45
		$expr = array(
46
			$search->compare( '==', 'order.type', \Aimeos\MShop\Order\Item\Base::TYPE_WEB ),
47
			$search->compare( '==', 'order.statuspayment', \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED )
48
		);
49
		$search->setConditions( $search->combine( '&&', $expr ) );
50
		$orderItems = $orderManager->searchItems( $search );
51
52
		if( ( $this->order = reset( $orderItems ) ) === false ) {
53
			throw new \RuntimeException( sprintf( 'No Order found with statuspayment "%1$s" and type "%2$s"', \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED, \Aimeos\MShop\Order\Item\Base::TYPE_WEB ) );
54
		}
55
56
57
		$this->orderMock = $this->getMockBuilder( \Aimeos\MShop\Order\Manager\Standard::class )
58
			->setConstructorArgs( array( $this->context ) )
59
			->setMethods( array( 'saveItem' ) )
60
			->getMock();
61
62
		$this->context->getConfig()->set( 'mshop/order/manager/name', 'MockPayPal' );
63
		\Aimeos\MShop\Order\Manager\Factory::injectManager( '\Aimeos\MShop\Order\Manager\MockPayPal', $this->orderMock );
64
	}
65
66
67
	protected function tearDown()
68
	{
69
		unset( $this->object );
70
		unset( $this->serviceItem );
71
		unset( $this->order );
72
	}
73
74
75
	public function testGetConfigBE()
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
76
	{
77
		$result = $this->object->getConfigBE();
78
79
		$this->assertEquals( 15, count( $result ) );
80
81
		foreach( $result as $key => $item ) {
82
			$this->assertInstanceOf( 'Aimeos\MW\Criteria\Attribute\Iface', $item );
83
		}
84
	}
85
86
87
	public function testCheckConfigBE()
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
88
	{
89
		$attributes = array(
90
			'paypalexpress.ApiUsername' => 'user',
91
			'paypalexpress.AccountEmail' => '[email protected]',
92
			'paypalexpress.ApiPassword' => 'pw',
93
			'paypalexpress.ApiSignature' => '1df23eh67',
94
			'payment.url-cancel' => 'http://cancelUrl',
95
			'payment.url-success' => 'http://returnUrl'
96
		);
97
98
		$result = $this->object->checkConfigBE( $attributes );
99
100
		$this->assertEquals( 15, count( $result ) );
101
		$this->assertEquals( null, $result['paypalexpress.ApiUsername'] );
102
		$this->assertEquals( null, $result['paypalexpress.AccountEmail'] );
103
		$this->assertEquals( null, $result['paypalexpress.ApiPassword'] );
104
		$this->assertEquals( null, $result['paypalexpress.ApiSignature'] );
105
	}
106
107
	public function testIsImplemented()
108
	{
109
		$this->assertTrue( $this->object->isImplemented( \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_CANCEL ) );
110
		$this->assertTrue( $this->object->isImplemented( \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_CAPTURE ) );
111
		$this->assertTrue( $this->object->isImplemented( \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_QUERY ) );
112
		$this->assertTrue( $this->object->isImplemented( \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_REFUND ) );
113
114
		$this->assertFalse( $this->object->isImplemented( -1 ) );
115
	}
116
117
118
	public function testProcess()
119
	{
120
		$this->object->expects( $this->once() )->method( 'send' )->will(
121
			$this->returnValue( '&ACK=Success&VERSION=87.0&BUILD=3136725&CORRELATIONID=1234567890&TOKEN=UT-99999999' )
122
		);
123
124
		$helperForm = $this->object->process( $this->order );
125
126
		$orderManager = \Aimeos\MShop\Order\Manager\Factory::create( $this->context );
127
		$orderBaseManager = $orderManager->getSubManager( 'base' );
128
129
		$refOrderBase = $orderBaseManager->load( $this->order->getBaseId() );
130
131
		$attributes = $refOrderBase->getService( 'payment', 'paypalexpress' )->getAttributeItems();
132
133
		$attributeList = [];
134
		foreach( $attributes as $attribute ) {
135
			$attributeList[$attribute->getCode()] = $attribute;
136
		}
137
138
		$this->assertInstanceOf( \Aimeos\MShop\Common\Helper\Form\Iface::class, $helperForm );
139
		$this->assertEquals( 'https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&useraction=commit&token=UT-99999999', $helperForm->getUrl() );
140
		$this->assertEquals( 'POST', $helperForm->getMethod() );
141
		$this->assertEquals( [], $helperForm->getValues() );
142
143
		$testData = array(
144
			'TOKEN' => 'UT-99999999'
145
		);
146
147
		foreach( $testData as $key => $value ) {
148
			$this->assertEquals( $attributeList[$key]->getValue(), $testData[$key] );
149
		}
150
	}
151
152
153
	public function testUpdateSync()
154
	{
155
		//DoExpressCheckout
156
157
		$params = array(
158
			'token' => 'UT-99999999',
159
			'PayerID' => 'PaypalUnitTestBuyer'
160
		);
161
162
		$request = $this->getMockBuilder( \Psr\Http\Message\ServerRequestInterface::class )->getMock();
163
164
		$request->expects( $this->once() )->method( 'getAttributes' )->will( $this->returnValue( [] ) );
165
		$request->expects( $this->once() )->method( 'getParsedBody' )->will( $this->returnValue( [] ) );
166
		$request->expects( $this->once() )->method( 'getQueryParams' )->will( $this->returnValue( $params ) );
167
168
		$this->object->expects( $this->once() )->method( 'send' )->will(
169
			$this->returnValue( '&TOKEN=UT-99999999&CORRELATIONID=1234567890&ACK=Success&VERSION=87.0&BUILD=3136725&PAYERID=PaypalUnitTestBuyer&TRANSACTIONID=111111110&PAYMENTSTATUS=Pending&PENDINGREASON=authorization&INVNUM=' . $this->order->getId() )
170
		);
171
172
		$result = $this->object->updateSync( $request, $this->order );
173
174
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Iface::class, $result );
175
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED, $result->getPaymentStatus() );
176
	}
177
178
179
	public function testUpdatePush()
180
	{
181
		//IPN Call
182
		$orderManager = \Aimeos\MShop\Order\Manager\Factory::create( $this->context );
183
		$orderBaseManager = $orderManager->getSubManager( 'base' );
184
185
		$price = $orderBaseManager->getItem( $this->order->getBaseId() )->getPrice();
186
		$amount = $price->getValue() + $price->getCosts();
187
188
		$params = array(
189
			'residence_country' => 'US',
190
			'receiver_email' => '[email protected]',
191
			'address_city' => 'San+Jose',
192
			'first_name' => 'John',
193
			'payment_status' => 'Completed',
194
			'invoice' => $this->order->getId(),
195
			'txn_id' => '111111111',
196
			'payment_amount' => $amount
197
		);
198
		$testData = array(
199
			'TRANSACTIONID' => '111111111',
200
			'111111111' => 'Completed'
201
		);
202
203
		$request = $this->getMockBuilder( \Psr\Http\Message\ServerRequestInterface::class )->getMock();
204
		$response = $this->getMockBuilder( \Psr\Http\Message\ResponseInterface::class )->getMock();
205
206
		$request->expects( $this->once() )->method( 'getQueryParams' )->will( $this->returnValue( $params ) );
207
		$response->expects( $this->once() )->method( 'withStatus' )
208
			->will( $this->returnValue( $response ) )
209
			->with( $this->equalTo( 200 ) );
210
211
		$cmpFcn = function( $subject ) {
212
			return $subject->getPaymentStatus() === \Aimeos\MShop\Order\Item\Base::PAY_RECEIVED;
213
		};
214
215
		$this->orderMock->expects( $this->once() )->method( 'saveItem' )->with( $this->callback( $cmpFcn ) );
216
		$this->object->expects( $this->once() )->method( 'send' )->will( $this->returnValue( 'VERIFIED' ) );
217
218
		$result = $this->object->updatePush( $request, $response );
219
		$this->assertInstanceOf( \Psr\Http\Message\ResponseInterface::class, $result );
220
221
		$refOrderBase = $orderBaseManager->load( $this->order->getBaseId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_SERVICE );
222
		$attributes = $refOrderBase->getService( 'payment', 'paypalexpress' )->getAttributeItems();
223
		$attrManager = $orderBaseManager->getSubManager( 'service' )->getSubManager( 'attribute' );
224
225
		$attributeList = [];
226
		foreach( $attributes as $attribute ) {
227
			//remove attr where txn ids as keys, because next test with same txn id would fail
228
			if( $attribute->getCode() === '111111110' || $attribute->getCode() === '111111111' ) {
229
				$attrManager->deleteItem( $attribute->getId() );
230
			}
231
232
			$attributeList[$attribute->getCode()] = $attribute;
233
		}
234
235
		foreach( $testData as $key => $value ) {
236
			$this->assertEquals( $attributeList[$key]->getValue(), $testData[$key] );
237
		}
238
	}
239
240
241
	public function testRefund()
242
	{
243
		$this->object->expects( $this->once() )->method( 'send' )->will(
244
			$this->returnValue( 'REFUNDTRANSACTIONID=88888888&FEEREFUNDAMT=2.00&TOTALREFUNDAMT=24.00&CURRENCYCODE=EUR&REFUNDSTATUS=delayed&PENDINGREASON=echeck&CORRELATIONID=1234567890&ACK=Success&VERSION=87.0&BUILD=3136725' )
245
		);
246
247
		$this->object->refund( $this->order );
248
249
		$testData = array(
250
			'TOKEN' => 'UT-99999999',
251
			'TRANSACTIONID' => '111111111',
252
			'REFUNDTRANSACTIONID' => '88888888'
253
		);
254
255
		$orderManager = \Aimeos\MShop\Order\Manager\Factory::create( $this->context );
256
		$orderBaseManager = $orderManager->getSubManager( 'base' );
257
258
		$refOrderBase = $orderBaseManager->load( $this->order->getBaseId() );
259
		$attributes = $refOrderBase->getService( 'payment', 'paypalexpress' )->getAttributeItems();
260
261
		$attributeList = [];
262
		foreach( $attributes as $attribute ) {
263
			$attributeList[$attribute->getCode()] = $attribute;
264
		}
265
266
		foreach( $testData as $key => $value ) {
267
			$this->assertEquals( $attributeList[$key]->getValue(), $testData[$key] );
268
		}
269
270
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_REFUND, $this->order->getPaymentStatus() );
271
	}
272
273
274
	public function testCapture()
275
	{
276
		$this->object->expects( $this->once() )->method( 'send' )->will(
277
			$this->returnValue( 'AUTHORIZATIONID=112233&TRANSACTIONID=111111111&PARENTTRANSACTIONID=12212AD&TRANSACTIONTYPE=express-checkout&AMT=22.30&FEEAMT=3.33&PAYMENTSTATUS=Completed&PENDINGREASON=None&CORRELATIONID=1234567890&ACK=Success&VERSION=87.0&BUILD=3136725' )
278
		);
279
280
		$this->object->capture( $this->order );
281
282
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_RECEIVED, $this->order->getPaymentStatus() );
283
	}
284
285
286
	public function testQueryPaymentReceived()
287
	{
288
		$this->object->expects( $this->once() )->method( 'send' )->will(
289
			$this->returnValue( 'SHIPPINGCALCULATIONMODE=Callback&INSURANCEOPTIONSELECTED=false&RECEIVERID=unit_1340199666_biz_api1.yahoo.de&PAYERID=unittest&PAYERSTATUS=verified&COUNTRYCODE=DE&FIRSTNAME=Unit&LASTNAME=Test&SHIPTOSTREET=Unitteststr. 11&TRANSACTIONID=111111111&PARENTTRANSACTIONID=111111111&TRANSACTIONTYPE=express-checkout&AMT=22.50CURRENCYCODE=EUR&FEEAMT=4.44&PAYMENTSTATUS=Completed&PENDINGREASON=None&INVNUM=34&CORRELATIONID=1f4b8e2c86ead&ACK=Success&VERSION=87.0&BUILD=3136725' )
290
		);
291
292
		$this->object->query( $this->order );
293
294
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_RECEIVED, $this->order->getPaymentStatus() );
295
	}
296
297
298
	public function testQueryPaymentRefused()
299
	{
300
		$this->object->expects( $this->once() )->method( 'send' )->will(
301
			$this->returnValue( 'SHIPPINGCALCULATIONMODE=Callback&INSURANCEOPTIONSELECTED=false&RECEIVERID=unit_1340199666_biz_api1.yahoo.de&PAYERID=unittest&PAYERSTATUS=verified&COUNTRYCODE=DE&FIRSTNAME=Unit&LASTNAME=Test&SHIPTOSTREET=Unitteststr. 11&TRANSACTIONID=111111111&PARENTTRANSACTIONID=111111111&TRANSACTIONTYPE=express-checkout&AMT=22.50CURRENCYCODE=EUR&FEEAMT=4.44&PAYMENTSTATUS=Expired&PENDINGREASON=None&INVNUM=34&CORRELATIONID=1f4b8e2c86ead&ACK=Success&VERSION=87.0&BUILD=3136725' )
302
		);
303
304
		$this->object->query( $this->order );
305
306
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_REFUSED, $this->order->getPaymentStatus() );
307
	}
308
309
310
	public function testCancel()
311
	{
312
		$this->object->expects( $this->once() )->method( 'send' )->will(
313
			$this->returnValue( 'CORRELATIONID=1234567890&ACK=Success&VERSION=87.0&BUILD=3136725' )
314
		);
315
316
		$this->object->cancel( $this->order );
317
318
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_CANCELED, $this->order->getPaymentStatus() );
319
	}
320
321
322
	public function testQueryPaymentAuthorized()
323
	{
324
		$this->object->expects( $this->once() )->method( 'send' )->will(
325
			$this->returnValue( 'SHIPPINGCALCULATIONMODE=Callback&INSURANCEOPTIONSELECTED=false&RECEIVERID=unit_1340199666_biz_api1.yahoo.de&PAYERID=unittest&PAYERSTATUS=verified&COUNTRYCODE=DE&FIRSTNAME=Unit&LASTNAME=Test&SHIPTOSTREET=Unitteststr. 11&TRANSACTIONID=111111111&PARENTTRANSACTIONID=111111111&TRANSACTIONTYPE=express-checkout&AMT=22.50CURRENCYCODE=EUR&FEEAMT=4.44&PAYMENTSTATUS=Pending&PENDINGREASON=authorization&INVNUM=34&CORRELATIONID=1234567890&ACK=Success&VERSION=87.0&BUILD=3136725' )
326
		);
327
328
		$this->object->query( $this->order );
329
330
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED, $this->order->getPaymentStatus() );
331
	}
332
333
334
	public function testWrongAuthorization()
335
	{
336
		$this->object->expects( $this->once() )->method( 'send' )->will(
337
			$this->returnValue( '&ACK=Error&VERSION=87.0&BUILD=3136725&CORRELATIONID=1234567890&L_ERRORCODE0=0000&L_SHORTMESSAGE0=wrong authorization test method error' )
338
		);
339
340
		$this->setExpectedException( \Aimeos\MShop\Service\Exception::class );
341
		$this->object->process( $this->order );
342
	}
343
344
345
	public function testSetPaymentStatusNone()
346
	{
347
		$class = new \ReflectionClass( \Aimeos\MShop\Service\Provider\Payment\PayPalExpress::class );
348
		$method = $class->getMethod( 'setPaymentStatus' );
349
		$method->setAccessible( true );
350
351
		$method->invokeArgs( $this->object, array( $this->order, [] ) );
352
353
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED, $this->order->getPaymentStatus() );
354
	}
355
356
357
	public function testSetPaymentPending()
358
	{
359
		$class = new \ReflectionClass( \Aimeos\MShop\Service\Provider\Payment\PayPalExpress::class );
360
		$method = $class->getMethod( 'setPaymentStatus' );
361
		$method->setAccessible( true );
362
363
		$method->invokeArgs( $this->object, array( $this->order, array( 'PAYMENTSTATUS' => 'Pending', 'PENDINGREASON' => 'error' ) ) );
364
365
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_PENDING, $this->order->getPaymentStatus() );
366
	}
367
368
369
	public function testSetPaymentRefunded()
370
	{
371
		$class = new \ReflectionClass( \Aimeos\MShop\Service\Provider\Payment\PayPalExpress::class );
372
		$method = $class->getMethod( 'setPaymentStatus' );
373
		$method->setAccessible( true );
374
375
		$method->invokeArgs( $this->object, array( $this->order, array( 'PAYMENTSTATUS' => 'Refunded' ) ) );
376
377
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_REFUND, $this->order->getPaymentStatus() );
378
	}
379
380
381
	public function testSetPaymentCanceled()
382
	{
383
		$class = new \ReflectionClass( \Aimeos\MShop\Service\Provider\Payment\PayPalExpress::class );
384
		$method = $class->getMethod( 'setPaymentStatus' );
385
		$method->setAccessible( true );
386
387
		$method->invokeArgs( $this->object, array( $this->order, array( 'PAYMENTSTATUS' => 'Voided' ) ) );
388
389
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_CANCELED, $this->order->getPaymentStatus() );
390
	}
391
392
393
	public function testSetPaymentInvalid()
394
	{
395
		$class = new \ReflectionClass( \Aimeos\MShop\Service\Provider\Payment\PayPalExpress::class );
396
		$method = $class->getMethod( 'setPaymentStatus' );
397
		$method->setAccessible( true );
398
399
		$method->invokeArgs( $this->object, array( $this->order, array( 'PAYMENTSTATUS' => 'Invalid' ) ) );
400
401
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED, $this->order->getPaymentStatus() );
402
	}
403
}
404