Completed
Push — master ( 1f0278...22c246 )
by
unknown
12s
created

TestCase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 60
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 30
nc 1
nop 0
dl 0
loc 60
rs 9.5555
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
namespace Moip\Tests;
4
5
use Moip\Auth\OAuth;
6
use Moip\Moip;
7
use Moip\Resource\Customer;
8
use Moip\Resource\Orders;
9
use PHPUnit_Framework_TestCase;
10
use Requests_Response;
11
12
/**
13
 * class TestCase.
14
 */
15
abstract class TestCase extends PHPUnit_Framework_TestCase
16
{
17
    /**
18
     * Variables representing the test modes. On MOCK mode no http request will be made.
19
     * In SANDBOX mode HTTP requests will be made to the Moip::SANDBOX_ENDPOINT, the authentication information
20
     * is retrieved from the MOIP_TOKEN and MOIP_KEY environment variables.
21
     */
22
    const MOCK = 'mock';
23
    const SANDBOX = 'sandbox';
24
25
    /**
26
     * Intance of \Moip\Moip.
27
     *
28
     * @var \Moip\Moip
29
     * */
30
    protected $moip;
31
32
    /**
33
     * @var string current format for dates.
34
     */
35
    protected $date_format = 'Y-m-d';
36
37
    /**
38
     * @var string date used for testing.
39
     */
40
    protected $date_string = '1989-06-01';
41
    //todo: add the ability to use the play(https://github.com/rodrigosaito/mockwebserver-player) files from the jada sdk
42
    //the two responses below were based on the moip Java sdk's test files (https://github.com/moip/moip-sdk-java/)
43
    /**
44
     * @var string response from the client moip API.
45
     */
46
    protected $body_client;
47
48
    /**
49
     * @var string response from the order moip API.
50
     */
51
    protected $body_order;
52
53
    /**
54
     * @var string response from moip API.
55
     */
56
    protected $body_cc_pay_pci;
57
58
    /**
59
     * @var string response from moip API.
60
     */
61
    protected $body_cc_pay_pci_store;
62
63
    /**
64
     * @var string response from moip API.
65
     */
66
    protected $body_cc_pay_pci_escrow;
67
68
    /**
69
     * @var string response from moip API.
70
     */
71
    protected $body_release_escrow;
72
73
    /**
74
     * @var string response from moip API.
75
     */
76
    protected $body_billet_pay;
77
78
    /**
79
     * @var string response from moip API.
80
     */
81
    protected $body_refund_full_bankaccount;
82
83
    /**
84
     * @var string response from moip API.
85
     */
86
    protected $body_refund_partial_bankaccount;
87
88
    /**
89
     * @var string response from moip API.
90
     */
91
    protected $body_notification_preference;
92
93
    /**
94
     * @var string response from moip API.
95
     */
96
    protected $body_moip_account;
97
98
    /**
99
     * @var string response from moip API.
100
     */
101
    protected $body_order_list;
102
103
    /**
104
     * @var string response from moip API.
105
     */
106
    protected $body_notification_list;
107
108
    /**
109
     * @var string holds the last generated customer ownId. In mock mode it'll be always the default, but it changes on sandbox mode.
110
     */
111
    protected $last_cus_id = 'meu_id_customer';
112
113
    /**
114
     * @var string same as `$last_cus_id` but for orders.
115
     *
116
     * @see $last_cus_id
117
     */
118
    protected $last_ord_id = 'meu_id_pedido';
119
    protected $sandbox_mock = self::MOCK;
120
121
    public function __construct()
122
    {
123
        parent::__construct();
124
125
        $this->body_client = $this->readJsonFile('jsons/customer/create');
126
127
        $this->body_order = $this->readJsonFile('jsons/order/create');
128
129
        $this->body_cc_pay_pci = $this->readJsonFile('jsons/payment/create_cc_pci');
130
131
        $this->body_cc_pay_pci_store = $this->readJsonFile('jsons/payment/create_cc_pci_store');
132
133
        $this->body_cc_pay_pci_escrow = $this->readJsonFile('jsons/payment/create_cc_pci_escrow');
134
135
        $this->body_release_escrow = $this->readJsonFile('jsons/escrow/release');
136
137
        $this->body_billet_pay = $this->readJsonFile('jsons/payment/create_billet');
138
139
        $this->body_billet_multipay = $this->readJsonFile('jsons/multipayment/create_billet');
0 ignored issues
show
Bug introduced by
The property body_billet_multipay does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
140
141
        $this->body_cc_multipay = $this->readJsonFile('jsons/multipayment/create_cc');
0 ignored issues
show
Bug introduced by
The property body_cc_multipay does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
142
143
        $this->body_refund_full_bankaccount = $this->readJsonFile('jsons/refund/full_bankaccount');
144
145
        $this->body_refund_partial_bankaccount = $this->readJsonFile('jsons/refund/partial_bankaccount');
146
147
        $this->body_notification_preference = $this->readJsonFile('jsons/notification/create');
148
149
        $this->body_moip_account = $this->readJsonFile('jsons/account/create');
150
151
        $this->body_order_list = $this->readJsonFile('jsons/order/get_list');
152
153
        $this->body_add_credit_card = $this->readJsonFile('jsons/customer/add_credit_card');
0 ignored issues
show
Bug introduced by
The property body_add_credit_card does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
154
155
        $this->body_list_webhook_no_filter = $this->readJsonFile('jsons/webhooks/get_no_filter');
0 ignored issues
show
Bug introduced by
The property body_list_webhook_no_filter does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
156
157
        $this->body_list_webhook_pagination = $this->readJsonFile('jsons/webhooks/get_pagination');
0 ignored issues
show
Bug introduced by
The property body_list_webhook_pagination does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
158
159
        $this->body_list_webhook_all_filters = $this->readJsonFile('jsons/webhooks/get_all_filters');
0 ignored issues
show
Bug introduced by
The property body_list_webhook_all_filters does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
160
161
        $this->body_notification_list = $this->readJsonFile('jsons/notification/list');
162
163
        $this->body_multiorder = $this->readJsonFile('jsons/multiorder/create');
0 ignored issues
show
Bug introduced by
The property body_multiorder does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
164
165
        $this->body_cc_delay_capture = $this->readJsonFile('jsons/payment/create_cc_delay_capture');
0 ignored issues
show
Bug introduced by
The property body_cc_delay_capture does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
166
167
        $this->body_capture_pay = $this->readJsonFile('jsons/payment/capture');
0 ignored issues
show
Bug introduced by
The property body_capture_pay does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
168
169
        $this->body_capture_multipay = $this->readJsonFile('jsons/multipayment/capture');
0 ignored issues
show
Bug introduced by
The property body_capture_multipay does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
170
171
        $this->body_cancel_pay = $this->readJsonFile('jsons/payment/cancel_pre_authorized');
0 ignored issues
show
Bug introduced by
The property body_cancel_pay does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
172
173
        $this->body_cancel_multipay = $this->readJsonFile('jsons/multipayment/cancel_pre_authorized');
0 ignored issues
show
Bug introduced by
The property body_cancel_multipay does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
174
175
        $this->body_get_pay = $this->readJsonFile('jsons/payment/get');
0 ignored issues
show
Bug introduced by
The property body_get_pay does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
176
177
        $this->body_get_multipay = $this->readJsonFile('jsons/multipayment/get');
0 ignored issues
show
Bug introduced by
The property body_get_multipay does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
178
179
        $this->body_keys = $this->readJsonFile('jsons/keys/get');
0 ignored issues
show
Bug introduced by
The property body_keys does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
180
    }
181
182
    /**
183
     * Sets up the fixture, for example, open a network connection.
184
     * This method is called before a test is executed.
185
     */
186
    public function setUp()
187
    {
188
        // check if we can run the request on sandbox
189
        $moip_access_token = getenv('MOIP_ACCESS_TOKEN');
190
191
        if ($moip_access_token) {
192
            $this->sandbox_mock = self::SANDBOX;
193
            $auth = new OAuth($moip_access_token);
194
        } else {
195
            $this->sandbox_mock = self::MOCK;
196
            $auth = $this->getMock('\Moip\Contracts\Authentication');
197
        }
198
        $this->moip = new Moip($auth, Moip::ENDPOINT_SANDBOX);
199
    }
200
201
    /**
202
     * Method to read JSON from a file.
203
     *
204
     * @param string $filename location of file
205
     */
206
    public function readJsonFile($filename)
207
    {
208
        return file_get_contents($filename.'.json', FILE_USE_INCLUDE_PATH);
209
    }
210
211
    /**
212
     * If in MOCK mode returns a mocked Requests_Sessesion if in SANDBOX mode, creates a new session.
213
     *
214
     * @param string $body        what the request will return
215
     * @param int    $status_code what http code the request will return
216
     */
217
    public function mockHttpSession($body, $status_code = 200)
218
    {
219
        if ($this->sandbox_mock == self::SANDBOX) {
220
            $this->moip->createNewSession();
221
222
            return;
223
        }
224
        $resp = new Requests_Response();
225
        $resp->body = $body;
226
        $resp->status_code = $status_code;
227
        $sess = $this->getMock('\Requests_Session');
228
        $sess->expects($this->once())->method('request')->willReturn($resp);
229
        $this->moip->setSession($sess);
230
    }
231
232
    /**
233
     * Creates a customer.
234
     *
235
     * @return Customer
236
     */
237
    public function createCustomer()
238
    {
239
        if ($this->sandbox_mock == self::SANDBOX) {
240
            $this->last_cus_id = uniqid('CUS-');
241
        } else {
242
            $this->last_cus_id = 'meu_id_sandbox';
243
        }
244
245
        $customer = $this->moip->customers()->setOwnId($this->last_cus_id)
246
            ->setBirthDate(\DateTime::createFromFormat($this->date_format, $this->date_string))
247
            ->setFullname('Jose Silva')
248
            ->setEmail('[email protected]')
249
            ->setTaxDocument('22222222222', 'CPF')
250
            ->setPhone(11, 66778899, 55)
251
            ->addAddress(Customer::ADDRESS_SHIPPING, 'Avenida Faria Lima', '2927', 'Itaim', 'Sao Paulo', 'SP', '01234000', '8');
252
253
        return $customer;
254
    }
255
256
    /**
257
     * Creates a account.
258
     *
259
     * @return Account
260
     */
261
    public function createAccount()
262
    {
263
        $moip = new Moip(new OAuth('1tldio91gi74r34zv30d4saz8yuuws5'), Moip::ENDPOINT_SANDBOX);
264
265
        $uniqEmail = 'fulano'.uniqid('MPA-').'@detal123.com.br';
266
267
        $account = $moip->accounts()
268
            ->setEmail($uniqEmail)
269
            ->setName('Fulano')
270
            ->setLastName('de Tal')
271
            ->setBirthDate('1987-11-27')
272
            ->setTaxDocument('22222222222')
273
            ->setPhone(11, 988888888)
274
            ->addAddress('Av. Ibirapuera', '2035', 'Moema', 'Sao Paulo', 'SP', '04078010')
275
            ->setIdentityDocument('411111115', 'SSP', '2000-05-06')
276
            ->create();
277
278
        return $account;
279
    }
280
281
    /**
282
     * Creates an order.
283
     *
284
     * @return Orders
285
     */
286
    public function createOrder()
287
    {
288 View Code Duplication
        if ($this->sandbox_mock == self::SANDBOX) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
289
            $this->last_ord_id = uniqid('ORD-');
290
        } else {
291
            $this->last_ord_id = 'meu_id_pedido';
292
        }
293
294
        $order = $this->moip->orders()->setCustomer($this->createCustomer())
295
            ->addItem('Nome do produto', 1, 'Mais info...', 100000)
296
            ->addItem('abacaxi', 2, 'Abacaxi de terra de areia', 990)
297
            ->setDiscount(1000)
298
            ->setShippingAmount(1490)
299
            ->setOwnId($this->last_ord_id);
300
301
        return $order;
302
    }
