CustomerTest::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\Bpack247;
4
5
use Bpost\BpostApiClient\Bpack247\Customer;
6
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
7
use Bpost\BpostApiClient\Exception\XmlException\BpostXmlNoUserIdFoundException;
8
use DateTime;
9
use DOMDocument;
10
use Exception;
11
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...
12
13
class CustomerTest extends PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * Create a generic DOM Document
17
     *
18
     * @return DOMDocument
19
     */
20
    private static function createDomDocument()
21
    {
22
        $document = new DOMDocument('1.0', 'utf-8');
23
        $document->preserveWhiteSpace = false;
24
        $document->formatOutput = true;
25
26
        return $document;
27
    }
28
29
    /**
30
     * Tests Customer->toXML
31
     */
32
    public function testToXML()
33
    {
34
        $data = array(
35
            'FirstName' => 'Tijs',
36
            'LastName' => 'Verkoyen',
37
            'Email' => '[email protected]',
38
            'Street' => 'Afrikalaan',
39
            'Number' => '289',
40
            'MobilePrefix' => '0032',
41
            'MobileNumber' => '486123456',
42
            'PostalCode' => '9000',
43
            'PreferredLanguage' => 'nl-BE',
44
            'Title' => 'Mr.',
45
        );
46
47
        $expectedDocument = self::createDomDocument();
48
        $customer = $expectedDocument->createElement(
49
            'Customer'
50
        );
51
        $customer->setAttribute(
52
            'xmlns',
53
            'http://schema.post.be/ServiceController/customer'
54
        );
55
        $customer->setAttribute(
56
            'xmlns:xsi',
57
            'http://www.w3.org/2001/XMLSchema-instance'
58
        );
59
        $customer->setAttribute(
60
            'xsi:schemaLocation',
61
            'http://schema.post.be/ServiceController/customer'
62
        );
63
        $customer->appendChild(
64
            $expectedDocument->createElement('FirstName', $data['FirstName'])
65
        );
66
        $customer->appendChild(
67
            $expectedDocument->createElement('LastName', $data['LastName'])
68
        );
69
        $customer->appendChild(
70
            $expectedDocument->createElement('Street', $data['Street'])
71
        );
72
        $customer->appendChild(
73
            $expectedDocument->createElement('Number', $data['Number'])
74
        );
75
        $customer->appendChild(
76
            $expectedDocument->createElement('Email', $data['Email'])
77
        );
78
        $customer->appendChild(
79
            $expectedDocument->createElement('MobilePrefix', $data['MobilePrefix'])
80
        );
81
        $customer->appendChild(
82
            $expectedDocument->createElement('MobileNumber', $data['MobileNumber'])
83
        );
84
        $customer->appendChild(
85
            $expectedDocument->createElement('PostalCode', $data['PostalCode'])
86
        );
87
        $customer->appendChild(
88
            $expectedDocument->createElement('PreferredLanguage', $data['PreferredLanguage'])
89
        );
90
        $customer->appendChild(
91
            $expectedDocument->createElement('Title', $data['Title'])
92
        );
93
        $expectedDocument->appendChild($customer);
94
95
        $customer = new Customer();
96
        $customer->setFirstName($data['FirstName']);
97
        $customer->setLastName($data['LastName']);
98
        $customer->setEmail($data['Email']);
99
        $customer->setStreet($data['Street']);
100
        $customer->setNumber($data['Number']);
101
        $customer->setMobileNumber($data['MobileNumber']);
102
        $customer->setPostalCode($data['PostalCode']);
103
        $customer->setPreferredLanguage($data['PreferredLanguage']);
104
        $customer->setTitle($data['Title']);
105
106
        $actualDocument = self::createDomDocument();
107
        $actualDocument->appendChild(
108
            $customer->toXML($actualDocument)
109
        );
110
111
        $this->assertSame($expectedDocument->saveXML(), $actualDocument->saveXML());
112
    }
113
114
    /**
115
     * Tests Customer->toXML
116
     */
117
    public function testCreateFromXML()
