Completed
Push — master ( 2e7298...3749b5 )
by
unknown
11s
created

MoipTestCase::mockHttpSession()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 14
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
3
namespace Moip\Tests;
4
5
use Moip\Moip;
6
use Moip\MoipBasicAuth;
7
use Moip\Resource\Customer;
8
use Moip\Resource\Orders;
9
use PHPUnit_Framework_TestCase as TestCase;
10
use Requests_Response;
11
12
/**
13
 * class MoipTestCase.
14
 */
15
abstract class MoipTestCase extends 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 holds the last generated customer ownId. In mock mode it'll be always the default, but it changes on sandbox mode.
61
     */
62
    protected $last_cus_id = 'meu_id_customer';
63
64
    /**
65
     * @var string same as `$last_cus_id` but for orders.
66
     *
67
     * @see $last_cus_id
68
     */
69
    protected $last_ord_id = 'meu_id_pedido';
70
71
    protected $sandbox_mock = self::MOCK;
72
73
    /**
74
     * Sets up the fixture, for example, open a network connection.
75
     * This method is called before a test is executed.
76
     */
77
    public function setUp()
78
    {
79
        // check if we can run the request on sandbox
80
        $moip_key = getenv('MOIP_KEY');
81
        $moip_token = getenv('MOIP_TOKEN');
82
83
        if ($moip_key && $moip_token) {
84
            $this->sandbox_mock = self::SANDBOX;
85
            $auth = new MoipBasicAuth($moip_token, $moip_key);
86
        } else {
87
            $this->sandbox_mock = self::MOCK;
88
            $auth = $this->getMock('\Moip\MoipAuthentication');
89
        }
90
        $this->moip = new Moip($auth, Moip::ENDPOINT_SANDBOX);
91
    }
92
93
    /**
94
     * If in MOCK mode returns a mocked Requests_Sessesion if in SANDBOX mode, creates a new session.
95
     *
96
     * @param string $body        what the request will return
97
     * @param int    $status_code what http code the request will return
98
     */
99
    public function mockHttpSession($body, $status_code = 200)
100
    {
101
        if ($this->sandbox_mock == self::SANDBOX) {
102
            $this->moip->createNewSession();
103
104
            return;
105
        }
106
        $resp = new Requests_Response();
107
        $resp->body = $body;
108
        $resp->status_code = $status_code;
109
        $sess = $this->getMock('\Requests_Session');
110
        $sess->expects($this->once())->method('request')->willReturn($resp);
111
        $this->moip->setSession($sess);
112
    }
113
114
    /**
115
     * Creates a customer.
116
     *
117
     * @return Customer
118
     */
119
    public function createCustomer()
120
    {
121
        if ($this->sandbox_mock == self::SANDBOX) {
122
            $this->last_cus_id = uniqid('CUS-');
123
        } else {
124
            $this->last_cus_id = 'meu_id_sandbox';
125
        }
126
127
        $customer = $this->moip->customers()->setOwnId($this->last_cus_id)
128
            ->setBirthDate(\DateTime::createFromFormat($this->date_format, $this->date_string))
1 ignored issue
show
Security Bug introduced by
It seems like \DateTime::createFromFor...at, $this->date_string) targeting DateTime::createFromFormat() can also be of type false; however, Moip\Resource\Customer::setBirthDate() does only seem to accept object<DateTime>|string, did you maybe forget to handle an error condition?
Loading history...
129
            ->setFullname('Jose Silva')
130
            ->setEmail('[email protected]')
131
            ->setTaxDocument('22222222222', 'CPF')
132
            ->setPhone(11, 66778899, 55)
133
            ->addAddress(Customer::ADDRESS_SHIPPING, 'Avenida Faria Lima', '2927', 'Itaim', 'Sao Paulo',
134
                'SP', '01234000', '8');
135
136
        return $customer;
137
    }
138
139
    /**
140
     * Creates an order.
141
     *
142
     * @return Orders
143
     */
144
    public function createOrder()
145
    {
146
        if ($this->sandbox_mock == self::SANDBOX) {
147
            $this->last_ord_id = uniqid('ORD-');
148
        } else {
149
            $this->last_ord_id = 'meu_id_pedido';
150
        }
151
152
        $order = $this->moip->orders()->setCustomer($this->createCustomer())
153
            ->addItem('Nome do produto', 1, 'Mais info...', 100000)
154
            ->addItem('abacaxi', 2, 'Abacaxi de terra de areia', 990)
155
            ->setDiscount(1000)
156
            ->setShippingAmount(1490)
157
            ->setOwnId($this->last_ord_id);
158
159
        return $order;
160
    }
161
162
    /**
163
     * Tears down the fixture, for example, close a network connection.
164
     * This method is called after a test is executed.
165
     */
166
    public function tearDown()
167
    {
168
        $this->moip = null;
169
    }
170
}
171