303
304
    /**
305
     * Creates a multiorder.
306
     *
307
     * @return Multiorders
308
     */
309
    public function createMultiorder()
310
    {
311 View Code Duplication
        if ($this->sandbox_mock == self::SANDBOX) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
312
            $this->last_ord_id = uniqid('MOR-');
313
        } else {
314
            $this->last_ord_id = 'meu_id_pedido';
315
        }
316
317
        $order = $this->moip->orders()->setOwnId(uniqid())
318
            ->addItem('bicicleta 1', 1, 'sku1', 10000)
319
            ->addItem('bicicleta 2', 1, 'sku2', 11000)
320
            ->addItem('bicicleta 3', 1, 'sku3', 12000)
321
            ->addItem('bicicleta 4', 1, 'sku4', 13000)
322
            ->setShippingAmount(3000)
323
            ->setAddition(1000)
324
            ->setDiscount(5000)
325
            ->setCustomer($this->createCustomer())
326
            ->addReceiver('MPA-VB5OGTVPCI52', 'PRIMARY', null);
327
328
        $order2 = $this->moip->orders()->setOwnId(uniqid())
329
            ->addItem('bicicleta 1', 1, 'sku1', 10000)
330
            ->addItem('bicicleta 2', 1, 'sku2', 11000)
331
            ->addItem('bicicleta 3', 1, 'sku3', 12000)
332
            ->setShippingAmount(3000)
333
            ->setAddition(1000)
334
            ->setDiscount(5000)
335
            ->setCustomer($this->createCustomer())
336
            ->addReceiver('MPA-IFYRB1HBL73Z', 'PRIMARY', null);
337
338
        $multiorder = $this->moip->multiorders()
339
                ->setOwnId(uniqid())
340
                ->addOrder($order)
341
                ->addOrder($order2);
342
343
        return $multiorder;
344
    }
345
346
    /**
347
     * Tears down the fixture, for example, close a network connection.
348
     * This method is called after a test is executed.
349
     */
350
    public function tearDown()
351
    {
352
        $this->moip = null;
353
    }
354
}
355