BoxTest::createDomDocument()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\Bpost\Order;
4
5
use Bpost\BpostApiClient\Bpost\Order\Address;
6
use Bpost\BpostApiClient\Bpost\Order\Box;
7
use Bpost\BpostApiClient\Bpost\Order\Box\AtHome;
8
use Bpost\BpostApiClient\Bpost\Order\Box\International;
9
use Bpost\BpostApiClient\Bpost\Order\Receiver;
10
use Bpost\BpostApiClient\Bpost\Order\Sender;
11
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
12
use DOMDocument;
13
use Exception;
14
use PHPUnit_Framework_TestCase;
15
16
class BoxTest extends PHPUnit_Framework_TestCase
17
{
18
    /**
19
     * Create a generic DOM Document
20
     *
21
     * @return DOMDocument
22
     */
23
    private static function createDomDocument()
24
    {
25
        $document = new DOMDocument('1.0', 'utf-8');
26
        $document->preserveWhiteSpace = false;
27
        $document->formatOutput = true;
28
29
        return $document;
30
    }
31
32
    /**
33
     * Tests Box->toXML
34
     */
35
    public function testNationalToXML()
36
    {
37
        $data = array(
38
            'sender' => array(
39
                'name' => 'Tijs Verkoyen',
40
                'company' => 'Sumo Coders',
41
                'address' => array(
42
                    'streetName' => 'Afrikalaan',
43
                    'number' => '289',
44
                    'box' => '3',
45
                    'postalCode' => '9000',
46
                    'locality' => 'Gent',
47
                    'countryCode' => 'BE',
48
                ),
49
                'emailAddress' => '[email protected]',
50
                'phoneNumber' => '+32 9 395 02 51',
51
            ),
52
            'nationalBox' => array(
53
                'atHome' => array(
54
                    'product' => 'bpack 24h Pro',
55
                    'weight' => 2000,
56
                    'receiver' => array(
57
                        'name' => 'Tijs Verkoyen',
58
                        'company' => 'Sumo Coders',
59
                        'address' => array(
60
                            'streetName' => 'Kerkstraat',
61
                            'number' => '108',
62
                            'postalCode' => '9050',
63
                            'locality' => 'Gentbrugge',
64
                            'countryCode' => 'BE',
65
                        ),
66
                        'emailAddress' => '[email protected]',
67
                        'phoneNumber' => '+32 9 395 02 51',
68
                    ),
69
                ),
70
            ),
71
            'remark' => 'remark',
72
            'barcode' => 'BARCODE',
73
        );
74
75
        $expectedDocument = self::createDomDocument();
76
        $box = $expectedDocument->createElement('box');
77
        $expectedDocument->appendChild($box);
78
        $sender = $expectedDocument->createElement('sender');
79
        foreach ($data['sender'] as $key => $value) {
80
            $key = 'common:' . $key;
81
            if ($key == 'common:address') {
82
                $address = $expectedDocument->createElement($key);
83
                foreach ($value as $key2 => $value2) {
84
                    $key2 = 'common:' . $key2;
85
                    $address->appendChild(
86
                        $expectedDocument->createElement($key2, $value2)
87
                    );
88
                }
89
                $sender->appendChild($address);
90
            } else {
91
                $sender->appendChild(
92
                    $expectedDocument->createElement($key, $value)
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array<string,string>; however, parameter $value of DOMDocument::createElement() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
                    $expectedDocument->createElement($key, /** @scrutinizer ignore-type */ $value)
Loading history...
93
                );
94
            }
95
        }
96
        $nationalBox = $expectedDocument->createElement('nationalBox');
97
        $atHome = $expectedDocument->createElement('atHome');
98
        $nationalBox->appendChild($atHome);
99
        $atHome->appendChild($expectedDocument->createElement('product', $data['nationalBox']['atHome']['product']));
100
        $atHome->appendChild($expectedDocument->createElement('weight', $data['nationalBox']['atHome']['weight']));
101
        $receiver = $expectedDocument->createElement('receiver');
102
        $atHome->appendChild($receiver);
103
        foreach ($data['nationalBox']['atHome']['receiver'] as $key => $value) {
104
            $key = 'common:' . $key;
105
            if ($key == 'common:address') {
106
                $address = $expectedDocument->createElement($key);
107
                foreach ($value as $key2 => $value2) {
108
                    $key2 = 'common:' . $key2;
109
                    $address->appendChild(
110
                        $expectedDocument->createElement($key2, $value2)
111
                    );
112
                }
113
                $receiver->appendChild($address);
114
            } else {
115
                $receiver->appendChild(
116
                    $expectedDocument->createElement($key, $value)
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array<string,string>; however, parameter $value of DOMDocument::createElement() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

116
                    $expectedDocument->createElement($key, /** @scrutinizer ignore-type */ $value)
Loading history...
117
                );
118
            }
119
        }
120
        $box->appendChild($sender);
121
        $box->appendChild($nationalBox);
122
        $box->appendChild($expectedDocument->createElement('remark', $data['remark']));
123
        $box->appendChild($expectedDocument->createElement('barcode', $data['barcode']));
124
125
        $actualDocument = self::createDomDocument();
126
        $address = new Address(
127
            $data['sender']['address']['streetName'],
128
            $data['sender']['address']['number'],
129
            $data['sender']['address']['box'],
130
            $data['sender']['address']['postalCode'],
131
            $data['sender']['address']['locality'],
132
            $data['sender']['address']['countryCode']
133
        );
134
135
        $sender = new Sender();
136
        $sender->setName($data['sender']['name']);
137
        $sender->setCompany($data['sender']['company']);
138
        $sender->setAddress($address);
139
        $sender->setEmailAddress($data['sender']['emailAddress']);
140
        $sender->setPhoneNumber($data['sender']['phoneNumber']);
141
142
        $address = new Address(
143
            $data['nationalBox']['atHome']['receiver']['address']['streetName'],
144
            $data['nationalBox']['atHome']['receiver']['address']['number'],
145
            null,
146
            $data['nationalBox']['atHome']['receiver']['address']['postalCode'],
147
            $data['nationalBox']['atHome']['receiver']['address']['locality'],
148
            $data['nationalBox']['atHome']['receiver']['address']['countryCode']
149
        );
150
151
        $receiver = new Receiver();
152
        $receiver->setAddress($address);
153
        $receiver->setName($data['nationalBox']['atHome']['receiver']['name']);
154
        $receiver->setCompany($data['nationalBox']['atHome']['receiver']['company']);
155
        $receiver->setPhoneNumber($data['nationalBox']['atHome']['receiver']['phoneNumber']);
156
        $receiver->setEmailAddress($data['nationalBox']['atHome']['receiver']['emailAddress']);
157
158
        $atHome = new AtHome();
159
        $atHome->setProduct($data['nationalBox']['atHome']['product']);
160
        $atHome->setWeight($data['nationalBox']['atHome']['weight']);
161
        $atHome->setReceiver($receiver);
162
163
        $box = new Box();
164
        $box->setSender($sender);
165
        $box->setNationalBox($atHome);
166
        $box->setRemark($data['remark']);
167
        $box->setBarcode($data['barcode']);
168
169
        $actualDocument->appendChild(
170
            $box->toXML($actualDocument, null)
171
        );
172
173
        $this->assertSame($expectedDocument->saveXML(), $actualDocument->saveXML());
174
    }
175
176
    /**
177
     * Tests Box->toXML
178
     */
179
    public function testInternationalToXML()
180
    {
181
        $data = array(
182
            'sender' => array(
183
                'name' => 'Tijs Verkoyen',
184
                'company' => 'Sumo Coders',
185
                'address' => array(
186
                    'streetName' => 'Afrikalaan',
187
                    'number' => '289',
188
                    'box' => '3',
189
                    'postalCode' => '9000',
190
                    'locality' => 'Gent',
191
                    'countryCode' => 'BE',
192
                ),
193
                'emailAddress' => '[email protected]',
194
                'phoneNumber' => '+32 9 395 02 51',
195
            ),
196
            'internationalBox' => array(
197
                'international' => array(
198
                    'product' => 'bpack World Express Pro',
199
                    'receiver' => array(
200
                        'name' => 'Tijs Verkoyen',
201
                        'company' => 'Sumo Coders',
202
                        'address' => array(
203
                            'streetName' => 'Kerkstraat',
204
                            'number' => '108',
205
                            'postalCode' => '9050',
206
                            'locality' => 'Gentbrugge',
207
                            'countryCode' => 'BE',
208
                        ),
209
                        'emailAddress' => '[email protected]',
210
                        'phoneNumber' => '+32 9 395 02 51',
211
                    ),
212
                ),
213
            ),
214
            'remark' => 'remark',
215
            'barcode' => 'BARCODE',
216
        );
217
        $expectedDocument = self::createDomDocument();
218
        $box = $expectedDocument->createElement('box');
219
        $expectedDocument->appendChild($box);
220
        $sender = $expectedDocument->createElement('sender');
221
        foreach ($data['sender'] as $key => $value) {
222
            $key = 'common:' . $key;
223
            if ($key == 'common:address') {
224
                $address = $expectedDocument->createElement($key);
225
                foreach ($value as $key2 => $value2) {
226
                    $key2 = 'common:' . $key2;
227
                    $address->appendChild(
228
                        $expectedDocument->createElement($key2, $value2)
229
                    );
230
                }
231
                $sender->appendChild($address);
232
            } else {
233
                $sender->appendChild(
234
                    $expectedDocument->createElement($key, $value)
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array<string,string>; however, parameter $value of DOMDocument::createElement() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

234
                    $expectedDocument->createElement($key, /** @scrutinizer ignore-type */ $value)
Loading history...
235
                );
236
            }
237
        }
238
        $nationalBox = $expectedDocument->createElement('internationalBox');
239
        $atHome = $expectedDocument->createElement('international:international');
240
        $nationalBox->appendChild($atHome);
241
        $atHome->appendChild(
242
            $expectedDocument->createElement(
243
                'international:product',
244
                $data['internationalBox']['international']['product']
245
            )
246
        );
247
        $receiver = $expectedDocument->createElement('international:receiver');
248
        $atHome->appendChild($receiver);
249
        foreach ($data['internationalBox']['international']['receiver'] as $key => $value) {
250
            $key = 'common:' . $key;
251
            if ($key == 'common:address') {
252
                $address = $expectedDocument->createElement($key);
253
                foreach ($value as $key2 => $value2) {
254
                    $key2 = 'common:' . $key2;
255
                    $address->appendChild(
256
                        $expectedDocument->createElement($key2, $value2)
257
                    );
258
                }
259
                $receiver->appendChild($address);
260
            } else {
261
                $receiver->appendChild(
262
                    $expectedDocument->createElement($key, $value)
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array<string,string>; however, parameter $value of DOMDocument::createElement() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

262
                    $expectedDocument->createElement($key, /** @scrutinizer ignore-type */ $value)
Loading history...
263
                );
264
            }
265
        }
266
        $box->appendChild($sender);
267
        $box->appendChild($nationalBox);
268
        $box->appendChild($expectedDocument->createElement('remark', $data['remark']));
269
        $box->appendChild($expectedDocument->createElement('barcode', $data['barcode']));
270
271
        $actualDocument = self::createDomDocument();
272
        $address = new Address(
273
            $data['sender']['address']['streetName'],
274
            $data['sender']['address']['number'],
275
            $data['sender']['address']['box'],
276
            $data['sender']['address']['postalCode'],
277
            $data['sender']['address']['locality'],
278
            $data['sender']['address']['countryCode']
279
        );
280
281
        $sender = new Sender();
282
        $sender->setName($data['sender']['name']);
283
        $sender->setCompany($data['sender']['company']);
284
        $sender->setAddress($address);
285
        $sender->setEmailAddress($data['sender']['emailAddress']);
286
        $sender->setPhoneNumber($data['sender']['phoneNumber']);
287
288
        $address = new Address(
289
            $data['internationalBox']['international']['receiver']['address']['streetName'],
290
            $data['internationalBox']['international']['receiver']['address']['number'],
291
            null,
292
            $data['internationalBox']['international']['receiver']['address']['postalCode'],
293
            $data['internationalBox']['international']['receiver']['address']['locality'],
294
            $data['internationalBox']['international']['receiver']['address']['countryCode']
295
        );
296
297
        $receiver = new Receiver();
298
        $receiver->setAddress($address);
299
        $receiver->setName($data['internationalBox']['international']['receiver']['name']);
300
        $receiver->setCompany($data['internationalBox']['international']['receiver']['company']);
301
        $receiver->setPhoneNumber($data['internationalBox']['international']['receiver']['phoneNumber']);
302
        $receiver->setEmailAddress($data['internationalBox']['international']['receiver']['emailAddress']);
303
304
        $international = new International();
305
        $international->setProduct($data['internationalBox']['international']['product']);
306
        $international->setReceiver($receiver);
307
308
        $box = new Box();
309
        $box->setSender($sender);
310
        $box->setInternationalBox($international);
311
        $box->setRemark($data['remark']);
312
        $box->setBarcode($data['barcode']);
313
314
        $actualDocument->appendChild(
315
            $box->toXML($actualDocument, null)
316
        );
317
318
        $this->assertSame($expectedDocument->saveXML(), $actualDocument->saveXML());
319
    }
320
321
    /**
322
     * Test validation in the setters
323
     */
324
    public function testFaultyProperties()
325
    {
326
        $box = new Box();
327
328
        try {
329
            $box->setStatus(str_repeat('a', 10));
330
            $this->fail('BpostInvalidValueException not launched');
331
        } catch (BpostInvalidValueException $e) {
332
            // Nothing, the exception is good
333
        } catch (Exception $e) {
334
            $this->fail('BpostInvalidValueException not caught');
335
        }
336
337
        // Exceptions were caught,
338
        $this->assertTrue(true);
339
    }
340
}
341