Completed
Push — master ( 0745a7...5ee26e )
by Jean C.
02:38
created

MoipTest::testLinks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace Moip\Tests;
4
5
use Moip\Exceptions;
6
use Moip\Moip;
7
use Moip\Utils;
8
use Requests_Exception;
9
10
/**
11
 * class MoipTest.
12
 */
13
class MoipTest extends MoipTestCase
14
{
15
    /**
16
     * Test should return instance of \Moip\Resource\Customer.
17
     */
18
    public function testShouldReceiveInstanceOfCustomer()
19
    {
20
        $customer = new \Moip\Resource\Customer($this->moip);
21
22
        $this->assertEquals($customer, $this->moip->customers());
23
    }
24
25
    /**
26
     * Test should return instance of \Moip\Resource\Account.
27
     */
28
    public function testShouldReceiveInstanceOfAccount()
29
    {
30
        $account = new \Moip\Resource\Account($this->moip);
31
32
        $this->assertEquals($account, $this->moip->accounts());
33
    }
34
35
    /**
36
     * Test should return instance of \Moip\Resource\Entry.
37
     */
38
    public function testShouldReceiveInstanceOfEntry()
39
    {
40
        $entry = new \Moip\Resource\Entry($this->moip);
41
42
        $this->assertEquals($entry, $this->moip->entries());
43
    }
44
45
    /**
46
     * Test should return instance of \Moip\Resource\Orders.
47
     */
48
    public function testShouldReceiveInstanceOfOrders()
49
    {
50
        $orders = new \Moip\Resource\Orders($this->moip);
51
52
        $this->assertEquals($orders, $this->moip->orders());
53
    }
54
55
    /**
56
     * Test should return instance of \Moip\Resource\Payment.
57
     */
58
    public function testShouldReceiveInstanceOfPayment()
59
    {
60
        $payment = new \Moip\Resource\Payment($this->moip);
61
62
        $this->assertEquals($payment, $this->moip->payments());
63
    }
64
65
    /**
66
     * Test should return instance of \Moip\Resource\Multiorders.
67
     */
68
    public function testShouldReceiveInstanceOfMultiorders()
69
    {
70
        $multiorders = new \Moip\Resource\Multiorders($this->moip);
71
72
        $this->assertEquals($multiorders, $this->moip->multiorders());
73
    }
74
75
    /**
76
     * Test if a \Moip\Exceptions\testShouldRaiseValidationException is thrown and is correctly constructed.
77
     */
78
    public function testShouldRaiseValidationException()
79
    {
80
        /*
81
         * WARNING FIXME:
82
         * The api has a bug right now it's return 'birthdateMatchesPattern' but thats wrong, it's supposed to return
83
         * customer.birthDate. I talked to the moip support team, they're aware of the bug and WILL be fixing this bug
84
         * wich means this test will eventually fail.
85
         */
86
        $body = '{"errors":[{"code":"CUS-007","path":"birthdateMatchesPattern","description":"O valor deve ser uma string"}]}';
87
        $model = json_decode($body);
88
        $error_model = $model->errors[0];
89
        $this->mockHttpSession($body, 400);
90
        try {
91
            $this->moip->customers()->setOwnId(uniqid())
92
                ->setFullname('Fulano teste')
93
                ->setEmail('[email protected]')
94
                ->setBirthDate('1111111')//invalid
95
                ->create();
96
        } catch (Exceptions\ValidationException $e) {
97
            $errors = $e->getErrors();
98
            $this->assertCount(1, $errors);
99
            $error = $errors[0];
100
            $this->assertEquals($error_model->code, $error->getCode(), 'getCode didn\'t returned the expected value');
101
            $this->assertEquals($error_model->path, $error->getPath(), 'getPath didn\'t returned the expected value');
102
103
            return;
104
        }
105
        $this->fail('Exception testShouldRaiseValidationException not thrown');
106
    }
107
108
    /**
109
     * Test if \Moip\Exceptios\UnautorizedException is thrown.
110
     */
111 View Code Duplication
    public function testShouldRaiseUnautorizedException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
112
    {
113
        if ($this->sandbox_mock == self::SANDBOX) {
114
            $this->markTestSkipped('Only testable in Mock mode');
115
116
            return;
117
        }
118
        $this->setExpectedException('\Moip\Exceptions\UnautorizedException');
119
        $body = '{ "ERROR" : "Token or Key are invalids" }'; // the body is not processed in any way, i'm putting this for completeness
120
        $this->mockHttpSession($body, 401);
121
        $this->moip->orders()->get('ORD-1AWC30TWYZMX');
122
    }
123
124
    /**
125
     * Test if UnexpectedException is thrown when 500 http status code is returned.
126
     */
127 View Code Duplication
    public function testShouldRaiseUnexpectedException500()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
128
    {
129
        if ($this->sandbox_mock == self::SANDBOX) {
130
            $this->markTestSkipped('Only testable in Mock mode');
131
132
            return;
133
        }
134
        $this->setExpectedException('\Moip\Exceptions\UnexpectedException');
135
        $this->mockHttpSession('error', 500); // the body isn't processed
136
        $this->moip->orders()->get('ORD-1AWC30TWYZMX');
137
    }
138
139
    /**
140
     * Test if UnexpectedException is thrown when a Requests_Exception is thrown.
141
     */
142
    public function testShouldRaiseUnexpectedExceptionNetworkError()
143
    {
144
        if ($this->sandbox_mock == self::SANDBOX) {
145
            $this->markTestSkipped('Only testable in Mock mode');
146
147
            return;
148
        }
149
        $sess = $this->getMock('\Requests_Session');
150
        $sess->expects($this->once())->method('request')->willThrowException(new Requests_Exception('test error',
151
            'test'));
152
        $this->moip->setSession($sess);
153
        try {
154
            $this->moip->orders()->get('ORD-1AWC30TWYZMX');
155
        } catch (Exceptions\UnexpectedException $e) {
156
            // test exception chaining
157
            $this->assertInstanceOf('Requests_Exception', $e->getPrevious());
158
159
            return;
160
        }
161
        $this->fail('Exception was not thrown');
162
    }
163
164
    /**
165
     * Test if we can connect to the API endpoints.
166
     * This is primarily to make user we are using HTTPS urls and the certification verification is ok.
167
     */
168
    public function testConnectEndPoints()
169
    {
170
        // create a valid session
171
        $this->moip->createNewSession();
172
        $sess = $this->moip->getSession();
173
        $requests = [['url' => Moip::ENDPOINT_PRODUCTION], ['url' => Moip::ENDPOINT_SANDBOX]];
174
        $resps = $sess->request_multiple($requests);
175
        $this->assertEquals('WELCOME', $resps[0]->body);
176
        $this->assertEquals('WELCOME', $resps[1]->body);
177
    }
178
179
    /**
180
     * Test the convertion from money to cents using floats.
181
     */
182
    public function testToCents()
183
    {
184
        $cases = [
185
            [6.9, 690],
186
            [6.99, 699],
187
            [10.32, 1032],
188
            [10.329, 1032],
189
            [10.93, 1093],
190
            [10.931, 1093],
191
            [10.01, 1001],
192
            [10.09, 1009],
193
            [.1, 10],
194
            [.01, 1],
195
            [9.999, 999],
196
        ];
197
198
        foreach ($cases as $case) {
199
            list($actual, $expected) = $case;
200
            $actual = Utils::toCents($actual);
201
202
            $this->assertEquals($expected, $actual);
203
        }
204
    }
205
206
    public function testShouldGetEndpoint()
207
    {
208
        $expected = constant('\Moip\Moip::ENDPOINT_SANDBOX');
209
210
        $this->assertEquals($expected, $this->moip->getEndpoint());
211
    }
212
}
213