Completed
Push — master ( dba3c8...01cd01 )
by Jean C.
07:48 queued 01:45
created

TestCase::mockHttpSession()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 2
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Moip\Tests;
4
5
use Moip\Auth\BasicAuth;
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
42
    //todo: add the ability to use the play(https://github.com/rodrigosaito/mockwebserver-player) files from the jada sdk
43
    //the two responses below were based on the moip Java sdk's test files (https://github.com/moip/moip-sdk-java/)
44
    /**
45
     * @var string response from the client moip API.
46
     */
47
    protected $body_client = '{"id":"CUS-CFMKXQBZNJQQ","ownId":"meu_id_sandbox","fullname":"Jose Silva","email":"[email protected]","phone":{"countryCode":"55","areaCode":"11","number":"66778899"},"birthDate":"1989-06-01","taxDocument":{"type":"CPF","number":"22222222222"},"shippingAddress":{"street":"Avenida Faria Lima","streetNumber":"2927","complement":"8","city":"Sao Paulo","state":"SP","country":"BRA","zipCode":"01234000"},"fundingInstruments":[],"createdAt":"2016-02-18T19:55:00.000-02","_links":{"self":{"href":"https://sandbox.moip.com.br/v2/customers/CUS-CFMKXQBZNJQQ"}}}';
48
49
    /**
50
     * @var string response from the order moip API.
51
     */
52
    protected $body_order = '{"id":"ORD-HG479ZEIB7LV","ownId":"meu_id_pedido","status":"CREATED","createdAt":"2016-02-19T12:24:55.849-02","updatedAt":"2016-02-19T12:24:55.849-02","amount":{"total":102470,"fees":0,"refunds":0,"liquid":0,"otherReceivers":0,"currency":"BRL","subtotals":{"shipping":1490,"addition":0,"discount":1000,"items":101980}},"items":[{"price":100000,"detail":"Mais info...","quantity":1,"product":"Nome do produto"},{"price":990,"detail":"Abacaxi de terra de areia","quantity":2,"product":"abacaxi"}],"customer":{"id":"CUS-7U5K9KWG8DBZ","ownId":"meu_id_saasdadadsnasdasddboxssssssssss","fullname":"Jose Silva","createdAt":"2016-02-18T20:03:28.000-02","birthDate":"1989-06-01T00:00:00.000-03","email":"[email protected]","phone":{"countryCode":"55","areaCode":"11","number":"66778899"},"taxDocument":{"type":"CPF","number":"22222222222"},"shippingAddress":{"zipCode":"01234000","street":"Avenida Faria Lima","streetNumber":"2927","complement":"8","city":"Sao Paulo","district":"Itaim","state":"SP","country":"BRA"},"_links":{"self":{"href":"https://sandbox.moip.com.br/v2/customers/CUS-7U5K9KWG8DBZ"}}},"payments":[],"refunds":[],"entries":[],"events":[{"type":"ORDER.CREATED","createdAt":"2016-02-19T12:24:55.849-02","description":""}],"receivers":[{"moipAccount":{"id":"MPA-7ED9D2D0BC81","login":"[email protected]","fullname":"Carmen Elisabete de Menezes ME"},"type":"PRIMARY","amount":{"total":102470,"fees":0,"refunds":0}}],"shippingAddress":{"zipCode":"01234000","street":"Avenida Faria Lima","streetNumber":"2927","complement":"8","city":"Sao Paulo","district":"Itaim","state":"SP","country":"BRA"},"_links":{"self":{"href":"https://sandbox.moip.com.br/v2/orders/ORD-HG479ZEIB7LV"},"checkout":{"payOnlineBankDebitItau":{"redirectHref":"https://checkout-sandbox.moip.com.br/debit/itau/ORD-HG479ZEIB7LV"},"payCreditCard":{"redirectHref":"https://checkout-sandbox.moip.com.br/creditcard/ORD-HG479ZEIB7LV"},"payBoleto":{"redirectHref":"https://checkout-sandbox.moip.com.br/boleto/ORD-HG479ZEIB7LV"}}}}';
