NationalTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 87
dl 0
loc 149
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A generateDomDocument() 0 30 1
A getXml() 0 3 1
A createDomDocument() 0 7 1
A testMethodsThatAreImplementedByChildren() 0 5 1
A testToXml() 0 29 1
A testCreateFromXml() 0 18 1
1
<?php
2
3
namespace Tests\Bpost\Order\Box;
4
5
use Bpost\BpostApiClient\Bpost\Order\Box\National;
6
use Bpost\BpostApiClient\Bpost\Order\Box\OpeningHour\Day;
7
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging;
8
use Bpost\BpostApiClient\Bpost\Order\Box\Option\SaturdayDelivery;
9
use Bpost\BpostApiClient\Bpost\ProductConfiguration\Option;
10
use Bpost\BpostApiClient\Common\XmlHelper;
11
use DOMDocument;
12
use DOMElement;
13
use PHPUnit_Framework_TestCase;
14
use SimpleXMLElement;
15
16
class NationalFake extends National
17
{
18
    /**
19
     * Return the object as an array for usage in the XML
20
     *
21
     * @param DomDocument $document
22
     * @param string      $prefix
23
     * @param string      $type
24
     *
25
     * @return DomElement
26
     */
27
    public function toXML(DOMDocument $document, $prefix = null, $type = null)
28
    {
29
        $nationalElement = $document->createElement(XmlHelper::getPrefixedTagName('nationalBox', $prefix));
30
        $boxElement = parent::toXML($document, null, 'nationalFake');
31
        $nationalElement->appendChild($boxElement);
32
33
        return $nationalElement;
34
    }
35
36
    /**
37
     * @param SimpleXMLElement $xml
38
     *
39
     * @return National
40
     */
41
    public static function createFromXML(SimpleXMLElement $xml, National $self = null)
42
    {
43
        return parent::createFromXML($xml->nationalFake, new self());
44
    }
45
}
46
47
class NationalTest extends PHPUnit_Framework_TestCase
48
{
49
    /**
50
     * Tests the methods that are implemented by the children
51
     * But we would like to have full coverage... Stats-porn!
52
     */
53
    public function testMethodsThatAreImplementedByChildren()
54
    {
55
        $possibleProductValues = National::getPossibleProductValues();
56
        $this->assertInternalType('array', $possibleProductValues);
57
        $this->assertEmpty($possibleProductValues);
58
    }
59
60
    public function testToXml()
61
    {
62
        $self = new NationalFake();
63
        $self->setProduct('bpack 24h Pro');
64
65
        $self->setOptions(array(
66
            new Messaging('infoDistributed', 'EN', null, '0476123456'),
67
            new Messaging('infoNextDay', 'EN', '[email protected]'),
68
        ));
69
        $self->addOption(new Messaging('infoReminder', 'EN', null, '0032475123456'));
70
        $self->addOption(new SaturdayDelivery());
71
        $self->setWeight(500);
72
73
        $self->setOpeningHours(array(
74
            new Day('Monday', '07:00-15:00'),
75
            new Day('Tuesday', '07:00-15:00'),
76
            new Day('Wednesday', '-/-'),
77
            new Day('Thursday', '07:00-15:00'),
78
            new Day('Friday', '10:00-12:00/13:00-17:30'),
79
        ));
80
        $self->addOpeningHour(new Day('Saturday', '10:00-12:00/13:00-17:30'));
81
82
        $self->setDesiredDeliveryPlace('Place your delivery instructions here');
83
84
        // Normal
85
        $rootDom = $this->createDomDocument();
86
        $document = $this->generateDomDocument($rootDom, $self->toXML($rootDom, 'tns'));
87
88
        $this->assertSame($this->getXml(), $document->saveXML());
89
    }
90
91
    public function testCreateFromXml()
92
    {
93
        $self = NationalFake::createFromXML(new SimpleXMLElement($this->getXml()));
94
95
        $this->assertSame('bpack 24h Pro', $self->getProduct());
96
97
        /** @var Option[] $options */
98
        $options = $self->getOptions();
99
        $this->assertNotNull($options);
100
        // @todo Fix options feeding and test it
101
102
        $this->assertSame(500, $self->getWeight());
103
104
        $openingHours = $self->getOpeningHours();
105
        $this->assertCount(6, $openingHours);
106
        $this->assertSame('-/-', $openingHours[2]->getValue());
107
108
        $this->assertSame('Place your delivery instructions here', $self->getDesiredDeliveryPlace());
109
    }
110
111
    private function getXml()
112
    {
113
        return <<<EOF
114
<?xml version="1.0" encoding="UTF-8"?>
115
<tns:nationalBox 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/">
116
  <nationalFake>
117
    <product>bpack 24h Pro</product>
118
    <options>
119
      <common:infoDistributed language="EN">
120
        <common:mobilePhone>0476123456</common:mobilePhone>
121
      </common:infoDistributed>
122
      <common:infoNextDay language="EN">
123
        <common:emailAddress>[email protected]</common:emailAddress>
124
      </common:infoNextDay>
125
      <common:infoReminder language="EN">
126
        <common:mobilePhone>0032475123456</common:mobilePhone>
127
      </common:infoReminder>
128
      <common:saturdayDelivery/>
129
    </options>
130
    <weight>500</weight>
131
    <openingHours>
132
      <Monday>07:00-15:00</Monday>
133
      <Tuesday>07:00-15:00</Tuesday>
134
      <Wednesday>-/-</Wednesday>
135
      <Thursday>07:00-15:00</Thursday>
136
      <Friday>10:00-12:00/13:00-17:30</Friday>
137
      <Saturday>10:00-12:00/13:00-17:30</Saturday>
138
    </openingHours>
139
    <desiredDeliveryPlace>Place your delivery instructions here</desiredDeliveryPlace>
140
  </nationalFake>
141
</tns:nationalBox>
142
143
EOF;
144
    }
145
146
    /**
147
     * Create a generic DOM Document
148
     *
149
     * @return DOMDocument
150
     */
151
    private function createDomDocument()
152
    {
153
        $document = new DOMDocument('1.0', 'UTF-8');
154
        $document->preserveWhiteSpace = false;
155
        $document->formatOutput = true;
156
157
        return $document;
158
    }
159
160
    /**
161
     * @param DOMDocument $document
162
     * @param DOMElement  $element
163
     *
164
     * @return DOMDocument
165
     */
166
    private function generateDomDocument(DOMDocument $document, DOMElement $element)
167
    {
168
        $element->setAttribute(
169
            'xmlns:common',
170
            'http://schema.post.be/shm/deepintegration/v3/common'
171
        );
172
        $element->setAttribute(
173
            'xmlns:tns',
174
            'http://schema.post.be/shm/deepintegration/v3/'
175
        );
176
        $element->setAttribute(
177
            'xmlns',
178
            'http://schema.post.be/shm/deepintegration/v3/national'
179
        );
180
        $element->setAttribute(
181
            'xmlns:international',
182
            'http://schema.post.be/shm/deepintegration/v3/international'
183
        );
184
        $element->setAttribute(
185
            'xmlns:xsi',
186
            'http://www.w3.org/2001/XMLSchema-instance'
187
        );
188
        $element->setAttribute(
189
            'xsi:schemaLocation',
190
            'http://schema.post.be/shm/deepintegration/v3/'
191
        );
192
193
        $document->appendChild($element);
194
195
        return $document;
196
    }
197
}
198