OrderTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 334
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 254
dl 0
loc 334
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testToXml() 0 65 1
A generateDomDocument() 0 30 1
A createDomDocument() 0 7 1
A getFetchOrderWithReferenceXml() 0 3 1
A testCreateFromXmlWithException() 0 3 1
B getCreateOrderXml() 0 3 1
B getFetchOrderXml() 0 3 1
A testCreateFromXml() 0 41 1
1
<?php
2
3
namespace Tests\Bpost;
4
5
use Bpost\BpostApiClient\Bpost\Order;
6
use Bpost\BpostApiClient\Bpost\Order\Address;
7
use Bpost\BpostApiClient\Bpost\Order\Box;
8
use Bpost\BpostApiClient\Bpost\Order\Box\AtBpost;
9
use Bpost\BpostApiClient\Bpost\Order\Box\Option\CashOnDelivery;
10
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging;
11
use Bpost\BpostApiClient\Bpost\Order\Box\Option\SaturdayDelivery;
12
use Bpost\BpostApiClient\Bpost\Order\Line;
13
use Bpost\BpostApiClient\Bpost\Order\PugoAddress;
14
use Bpost\BpostApiClient\Bpost\Order\Sender;
15
use DOMDocument;
16
use DOMElement;
17
use PHPUnit_Framework_TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit_Framework_TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use SimpleXMLElement;
19
20
class OrderTest extends PHPUnit_Framework_TestCase
21
{
22
    public function testToXml()
23
    {
24
        $self = new Order('bpack@bpost VAS 038 - COD+SAT+iD');
25
        $self->setCostCenter('Cost Center');
26
27
        $self->setLines(array(new Line('Product 1', 1)));
28
        $self->addLine(new Line('Product 1', 5));
29
30
        $senderAddress = new Address();
31
        $senderAddress->setStreetName('MUNT');
32
        $senderAddress->setNumber(1);
33
        $senderAddress->setBox(1);
34
        $senderAddress->setPostalCode(1000);
35
        $senderAddress->setLocality('Brussel');
36
        $senderAddress->setCountryCode('BE');
37
        $senderAddress->setBox(1);
38
39
        $pugoAddress = new PugoAddress();
40
        $pugoAddress->setStreetName('Turnhoutsebaan');
41
        $pugoAddress->setNumber(468);
42
        $pugoAddress->setBox('A');
43
        $pugoAddress->setPostalCode(2110);
44
        $pugoAddress->setLocality('Wijnegem');
45
        $pugoAddress->setCountryCode('BE');
46
47
        $sender = new Sender();
48
        $sender->setName('SENDER NAME');
49
        $sender->setCompany('SENDER COMPANY');
50
        $sender->setAddress($senderAddress);
51
        $sender->setEmailAddress('[email protected]');
52
        $sender->setPhoneNumber('022011111');
53
54
        $atBpost = new AtBpost();
55
56
        $atBpost->setOptions(array(
57
            new Messaging('infoDistributed', 'EN', null, '0476123456'),
58
            new Messaging('keepMeInformed', 'EN', null, '0032475123456'),
59
        ));
60
        $atBpost->addOption(new SaturdayDelivery());
61
        $atBpost->addOption(new CashOnDelivery(1251, 'BE19210023508812', 'GEBABEBB'));
62
63
        $atBpost->setWeight(2000);
64
65
        $atBpost->setPugoId(207500);
66
        $atBpost->setPugoName('WIJNEGEM');
67
        $atBpost->setPugoAddress($pugoAddress);
68
        $atBpost->setReceiverName('RECEIVER NAME');
69
        $atBpost->setReceiverCompany('RECEIVER COMPANY');
70
        $atBpost->setRequestedDeliveryDate('2020-10-22');
71
72
        $box = new Box();
73
        $box->setSender($sender);
74
        $box->setNationalBox($atBpost);
75
        $box->setRemark('bpack@bpost VAS 038 - COD+SAT+iD');
76
        $box->setAdditionalCustomerReference('Reference that can be used for cross-referencing');
77
78
        $self->setBoxes(array());
79
        $this->assertCount(0, $self->getBoxes());
80
        $self->addBox($box);
81
82
        // Normal
83
        $rootDom = $this->createDomDocument();
84
        $document = $this->generateDomDocument($rootDom, $self->toXML($rootDom, '107423'));
85
86
        $this->assertSame($this->getCreateOrderXml(), $document->saveXML());
87
    }
88
89
    /**
90
     * @expectedException \Bpost\BpostApiClient\Exception\XmlException\BpostXmlNoReferenceFoundException
91
     */
92
    public function testCreateFromXmlWithException()
93
    {
94
        Order::createFromXML(new SimpleXMLElement($this->getFetchOrderWithReferenceXml()));
95
    }
96
97
    public function testCreateFromXml()
98
    {
99
        $self = Order::createFromXML(new SimpleXMLElement($this->getFetchOrderXml()));
100
101
        $this->assertSame('bpost_ref_56e02a5047119', $self->getReference());
102
        $this->assertNotNull($self->getLines());
103
        $this->assertCount(2, $self->getLines());
104
105
        $this->assertNotNull($self->getBoxes());
106
        $this->assertCount(1, $self->getBoxes());
107
        $this->assertSame('Cost Center', $self->getCostCenter());
108
109
        /** @var Box $box */
110
        $box = current($self->getBoxes());
111
112
        $this->assertSame('Sender name', $box->getSender()->getName());
113
        $this->assertSame('Sender company', $box->getSender()->getCompany());
114
        $this->assertSame('[email protected]', $box->getSender()->getEmailAddress());
115
        $this->assertSame('0434343434', $box->getSender()->getPhoneNumber());
116
        $this->assertSame('Sender street', $box->getSender()->getAddress()->getStreetName());
117
118
        $this->assertSame('WORDPRESS 4.4.2 / WOOCOMMERCE 2.5.2', $box->getAdditionalCustomerReference());
119
        $this->assertSame(Box::BOX_STATUS_PENDING, $box->getStatus());
120
        $this->assertSame('Plouf a la playa', $box->getRemark());
121
        $this->assertNull($box->getInternationalBox());
122
        $this->assertNotNull($box->getNationalBox());
123
124
        /** @var AtBpost $nationalBox */
125
        $nationalBox = $box->getNationalBox();
126
        $this->assertInstanceOf('Bpost\BpostApiClient\Bpost\Order\Box\AtBpost', $nationalBox);
127
        $this->assertSame('bpack@bpost', $nationalBox->getProduct());
128
        $this->assertSame(1234, $nationalBox->getWeight());
129
        $this->assertNull($nationalBox->getOpeningHours());
130
        $this->assertSame('33100', $nationalBox->getPugoId());
131
        $this->assertSame('ANDERLECHT AUTONOMIE', $nationalBox->getPugoName());
132
        $this->assertSame('Receiver name', $nationalBox->getReceiverName());
133
        $this->assertSame('Receiver company', $nationalBox->getReceiverCompany());
134
        $this->assertSame('2016-03-19+01:00', $nationalBox->getRequestedDeliveryDate());
135
        $this->assertSame('Rue de l\'Autonomie', $nationalBox->getPugoAddress()->getStreetName());
136
137
        $this->assertNotNull($nationalBox->getOptions());
138
        // $this->assertCount(6, $nationalBox->getOptions());
139
    }
140
141
    private function getFetchOrderWithReferenceXml()
142
    {
143
        return <<<XML
144
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
145
<orderInfo xmlns="http://schema.post.be/shm/deepintegration/v3/" xmlns:ns2="http://schema.post.be/shm/deepintegration/v3/common" xmlns:ns3="http://schema.post.be/shm/deepintegration/v3/national" xmlns:ns4="http://schema.post.be/shm/deepintegration/v3/international">
146
  <accountId>107423</accountId>
147
  <costCenter>Cost Center</costCenter>
148
</orderInfo>
149
150
XML;
151
    }
152
153
    private function getFetchOrderXml()
154
    {
155
        return <<<XML
156
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
157
<orderInfo xmlns="http://schema.post.be/shm/deepintegration/v3/" xmlns:ns2="http://schema.post.be/shm/deepintegration/v3/common" xmlns:ns3="http://schema.post.be/shm/deepintegration/v3/national" xmlns:ns4="http://schema.post.be/shm/deepintegration/v3/international">
158
  <accountId>107423</accountId>
159
  <reference>bpost_ref_56e02a5047119</reference>
160
  <costCenter>Cost Center</costCenter>
161
  <orderLine>
162
    <text>Product 1</text>
163
    <nbOfItems>1</nbOfItems>
164
  </orderLine>
165
  <orderLine>
166
    <text>Product 1</text>
167
    <nbOfItems>5</nbOfItems>
168
  </orderLine>
169
  <box>
170
    <sender>
171
      <ns2:name>Sender name</ns2:name>
172
      <ns2:company>Sender company</ns2:company>
173
      <ns2:address>
174
        <ns2:streetName>Sender street</ns2:streetName>
175
        <ns2:number>1</ns2:number>
176
        <ns2:box>A</ns2:box>
177
        <ns2:postalCode>1000</ns2:postalCode>
178
        <ns2:locality>Bruxelles</ns2:locality>
179
        <ns2:countryCode>BE</ns2:countryCode>
180
      </ns2:address>
181
      <ns2:emailAddress>[email protected]</ns2:emailAddress>
182
      <ns2:phoneNumber>0434343434</ns2:phoneNumber>
183
    </sender>
184
    <nationalBox>
185
      <ns3:atBpost>
186
        <ns3:product>bpack@bpost</ns3:product>
187
        <ns3:options>
188
          <ns2:infoDistributed language="FR">
189
            <ns2:emailAddress>[email protected]</ns2:emailAddress>
190
          </ns2:infoDistributed>
191
          <ns2:keepMeInformed language="EN">
192
            <ns2:emailAddress>[email protected]</ns2:emailAddress>
193
          </ns2:keepMeInformed>
194
          <ns2:insured>
195
            <ns2:additionalInsurance value="2"/>
196
          </ns2:insured>
197
          <ns2:signed/>
198
          <ns2:saturdayDelivery/>
199
          <ns2:cod>
200
            <ns2:codAmount>1234</ns2:codAmount>
201
            <ns2:iban>BE19 2100 2350 8812</ns2:iban>
202
            <ns2:bic>GEBABEBB</ns2:bic>
203
          </ns2:cod>
204
        </ns3:options>
205
        <ns3:weight>1234</ns3:weight>
206
        <ns3:openingHours/>
207
        <ns3:pugoId>33100</ns3:pugoId>
208
        <ns3:pugoName>ANDERLECHT AUTONOMIE</ns3:pugoName>
209
        <ns3:pugoAddress>
210
          <ns2:streetName>Rue de l'Autonomie</ns2:streetName>
211
          <ns2:number>6A</ns2:number>
212
          <ns2:postalCode>1070</ns2:postalCode>
213
          <ns2:locality>Anderlecht</ns2:locality>
214
          <ns2:countryCode>BE</ns2:countryCode>
215
        </ns3:pugoAddress>
216
        <ns3:receiverName>Receiver name</ns3:receiverName>
217
        <ns3:receiverCompany>Receiver company</ns3:receiverCompany>
218
        <ns3:requestedDeliveryDate>2016-03-19+01:00</ns3:requestedDeliveryDate>
219
      </ns3:atBpost>
220
    </nationalBox>
221
    <remark>Plouf a la playa</remark>
222
    <additionalCustomerReference>WORDPRESS 4.4.2 / WOOCOMMERCE 2.5.2</additionalCustomerReference>
223
    <status>PENDING</status>
224
  </box>
225
</orderInfo>
226
227
XML;
228
    }
229
230
    private function getCreateOrderXml()
231
    {
232
        return <<< XML
233
<?xml version="1.0" encoding="UTF-8"?>
234
<tns:order xmlns="http://schema.post.be/shm/deepintegration/v3/national" xmlns:common="http://schema.post.be/shm/deepintegration/v3/common" xmlns:tns="http://schema.post.be/shm/deepintegration/v3/" xmlns:international="http://schema.post.be/shm/deepintegration/v3/international" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schema.post.be/shm/deepintegration/v3/">
235
  <tns:accountId>107423</tns:accountId>
236
  <tns:reference>bpack@bpost VAS 038 - COD+SAT+iD</tns:reference>
237
  <tns:costCenter>Cost Center</tns:costCenter>
238
  <tns:orderLine>
239
    <tns:text>Product 1</tns:text>
240
    <tns:nbOfItems>1</tns:nbOfItems>
241
  </tns:orderLine>
242
  <tns:orderLine>
243
    <tns:text>Product 1</tns:text>
244
    <tns:nbOfItems>5</tns:nbOfItems>
245
  </tns:orderLine>
246
  <tns:box>
247
    <tns:sender>
248
      <common:name>SENDER NAME</common:name>
249
      <common:company>SENDER COMPANY</common:company>
250
      <common:address>
251
        <common:streetName>MUNT</common:streetName>
252
        <common:number>1</common:number>
253
        <common:box>1</common:box>
254
        <common:postalCode>1000</common:postalCode>
255
        <common:locality>Brussel</common:locality>
256
        <common:countryCode>BE</common:countryCode>
257
      </common:address>
258
      <common:emailAddress>[email protected]</common:emailAddress>
259
      <common:phoneNumber>022011111</common:phoneNumber>
260
    </tns:sender>
261
    <tns:nationalBox>
262
      <atBpost>
263
        <product>bpack@bpost</product>
264
        <options>
265
          <common:infoDistributed language="EN">
266
            <common:mobilePhone>0476123456</common:mobilePhone>
267
          </common:infoDistributed>
268
          <common:keepMeInformed language="EN">
269
            <common:mobilePhone>0032475123456</common:mobilePhone>
270
          </common:keepMeInformed>
271
          <common:saturdayDelivery/>
272
          <common:cod>
273
            <common:codAmount>1251</common:codAmount>
274
            <common:iban>BE19210023508812</common:iban>
275
            <common:bic>GEBABEBB</common:bic>
276
          </common:cod>
277
        </options>
278
        <weight>2000</weight>
279
        <pugoId>207500</pugoId>
280
        <pugoName>WIJNEGEM</pugoName>
281
        <pugoAddress>
282
          <common:streetName>Turnhoutsebaan</common:streetName>
283
          <common:number>468</common:number>
284
          <common:box>A</common:box>
285
          <common:postalCode>2110</common:postalCode>
286
          <common:locality>Wijnegem</common:locality>
287
          <common:countryCode>BE</common:countryCode>
288
        </pugoAddress>
289
        <receiverName>RECEIVER NAME</receiverName>
290
        <receiverCompany>RECEIVER COMPANY</receiverCompany>
291
        <requestedDeliveryDate>2020-10-22</requestedDeliveryDate>
292
      </atBpost>
293
    </tns:nationalBox>
294
    <tns:remark>bpack@bpost VAS 038 - COD+SAT+iD</tns:remark>
295
    <tns:additionalCustomerReference>Reference that can be used for cross-referencing</tns:additionalCustomerReference>
296
  </tns:box>
297
</tns:order>
298
299
XML;
300
    }
301
302
    /**
303
     * Create a generic DOM Document
304
     *
305
     * @return DOMDocument
306
     */
307
    private function createDomDocument()
308
    {
309
        $document = new DOMDocument('1.0', 'UTF-8');
310
        $document->preserveWhiteSpace = false;
311
        $document->formatOutput = true;
312
313
        return $document;
314
    }
315
316
    /**
317
     * Generate the document, by adding the namespace declarations
318
     *
319
     * @param DOMDocument $document
320
     * @param DOMElement  $element
321
     *
322
     * @return DOMDocument
323
     */
324
    private function generateDomDocument(DOMDocument $document, DOMElement $element)
325
    {
326
        $element->setAttribute(
327
            'xmlns:common',
328
            'http://schema.post.be/shm/deepintegration/v3/common'
329
        );
330
        $element->setAttribute(
331
            'xmlns:tns',
332
            'http://schema.post.be/shm/deepintegration/v3/'
333
        );
334
        $element->setAttribute(
335
            'xmlns',
336
            'http://schema.post.be/shm/deepintegration/v3/national'
337
        );
338
        $element->setAttribute(
339
            'xmlns:international',
340
            'http://schema.post.be/shm/deepintegration/v3/international'
341
        );
342
        $element->setAttribute(
343
            'xmlns:xsi',
344
            'http://www.w3.org/2001/XMLSchema-instance'
345
        );
346
        $element->setAttribute(
347
            'xsi:schemaLocation',
348
            'http://schema.post.be/shm/deepintegration/v3/'
349
        );
350
351
        $document->appendChild($element);
352
353
        return $document;
354
    }
355
}
356