MoipTest::testToCents()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 17
nc 2
nop 0
1
<?php
2
3
namespace Moip\Tests;
4
5
use Moip\Exceptions;
6
use Moip\Helper\Utils;
7
use Moip\Moip;
8
use Requests_Exception;
9
10
/**
11
 * class MoipTest.
12
 */
13
class MoipTest extends TestCase
14
{
15
    /**
16
     * MoipTest 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
     * MoipTest 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
     * MoipTest 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
     * MoipTest 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
     * MoipTest 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
     * MoipTest 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
     * MoipTest 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
91
        try {
92
            $this->moip->customers()->setOwnId(uniqid())
93
                ->setFullname('Fulano teste')
94
                ->setEmail('[email protected]')
95
                ->setBirthDate('1111111')//invalid
96
                ->create();
97
        } catch (Exceptions\ValidationException $e) {
98
            $errors = $e->getErrors();
99
            $this->assertCount(1, $errors);
100
            $error = $errors[0];
101
            $this->assertEquals($error_model->code, $error->getCode(), 'getCode didn\'t returned the expected value');
102
            $this->assertEquals($error_model->path, $error->getPath(), 'getPath didn\'t returned the expected value');
103
104
            return;
105
        }
106
        $this->fail('Exception testShouldRaiseValidationException not thrown');
107
    }
108
109
    /**
110
     * MoipTest if \Moip\Exceptios\UnautorizedException is thrown.
111
     *
112
     * @expectedException Moip\Exceptions\UnautorizedException
113
     */
114 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...
115
    {
116
        if ($this->sandbox_mock == self::SANDBOX) {
117
            $this->markTestSkipped('Only testable in Mock mode');
118
119
            return;
120
        }
121
        $body = '{ "ERROR" : "Token or Key are invalids" }'; // the body is not processed in any way, i'm putting this for completeness
122
        $this->mockHttpSession($body, 401);
123
        $this->moip->orders()->get('ORD-1AWC30TWYZMX');
124
    }
125
126
    /**
127
     * MoipTest if UnexpectedException is thrown when 500 http status code is returned.
128
     *
129
     * @expectedException Moip\Exceptions\UnexpectedException
130
     */
131 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...
132
    {
133
        if ($this->sandbox_mock == self::SANDBOX) {
134
            $this->markTestSkipped('Only testable in Mock mode');
135
136
            return;
137
        }
138
        $this->mockHttpSession('error', 500); // the body isn't processed
139
        $this->moip->orders()->get('ORD-1AWC30TWYZMX');
140
    }
141
142
    /**
143
     * MoipTest if UnexpectedException is thrown when a Requests_Exception is thrown.
144
     */
145
    public function testShouldRaiseUnexpectedExceptionNetworkError()
146
    {
147
        if ($this->sandbox_mock == self::SANDBOX) {
148
            $this->markTestSkipped('Only testable in Mock mode');
149
150
            return;
151
        }
152
        $sess = $this->getMockBuilder('\Requests_Session')->getMock();
153
        $sess->expects($this->once())->method('request')->willThrowException(new Requests_Exception('test error',
154
            'test'));
155
        $this->moip->setSession($sess);
0 ignored issues
show
Documentation introduced by
$sess is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Requests_Session>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
156
157
        try {
158
            $this->moip->orders()->get('ORD-1AWC30TWYZMX');
159
        } catch (Exceptions\UnexpectedException $e) {
160
            // test exception chaining
161
            $this->assertInstanceOf('Requests_Exception', $e->getPrevious());
162
163
            return;
164
        }
165
        $this->fail('Exception was not thrown');
166
    }
167
168
    /**
169
     * MoipTest if we can connect to the API endpoints.
170
     * This is primarily to make user we are using HTTPS urls and the certification verification is ok.
171
     */
172
    public function testConnectEndPoints()
173
    {
174
        // create a valid session
175
        $this->moip->createNewSession();
176
        $sess = $this->moip->getSession();
177
        $requests = [['url' => Moip::ENDPOINT_PRODUCTION], ['url' => Moip::ENDPOINT_SANDBOX]];
178
        $resps = $sess->request_multiple($requests);
179
        $this->assertEquals('WELCOME', $resps[0]->body);
180
        $this->assertEquals('WELCOME', $resps[1]->body);
181
    }
182
183
    /**
184
     * MoipTest the convertion from money to cents using floats.
185
     */
186
    public function testToCents()
187
    {
188
        $cases = [
189
            [6.9, 690],
190
            [6.99, 699],
191
            [10.32, 1032],
192
            [10.329, 1032],
193
            [10.93, 1093],
194
            [10.931, 1093],
195
            [10.01, 1001],
196
            [10.09, 1009],
197
            [.1, 10],
198
            [.01, 1],
199
            [9.999, 999],
200
        ];
201
202
        foreach ($cases as $case) {
203
            list($actual, $expected) = $case;
204
            $actual = Utils::toCents($actual);
205
206
            $this->assertEquals($expected, $actual);
207
        }
208
    }
209
210
    public function testShouldGetEndpoint()
211
    {
212
        $expected = constant('\Moip\Moip::ENDPOINT_SANDBOX');
213
214
        $this->assertEquals($expected, $this->moip->getEndpoint());
215
    }
216
}
217