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\Bpost\Order;
4
5
use Bpost\BpostApiClient\Bpost\Order\Address;
6
use Bpost\BpostApiClient\Bpost\Order\Customer;
7
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException;
8
use DOMDocument;
9
use Exception;
10
use PHPUnit_Framework_TestCase;
11
12
class CustomerTest extends PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * Create a generic DOM Document
16
     *
17
     * @return DOMDocument
18
     */
19
    private static function createDomDocument()
20
    {
21
        $document = new DOMDocument('1.0', 'utf-8');
22
        $document->preserveWhiteSpace = false;
23
        $document->formatOutput = true;
24
25
        return $document;
26
    }
27
28
    /**
29
     * Tests Customer->toXML
30
     */
31
    public function testToXML()
32
    {
33
        $data = array(
34
            'name' => 'Tijs Verkoyen',
35
            'company' => 'Sumo Coders',
36
            'address' => array(
37
                'streetName' => 'Afrikalaan',
38
                'number' => '289',
39
                'box' => '3',
40
                'postalCode' => '9000',
41
                'locality' => 'Gent',
42
                'countryCode' => 'BE',
43
            ),
44
            'emailAddress' => '[email protected]',
45
            'phoneNumber' => '+32 9 395 02 51',
46
        );
47
48
        $expectedDocument = self::createDomDocument();
49
        $sender = $expectedDocument->createElement('customer');
50
        foreach ($data as $key => $value) {
51
            $key = 'common:' . $key;
52
            if ($key == 'common:address') {
53
                $address = $expectedDocument->createElement($key);
54
                foreach ($value as $key2 => $value2) {
55
                    $key2 = 'common:' . $key2;
56
                    $address->appendChild(
57
                        $expectedDocument->createElement($key2, $value2)
58
                    );
59
                }
60
                $sender->appendChild($address);
61
            } else {
62
                $sender->appendChild(
63
                    $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

63
                    $expectedDocument->createElement($key, /** @scrutinizer ignore-type */ $value)
Loading history...
64
                );
65
            }
66
        }
67
        $expectedDocument->appendChild($sender);
68
69
        $actualDocument = self::createDomDocument();
70
        $address = new Address(
71
            $data['address']['streetName'],
72
            $data['address']['number'],
73
            $data['address']['box'],
74
            $data['address']['postalCode'],
75
            $data['address']['locality'],
76
            $data['address']['countryCode']
77
        );
78
        $customer = new Customer();
79
        $customer->setName($data['name']);
80
        $customer->setCompany($data['company']);
81
        $customer->setAddress($address);
82
        $customer->setEmailAddress($data['emailAddress']);
83
        $customer->setPhoneNumber($data['phoneNumber']);
84
        $actualDocument->appendChild(
85
            $customer->toXML($actualDocument, null)
86
        );
87
88
        $this->assertSame($expectedDocument->saveXML(), $actualDocument->saveXML());
89
    }
90
91
    /**
92
     * Test validation in the setters
93
     */
94
    public function testFaultyProperties()
95
    {
96
        $customer = new Customer();
97
98
        try {
99
            $customer->setEmailAddress(str_repeat('a', 51));
100
            $this->fail('BpostInvalidLengthException not launched');
101
        } catch (BpostInvalidLengthException $e) {
102
            // Nothing, the exception is good
103
        } catch (Exception $e) {
104
            $this->fail('BpostInvalidLengthException not caught');
105
        }
106
107
        try {
108
            $customer->setPhoneNumber(str_repeat('a', 21));
109
            $this->fail('BpostInvalidLengthException not launched');
110
        } catch (BpostInvalidLengthException $e) {
111
            // Nothing, the exception is good
112
        } catch (Exception $e) {
113
            $this->fail('BpostInvalidLengthException not caught');
114
        }
115
116
        // Exceptions were caught,
117
        $this->assertTrue(true);
118
    }
119
}
120