InternationalTest::testToXML()   C
last analyzed

Complexity

Conditions 12
Paths 80

Size

Total Lines 147
Code Lines 105

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 105
dl 0
loc 147
rs 5.5733
c 0
b 0
f 0
cc 12
nc 80
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Tests\Bpost\Order\Box;
4
5
use Bpost\BpostApiClient\Bpost\Order\Address;
6
use Bpost\BpostApiClient\Bpost\Order\Box\CustomsInfo\CustomsInfo;
7
use Bpost\BpostApiClient\Bpost\Order\Box\International;
8
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging;
9
use Bpost\BpostApiClient\Bpost\Order\Receiver;
10
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
11
use DOMDocument;
12
use Exception;
13
use PHPUnit_Framework_TestCase;
14
15
class InternationalTest extends PHPUnit_Framework_TestCase
16
{
17
    /**
18
     * Create a generic DOM Document
19
     *
20
     * @return DOMDocument
21
     */
22
    private static function createDomDocument()
23
    {
24
        $document = new DOMDocument('1.0', 'utf-8');
25
        $document->preserveWhiteSpace = false;
26
        $document->formatOutput = true;
27
28
        return $document;
29
    }
30
31
    /**
32
     * Tests International->toXML
33
     */
34
    public function testToXML()
35
    {
36
        $data = array(
37
            'international' => array(
38
                'product' => 'bpack World Express Pro',
39
                'options' => array(
40
                    array(
41
                        'common:infoNextDay' => array(
42
                            '@attributes' => array(
43
                                'language' => 'NL',
44
                            ),
45
                            'common:emailAddress' => '[email protected]',
46
                        ),
47
                    ),
48
                ),
49
                'receiver' => array(
50
                    'name' => 'Tijs Verkoyen',
51
                    'company' => 'Sumo Coders',
52
                    'address' => array(
53
                        'streetName' => 'Afrikalaan',
54
                        'number' => '289',
55
                        'box' => '3',
56
                        'postalCode' => '9000',
57
                        'locality' => 'Gent',
58
                        'countryCode' => 'BE',
59
                    ),
60
                    'emailAddress' => '[email protected]',
61
                    'phoneNumber' => '+32 9 395 02 51',
62
                ),
63
                'parcelWeight' => 2000,
64
                'customsInfo' => array(
65
                    'parcelValue' => 700,
66
                    'contentDescription' => 'BOOK',
67
                    'shipmentType' => 'DOCUMENTS',
68
                    'parcelReturnInstructions' => 'RTS',
69
                    'privateAddress' => false,
70
                ),
71
            ),
72
        );
73
74
        $expectedDocument = self::createDomDocument();
75
        $nationalBox = $expectedDocument->createElement('internationalBox');
76
        $expectedDocument->appendChild($nationalBox);
77
        $international = $expectedDocument->createElement('international:international');
78
        $nationalBox->appendChild($international);
79
        $international->appendChild(
80
            $expectedDocument->createElement('international:product', $data['international']['product'])
81
        );
82
        $options = $expectedDocument->createElement('international:options');
83
        foreach ($data['international']['options'] as $value) {
84
            foreach ($value as $key2 => $value2) {
85
                $element = $expectedDocument->createElement($key2);
86
                foreach ($value2 as $key3 => $value3) {
87
                    if ($key3 == '@attributes') {
88
                        foreach ($value3 as $key4 => $value4) {
89
                            $element->setAttribute($key4, $value4);
90
                        }
91
                    } else {
92
                        $element->appendChild(
93
                            $expectedDocument->createElement($key3, $value3)
94
                        );
95
                    }
96
                }
97
98
                $options->appendChild($element);
99
            }
100
        }
101
        $international->appendChild($options);
102
        $receiver = $expectedDocument->createElement('international:receiver');
103
        $international->appendChild($receiver);
104
        foreach ($data['international']['receiver'] as $key => $value) {
105
            $key = 'common:' . $key;
106
            if ($key == 'common:address') {
107
                $address = $expectedDocument->createElement($key);
108
                foreach ($value as $key2 => $value2) {
109
                    $key2 = 'common:' . $key2;
110
                    $address->appendChild(
111
                        $expectedDocument->createElement($key2, $value2)
112
                    );
113
                }
114
                $receiver->appendChild($address);
115
            } else {
116
                $receiver->appendChild(
117
                    $expectedDocument->createElement($key, $value)
118
                );
119
            }
120
        }
121
        $international->appendChild(
122
            $expectedDocument->createElement('international:parcelWeight', $data['international']['parcelWeight'])
123
        );
124
        $customsInfo = $expectedDocument->createElement('international:customsInfo');
125
        foreach ($data['international']['customsInfo'] as $key => $value) {
126
            if ($key == 'privateAddress') {
127
                $value = ($value) ? 'true' : 'false';
128
            }
129
            $customsInfo->appendChild(
130
                $expectedDocument->createElement('international:' . $key, $value)
131
            );
132
        }
133
        $international->appendChild($customsInfo);
134
135
        $actualDocument = self::createDomDocument();
136
        $address = new Address(
137
            $data['international']['receiver']['address']['streetName'],
138
            $data['international']['receiver']['address']['number'],
139
            $data['international']['receiver']['address']['box'],
140
            $data['international']['receiver']['address']['postalCode'],
141
            $data['international']['receiver']['address']['locality'],
142
            $data['international']['receiver']['address']['countryCode']
143
        );
144
145
        $receiver = new Receiver();
146
        $receiver->setName($data['international']['receiver']['name']);
147
        $receiver->setCompany($data['international']['receiver']['company']);
148
        $receiver->setAddress($address);
149
        $receiver->setEmailAddress($data['international']['receiver']['emailAddress']);
150
        $receiver->setPhoneNumber($data['international']['receiver']['phoneNumber']);
151
152
        $customsInfo = new CustomsInfo();
153
        $customsInfo->setParcelValue($data['international']['customsInfo']['parcelValue']);
154
        $customsInfo->setContentDescription($data['international']['customsInfo']['contentDescription']);
155
        $customsInfo->setShipmentType($data['international']['customsInfo']['shipmentType']);
156
        $customsInfo->setParcelReturnInstructions($data['international']['customsInfo']['parcelReturnInstructions']);
157
        $customsInfo->setPrivateAddress($data['international']['customsInfo']['privateAddress']);
158
159
        $messaging = new Messaging(
160
            'infoNextDay',
161
            $data['international']['options'][0]['common:infoNextDay']['@attributes']['language'],
162
            $data['international']['options'][0]['common:infoNextDay']['common:emailAddress']
163
        );
164
165
        $international = new International();
166
        $international->setProduct($data['international']['product']);
167
        $international->setReceiver($receiver);
168
        $international->setParcelWeight($data['international']['parcelWeight']);
169
        $international->setCustomsInfo($customsInfo);
170
171
        $international->addOption($messaging);
172
173
        // I know, the line below is kinda bogus, but it will make sure all code is tested
174
        $international->setOptions(array($messaging));
175
176
        $actualDocument->appendChild(
177
            $international->toXML($actualDocument)
178
        );
179
180
        $this->assertSame($expectedDocument->saveXML(), $actualDocument->saveXML());
181
    }
182
183
    /**
184
     * Test validation in the setters
185
     */
186
    public function testFaultyProperties()
187
    {
188
        $international = new International();
189
190
        try {
191
            $international->setProduct(str_repeat('a', 10));
192
            $this->fail('BpostInvalidValueException not launched');
193
        } catch (BpostInvalidValueException $e) {
194
            // Nothing, the exception is good
195
        } catch (Exception $e) {
196
            $this->fail('BpostInvalidValueException not caught');
197
        }
198
199
        // Exceptions were caught,
200
        $this->assertTrue(true);
201
    }
202
}
203