1 | <?php |
||
2 | |||
3 | namespace Tests\Bpost\Order; |
||
4 | |||
5 | use Bpost\BpostApiClient\Bpost\Order\Address; |
||
6 | use Bpost\BpostApiClient\Bpost\Order\Sender; |
||
7 | use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException; |
||
8 | use DOMDocument; |
||
9 | use Exception; |
||
10 | use PHPUnit_Framework_TestCase; |
||
11 | |||
12 | class SenderTest 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 Sender->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('sender'); |
||
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
![]() |
|||
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 | $sender = new Sender(); |
||
79 | $sender->setName($data['name']); |
||
80 | $sender->setCompany($data['company']); |
||
81 | $sender->setAddress($address); |
||
82 | $sender->setEmailAddress($data['emailAddress']); |
||
83 | $sender->setPhoneNumber($data['phoneNumber']); |
||
84 | $actualDocument->appendChild( |
||
85 | $sender->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 | $sender = new Sender(); |
||
97 | |||
98 | try { |
||
99 | $sender->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 | $sender->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 |