118
    {
119
        $data = array(
120
            'UserID' => '5f1f1b07-a8c4-4d4c-bd5b-cdace6cb7c84',
121
            'FirstName' => 'Bruno',
122
            'LastName' => 'Vandenabeele',
123
            'Street' => 'Oplintersesteenweg',
124
            'Number' => '629',
125
            'CompanyName' => 'Bpost',
126
            'Country' => 'BE',
127
            'DateOfBirth' => '1974-07-02',
128
            'DeliveryCode' => '344337728',
129
            'Email' => '[email protected]',
130
            'MobilePrefix' => '0032',
131
            'MobileNumber' => '475813445',
132
            'Postalcode' => '3300',
133
            'PreferredLanguage' => 'nl-BE',
134
            'ReceivePromotions' => true,
135
            'actived' => true,
136
            'Title' => 'Mr',
137
            'Town' => 'Tienen',
138
            'PackStations' => array(
139
                array(
140
                    'OrderNumber' => '1',
141
                    'PackStationId' => '14472',
142
                ),
143
            ),
144
        );
145
146
        $document = self::createDomDocument();
147
        $customerElement = $document->createElement('Customer');
148
        foreach ($data as $key => $value) {
149
            if ($key == 'PackStations') {
150
                continue;
151
            }
152
153
            $customerElement->appendChild(
154
                $document->createElement($key, $value)
155
            );
156
        }
157
        $customerPackStation = $document->createElement('CustomerPackStation');
158
        $customerPackStation->appendChild(
159
            $document->createElement('OrderNumber', 1)
160
        );
161
        $customerPackStation->appendChild(
162
            $document->createElement('PackstationID', 14472)
163
        );
164
        $packStations = $document->createElement('PackStations');
165
        $packStations->appendChild($customerPackStation);
166
        $customerElement->appendChild($packStations);
167
        $document->appendChild($customerElement);
168
169
        $customer = Customer::createFromXML(
170
            simplexml_load_string(
171
                $document->saveXML()
172
            )
173
        );
174
175
        $this->assertSame($data['UserID'], $customer->getUserID());
176
        $this->assertSame($data['FirstName'], $customer->getFirstName());
177
        $this->assertSame($data['LastName'], $customer->getLastName());
178
        $this->assertSame($data['Street'], $customer->getStreet());
179
        $this->assertSame($data['Number'], $customer->getNumber());
180
        $this->assertSame($data['CompanyName'], $customer->getCompanyName());
181
        $this->assertEquals(new DateTime($data['DateOfBirth']), $customer->getDateOfBirth());
182
        $this->assertSame($data['DeliveryCode'], $customer->getDeliveryCode());
183
        $this->assertSame($data['Email'], $customer->getEmail());
184
        $this->assertSame($data['MobilePrefix'], $customer->getMobilePrefix());
185
        $this->assertSame($data['MobileNumber'], $customer->getMobileNumber());
186
        $this->assertSame($data['Postalcode'], $customer->getPostalCode());
187
        $this->assertSame($data['PreferredLanguage'], $customer->getPreferredLanguage());
188
        $this->assertSame($data['ReceivePromotions'], $customer->getReceivePromotions());
189
        $this->assertSame($data['actived'], $customer->getActivated());
190
        $this->assertSame($data['Title'] . '.', $customer->getTitle());
191
        $this->assertSame($data['Town'], $customer->getTown());
192
        $packStations = $customer->getPackStations();
193
        $this->assertSame($data['PackStations'][0]['OrderNumber'], $packStations[0]->getOrderNumber());
194
        $this->assertSame($data['PackStations'][0]['PackStationId'], $packStations[0]->getPackStationId());
195
196
        try {
197
            $xml = simplexml_load_string('<Customer></Customer>');
198
            Customer::createFromXML($xml);
199
            $this->fail('BpostXmlNoUserIdFoundException not launched');
200
        } catch (BpostXmlNoUserIdFoundException $e) {
201
            // Nothing, the exception is good
202
            $this->assertTrue(true);
203
        } catch (Exception $e) {
204
            $this->fail('BpostXmlNoUserIdFoundException not caught');
205
        }
206
    }
207
208
    /**
209
     * Test validation in the setters
210
     */
211
    public function testFaultyProperties()
212
    {
213
        $customer = new Customer();
214
215
        try {
216
            $customer->setPreferredLanguage(str_repeat('a', 10));
217
            $this->fail('BpostInvalidValueException not launched');
218
        } catch (BpostInvalidValueException $e) {
219
            // Nothing, the exception is good
220
        } catch (Exception $e) {
221
            $this->fail('BpostInvalidValueException not caught');
222
        }
223
224
        try {
225
            $customer->setTitle(str_repeat('a', 10));
226
            $this->fail('BpostInvalidValueException not launched');
227
        } catch (BpostInvalidValueException $e) {
228
            // Nothing, the exception is good
229
        } catch (Exception $e) {
230
            $this->fail('BpostInvalidValueException not caught');
231
        }
232
233
        // Exceptions were caught,
234
        $this->assertTrue(true);
235
    }
236
237
    /**
238
     * Test some missing stuff to get full coverage
239
     */
240
    public function testMissingProperties()
241
    {
242
        $data = array(
243
            'IsComfortZoneUser' => true,
244
            'OptIn' => true,
245
            'UseInformationForThirdParty' => false,
246
            'UserName' => 'UserName',
247
            'PackStations' => array(
248
                'Foo',
249
                'Bar',
250
            ),
251
        );
252
253
        $customer = new Customer();
254
255
        $customer->setIsComfortZoneUser($data['IsComfortZoneUser']);
256
        $this->assertSame($data['IsComfortZoneUser'], $customer->getIsComfortZoneUser());
257
        $customer->setOptIn($data['OptIn']);
258
        $this->assertSame($data['OptIn'], $customer->getOptIn());
259
        $customer->setUseInformationForThirdParty($data['UseInformationForThirdParty']);
260
        $this->assertSame($data['UseInformationForThirdParty'], $customer->getUseInformationForThirdParty());
261
        $customer->setUserName($data['UserName']);
262
        $this->assertSame($data['UserName'], $customer->getUserName());
263
        $customer->setPackStations($data['PackStations']);
264
        $this->assertSame($data['PackStations'], $customer->getPackStations());
265
    }
266
}
267