53
54
    /**
55
     * @var string response from moip API.
56
     */
57
    protected $body_cc_pay_pci = '{"id":"PAY-L6J2NKS9OGYU","status":"IN_ANALYSIS","delayCapture":false,"amount":{"total":102470,"fees":5695,"refunds":0,"liquid":96775,"currency":"BRL"},"installmentCount":1,"fundingInstrument":{"creditCard":{"id":"CRC-2TJ13YB4Y1WU","brand":"MASTERCARD","first6":"555566","last4":"8884","holder":{"birthdate":"1989-06-01","birthDate":"1989-06-01","taxDocument":{"type":"CPF","number":"22222222222"},"fullname":"Jose Silva"}},"method":"CREDIT_CARD"},"fees":[{"type":"TRANSACTION","amount":5695}],"events":[{"type":"PAYMENT.IN_ANALYSIS","createdAt":"2016-02-19T18:18:54.535-02"},{"type":"PAYMENT.CREATED","createdAt":"2016-02-19T18:18:51.946-02"}],"_links":{"order":{"href":"https://sandbox.moip.com.br/v2/orders/ORD-8UDL4K9VRJTB","title":"ORD-8UDL4K9VRJTB"},"self":{"href":"https://sandbox.moip.com.br/v2/payments/PAY-L6J2NKS9OGYU"}},"createdAt":"2016-02-19T18:18:51.944-02","updatedAt":"2016-02-19T18:18:54.535-02"}';
58
59
    /**
60
     * @var string response from moip API.
61
     */
62
    protected $body_billet_pay = '{"id":"PAY-XNVIBO5MIQ9S","status":"WAITING","delayCapture":false,"amount":{"total":102470,"fees":3645,"refunds":0,"liquid":98825,"currency":"BRL"},"installmentCount":1,"fundingInstrument":{"boleto":{"expirationDate":"2016-05-21","lineCode":"23793.39126 60000.062608 32001.747909 7 68010000102470"},"method":"BOLETO"},"fees":[{"type":"TRANSACTION","amount":3645}],"events":[{"type":"PAYMENT.CREATED","createdAt":"2016-05-20T15:19:47.000-03"},{"type":"PAYMENT.WAITING","createdAt":"2016-05-20T15:19:47.000-03"}],"_links":{"order":{"href":"https://sandbox.moip.com.br/v2/orders/ORD-3KSQDBJSTIF6","title":"ORD-3KSQDBJSTIF6"},"payBoleto":{"redirectHref":"https://checkout-sandbox.moip.com.br/boleto/PAY-XNVIBO5MIQ9S"},"self":{"href":"https://sandbox.moip.com.br/v2/payments/PAY-XNVIBO5MIQ9S"}},"updatedAt":"2016-05-20T15:19:47.000-03","createdAt":"2016-05-20T15:19:47.000-03"}';
63
64
    /**
65
     * @var string holds the last generated customer ownId. In mock mode it'll be always the default, but it changes on sandbox mode.
66
     */
67
    protected $last_cus_id = 'meu_id_customer';
68
69
    /**
70
     * @var string same as `$last_cus_id` but for orders.
71
     *
72
     * @see $last_cus_id
73
     */
74
    protected $last_ord_id = 'meu_id_pedido';
75
76
    protected $sandbox_mock = self::MOCK;
77
78
    /**
79
     * Sets up the fixture, for example, open a network connection.
80
     * This method is called before a test is executed.
81
     */
82
    public function setUp()
83
    {
84
        // check if we can run the request on sandbox
85
        $moip_key = getenv('MOIP_KEY');
86
        $moip_token = getenv('MOIP_TOKEN');
87
88
        if ($moip_key && $moip_token) {
89
            $this->sandbox_mock = self::SANDBOX;
90
            $auth = new BasicAuth($moip_token, $moip_key);
91
        } else {
92
            $this->sandbox_mock = self::MOCK;
93
            $auth = $this->getMock('\Moip\Contracts\Authentication');
94
        }
95
        $this->moip = new Moip($auth, Moip::ENDPOINT_SANDBOX);
96
    }
