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->getMock(Customer::class, [], [], '', false); |
139
|
|
|
$this->checkout->setCustomer($customer); |
140
|
|
|
|
141
|
|
|
$this->assertSame($customer, $this->checkout->getCustomer()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testSerializeShouldXMLEmpty() |
145
|
|
|
{ |
146
|
|
|
$checkout = new Checkout; |
147
|
|
|
$xml = $checkout->xmlSerialize(); |
148
|
|
|
|
149
|
|
|
$this->assertInstanceOf(SimpleXMLElement::class, $xml); |
150
|
|
|
|
151
|
|
|
$expected = simplexml_load_file(__DIR__.'/xml/checkoutEmpty.xml'); |
152
|
|
|
$this->assertEquals($expected, $xml); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @test |
157
|
|
|
*/ |
158
|
|
|
public function testSerializeShouldReturnXMLCustomer() |
159
|
|
|
{ |
160
|
|
|
$customer = new Customer('[email protected]'); |
161
|
|
|
$checkout = new Checkout; |
162
|
|
|
$checkout->setCustomer($customer); |
163
|
|
|
|
164
|
|
|
$xml = $checkout->xmlSerialize(); |
165
|
|
|
|
166
|
|
|
$this->assertInstanceOf(SimpleXMLElement::class, $xml); |
167
|
|
|
$expected = simplexml_load_file(__DIR__.'/xml/checkoutCustomer.xml'); |
168
|
|
|
$this->assertEquals($expected, $xml); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @test |
173
|
|
|
*/ |
174
|
|
|
public function testSerializeShouldReturnXMLFull() |
175
|
|
|
{ |
176
|
|
|
$items = new Items; |
177
|
|
|
$items->add(new Item(77, 'Produto 01', 2.5, 4, 20, 300)); |
178
|
|
|
$items->add(new Item(88, 'Produto 02', 342.51, 3, 134.98, 1000)); |
179
|
|
|
|
180
|
|
|
$shippingAddress = new Address('CE', 'Ortega do Norte', '40610-912', 'Ipe', 'R. Regina Salas', '3601', 'Bl.A'); |
181
|
|
|
$shipping = new Shipping(1, $shippingAddress, 23.45); |
182
|
|
|
|
183
|
|
|
$order = new Order($items); |
184
|
|
|
$order->setReference('REF1234'); |
185
|
|
|
$order->setExtraAmount(1.01); |
186
|
|
|
$order->setShipping($shipping); |
187
|
|
|
|
188
|
|
|
$customerAddress = new Address('AC', 'Sao Maite', '99500-079', 'Centro', 'Rua David Delgado', '55', 'Fundos'); |
189
|
|
|
$customerPhone = new Phone('11', '99999999'); |
190
|
|
|
$customer = new Customer('[email protected]', 'FooBar', $customerPhone, $customerAddress); |
191
|
|
|
|
192
|
|
|
$checkout = new Checkout($order); |
193
|
|
|
$checkout->setCustomer($customer); |
194
|
|
|
$checkout->setRedirectTo('http://localhost/return.php'); |
195
|
|
|
$checkout->setMaxUses(5); |
196
|
|
|
$checkout->setMaxAge(60); |
197
|
|
|
|
198
|
|
|
$xml = $checkout->xmlSerialize(); |
199
|
|
|
|
200
|
|
|
$this->assertInstanceOf(SimpleXMLElement::class, $xml); |
201
|
|
|
$expected = simplexml_load_file(__DIR__.'/xml/checkoutFull.xml'); |
202
|
|
|
$this->assertEquals($expected, $xml); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|