Skip to content

Commit d173822

Browse files
Added test cases AIO gateway.
1 parent cea94d5 commit d173822

23 files changed

+638
-83
lines changed

src/Message/AbstractIncomingRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ abstract class AbstractIncomingRequest extends AbstractRequest
2121
*/
2222
public function getData(): array
2323
{
24-
$this->validate(array_keys($parameters = $this->getIncomingParameters()));
24+
call_user_func_array([$this, 'validate'], array_keys($parameters = $this->getIncomingParameters()));
2525

2626
return $parameters;
2727
}

src/Message/AbstractResponse.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ abstract class AbstractResponse extends BaseAbstractResponse
2323
*/
2424
public function __get($name)
2525
{
26-
return $this->data[$name] ?? null;
26+
if (isset($this->data[$name])) {
27+
return $this->data[$name];
28+
} else {
29+
trigger_error(sprintf('Undefined property: %s::%s', __CLASS__, '$'.$name), E_USER_NOTICE);
30+
31+
return null;
32+
}
2733
}
2834
}

src/Message/AllInOne/AbstractRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ abstract class AbstractRequest extends BaseAbstractRequest
3232
public function getData(): array
3333
{
3434
$parameters = $this->getParameters();
35-
unset($parameters['secretKey']);
35+
unset($parameters['secretKey'], $parameters['testMode']);
3636
$this->validate('partnerCode', 'accessKey', 'requestId', 'orderId', 'secretKey', 'requestType');
3737
$parameters['signature'] = $this->generateSignature();
3838

@@ -51,6 +51,6 @@ public function sendData($data)
5151
$responseClass = $this->responseClass;
5252
$responseData = json_decode($response->getBody()->getContents(), true);
5353

54-
return $this->response = new $responseClass($this, $responseData);
54+
return $this->response = new $responseClass($this, $responseData ?? []);
5555
}
5656
}

src/Message/AllInOne/Concerns/RequestSignature.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected function generateSignature(): string
4040
*/
4141
protected function getSignatureParameters(): array
4242
{
43-
switch ($this->getParameter('requestType')) {
43+
switch ($requestType = $this->getParameter('requestType')) {
4444
case 'captureMoMoWallet':
4545
return [
4646
'partnerCode', 'accessKey', 'requestId', 'amount', 'orderId', 'orderInfo', 'returnUrl', 'notifyUrl',
@@ -53,7 +53,7 @@ protected function getSignatureParameters(): array
5353
'partnerCode', 'accessKey', 'requestId', 'orderId', 'requestType',
5454
];
5555
default:
56-
throw new InvalidArgumentException(sprintf('Request type: (%s) is not valid!', $requestType));
56+
throw new InvalidArgumentException(sprintf('Request type: `%s` is not valid!', $requestType));
5757
}
5858
}
5959
}

src/Message/AllInOne/Concerns/ResponseSignatureValidation.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Omnipay\MoMo\Message\AllInOne\Concerns;
99

10+
use InvalidArgumentException;
1011
use Omnipay\MoMo\Support\Signature;
1112
use Omnipay\Common\Exception\InvalidResponseException;
1213

@@ -20,6 +21,7 @@ trait ResponseSignatureValidation
2021
* Kiểm tra tính hợp lệ của dữ liệu do MoMo phản hồi.
2122
*
2223
* @throws InvalidResponseException
24+
* @throws InvalidArgumentException
2325
*/
2426
protected function validateSignature(): void
2527
{
@@ -41,10 +43,11 @@ protected function validateSignature(): void
4143
* Trả về danh sách các param data đã dùng để tạo chữ ký dữ liệu theo requestType truyền vào.
4244
*
4345
* @return array
46+
* @throws InvalidArgumentException
4447
*/
4548
protected function getSignatureParameters(): array
4649
{
47-
switch ($this->data['requestType']) {
50+
switch ($requestType = $this->data['requestType']) {
4851
case 'captureMoMoWallet':
4952
return [
5053
'requestId', 'orderId', 'message', 'localMessage', 'payUrl', 'errorCode', 'requestType',
@@ -65,7 +68,7 @@ protected function getSignatureParameters(): array
6568
'localMessage', 'requestType',
6669
];
6770
default:
68-
return [];
71+
throw new InvalidArgumentException(sprintf('Request type: `%s` is not valid!', $requestType));
6972
}
7073
}
7174
}

src/Message/AllInOne/NotificationRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ protected function getIncomingParametersBag(): ParameterBag
2525
$json = $this->httpRequest->getContent();
2626
$data = json_decode($json, true);
2727

28-
return new ParameterBag($data);
28+
return new ParameterBag($data ?? []);
2929
}
3030
}