97
98
    /**
99
     * If in MOCK mode returns a mocked Requests_Sessesion if in SANDBOX mode, creates a new session.
100
     *
101
     * @param string $body        what the request will return
102
     * @param int    $status_code what http code the request will return
103
     */
104
    public function mockHttpSession($body, $status_code = 200)
105
    {
106
        if ($this->sandbox_mock == self::SANDBOX) {
107
            $this->moip->createNewSession();
108
109
            return;
110
        }
111
        $resp = new Requests_Response();
112
        $resp->body = $body;
113
        $resp->status_code = $status_code;
114
        $sess = $this->getMock('\Requests_Session');
115
        $sess->expects($this->once())->method('request')->willReturn($resp);
116
        $this->moip->setSession($sess);
117
    }
118
119
    /**
120
     * Creates a customer.
121
     *
122
     * @return Customer
123
     */
124
    public function createCustomer()
125
    {
126
        if ($this->sandbox_mock == self::SANDBOX) {
127
            $this->last_cus_id = uniqid('CUS-');
128
        } else {
129
            $this->last_cus_id = 'meu_id_sandbox';
130
        }
131
132
        $customer = $this->moip->customers()->setOwnId($this->last_cus_id)
133
            ->setBirthDate(\DateTime::createFromFormat($this->date_format, $this->date_string))
134
            ->setFullname('Jose Silva')
135
            ->setEmail('[email protected]')
136
            ->setTaxDocument('22222222222', 'CPF')
137
            ->setPhone(11, 66778899, 55)
138
            ->addAddress(Customer::ADDRESS_SHIPPING, 'Avenida Faria Lima', '2927', 'Itaim', 'Sao Paulo',
139
                'SP', '01234000', '8');
140
141
        return $customer;
142
    }
143
144
    /**
145
     * Creates a account.
146
     *
147
     * @return Account
148
     */
149
    public function createAccount()
150
    {
151
        $moip = new Moip(new MoipOAuth('1tldio91gi74r34zv30d4saz8yuuws5'), Moip::ENDPOINT_SANDBOX);
152
153
        $uniqEmail = 'fulano'.uniqid('MPA-').'@detal123.com.br';
154
155
        $account = $moip->accounts()
156
            ->setEmail($uniqEmail)
157
            ->setName('Fulano')
158
            ->setLastName('de Tal')
159
            ->setBirthDate('1987-11-27')
160
            ->setTaxDocument('22222222222')
161
            ->setPhone(11, 988888888)
162
            ->addAddress('Av. Ibirapuera', '2035', 'Moema', 'Sao Paulo', 'SP', '04078010')
163
            ->setIdentityDocument('411111115', 'SSP', '2000-05-06')
164
            ->create();
165
166
        return $account;
167
    }
168
169
    /**
170
     * Creates an order.
171
     *
172
     * @return Orders
173
     */
174
    public function createOrder()
175
    {
176
        if ($this->sandbox_mock == self::SANDBOX) {
177
            $this->last_ord_id = uniqid('ORD-');
178
        } else {
179
            $this->last_ord_id = 'meu_id_pedido';
180
        }
181
182
        $order = $this->moip->orders()->setCustomer($this->createCustomer())
183
            ->addItem('Nome do produto', 1, 'Mais info...', 100000)
184
            ->addItem('abacaxi', 2, 'Abacaxi de terra de areia', 990)
185
            ->setDiscount(1000)
186
            ->setShippingAmount(1490)
187
            ->setOwnId($this->last_ord_id);
188
189
        return $order;
190
    }
191
192
    /**
193
     * Tears down the fixture, for example, close a network connection.
194
     * This method is called after a test is executed.
195
     */
196
    public function tearDown()
197
    {
198
        $this->moip = null;
199
    }
200
}
201