Completed
Push — master ( 4fb3b2...b5a0c8 )
by Jean C.
10s
created

testCreateOrderAddingReceiverAmountFixed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace Moip\Tests\Resource;
4
5
use Moip\Resource\Orders;
6
use Moip\Tests\TestCase;
7
8
class OrdersTest extends TestCase
9
{
10
    /**
11
     * Send http request.
12
     *
13
     * @param \Moip\Resource\Orders $order
14
     * @param string                $body
15
     *
16
     * @return \Moip\Resource\Orders
17
     */
18
    private function executeOrder(Orders $order = null, $body = null)
19
    {
20
        if (empty($body)) {
21
            $body = $this->body_order;
22
        }
23
        if (empty($order)) {
24
            $order = $this->createOrder();
25
        }
26
        $this->mockHttpSession($body);
27
28
        return $order->create();
0 ignored issues
show
Bug Compatibility introduced by
The expression $order->create(); of type Moip\Resource\Orders|stdClass adds the type stdClass to the return on line 28 which is incompatible with the return type documented by Moip\Tests\Resource\OrdersTest::executeOrder of type Moip\Resource\Orders.
Loading history...
29
    }
30
31
    /**
32
     * @const string
33
     */
34
    public function testAssertConstPath()
35
    {
36
        $this->assertEquals('orders', Orders::PATH);
37
    }
38
39
    /**
40
     * Defines what kind of payee as pripmary.
41
     *
42
     * @const string
43
     */
44
    public function testAssertConstReceiverTypePrimary()
45
    {
46
        $this->assertEquals('PRIMARY', Orders::RECEIVER_TYPE_PRIMARY);
47
    }
48
49
    /**
50
     * Defines what kind of payee as secundary.
51
     *
52
     * @const string
53
     */
54
    public function testAssertConstReceiverTypeSecpndary()
55
    {
56
        $this->assertEquals('SECONDARY', Orders::RECEIVER_TYPE_SECONDARY);
57
    }
58
59
    /**
60
     * Currency used in the application.
61
     *
62
     * @const string
63
     */
64
    public function testAssertConstAmountCurrency()
65
    {
66
        $this->assertEquals('BRL', Orders::AMOUNT_CURRENCY);
67
    }
68
69
    /**
70
     * MoipTest creating an order.
71
     */
72
    public function testCreateOrder()
73
    {
74
        $order_created = $this->executeOrder();
75
76
        $this->assertEquals($this->last_ord_id, $order_created->getOwnId());
77
        $this->assertEquals('CREATED', $order_created->getStatus());
78
    }
79
80
    /**
81
     * Teste if created itens price is correct.
82
     */
83
    public function testItens()
84
    {
85
        $order_created = $this->executeOrder();
86
        $itens = $order_created->getItemIterator()->getArrayCopy();
87
        $this->assertEquals(100000, $itens[0]->price);
88
        $this->assertEquals(990, $itens[1]->price);
89
    }
90
91
    /**
92
     *MoipTest if the total is correct.
93
     */
94
    public function testTotal()
95
    {
96
        $order = $this->executeOrder();
97
98
        $total = $order->getSubtotalItems() + $order->getSubtotalShipping() + $order->getSubtotalAddition() - $order->getSubtotalDiscount();
99
        $this->assertEquals($total, $order->getAmountTotal());
100
    }
101
102
    /**
103
     * MoipTest if the total is equal to the expected total.
104
     */
105
    public function testTotalConstant()
106
    {
107
        $order = $this->executeOrder();
108
        $expected = (100000 + 2 * 990 + 1490) - 1000;
109
        $total_calculated = $order->getSubtotalItems() + $order->getSubtotalShipping() + $order->getSubtotalAddition() - $order->getSubtotalDiscount();
110
111
        $this->assertEquals($expected, $total_calculated);
112
        $this->assertEquals($expected, $order->getAmountTotal());
113
    }
114
115
    /**
116
     * MoipTest if order is created with installment preferences.
117
     */
118
    public function testCreateOrderWithInstallmentPreferences() 
119
    {
120
        $quantity = [1,6];
121
        $discount = 0;
122
        $additional = 100;
123
        $order = $this->createOrder()->setInstallmentCheckoutPreferences($quantity, $discount, $additional);
124
        $returned_order = $this->executeOrder($order);
125
126
        $this->assertNotEmpty($returned_order->getId());
127
        $this->assertEquals([1,6],$returned_order->getCheckoutPreferences()->installments->quantity);
128
    }
129
130
    public function testCreateOrderAddingReceiverNoAmount() 
131
    {
132
        $order = $this->createOrder()->addReceiver('MPA-7ED9D2D0BC81', 'PRIMARY');
133
        $returned_order = $this->executeOrder($order);
134
        $this->assertNotEmpty($returned_order->getId());
135
        $receivers = $returned_order->getReceiverIterator();
136
        $this->assertEquals('MPA-7ED9D2D0BC81',$receivers[0]->moipAccount->id);
137
    }
138
    
139
    public function testCreateOrderAddingReceiverAmountFixed() 
140
    {
141
        $order = $this->createOrder()->addReceiver('MPA-7ED9D2D0BC81', 'PRIMARY', 30000);
142
        $receivers = $order->getReceiverIterator();
143
        $this->assertEquals(30000, $receivers[0]->amount->fixed);
144
    }
145
    
146
    public function testCreateOrderAddingReceiverAmountPercentual() 
147
    {
148
        $order = $this->createOrder()->addReceiver('MPA-7ED9D2D0BC81', 'PRIMARY', NULL, 40);
149
        $receivers = $order->getReceiverIterator();
150
        $this->assertEquals(40, $receivers[0]->amount->percentual);
151
    }
152
    
153
    public function testCreateOrderAddingReceiverFeePayor() 
154
    {
155
        $order = $this->createOrder()->addReceiver('MPA-7ED9D2D0BC81', 'PRIMARY', NULL, 40, true);
156
        $receivers = $order->getReceiverIterator();
157
        $this->assertEquals(40, $receivers[0]->amount->percentual);
158
        $this->assertTrue($receivers[0]->feePayor);
159
        $order2 = $this->createOrder()->addReceiver('MPA-7ED9D2D0BC81', 'PRIMARY', 30000, NULL, true);
160
        $receivers2 = $order2->getReceiverIterator();
161
        $this->assertEquals(30000, $receivers2[0]->amount->fixed);
162
        $this->assertTrue($receivers2[0]->feePayor);
163
    }
164
}
165