Completed
Push — master ( 1bfe5f...3b0799 )
by Aimeos
10:36
created

PayPalExpressTest::testUpdateSync()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
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-2017
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::createManager( $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 = new \Aimeos\MShop\Service\Provider\Payment\PayPalExpress( $this->context, $this->serviceItem );
37
38
39
		$orderManager = \Aimeos\MShop\Order\Manager\Factory::createManager( $this->context );
40
41
		$search = $orderManager->createSearch();
42
		$expr = array(
43
			$search->compare( '==', 'order.type', \Aimeos\MShop\Order\Item\Base::TYPE_WEB ),
44
			$search->compare( '==', 'order.statuspayment', \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED )
45
		);
46
		$search->setConditions( $search->combine( '&&', $expr ) );
47
		$orderItems = $orderManager->searchItems( $search );
48
49
		if( ( $this->order = reset( $orderItems ) ) === false ) {
50
			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 ) );
51
		}
52
53
54
		$this->orderMock = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Standard' )
55
			->setConstructorArgs( array( $this->context ) )
56
			->setMethods( array( 'saveItem' ) )
57
			->getMock();
58
59
		$this->context->getConfig()->set( 'mshop/order/manager/name', 'MockPayPal' );
60
		\Aimeos\MShop\Order\Manager\Factory::injectManager( '\\Aimeos\\MShop\\Order\\Manager\\MockPayPal', $this->orderMock );
61
	}
62
63
64
	protected function tearDown()
65
	{
66
		unset( $this->object );
67
		unset( $this->serviceItem );
68
		unset( $this->order );
69
	}
70
71
72
	public function testGetConfigBE()
73
	{
74
		$result = $this->object->getConfigBE();
75
76
		$this->assertEquals( 12, count( $result ) );
77
78
		foreach( $result as $key => $item ) {
79
			$this->assertInstanceOf( 'Aimeos\MW\Criteria\Attribute\Iface', $item );
80
		}
81
	}
82
83
84
	public function testCheckConfigBE()
85
	{
86
		$attributes = array(
87
			'paypalexpress.ApiUsername' => 'user',
88
			'paypalexpress.AccountEmail' => '[email protected]',
89
			'paypalexpress.ApiPassword' => 'pw',
90
			'paypalexpress.ApiSignature' => '1df23eh67',
91
			'payment.url-cancel' => 'http://cancelUrl',
92
			'payment.url-success' => 'http://returnUrl'
93
		);
94
95
		$result = $this->object->checkConfigBE( $attributes );
96
97
		$this->assertEquals( 12, count( $result ) );
98
		$this->assertEquals( null, $result['paypalexpress.ApiUsername'] );
99
		$this->assertEquals( null, $result['paypalexpress.AccountEmail'] );
100
		$this->assertEquals( null, $result['paypalexpress.ApiPassword'] );
101
		$this->assertEquals( null, $result['paypalexpress.ApiSignature'] );
102
	}
103
104
	public function testIsImplemented()
105
	{
106
		$this->assertTrue( $this->object->isImplemented( \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_CANCEL ) );
107
		$this->assertTrue( $this->object->isImplemented( \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_CAPTURE ) );
108
		$this->assertTrue( $this->object->isImplemented( \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_QUERY ) );
109
		$this->assertTrue( $this->object->isImplemented( \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_REFUND ) );
110
111
		$this->assertFalse( $this->object->isImplemented( -1 ) );
112
	}
113
114
115
	public function testProcess()
116
	{
117
		$what = array( 'PAYMENTREQUEST_0_AMT' => 18.50 );
118
		$error = '&ACK=Error&VERSION=87.0&BUILD=3136725&CORRELATIONID=1234567890&L_ERRORCODE0=0000&L_SHORTMESSAGE0=process method error';
119
		$success = '&ACK=Success&VERSION=87.0&BUILD=3136725&CORRELATIONID=1234567890&TOKEN=UT-99999999';
120
121
		$com = new TestPayPalExpress();
122
		$com->addRule( $what, $error, $success );
123
		$this->object->setCommunication( $com );
124
125
		$helperForm = $this->object->process( $this->order );
126
127
		$orderManager = \Aimeos\MShop\Order\Manager\Factory::createManager( $this->context );
128
		$orderBaseManager = $orderManager->getSubManager( 'base' );
129
130
		$refOrderBase = $orderBaseManager->load( $this->order->getBaseId() );
131
132
		$attributes = $refOrderBase->getService( 'payment', 'paypalexpress' )->getAttributes();
133
134
		$attributeList = [];
135
		foreach( $attributes as $attribute ) {
136
			$attributeList[$attribute->getCode()] = $attribute;
137
		}
138
139
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Helper\\Form\\Iface', $helperForm );
140
		$this->assertEquals( 'https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&useraction=commit&token=UT-99999999', $helperForm->getUrl() );
141
		$this->assertEquals( 'POST', $helperForm->getMethod() );
142
		$this->assertEquals( [], $helperForm->getValues() );
143
144
		$testData = array(
145
			'TOKEN' => 'UT-99999999'
146
		);
147
148
		foreach( $testData as $key => $value ) {
149
			$this->assertEquals( $attributeList[$key]->getValue(), $testData[$key] );
150
		}
151
	}
