1 | <?php |
||
2 | |||
3 | namespace Tests\TestModel\Instance; |
||
4 | |||
5 | use Xml\Impl\Util\StringUtil; |
||
6 | use Xml\ModelInstanceInterface; |
||
7 | use Xml\Impl\Parser\AbstractModelParser; |
||
8 | use Tests\TestModel\{ |
||
9 | Gender, |
||
10 | TestModelConstants, |
||
11 | TestModelParser |
||
12 | }; |
||
13 | use Tests\TestModel\TestModelTest; |
||
14 | use Xml\Instance\ModelElementInstanceInterface; |
||
15 | use Xml\Impl\Instance\ModelElementInstanceImpl; |
||
16 | |||
17 | class AlternativeNsTest extends TestModelTest |
||
18 | { |
||
19 | private const MECHANICAL_NS = "http://test.org/mechanical"; |
||
20 | private const YET_ANOTHER_NS = "http://test.org/yans"; |
||
21 | |||
22 | protected function setUp(): void |
||
23 | { |
||
24 | parent::parseModel("AlternativeNsTest"); |
||
25 | $modelImpl = $this->modelInstance->getModel(); |
||
26 | $modelImpl->declareAlternativeNamespace(self::MECHANICAL_NS, TestModelConstants::NEWER_NAMESPACE); |
||
27 | $modelImpl->declareAlternativeNamespace(self::YET_ANOTHER_NS, TestModelConstants::NEWER_NAMESPACE); |
||
28 | } |
||
29 | |||
30 | protected function tearDown(): void |
||
31 | { |
||
32 | $modelImpl = $this->modelInstance->getModel(); |
||
33 | $modelImpl->undeclareAlternativeNamespace(self::MECHANICAL_NS); |
||
34 | $modelImpl->undeclareAlternativeNamespace(self::YET_ANOTHER_NS); |
||
35 | } |
||
36 | |||
37 | public function testGetUniqueChildElementByNameNsForAlternativeNs(): void |
||
38 | { |
||
39 | $this->hedwig = $this->modelInstance->getModelElementById("hedwig"); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
40 | $this->assertFalse(empty($this->hedwig)); |
||
41 | $childElementByNameNs = $this->hedwig->getUniqueChildElementByNameNs(TestModelConstants::NEWER_NAMESPACE, "wings"); |
||
42 | $this->assertFalse(empty($childElementByNameNs)); |
||
43 | $this->assertEquals("wusch", $childElementByNameNs->getTextContent()); |
||
44 | } |
||
45 | |||
46 | public function testGetUniqueChildElementByNameNsForSecondAlternativeNs(): void |
||
47 | { |
||
48 | // given |
||
49 | $donald = $this->modelInstance->getModelElementById("donald"); |
||
50 | |||
51 | // when |
||
52 | $childElementByNameNs = $donald->getUniqueChildElementByNameNs(TestModelConstants::NEWER_NAMESPACE, "wings"); |
||
53 | |||
54 | // then |
||
55 | $this->assertFalse(empty($childElementByNameNs)); |
||
56 | $this->assertEquals("flappy", $childElementByNameNs->getTextContent()); |
||
57 | } |
||
58 | |||
59 | public function testGetChildElementsByTypeForAlternativeNs(): void |
||
60 | { |
||
61 | $birdo = $this->modelInstance->getModelElementById("birdo"); |
||
62 | $this->assertFalse(empty($birdo)); |
||
63 | |||
64 | $elements = $birdo->getChildElementsByType(Wings::class); |
||
65 | $this->assertCount(1, $elements); |
||
66 | $this->assertEquals("zisch", $elements[0]->getTextContent()); |
||
67 | } |
||
68 | |||
69 | public function testGetChildElementsByTypeForSecondAlternativeNs(): void |
||
70 | { |
||
71 | // given |
||
72 | $donald = $this->modelInstance->getModelElementById("donald"); |
||
73 | |||
74 | // when |
||
75 | $elements = $donald->getChildElementsByType(Wings::class); |
||
76 | |||
77 | // then |
||
78 | $this->assertCount(1, $elements); |
||
79 | $this->assertEquals("flappy", $elements[0]->getTextContent()); |
||
80 | } |
||
81 | |||
82 | public function testGetAttributeValueNsForAlternativeNs(): void |
||
83 | { |
||
84 | $plucky = $this->modelInstance->getModelElementById("plucky"); |
||
85 | $this->assertFalse(empty($plucky)); |
||
86 | |||
87 | $this->assertFalse($plucky->canHaveExtendedWings()); |
||
88 | } |
||
89 | |||
90 | public function testGetAttributeValueNsForSecondAlternativeNs(): void |
||
91 | { |
||
92 | // given |
||
93 | $donald = $this->modelInstance->getModelElementById("donald"); |
||
94 | |||
95 | // when |
||
96 | $extendedWings = $donald->canHaveExtendedWings(); |
||
0 ignored issues
–
show
|
|||
97 | |||
98 | // then |
||
99 | $this->assertTrue($donald->canHaveExtendedWings()); |
||
100 | } |
||
101 | |||
102 | public function testModifyingAttributeWithAlternativeNamespaceKeepsAlternativeNamespace(): void |
||
103 | { |
||
104 | $plucky = $this->modelInstance->getModelElementById("plucky"); |
||
105 | $this->assertFalse(empty($plucky)); |
||
106 | //validate old value |
||
107 | |||
108 | $this->assertFalse($plucky->canHaveExtendedWings()); |
||
109 | //change it |
||
110 | $plucky->setCanHaveExtendedWings(true); |
||
111 | |||
112 | $attributeValueNs = $plucky->getAttributeValueNs(self::MECHANICAL_NS, "canHaveExtendedWings"); |
||
113 | $this->assertEquals("true", $attributeValueNs); |
||
114 | } |
||
115 | |||
116 | public function testModifyingAttributeWithSecondAlternativeNamespaceKeepsSecondAlternativeNamespace(): void |
||
117 | { |
||
118 | // given |
||
119 | $donald = $this->modelInstance->getModelElementById("donald"); |
||
120 | |||
121 | // when |
||
122 | $donald->setCanHaveExtendedWings(false); |
||
123 | |||
124 | // then |
||
125 | $attributeValueNs = $donald->getAttributeValueNs(self::YET_ANOTHER_NS, "canHaveExtendedWings"); |
||
126 | $this->assertEquals("false", $attributeValueNs); |
||
127 | } |
||
128 | |||
129 | public function testModifyingAttributeWithNewNamespaceKeepsNewNamespace(): void |
||
130 | { |
||
131 | $bird = $this->createBird($this->modelInstance, "waldo", Gender::MALE); |
||
132 | $bird->setCanHaveExtendedWings(true); |
||
133 | $attributeValueNs = $bird->getAttributeValueNs(TestModelConstants::NEWER_NAMESPACE, "canHaveExtendedWings"); |
||
134 | $this->assertEquals("true", $attributeValueNs); |
||
135 | } |
||
136 | |||
137 | public function testModifyingElementWithAlternativeNamespaceKeepsAlternativeNamespace(): void |
||
138 | { |
||
139 | $birdo = $this->modelInstance->getModelElementById("birdo"); |
||
140 | $this->assertFalse($birdo === null); |
||
141 | $wings = $birdo->getWings(); |
||
142 | $this->assertFalse($wings === null); |
||
143 | $wings->setTextContent("kawusch"); |
||
144 | |||
145 | $childElementsByNameNs = $birdo->getDomElement()->getChildElementsByNameNs(self::MECHANICAL_NS, "wings"); |
||
146 | $this->assertCount(1, $childElementsByNameNs); |
||
147 | $this->assertEquals("kawusch", $childElementsByNameNs[0]->getTextContent()); |
||
148 | } |
||
149 | |||
150 | public function testModifyingElementWithSecondAlternativeNamespaceKeepsSecondAlternativeNamespace(): void |
||
151 | { |
||
152 | // given |
||
153 | $donald = $this->modelInstance->getModelElementById("donald"); |
||
154 | $wings = $donald->getWings(); |
||
155 | |||
156 | // when |
||
157 | $wings->setTextContent("kawusch"); |
||
158 | |||
159 | // then |
||
160 | $childElementsByNameNs = $donald->getDomElement()->getChildElementsByNameNs(self::YET_ANOTHER_NS, "wings"); |
||
161 | $this->assertCount(1, $childElementsByNameNs); |
||
162 | $this->assertEquals("kawusch", $childElementsByNameNs[0]->getTextContent()); |
||
163 | } |
||
164 | |||
165 | public function testModifyingElementWithNewNamespaceKeepsNewNamespace(): void |
||
166 | { |
||
167 | $bird = $this->createBird($this->modelInstance, "waldo", Gender::MALE); |
||
168 | $bird->setWings($this->modelInstance->newInstance(Wings::class)); |
||
169 | |||
170 | $childElementsByNameNs = $bird->getDomElement()->getChildElementsByNameNs(TestModelConstants::NEWER_NAMESPACE, "wings"); |
||
171 | $this->assertCount(1, $childElementsByNameNs); |
||
172 | } |
||
173 | |||
174 | public function testUseExistingNamespace(): void |
||
175 | { |
||
176 | $this->assertThatThereIsNoNewerNamespaceUrl(); |
||
177 | $plucky = $this->modelInstance->getModelElementById("plucky"); |
||
178 | $plucky->setAttributeValueNs(self::MECHANICAL_NS, "canHaveExtendedWings", "true"); |
||
179 | |||
180 | $donald = $this->modelInstance->getModelElementById("donald"); |
||
181 | $donald->setAttributeValueNs(self::YET_ANOTHER_NS, "canHaveExtendedWings", "false"); |
||
182 | $this->assertThatThereIsNoNewerNamespaceUrl(); |
||
183 | |||
184 | $this->assertTrue($plucky->canHaveExtendedWings()); |
||
185 | $this->assertThatThereIsNoNewerNamespaceUrl(); |
||
186 | } |
||
187 | |||
188 | protected function assertThatThereIsNoNewerNamespaceUrl(): void |
||
189 | { |
||
190 | $rootElement = $this->modelInstance->getDocument()->getDomSource()->documentElement; |
||
191 | foreach ($rootElement->attributes as $attr) { |
||
192 | $nodeValue = $attr->nodeValue; |
||
193 | $this->assertFalse(TestModelConstants::NEWER_NAMESPACE == $nodeValue); |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 |