1
|
|
|
<?php |
2
|
|
|
namespace PHPSC\PagSeguro\Requests\PreApprovals; |
3
|
|
|
|
4
|
|
|
use SimpleXMLElement; |
5
|
|
|
use DateTime; |
6
|
|
|
use PHPSC\PagSeguro\Customer\Customer; |
7
|
|
|
use PHPSC\PagSeguro\Customer\Phone; |
8
|
|
|
use PHPSC\PagSeguro\Customer\Address; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Adelar Tiemann Junior <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class RequestTest extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Request |
17
|
|
|
*/ |
18
|
|
|
private $request; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var PreApproval |
22
|
|
|
*/ |
23
|
|
|
private $preApproval; |
24
|
|
|
|
25
|
|
|
protected function setUp() |
26
|
|
|
{ |
27
|
|
|
$this->preApproval = $this->getMock(PreApproval::class, [], [], '', false); |
28
|
|
|
$this->request = new Request($this->preApproval); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @test |
33
|
|
|
*/ |
34
|
|
|
public function constructShouldConfigureTheAttributes() |
35
|
|
|
{ |
36
|
|
|
$this->assertAttributeInstanceOf(PreApproval::class, 'preApproval', $this->request); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @test |
41
|
|
|
*/ |
42
|
|
|
public function getPreApprovalShouldReturnConfiguredPreApproval() |
43
|
|
|
{ |
44
|
|
|
$this->assertInstanceOf(PreApproval::class, $this->request->getPreApproval()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @test |
49
|
|
|
*/ |
50
|
|
|
public function getReferenceShouldReturnConfiguredReference() |
51
|
|
|
{ |
52
|
|
|
$this->request->setReference('someRef'); |
53
|
|
|
|
54
|
|
|
$this->assertEquals('someRef', $this->request->getReference()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @test |
59
|
|
|
*/ |
60
|
|
|
public function setReferenceShouldChangeTheAttribute() |
61
|
|
|
{ |
62
|
|
|
$this->request->setReference('test'); |
63
|
|
|
|
64
|
|
|
$this->assertAttributeEquals('test', 'reference', $this->request); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @test |
69
|
|
|
*/ |
70
|
|
|
public function getRedirectToShouldReturnConfiguredRedirectTo() |
71
|
|
|
{ |
72
|
|
|
$this->request->setRedirectTo('someRedirect'); |
73
|
|
|
|
74
|
|
|
$this->assertEquals('someRedirect', $this->request->getRedirectTo()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @test |
79
|
|
|
*/ |
80
|
|
|
public function setRedirectToShouldChangeTheAttribute() |
81
|
|
|
{ |
82
|
|
|
$this->request->setRedirectTo('otherRedirect'); |
83
|
|
|
|
84
|
|
|
$this->assertAttributeEquals('otherRedirect', 'redirectTo', $this->request); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @test |
89
|
|
|
*/ |
90
|
|
|
public function getReviewOnShouldReturnConfiguredReviewOn() |
91
|
|
|
{ |
92
|
|
|
$this->request->setReviewOn('someReview'); |
93
|
|
|
|
94
|
|
|
$this->assertEquals('someReview', $this->request->getReviewOn()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @test |
99
|
|
|
*/ |
100
|
|
|
public function setReviewOnToShouldChangeTheAttribute() |
101
|
|
|
{ |
102
|
|
|
$this->request->setReviewOn('otherReview'); |
103
|
|
|
|
104
|
|
|
$this->assertAttributeEquals('otherReview', 'reviewOn', $this->request); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @test |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public function getCustomerShouldReturnConfiguredCustomer() |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$customer = $this->getMock(Customer::class, [], [], '', false); |
113
|
|
|
$this->request->setCustomer($customer); |
114
|
|
|
|
115
|
|
|
$this->assertSame($customer, $this->request->getCustomer()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @test |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
public function setCustomerToShouldChangeTheAttribute() |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$customer = $this->getMock(Customer::class, [], [], '', false); |
124
|
|
|
$this->request->setCustomer($customer); |
125
|
|
|
|
126
|
|
|
$this->assertAttributeSame($customer, 'customer', $this->request); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testSerializeShouldXMLFull() |
130
|
|
|
{ |
131
|
|
|
$preApproval = new PreApproval; |
132
|
|
|
$preApproval->setChargeType('auto'); |
133
|
|
|
$preApproval->setName('Assinatura Revista'); |
134
|
|
|
$preApproval->setPeriod('MONTHLY'); |
135
|
|
|
$preApproval->setFinalDate(new DateTime('2016-11-18', new \DateTimeZone('UTC'))); |
136
|
|
|
$preApproval->setMaxTotalAmount(3000); |
137
|
|
|
$preApproval->setDetails('Cobranca Mensal da Revista'); |
138
|
|
|
$preApproval->setAmountPerPayment(100); |
139
|
|
|
$preApproval->setMaxAmountPerPayment(150); |
140
|
|
|
$preApproval->setInitialDate(new DateTime('2015-11-18', new \DateTimeZone('UTC'))); |
141
|
|
|
$preApproval->setMaxPaymentsPerPeriod(12); |
142
|
|
|
$preApproval->setMaxAmountPerPeriod(1200); |
143
|
|
|
|
144
|
|
|
$customerAddress = new Address('AC', 'Sao Maite', '99500-079', 'Centro', 'Rua David Delgado', '55', 'Fundos'); |
145
|
|
|
$customerPhone = new Phone('11', '99999999'); |
146
|
|
|
$customer = new Customer('[email protected]', 'FooBar', $customerPhone, $customerAddress); |
147
|
|
|
|
148
|
|
|
$request = new Request($preApproval); |
149
|
|
|
$request->setCustomer($customer); |
150
|
|
|
$request->setReference('abcdef'); |
151
|
|
|
$request->setReviewOn('http://localhost/return.php'); |
152
|
|
|
$request->setRedirectTo('http://localhost/success.php'); |
153
|
|
|
|
154
|
|
|
$xml = $request->xmlSerialize(); |
155
|
|
|
|
156
|
|
|
$this->assertInstanceOf(SimpleXMLElement::class, $xml); |
157
|
|
|
$expected = simplexml_load_file(__DIR__.'/xml/preAprovalsRequestFull.xml'); |
158
|
|
|
$this->assertEquals($expected, $xml); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
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.