152
153
154
	public function testUpdateSync()
155
	{
156
		//DoExpressCheckout
157
158
		$what = array( 'TOKEN' => 'UT-99999999' );
159
160
		$error = '&ACK=Error&VERSION=87.0&BUILD=3136725&CORRELATIONID=1234567890&L_ERRORCODE0=0000&L_SHORTMESSAGE0=updatesync method error';
161
		$success = '&TOKEN=UT-99999999&CORRELATIONID=1234567890&ACK=Success&VERSION=87.0&BUILD=3136725&PAYERID=PaypalUnitTestBuyer&TRANSACTIONID=111111110&PAYMENTSTATUS=Pending&PENDINGREASON=authorization&INVNUM=' . $this->order->getId();
162
163
		$com = new TestPayPalExpress();
164
		$com->addRule( $what, $error, $success );
165
		$this->object->setCommunication( $com );
166
167
		$params = array(
168
			'token' => 'UT-99999999',
169
			'PayerID' => 'PaypalUnitTestBuyer'
170
		);
171
172
		$request = $this->getMockBuilder( '\Psr\Http\Message\ServerRequestInterface' )->getMock();
173
174
		$request->expects( $this->once() )->method( 'getAttributes' )->will( $this->returnValue( [] ) );
175
		$request->expects( $this->once() )->method( 'getParsedBody' )->will( $this->returnValue( [] ) );
176
		$request->expects( $this->once() )->method( 'getQueryParams' )->will( $this->returnValue( $params ) );
177
178
		$result = $this->object->updateSync( $request, $this->order );
179
180
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Order\\Item\\Iface', $result );
181
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED, $result->getPaymentStatus() );
182
	}
183
184
185
	public function testUpdatePush()
186
	{
187
		//IPN Call
188
		$orderManager = \Aimeos\MShop\Order\Manager\Factory::createManager( $this->context );
189
		$orderBaseManager = $orderManager->getSubManager( 'base' );
190
191
		$price = $orderBaseManager->getItem( $this->order->getBaseId() )->getPrice();
192
		$amount = $price->getValue() + $price->getCosts();
193
		$what = array(
194
			'residence_country' => 'US',
195
			'address_city' => 'San+Jose',
196
			'first_name' => 'John',
197
			'payment_status' => 'Completed',
198
			'invoice' => $this->order->getId(),
199
			'txn_id' => '111111111',
200
			'payment_amount' => $amount,
201
			'receiver_email' => '[email protected]',
202
		);
203
		$error = 'INVALID';
204
		$success = 'VERIFIED';
205
206
		$com = new TestPayPalExpress();
207
		$com->addRule( $what, $error, $success );
208
		$this->object->setCommunication( $com );
209
210
211
		$params = array(
212
			'residence_country' => 'US',
213
			'receiver_email' => '[email protected]',
214
			'address_city' => 'San+Jose',
215
			'first_name' => 'John',
216
			'payment_status' => 'Completed',
217
			'invoice' => $this->order->getId(),
218
			'txn_id' => '111111111',
219
			'payment_amount' => $amount
220
		);
221
		$testData = array(
222
			'TRANSACTIONID' => '111111111',
223
			'111111110' => 'Pending',
224
			'111111111' => 'Completed'
225
		);
226
227
		$request = $this->getMockBuilder( '\Psr\Http\Message\ServerRequestInterface' )->getMock();
228
		$response = $this->getMockBuilder( '\Psr\Http\Message\ResponseInterface' )->getMock();
229
230
		$request->expects( $this->once() )->method( 'getQueryParams' )->will( $this->returnValue( $params ) );
231
		$response->expects( $this->once() )->method( 'withStatus' )
232
			->will( $this->returnValue( $response ) )
233
			->with( $this->equalTo( 200 ) );
234
235
		$cmpFcn = function( $subject ) {
236
			return $subject->getPaymentStatus() === \Aimeos\MShop\Order\Item\Base::PAY_RECEIVED;
237
		};
238
239
		$this->orderMock->expects( $this->once() )->method( 'saveItem' )->with( $this->callback( $cmpFcn ) );
240
241
		$result = $this->object->updatePush( $request, $response );
242
		$this->assertInstanceOf( '\Psr\Http\Message\ResponseInterface', $result );
243
244
		$refOrderBase = $orderBaseManager->load( $this->order->getBaseId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_SERVICE );
245
		$attributes = $refOrderBase->getService( 'payment', 'paypalexpress' )->getAttributes();
246
		$attrManager = $orderBaseManager->getSubManager( 'service' )->getSubManager( 'attribute' );
247
248
		$attributeList = [];
249
		foreach( $attributes as $attribute ) {
250
			//remove attr where txn ids as keys, because next test with same txn id would fail
251
			if( $attribute->getCode() === '111111110' || $attribute->getCode() === '111111111' ) {
252
				$attrManager->deleteItem( $attribute->getId() );
253
			}
254
255
			$attributeList[$attribute->getCode()] = $attribute;
256
		}
257
258
		foreach( $testData as $key => $value ) {
259
			$this->assertEquals( $attributeList[$key]->getValue(), $testData[$key] );
260
		}
261
	}