src/Message/AllInOne/RefundRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function setTransId(string $id): void
3131
*/
3232
public function getData(): array
3333
{
34-
$this->validate('transId');
34+
$this->validate('transId', 'amount');
3535
$this->setParameter('requestType', 'refundMoMoWallet');
3636

3737
return parent::getData();

src/Message/AllInOne/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Omnipay\MoMo\Message\AllInOne;
99

10-
use Omnipay\Common\Message\AbstractResponse;
10+
use Omnipay\MoMo\Message\AbstractResponse;
1111
use Omnipay\Common\Message\RequestInterface;
1212

1313
/**
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/omnipay-momo
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace Omnipay\MoMo\Tests\AllInOne;
9+
10+
use Omnipay\MoMo\Message\AllInOne\NotificationRequest;
11+
use Omnipay\Omnipay;
12+
use Omnipay\Tests\GatewayTestCase;
13+
use Omnipay\Common\Exception\InvalidRequestException;
14+
use Omnipay\MoMo\Message\AllInOne\CompletePurchaseRequest;
15+
16+
/**
17+
* @author Vuong Minh <[email protected]>
18+
* @since 1.0.0
19+
*/
20+
class AllInOneGatewayTest extends GatewayTestCase
21+
{
22+
/**
23+
* @var \Symfony\Component\HttpFoundation\ParameterBag
24+
*/
25+
protected $request;
26+
27+
protected function setUp()
28+
{
29+
$this->gateway = Omnipay::create('MoMo_AllInOne', $this->getHttpClient(), $this->getHttpRequest());
30+
$this->gateway->setAccessKey('E8HZuQRy2RsjVtZp');
31+
$this->gateway->setPartnerCode('MOMO0HGO20180417');
32+
$this->gateway->setSecretKey('fj00YKnJhmYqahaFWUgkg75saNTzMrbO');
33+
$this->gateway->setTestMode(true);
34+
35+
parent::setUp();
36+
}
37+
38+
public function testPurchaseSuccess()
39+
{
40+
$this->setMockHttpResponse('PurchaseSuccess.txt');
41+
$response = $this->gateway->purchase([
42+
'amount' => 99999999,
43+
'returnUrl' => 'http://localhost',
44+
'notifyUrl' => 'http://localhost',
45+
'orderId' => microtime(true),
46+
'requestId' => microtime(true),
47+
])->send();
48+
49+
$this->assertFalse($response->isSuccessful());
50+
$this->assertTrue($response->isRedirect());
51+
$this->assertContains('momo.vn', $response->getRedirectUrl());
52+
}
53+
54+
public function testPurchaseFailure()
55+
{
56+
$this->setMockHttpResponse('PurchaseFailure.txt');
57+
$response = $this->gateway->purchase([
58+
'amount' => 0,
59+
'returnUrl' => 'http://localhost',
60+
'notifyUrl' => 'http://localhost',
61+
'orderId' => microtime(true),
62+
'requestId' => microtime(true),
63+
])->send();
64+
65+
$this->assertFalse($response->isSuccessful());
66+
$this->assertFalse($response->isRedirect());
67+
}
68+
69+
public function testQueryTransaction()
70+
{
71+
$this->setMockHttpResponse('QueryTransaction.txt');
72+
$response = $this->gateway->queryTransaction([
73+
'orderId' => '0.38857000 1561110614',
74+
'requestId' => microtime(true),
75+
])->send();
76+
77+
$this->assertFalse($response->isSuccessful());
78+
$this->assertEquals(500000, $response->amount);
79+
}
80+
81+
public function testRefund()
82+
{
83+
$this->setMockHttpResponse('Refund.txt');
84+
$response = $this->gateway->refund([
85+
'orderId' => '0.38857000 1561110614',
86+
'requestId' => microtime(true),
87+
'transId' => 123,
88+
'amount' => 10000,
89+
])->send();
90+
91+
$this->assertEquals(123, $response->transId);
92+
$this->assertEquals(10000, $response->amount);
93+
$this->assertEquals('0.38857000 1561110614', $response->orderId);
94+
}
95+
96+
public function testQueryRefund()
97+
{
98+
$this->setMockHttpResponse('QueryRefund.txt');
99+
$response = $this->gateway->queryRefund([
100+
'orderId' => '0.38857000 1561110614',
101+
'requestId' => microtime(true),
102+
])->send();
103+
104+
$this->assertEquals('99', $response->getCode());
105+
}
106+
107+
public function testCompletePurchaseSuccess()
108+
{
109+
$request = $this->gateway->completePurchase();
110+
$this->assertInstanceOf(CompletePurchaseRequest::class, $request);
111+
$this->expectException(InvalidRequestException::class);
112+
$request->getData();
113+
}
114+
115+
public function testNotification()
116+
{
117+
$request = $this->gateway->notification();
118+
$this->assertInstanceOf(NotificationRequest::class, $request);
119+
$this->expectException(InvalidRequestException::class);
120+
$request->getData();
121+
}
122+
123+
/**
124+
* @doesNotPerformAssertions
125+
*/
126+
public function testDefaultParametersHaveMatchingMethods()
127+
{
128+
parent::testDefaultParametersHaveMatchingMethods();
129+
}
130+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/omnipay-momo
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace Omnipay\MoMo\Tests\AllInOne\Message;
9+
10+
use Omnipay\Tests\TestCase;
11+
use Omnipay\MoMo\Message\AllInOne\CompletePurchaseRequest;
12+
13+
/**
14+
* @author Vuong Minh <[email protected]>
15+
* @since 1.0.0
16+
*/
17+
class CompletePurchaseRequestTest extends TestCase
18+
{
19+
/**
20+
* @var CompletePurchaseRequest
21+
*/
22+
private $request;
23+
24+
public function setUp()
25+
{
26+
$client = $this->getHttpClient();
27+
$request = $this->getHttpRequest();
28+
$params = [
29+
'partnerCode', 'accessKey', 'requestId', 'amount', 'orderId', 'orderInfo', 'orderType', 'transId',
30+
'message', 'localMessage', 'responseTime', 'errorCode', 'extraData', 'signature', 'payType',
31+
];
32+
$request->query->replace(array_combine($params, range(1, 15)));
33+
$this->request = new CompletePurchaseRequest($client, $request);
34+
}
35+
36+
public function testGetData()
37+
{
38+
$data = $this->request->getData();
39+
$this->assertEquals(1, $data['partnerCode']);
40+
$this->assertEquals(2, $data['accessKey']);
41+
$this->assertEquals(3, $data['requestId']);
42+
$this->assertEquals(4, $data['amount']);
43+
$this->assertEquals(5, $data['orderId']);
44+
$this->assertEquals(6, $data['orderInfo']);
45+
$this->assertEquals(7, $data['orderType']);
46+
$this->assertEquals(8, $data['transId']);
47+
$this->assertEquals(9, $data['message']);
48+
$this->assertEquals(10, $data['localMessage']);
49+
$this->assertEquals(11, $data['responseTime']);
50+
$this->assertEquals(12, $data['errorCode']);
51+
$this->assertEquals(13, $data['extraData']);
52+
$this->assertEquals(14, $data['signature']);
53+
$this->assertEquals(15, $data['payType']);
54+
}
55+
}

0 commit comments

Comments
 (0)