Completed
Push — master ( dc1094...5b5a29 )
by Jean C.
9s
created

MoipTest::testShouldRaiseUnautorizedException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 11
Ratio 91.67 %

Importance

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