262
263
264
	public function testRefund()
265
	{
266
		$what = array(
267
			'METHOD' => 'RefundTransaction',
268
			'REFUNDSOURCE' => 'instant',
269
			'REFUNDTYPE' => 'Full',
270
			'TRANSACTIONID' => '111111111',
271
			'INVOICEID' => $this->order->getId()
272
		);
273
		$error = '&ACK=Error&VERSION=87.0&BUILD=3136725&CORRELATIONID=1234567890&L_ERRORCODE0=0000&L_SHORTMESSAGE0=refund method error';
274
		$success = 'REFUNDTRANSACTIONID=88888888&FEEREFUNDAMT=2.00&TOTALREFUNDAMT=24.00&CURRENCYCODE=EUR&REFUNDSTATUS=delayed&PENDINGREASON=echeck&CORRELATIONID=1234567890&ACK=Success&VERSION=87.0&BUILD=3136725';
275
276
		$com = new TestPayPalExpress();
277
		$com->addRule( $what, $error, $success );
278
		$this->object->setCommunication( $com );
279
280
		$this->object->refund( $this->order );
281
282
		$testData = array(
283
			'TOKEN' => 'UT-99999999',
284
			'TRANSACTIONID' => '111111111',
285
			'REFUNDTRANSACTIONID' => '88888888'
286
		);
287
288
		$orderManager = \Aimeos\MShop\Order\Manager\Factory::createManager( $this->context );
289
		$orderBaseManager = $orderManager->getSubManager( 'base' );
290
291
		$refOrderBase = $orderBaseManager->load( $this->order->getBaseId() );
292
		$attributes = $refOrderBase->getService( 'payment', 'paypalexpress' )->getAttributes();
293
294
		$attributeList = [];
295
		foreach( $attributes as $attribute ) {
296
			$attributeList[$attribute->getCode()] = $attribute;
297
		}
298
299
		foreach( $testData as $key => $value ) {
300
			$this->assertEquals( $attributeList[$key]->getValue(), $testData[$key] );
301
		}
302
303
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_REFUND, $this->order->getPaymentStatus() );
304
	}
305
306
307
	public function testCapture()
308
	{
309
		$orderManager = \Aimeos\MShop\Order\Manager\Factory::createManager( $this->context );
310
		$orderBaseManager = $orderManager->getSubManager( 'base' );
311
		$baseItem = $orderBaseManager->getItem( $this->order->getBaseId() );
312
313
		$what = array(
314
			'METHOD' => 'DoCapture',
315
			'COMPLETETYPE' => 'Complete',
316
			'AUTHORIZATIONID' => '111111111',
317
			'INVNUM' => $this->order->getId(),
318
			'CURRENCYCODE' => $baseItem->getPrice()->getCurrencyId(),
319
			'AMT' => ( $baseItem->getPrice()->getValue() + $baseItem->getPrice()->getCosts() )
320
		);
321
		$error = '&ACK=Error&VERSION=87.0&BUILD=3136725&CORRELATIONID=1234567890&L_ERRORCODE0=0000&L_SHORTMESSAGE0=capture method error';
322
		$success = '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';
323
324
		$com = new TestPayPalExpress();
325
		$com->addRule( $what, $error, $success );
326
		$this->object->setCommunication( $com );
327
328
		$this->object->capture( $this->order );
329
330
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_RECEIVED, $this->order->getPaymentStatus() );
331
	}
