Completed
Push — master ( ac749f...8181ea )
by Vuong
01:54
created

TestCase::purchase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * @link https://github.com/yiiviet/yii2-payment
4
 * @copyright Copyright (c) 2017 Yii2VN
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
namespace yiiviet\tests\unit\payment;
9
10
use Yii;
11
12
use yii\helpers\ArrayHelper;
13
14
use PHPUnit\Framework\TestCase as BaseTestCase;
15
16
/**
17
 * Class TestCase
18
 *
19
 * @author Vuong Minh <[email protected]>
20
 * @since 1.0
21
 */
22
abstract class TestCase extends BaseTestCase
23
{
24
    /**
25
     * @var \yiiviet\payment\BasePaymentGateway
26
     */
27
    public $gateway;
28
29
    public function setUp()
30
    {
31
        parent::setUp();
32
33
        $this->mockApplication();
34
        $this->gateway = Yii::$app->get('paymentGateways')->getGateway(static::gatewayId());
35
    }
36
37
    public function tearDown()
38
    {
39
        parent::tearDown();
40
41
        $this->destroyApplication();
42
        $this->gateway = null;
43
    }
44
45
    abstract public static function gatewayId(): string;
46
47
48
    public function testEnsureInstance()
49
    {
50
        $this->assertInstanceOf('\yiiviet\payment\BasePaymentGateway', $this->gateway);
51
        $this->assertTrue($this->gateway->sandbox);
52
    }
53
54
    /**
55
     * Populates Yii::$app with a new application
56
     * The application will be destroyed on tearDown() automatically.
57
     * @param array $config The application configuration, if needed
58
     * @param string $appClass name of the application class to create
59
     */
60
    protected function mockApplication($config = [], $appClass = '\yii\web\Application')
61
    {
62
        new $appClass(ArrayHelper::merge([
63
            'id' => 'testapp',
64
            'basePath' => __DIR__,
65
            'vendorPath' => dirname(__DIR__, 2) . '/vendor',
66
            'components' => [
67
                'cache' => 'yii\caching\DummyCache',
68
                'request' => [
69
                    'hostInfo' => 'http://domain.com',
70
                    'scriptUrl' => '/index.php'
71
                ],
72
                'paymentGateways' => [
73
                    'class' => 'yiiviet\payment\PaymentGatewayCollection',
74
                    'gatewayConfig' => [
75
                        'sandbox' => true
76
                    ],
77
                    'gateways' => [
78
                        'BK' => 'yiiviet\payment\baokim\PaymentGateway',
79
                        'NL' => 'yiiviet\payment\nganluong\PaymentGateway',
80
                        'OP' => 'yiiviet\payment\onepay\PaymentGateway',
81
                        'VNP' => 'yiiviet\payment\vnpayment\PaymentGateway'
82
                    ]
83
                ]
84
            ],
85
        ], $config));
86
    }
87
88
    /**
89
     * Destroys application in Yii::$app by setting it to null.
90
     */
91
    protected function destroyApplication()
92
    {
93
        Yii::$app = null;
94
    }
95
96
    /**
97
     * @param array $data
98
     * @param null $clientId
99
     * @return \vxm\gatewayclients\ResponseData
100
     */
101
    protected function purchase(array $data, $clientId = null)
102
    {
103
        return Yii::$app->get('paymentGateways')->purchase($data, static::gatewayId(), $clientId);
104
    }
105
106
    /**
107
     * @param array $data
108
     * @param null $clientId
109
     * @return \vxm\gatewayclients\ResponseData
110
     */
111
    protected function queryDR(array $data, $clientId = null)
112
    {
113
        return Yii::$app->get('paymentGateways')->queryDR($data, static::gatewayId(), $clientId);
114
    }
115
116
    /**
117
     * @param \yii\web\Request $request
118
     * @param null $clientId
119
     * @return \yiiviet\payment\VerifiedData
120
     */
121
    protected function verifyRequestPurchaseSuccess(\yii\web\Request $request = null, $clientId = null)
122
    {
123
        return Yii::$app->get('paymentGateways')->verifyRequestPurchaseSuccess(static::gatewayId(), $request, $clientId);
124
    }
125
126
    /**
127
     * @param \yii\web\Request $request
128
     * @param null $clientId
129
     * @return \yiiviet\payment\VerifiedData
130
     */
131
    protected function verifyRequestIPN(\yii\web\Request $request = null, $clientId = null)
132
    {
133
        return Yii::$app->get('paymentGateways')->verifyRequestIPN(static::gatewayId(), $request, $clientId);
134
    }
135
136
}
137