1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bpost\BpostApiClient\tests; |
4
|
|
|
|
5
|
|
|
use Bpost\BpostApiClient\Bpost; |
6
|
|
|
use Bpost\BpostApiClient\Bpost\Order; |
7
|
|
|
use Bpost\BpostApiClient\Bpost\Order\Address; |
8
|
|
|
use Bpost\BpostApiClient\Bpost\Order\Box; |
9
|
|
|
use Bpost\BpostApiClient\Bpost\Order\Box\AtHome; |
10
|
|
|
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging; |
11
|
|
|
use Bpost\BpostApiClient\Bpost\Order\Line as OrderLine; |
12
|
|
|
use Bpost\BpostApiClient\Bpost\Order\Receiver; |
13
|
|
|
use Bpost\BpostApiClient\Bpost\Order\Sender; |
14
|
|
|
use PHPUnit_Framework_TestCase; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* test case. |
18
|
|
|
*/ |
19
|
|
|
class BpostTest extends PHPUnit_Framework_TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Bpost |
23
|
|
|
*/ |
24
|
|
|
private $bpost; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Prepares the environment before running a test. |
28
|
|
|
*/ |
29
|
|
|
protected function setUp() |
30
|
|
|
{ |
31
|
|
|
parent::setUp(); |
32
|
|
|
$this->bpost = new Bpost(ACCOUNT_ID, PASSPHRASE); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Cleans up the environment after running a test. |
37
|
|
|
*/ |
38
|
|
|
protected function tearDown() |
39
|
|
|
{ |
40
|
|
|
$this->bpost = null; |
41
|
|
|
parent::tearDown(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Tests Bpost->getTimeOut() |
46
|
|
|
*/ |
47
|
|
|
public function testGetTimeOut() |
48
|
|
|
{ |
49
|
|
|
$this->bpost->setTimeOut(5); |
50
|
|
|
$this->assertSame(5, $this->bpost->getTimeOut()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Tests Bpost->getUserAgent() |
55
|
|
|
*/ |
56
|
|
|
public function testGetUserAgent() |
57
|
|
|
{ |
58
|
|
|
$this->bpost->setUserAgent('testing/1.0.0'); |
59
|
|
|
$this->assertSame('PHP Bpost/' . Bpost::VERSION . ' testing/1.0.0', $this->bpost->getUserAgent()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Create an order with 2 lines and 1 box |
64
|
|
|
* |
65
|
|
|
* @return Order |
66
|
|
|
*/ |
67
|
|
|
protected function createAtHomeOrderObject() |
68
|
|
|
{ |
69
|
|
|
// create order |
70
|
|
|
$orderId = time(); |
71
|
|
|
$order = new Order($orderId); |
72
|
|
|
$order->setCostCenter('Cost Center'); |
73
|
|
|
|
74
|
|
|
// add lines |
75
|
|
|
$line1 = new OrderLine('Beer', 1); |
76
|
|
|
$order->addLine($line1); |
77
|
|
|
$line2 = new OrderLine('Whisky', 100); |
78
|
|
|
$order->addLine($line2); |
79
|
|
|
|
80
|
|
|
// add box |
81
|
|
|
$address = new Address(); |
82
|
|
|
$address->setStreetName('Afrikalaan'); |
83
|
|
|
$address->setNumber('289'); |
84
|
|
|
$address->setPostalCode('9000'); |
85
|
|
|
$address->setLocality('Gent'); |
86
|
|
|
$address->setCountryCode('BE'); |
87
|
|
|
|
88
|
|
|
$sender = new Sender(); |
89
|
|
|
$sender->setAddress($address); |
90
|
|
|
$sender->setName('Tijs Verkoyen'); |
91
|
|
|
$sender->setCompany('Sumo Coders'); |
92
|
|
|
$sender->setPhoneNumber('+32 9 395 02 51'); |
93
|
|
|
$sender->setEmailAddress('[email protected]'); |
94
|
|
|
|
95
|
|
|
$box = new Box(); |
96
|
|
|
$box->setSender($sender); |
97
|
|
|
$box->setRemark('Remark'); |
98
|
|
|
|
99
|
|
|
// add label |
100
|
|
|
$address = new Address(); |
101
|
|
|
$address->setStreetName('Kerkstraat'); |
102
|
|
|
$address->setNumber('108'); |
103
|
|
|
$address->setPostalCode('9050'); |
104
|
|
|
$address->setLocality('Gentbrugge'); |
105
|
|
|
$address->setCountryCode('BE'); |
106
|
|
|
|
107
|
|
|
$receiver = new Receiver(); |
108
|
|
|
$receiver->setAddress($address); |
109
|
|
|
$receiver->setName('Tijs Verkoyen'); |
110
|
|
|
$receiver->setCompany('Sumo Coders'); |
111
|
|
|
$receiver->setPhoneNumber('+32 9 395 02 51'); |
112
|
|
|
$receiver->setEmailAddress('[email protected]'); |
113
|
|
|
|
114
|
|
|
// options |
115
|
|
|
$option = new Messaging('infoDistributed', 'NL', '[email protected]'); |
116
|
|
|
|
117
|
|
|
// @Home |
118
|
|
|
$atHome = new AtHome(); |
119
|
|
|
$atHome->setProduct('bpack 24h Pro'); |
120
|
|
|
$atHome->setWeight(2000); |
121
|
|
|
$atHome->setReceiver($receiver); |
122
|
|
|
$atHome->addOption($option); |
123
|
|
|
$box->setNationalBox($atHome); |
124
|
|
|
|
125
|
|
|
$order->addBox($box); |
126
|
|
|
|
127
|
|
|
return $order; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Tests Bpost->createOrReplaceOrder |
132
|
|
|
*/ |
133
|
|
|
public function testCreateOrReplaceOrder() |
134
|
|
|
{ |
135
|
|
|
$order = $this->createAtHomeOrderObject(); |
136
|
|
|
$response = $this->bpost->createOrReplaceOrder($order); |
137
|
|
|
$this->assertTrue($response); |
138
|
|
|
|
139
|
|
|
$this->bpost->modifyOrderStatus($order->getReference(), 'CANCELLED'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Tests Bpost->modifyOrderStatus |
144
|
|
|
*/ |
145
|
|
|
public function testModifyOrderStatus() |
146
|
|
|
{ |
147
|
|
|
$order = $this->createAtHomeOrderObject(); |
148
|
|
|
$this->bpost->createOrReplaceOrder($order); |
149
|
|
|
$response = $this->bpost->modifyOrderStatus($order->getReference(), 'OPEN'); |
150
|
|
|
$this->assertTrue($response); |
151
|
|
|
|
152
|
|
|
$this->bpost->modifyOrderStatus($order->getReference(), 'CANCELLED'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Tests Bpost->fetchOrder |
157
|
|
|
*/ |
158
|
|
|
public function testFetchOrder() |
159
|
|
|
{ |
160
|
|
|
$order = $this->createAtHomeOrderObject(); |
161
|
|
|
$this->bpost->createOrReplaceOrder($order); |
162
|
|
|
$response = $this->bpost->fetchOrder($order->getReference()); |
163
|
|
|
$this->assertInstanceOf('\\Bpost\BpostApiClient\\Bpost\\Order', $response); |
164
|
|
|
$this->assertEquals($order->getReference(), $response->getReference()); |
165
|
|
|
|
166
|
|
|
$this->bpost->modifyOrderStatus($order->getReference(), 'CANCELLED'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Tests Bpost->createLabelForOrder |
171
|
|
|
*/ |
172
|
|
|
public function testCreateLabelForOrder() |
173
|
|
|
{ |
174
|
|
|
$order = $this->createAtHomeOrderObject(); |
175
|
|
|
$this->bpost->createOrReplaceOrder($order); |
176
|
|
|
$response = $this->bpost->createLabelForOrder($order->getReference()); |
177
|
|
|
$this->assertInternalType('array', $response); |
178
|
|
|
foreach ($response as $label) { |
179
|
|
|
$this->assertInstanceOf('\\Bpost\\BpostApiClient\BPost\Label', $label); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$this->bpost->modifyOrderStatus($order->getReference(), 'CANCELLED'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Tests Bpost->createLabelForBox |
187
|
|
|
* |
188
|
|
|
* @group qsd |
189
|
|
|
*/ |
190
|
|
|
public function testCreateLabelForBox() |
191
|
|
|
{ |
192
|
|
|
$order = $this->createAtHomeOrderObject(); |
193
|
|
|
$this->bpost->createOrReplaceOrder($order); |
194
|
|
|
$response = $this->bpost->createLabelForOrder($order->getReference()); |
195
|
|
|
$response = $this->bpost->createLabelForBox($response[0]->getBarcode()); |
196
|
|
|
$this->assertInternalType('array', $response); |
197
|
|
|
foreach ($response as $label) { |
198
|
|
|
$this->assertInstanceOf('\\Bpost\\BpostApiClient\BPost\Label', $label); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$this->bpost->modifyOrderStatus($order->getReference(), 'CANCELLED'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Tests Bpost->createLabelInBulkForOrders |
206
|
|
|
* |
207
|
|
|
* @group aze |
208
|
|
|
*/ |
209
|
|
|
public function testCreateLabelInBulkForOrders() |
210
|
|
|
{ |
211
|
|
|
$order1 = $this->createAtHomeOrderObject(); |
212
|
|
|
$this->bpost->createOrReplaceOrder($order1); |
213
|
|
|
|
214
|
|
|
$order2 = $this->createAtHomeOrderObject(); |
215
|
|
|
$this->bpost->createOrReplaceOrder($order2); |
216
|
|
|
|
217
|
|
|
$this->bpost->setTimeOut(60); |
218
|
|
|
$response = $this->bpost->createLabelInBulkForOrders( |
219
|
|
|
array( |
220
|
|
|
$order1->getReference(), |
221
|
|
|
$order2->getReference(), |
222
|
|
|
) |
223
|
|
|
); |
224
|
|
|
|
225
|
|
|
$this->assertInternalType('array', $response); |
226
|
|
|
foreach ($response as $label) { |
227
|
|
|
$this->assertInstanceOf('\\Bpost\\BpostApiClient\BPost\Label', $label); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$this->bpost->modifyOrderStatus($order1->getReference(), 'CANCELLED'); |
231
|
|
|
$this->bpost->modifyOrderStatus($order2->getReference(), 'CANCELLED'); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|