332
333
	public function testQueryPaymentReceived()
334
	{
335
		$what = array(
336
			'METHOD' => 'GetTransactionDetails',
337
			'TRANSACTIONID' => '111111111'
338
		);
339
		$error = '&ACK=Error&VERSION=87.0&BUILD=3136725&CORRELATIONID=1234567890&L_ERRORCODE0=0000&L_SHORTMESSAGE0=query payment received test method error';
340
		$success = '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';
341
342
		$com = new TestPayPalExpress();
343
		$com->addRule( $what, $error, $success );
344
		$this->object->setCommunication( $com );
345
346
		$this->object->query( $this->order );
347
348
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_RECEIVED, $this->order->getPaymentStatus() );
349
	}
350
351
352
	public function testQueryPaymentRefused()
353
	{
354
		$what = array(
355
			'METHOD' => 'GetTransactionDetails',
356
			'TRANSACTIONID' => '111111111',
357
		);
358
		$error = '&ACK=Error&VERSION=87.0&BUILD=3136725&CORRELATIONID=1234567890&L_ERRORCODE0=0000&L_SHORTMESSAGE0=query payment refused test method error';
359
		$success = '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';
360
361
		$com = new TestPayPalExpress();
362
		$com->addRule( $what, $error, $success );
363
		$this->object->setCommunication( $com );
364
365
		$this->object->query( $this->order );
366
367
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_REFUSED, $this->order->getPaymentStatus() );
368
	}
369
370
371
	public function testCancel()
372
	{
373
		$what = array(
374
			'METHOD' => 'DoVoid',
375
			'AUTHORIZATIONID' => '111111111',
376
		);
377
		$error = '&ACK=Error&VERSION=87.0&BUILD=3136725&CORRELATIONID=1234567890&L_ERRORCODE0=0000&L_SHORTMESSAGE0=cancel test method error';
378
		$success = 'CORRELATIONID=1234567890&ACK=Success&VERSION=87.0&BUILD=3136725';
379
380
		$com = new TestPayPalExpress();
381
		$com->addRule( $what, $error, $success );
382
		$this->object->setCommunication( $com );
383
384
		$this->object->cancel( $this->order );
385
386
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_CANCELED, $this->order->getPaymentStatus() );
387
	}
388
389
390
	public function testQueryPaymentAuthorized()
391
	{
392
		$what = array(
393
			'METHOD' => 'GetTransactionDetails',
394
			'TRANSACTIONID' => '111111111',
395
		);
396
		$error = '&ACK=Error&VERSION=87.0&BUILD=3136725&CORRELATIONID=1234567890&L_ERRORCODE0=0000&L_SHORTMESSAGE0=query payment authorized test method error';
397
		$success = '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';
398
399
		$com = new TestPayPalExpress();
400
		$com->addRule( $what, $error, $success );
401
		$this->object->setCommunication( $com );
402
403
		$this->object->query( $this->order );
404
405
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED, $this->order->getPaymentStatus() );
406
	}
407
408
409
	public function testWrongAuthorization()
410
	{
411
		$what = array(
412
			'VERSION' => '87.0',
413
			'SIGNATURE' => 'signature',
414
			'USER' => 'name',
415
			'PWD' => 'pw',
416
		);
417
		$error = '&ACK=Error&VERSION=87.0&BUILD=3136725&CORRELATIONID=1234567890&L_ERRORCODE0=0000&L_SHORTMESSAGE0=wrong authorization test method error';
418
		$success = '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';
419
420
		$com = new TestPayPalExpress();
421
		$com->addRule( $what, $error, $success );
422
		$this->object->setCommunication( $com );
423
424
		$this->setExpectedException( '\\Aimeos\\MShop\\Service\\Exception' );
425
		$this->object->process( $this->order );
426
	}
427
428
429
	public function testSetPaymentStatusNone()
