TestCase::destroyApplication()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
            'bootstrap' => ['yiiviet\payment\Bootstrap'],
67
            'components' => [
68
                'cache' => 'yii\caching\DummyCache',
69
                'request' => [
70
                    'hostInfo' => 'http://domain.com',
71
                    'scriptUrl' => '/index.php'
72
                ],
73
                'paymentGateways' => [
74
                    'class' => 'yiiviet\payment\PaymentGatewayCollection',
75
                    'gatewayConfig' => [
76
                        'sandbox' => true
77
                    ],
78
                    'gateways' => [
79
                        'BK' => 'yiiviet\payment\baokim\PaymentGateway',
80
                        'NL' => 'yiiviet\payment\nganluong\PaymentGateway',
81
                        'OP' => 'yiiviet\payment\onepay\PaymentGateway',
82
                        'VNP' => 'yiiviet\payment\vnpayment\PaymentGateway',
83
                        'VTC' => 'yiiviet\payment\vtcpay\PaymentGateway',
84
                        'MM' => 'yiiviet\payment\momo\PaymentGateway'
85
                    ]
86
                ]
87
            ],
88
        ], $config));
89
    }
90
91
    /**
92
     * Destroys application in Yii::$app by setting it to null.
93
     */
94
    protected function destroyApplication()
95
    {
96
        Yii::$app = null;
97
    }
98
99
    /**
100
     * @param array $data
101
     * @param null $clientId
102
     * @return \vxm\gatewayclients\ResponseData
103
     */
104
    protected function purchase(array $data, $clientId = null)
105
    {
106
        return Yii::$app->get('paymentGateways')->purchase($data, static::gatewayId(), $clientId);
107
    }
108
109
    /**
110
     * @param array $data
111
     * @param null $clientId
112
     * @return \vxm\gatewayclients\ResponseData
113
     */
114
    protected function queryDR(array $data, $clientId = null)
115
    {
116
        return Yii::$app->get('paymentGateways')->queryDR($data, static::gatewayId(), $clientId);
117
    }
118
119
    /**
120
     * @param array $data
121
     * @param null $clientId
122
     * @return \vxm\gatewayclients\ResponseData
123
     */
124
    protected function refund(array $data, $clientId = null)
125
    {
126
        return Yii::$app->get('paymentGateways')->refund($data, static::gatewayId(), $clientId);
127
    }
128
129
    /**
130
     * @param array $data
131
     * @param null $clientId
132
     * @return \vxm\gatewayclients\ResponseData
133
     */
134
    protected function queryRefund(array $data, $clientId = null)
135
    {
136
        return Yii::$app->get('paymentGateways')->queryRefund($data, static::gatewayId(), $clientId);
137
    }
138
139
    /**
140
     * @param \yii\web\Request $request
141
     * @param null $clientId
142
     * @return \yiiviet\payment\VerifiedData
143
     */
144
    protected function verifyRequestPurchaseSuccess(\yii\web\Request $request = null, $clientId = null)
145
    {
146
        return Yii::$app->get('paymentGateways')->verifyRequestPurchaseSuccess(static::gatewayId(), $request, $clientId);
147
    }
148
149
    /**
150
     * @param \yii\web\Request $request
151
     * @param null $clientId
152
     * @return \yiiviet\payment\VerifiedData
153
     */
154
    protected function verifyRequestIPN(\yii\web\Request $request = null, $clientId = null)
155
    {
156
        return Yii::$app->get('paymentGateways')->verifyRequestIPN(static::gatewayId(), $request, $clientId);
157
    }
158
159
}
160