MessagingTest::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\Box\Option;
4
5
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging;
6
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException;
7
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
8
use DOMDocument;
9
use Exception;
10
use PHPUnit_Framework_TestCase;
11
12
class MessagingTest 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 Messaging->ToXML
30
     */
31
    public function testToXML()
32
    {
33
        $data = array(
34
            'infoDistributed' => array(
35
                '@attributes' => array(
36
                    'language' => 'EN',
37
                ),
38
                'mobilePhone' => '0495151689',
39
            ),
40
        );
41
42
        $expectedDocument = self::createDomDocument();
43
        $infoDistributed = $expectedDocument->createElement('infoDistributed');
44
        $infoDistributed->setAttribute('language', $data['infoDistributed']['@attributes']['language']);
45
        $infoDistributed->appendChild(
46
            $expectedDocument->createElement(
47
                'mobilePhone',
48
                $data['infoDistributed']['mobilePhone']
49
            )
50
        );
51
        $expectedDocument->appendChild($infoDistributed);
52
53
        $actualDocument = self::createDomDocument();
54
        $messaging = new Messaging(
55
            'infoDistributed',
56
            $data['infoDistributed']['@attributes']['language'],
57
            null,
58
            $data['infoDistributed']['mobilePhone']
59
        );
60
        $actualDocument->appendChild(
61
            $messaging->toXML($actualDocument, null)
62
        );
63
        $this->assertEquals($expectedDocument, $actualDocument);
64
65
        $data = array(
66
            'infoNextDay' => array(
67
                '@attributes' => array(
68
                    'language' => 'EN',
69
                ),
70
                'emailAddress' => '[email protected]',
71
            ),
72
        );
73
74
        $expectedDocument = self::createDomDocument();
75
        $infoNextDay = $expectedDocument->createElement('infoNextDay');
76
        $infoNextDay->setAttribute('language', $data['infoNextDay']['@attributes']['language']);
77
        $infoNextDay->appendChild(
78
            $expectedDocument->createElement(
79
                'emailAddress',
80
                $data['infoNextDay']['emailAddress']
81
            )
82
        );
83
        $expectedDocument->appendChild($infoNextDay);
84
85
        $actualDocument = self::createDomDocument();
86
        $messaging = new Messaging(
87
            'infoNextDay',
88
            $data['infoNextDay']['@attributes']['language'],
89
            $data['infoNextDay']['emailAddress']
90
        );
91
        $actualDocument->appendChild(
92
            $messaging->toXML($actualDocument, null)
93
        );
94
        $this->assertEquals($expectedDocument, $actualDocument);
95
    }
96
97
    /**
98
     * Test validation in the setters
99
     */
100
    public function testFaultyProperties()
101
    {
102
        try {
103
            new Messaging(str_repeat('a', 10), 'NL');
104
            $this->fail('BpostInvalidValueException not launched');
105
        } catch (BpostInvalidValueException $e) {
106
            // Nothing, the exception is good
107
        } catch (Exception $e) {
108
            $this->fail('BpostInvalidValueException not caught');
109
        }
110
111
        try {
112
            new Messaging('infoDistributed', str_repeat('a', 10));
113
            $this->fail('BpostInvalidValueException not launched');
114
        } catch (BpostInvalidValueException $e) {
115
            // Nothing, the exception is good
116
        } catch (Exception $e) {
117
            $this->fail('BpostInvalidValueException not caught');
118
        }
119
120
        try {
121
            new Messaging('infoDistributed', 'NL', str_repeat('a', 51));
122
            $this->fail('BpostInvalidLengthException not launched');
123
        } catch (BpostInvalidLengthException $e) {
124
            // Nothing, the exception is good
125
        } catch (Exception $e) {
126
            $this->fail('BpostInvalidLengthException not caught');
127
        }
128
129
        try {
130
            new Messaging('infoDistributed', 'NL', null, str_repeat('a', 21));
131
            $this->fail('BpostInvalidLengthException not launched');
132
        } catch (BpostInvalidLengthException $e) {
133
            // Nothing, the exception is good
134
        } catch (Exception $e) {
135
            $this->fail('BpostInvalidLengthException not caught');
136
        }
137
138
        // Exceptions were caught,
139
        $this->assertTrue(true);
140
    }
141
}
142