Passed
Pull Request — develop (#53)
by Luís
03:03
created

CheckoutTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 13
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 138
rs 10
1
<?php
2
3
namespace PHPSC\PagSeguro\Requests\Checkout;
4
5
use SimpleXMLElement;
6
use PHPSC\PagSeguro\Items\Items;
7
use PHPSC\PagSeguro\Items\Item;
8
use PHPSC\PagSeguro\Shipping\Shipping;
9
use PHPSC\PagSeguro\Customer\Customer;
10
use PHPSC\PagSeguro\Customer\Phone;
11
use PHPSC\PagSeguro\Customer\Address;
12
13
/**
14
 * @author Luís Otávio Cobucci Oblonczyk <[email protected]>
15
 */
16
class CheckoutTest extends \PHPUnit_Framework_TestCase
17
{
18
    /**
19
     * @var Checkout
20
     */
21
    private $checkout;
22
23
    /**
24
     * @var Order
25
     */
26
    private $order;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function setUp()
32
    {
33
        $this->order    = new Order();
34
        $this->checkout = new Checkout($this->order);
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function constructShouldConfigureAttributes()
41
    {
42
        $this->assertAttributeSame($this->order, 'order', $this->checkout);
43
    }
44
45
    /**
46
     * @test
47
     */
48
    public function constructShouldCreateANewOrderWhenItWasntInformed()
49
    {
50
        $this->assertAttributeInstanceOf(
51
            Order::class,
52
            'order',
53
            new Checkout()
54
        );
55
    }
56
57
    public function getOrderShouldReturnConfiguredOrder()
58
    {
59
        $this->assertSame($this->order, $this->checkout->getOrder());
60
    }
61
62
    /**
63
     * @test
64
     */
65
    public function setCustomerShouldConfigureTheReference()
66
    {
67
        $customer = new Customer('[email protected]');
68
        $this->checkout->setCustomer($customer);
69
70
        $this->assertAttributeSame($customer, 'customer', $this->checkout);
71
    }
72
73
    /**
74
     * @test
75
     */
76
    public function setRedirectToShouldConfigureTheRedirectionUri()
77
    {
78
        $this->checkout->setRedirectTo('http://test.com');
79
80
        $this->assertAttributeEquals('http://test.com', 'redirectTo', $this->checkout);
81
    }
82
83
    /**
84
     * @test
85
     */
86
    public function setMaxAgeShouldConfigureTheMaximumAge()
87
    {
88
        $this->checkout->setMaxAge(1);
89
90
        $this->assertAttributeEquals(1, 'maxAge', $this->checkout);
91
    }
92
93
    /**
94
     * @test
95
     */
96
    public function setMaxUsesShouldTheNumberOfUses()
97
    {
98
        $this->checkout->setMaxUses(1);
99
100
        $this->assertAttributeEquals(1, 'maxUses', $this->checkout);
101
    }
102
103
    /**
104
     * @test
105
     */
106
    public function getMaxAgeShouldReturnConfiguredMaxAge()
107
    {
108
        $this->checkout->setMaxAge(12);
109
110
        $this->assertEquals(12, $this->checkout->getMaxAge());
111
    }
112
113
    /**
114
     * @test
115
     */
116
    public function getMaxUsesShouldReturnConfiguredMaxUses()
117
    {
118
        $this->checkout->setMaxUses(7);
119
120
        $this->assertEquals(7, $this->checkout->getMaxUses());
121
    }
122
123
    /**
124
     * @test
125
     */
126
    public function getRedirectToShouldReturnConfiguredRedirectTo()
127
    {
128
        $this->checkout->setRedirectTo('someRedirect');
129
130
        $this->assertEquals('someRedirect', $this->checkout->getRedirectTo());
131
    }
132
133
    /**
134
     * @test
135
     */
136
    public function getCustomerShouldReturnConfiguredCustomer()
137
    {
138
        $customer = $this->createMock(Customer::class);
139
        $this->checkout->setCustomer($customer);
140
141
        $this->assertSame($customer, $this->checkout->getCustomer());
142
    }
143
144
    /**
145
     * @test
146
     */
147
    public function setNotificationURLShouldConfigureTheNotificationUri()
148
    {
149
        $uri = 'http://chibungo.com';
150
        $this->checkout->setNotificationURL($uri);
151
        $this->assertAttributeEquals($uri, 'notificationURL', $this->checkout);
152
    }
153
154
    /**
155
     * @test
156
     */
157
    public function serializeShouldXMLEmpty()
158
    {
159
        $checkout = new Checkout;
160
        $xml      = $checkout->xmlSerialize();
161
162
        $this->assertInstanceOf(SimpleXMLElement::class, $xml);
163
164
        $expected = simplexml_load_file(__DIR__.'/xml/checkoutEmpty.xml');
165
        $this->assertEquals($expected, $xml);
166
    }
167
168
    /**
169
     * @test
170
     */
171
    public function serializeShouldReturnXMLCustomer()
172
    {
173
        $customer = new Customer('[email protected]');
174
        $checkout = new Checkout;
175
        $checkout->setCustomer($customer);
176
177
        $xml = $checkout->xmlSerialize();
178
179
        $this->assertInstanceOf(SimpleXMLElement::class, $xml);
180
        $expected = simplexml_load_file(__DIR__.'/xml/checkoutCustomer.xml');
181
        $this->assertEquals($expected, $xml);
182
    }
183
184
    /**
185
     * @test
186
     */
187
    public function serializeShouldReturnXMLFull()
188
    {
189
        $items = new Items;
190
        $items->add(new Item(77, 'Produto 01', 2.5, 4, 20, 300));
191
        $items->add(new Item(88, 'Produto 02', 342.51, 3, 134.98, 1000));
192
193
        $shippingAddress = new Address('CE', 'Ortega do Norte', '40610-912', 'Ipe', 'R. Regina Salas', '3601', 'Bl.A');
194
        $shipping        = new Shipping(1, $shippingAddress, 23.45);
195
196
        $order = new Order($items);
197
        $order->setReference('REF1234');
198
        $order->setExtraAmount(-10.30);
199
        $order->setShipping($shipping);
200
201
        $customerAddress = new Address('AC', 'Sao Maite', '99500-079', 'Centro', 'Rua David Delgado', '55', 'Fundos');
202
        $customerPhone   = new Phone('11', '99999999');
203
        $customer        = new Customer('[email protected]', 'FooBar', $customerPhone, $customerAddress);
204
205
        $checkout = new Checkout($order);
206
        $checkout->setCustomer($customer);
207
        $checkout->setRedirectTo('http://localhost/return.php');
208
        $checkout->setMaxUses(5);
209
        $checkout->setMaxAge(60);
210
211
        $xml = $checkout->xmlSerialize();
212
213
        $this->assertInstanceOf(SimpleXMLElement::class, $xml);
214
        $expected = simplexml_load_file(__DIR__ . '/xml/checkoutFull.xml');
215
        $this->assertEquals($expected, $xml);
216
    }
217
}
218