430
	{
431
		$class = new \ReflectionClass( '\Aimeos\MShop\Service\Provider\Payment\PayPalExpress' );
432
		$method = $class->getMethod( 'setPaymentStatus' );
433
		$method->setAccessible( true );
434
435
		$method->invokeArgs( $this->object, array( $this->order, [] ) );
436
437
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED, $this->order->getPaymentStatus() );
438
	}
439
440
441
	public function testSetPaymentPending()
442
	{
443
		$class = new \ReflectionClass( '\Aimeos\MShop\Service\Provider\Payment\PayPalExpress' );
444
		$method = $class->getMethod( 'setPaymentStatus' );
445
		$method->setAccessible( true );
446
447
		$method->invokeArgs( $this->object, array( $this->order, array( 'PAYMENTSTATUS' => 'Pending', 'PENDINGREASON' => 'error' ) ) );
448
449
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_PENDING, $this->order->getPaymentStatus() );
450
	}
451
452
453
	public function testSetPaymentRefunded()
454
	{
455
		$class = new \ReflectionClass( '\Aimeos\MShop\Service\Provider\Payment\PayPalExpress' );
456
		$method = $class->getMethod( 'setPaymentStatus' );
457
		$method->setAccessible( true );
458
459
		$method->invokeArgs( $this->object, array( $this->order, array( 'PAYMENTSTATUS' => 'Refunded' ) ) );
460
461
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_REFUND, $this->order->getPaymentStatus() );
462
	}
463
464
465
	public function testSetPaymentCanceled()
466
	{
467
		$class = new \ReflectionClass( '\Aimeos\MShop\Service\Provider\Payment\PayPalExpress' );
468
		$method = $class->getMethod( 'setPaymentStatus' );
469
		$method->setAccessible( true );
470
471
		$method->invokeArgs( $this->object, array( $this->order, array( 'PAYMENTSTATUS' => 'Voided' ) ) );
472
473
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_CANCELED, $this->order->getPaymentStatus() );
474
	}
475
476
477
	public function testSetPaymentInvalid()
478
	{
479
		$class = new \ReflectionClass( '\Aimeos\MShop\Service\Provider\Payment\PayPalExpress' );
480
		$method = $class->getMethod( 'setPaymentStatus' );
481
		$method->setAccessible( true );
482
483
		$method->invokeArgs( $this->object, array( $this->order, array( 'PAYMENTSTATUS' => 'Invalid' ) ) );
484
485
		$this->assertEquals( \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED, $this->order->getPaymentStatus() );
486
	}
487
}
488
489
490
class TestPayPalExpress implements \Aimeos\MW\Communication\Iface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
491
{
492
	private $rules = [];
493
494
495
	/**
496
	 * Adds rules to the communication object.
497
	 *
498
	 * @param array $what List of rules for the unit tests.
499
	 * @param string $error Error message if some of the tests fails.
500
	 * @param string $success Success message if all tests were passed.
501
	 */
502
	public function addRule( array $what, $error, $success )
503
	{
504
		$this->rules['set'] = $what;
505
		$this->rules['error'] = $error;
506
		$this->rules['success'] = $success;
507
	}
508
509
510
	/**
511
	 * Get rules of the communication object.
512
	 *
513
	 * @return array rules for internal check
514
	 */
515
	public function getRules()
516
	{
517
		return $this->rules;
518
	}
519
520
521
	/**
522
	 * Sends request parameters to the providers interface.
523
	 *
524
	 * @param string $target Receivers address e.g. url.
525
	 * @param string $method Initial method (e.g. post or get)
526
	 * @param mixed $payload Update information whose format depends on the payment provider
527
	 * @return string response body of a http request
528
	 */
529
	public function transmit( $target, $method, $payload )
530
	{
531
		if( !isset( $this->rules['set'] ) ) {
532
			throw new \Aimeos\MW\Communication\Exception( sprintf( 'No rules for unit tests was set' ) );
533
		}
534
535
		if( !isset( $this->rules['error'] ) ) {
536
			throw new \Aimeos\MW\Communication\Exception( sprintf( 'No error message for unit tests was set' ) );
537
		}
538
539
		if( !isset( $this->rules['success'] ) ) {
540
			throw new \Aimeos\MW\Communication\Exception( sprintf( 'No success message for unit tests was set' ) );
541
		}
542
543
		$params = [];
544
		parse_str( $payload, $params );
545
546
		foreach( $this->rules['set'] as $key => $value )
547
		{
548
			if( $params[$key] != $value ) {
549
				return $this->rules['error'];
550
			}
551
		}
552
553
		return $this->rules['success'];
554
	}
555
}