Skip to content

Commit 3b01e32

Browse files
committed
2.0.2
1 parent 634dc40 commit 3b01e32

File tree

3 files changed

+81
-44
lines changed

3 files changed

+81
-44
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
xml-constrcutor
22
===
33

4+
v2.0.2 [2023-01-07]
5+
---
6+
7+
- Added some tests.
8+
- Small fixes and improvements.
9+
410
v2.0.1 [2023-01-07]
511
---
612

src/XmlConstructor.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,15 @@
5757
class XmlConstructor
5858
{
5959
/**
60-
* @var boolean
60+
* @var bool
6161
*/
6262
private $flushed = false;
6363
/**
64-
* @var boolean
64+
* @var bool
65+
*/
66+
private $made = false;
67+
/**
68+
* @var bool
6569
*/
6670
private $hasDocumentStart = false;
6771
/**
@@ -140,6 +144,12 @@ public function __construct(array $config = [])
140144
*/
141145
public function fromArray(array $in)
142146
{
147+
if ($this->made) {
148+
throw new RuntimeException('XML document is made already.');
149+
}
150+
151+
$this->made = true;
152+
143153
if ($this->flushed) {
144154
throw new RuntimeException('The constructor is closed. You have to create new one to create an XML document'
145155
. ' again.');

tests/XmlConstructTest.php

Lines changed: 63 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
use bupy7\xml\constructor\RuntimeException;
66
use bupy7\xml\constructor\XmlConstructor;
77

8-
use function preg_replace;
9-
108
/**
119
* @since 1.2.1
1210
*/
@@ -112,6 +110,20 @@ public function testDefaultDocument2()
112110
$this->assertEquals($expected, $out);
113111
}
114112

113+
/**
114+
* @since 2.0.2
115+
*/
116+
public function testDefaultDocument3()
117+
{
118+
$expected = <<<XML
119+
<?xml version="1.0" encoding="UTF-8"?>
120+
121+
XML;
122+
$xml = new XmlConstructor();
123+
$out = $xml->toOutput();
124+
$this->assertEquals($expected, $out);
125+
}
126+
115127
/**
116128
* @since 2.0.1
117129
*/
@@ -247,10 +259,11 @@ public function testCdataContent()
247259
<tag1><![CDATA[<b>content1</b>]]></tag1>
248260
<tag2>content2</tag2>
249261
</root>
262+
250263
XML;
251264
$xml = new XmlConstructor();
252265
$out = $xml->fromArray($this->in3)->toOutput();
253-
$this->assertEquals($this->prepare($expected), $this->prepare($out));
266+
$this->assertEquals($expected, $out);
254267
}
255268

256269
/**
@@ -260,73 +273,81 @@ public function testInvalidConfiguration()
260273
{
261274
$expected = <<<XML
262275
<?xml version="1.0" encoding="UTF-8"?>
276+
263277
XML;
264278
$xml = new XmlConstructor();
265279
$out = $xml->fromArray(['incorrect' => 'array'])->toOutput();
266-
$this->assertEquals($this->prepare($expected), $this->prepare($out));
280+
$this->assertEquals($expected, $out);
267281
}
268282

269283
/**
270-
* @since 2.0.0
284+
* @since 2.0.2
271285
*/
272-
public function testDoubleToOutput()
286+
public function testDoubleToOutput1()
273287
{
274-
$expected = <<<XML
275-
<?xml version="1.0" encoding="UTF-8"?>
276-
<root>
277-
<tag1 attr1="val1" attr2="val2"/>
278-
<tag2>content2</tag2>
279-
<tag3>
280-
<tag4>content4</tag4>
281-
</tag3>
282-
</root>
283-
XML;
284288
$xml = new XmlConstructor();
285289

286-
$out = $xml->fromArray($this->in1)->toOutput();
287-
$this->assertEquals($this->prepare($expected), $this->prepare($out));
290+
$xml->fromArray($this->in1)->toOutput();
288291

289292
$this->expectException(RuntimeException::class);
290293
$this->expectExceptionMessage('The constructor is closed. You have to create new one to flush its again.');
291294
$xml->toOutput();
292295
}
293296

294297
/**
295-
* @since 2.0.0
298+
* @since 2.0.2
296299
*/
297-
public function testDoubleFromArray()
300+
public function testDoubleToOutput2()
298301
{
299-
$expected = <<<XML
300-
<?xml version="1.1" encoding="ASCII"?>
301-
<root>
302-
<tag1 attr1="val1" attr2="val2"/>
303-
<tag2>content2</tag2>
304-
<tag3>
305-
<tag4>content4</tag4>
306-
</tag3>
307-
</root>
302+
$xml = new XmlConstructor();
308303

309-
XML;
310-
$xml = new XmlConstructor([
311-
'startDocument' => ['1.1', 'ASCII'],
312-
'indentString' => ' ',
313-
]);
304+
$xml->toOutput();
314305

315-
$out = $xml->fromArray($this->in1)->toOutput();
316-
$this->assertEquals($expected, $out);
306+
$this->expectException(RuntimeException::class);
307+
$this->expectExceptionMessage('The constructor is closed. You have to create new one to flush its again.');
308+
$xml->toOutput();
309+
}
310+
311+
/**
312+
* @since 2.0.2
313+
*/
314+
public function testDoubleFromArray1()
315+
{
316+
$xml = new XmlConstructor();
317+
318+
$xml->fromArray($this->in1);
317319

318320
$this->expectException(RuntimeException::class);
319-
$this->expectExceptionMessage('The constructor is closed. You have to create new one to create '
320-
. 'an XML document again.');
321+
$this->expectExceptionMessage('XML document is made already.');
321322
$xml->fromArray($this->in1);
322323
}
323324

324325
/**
325-
* @param string $xml
326-
* @return string
326+
* @since 2.0.2
327327
*/
328-
private function prepare($xml)
328+
public function testDoubleFromArray2()
329329
{
330-
return preg_replace('/\s/', '', $xml);
330+
$xml = new XmlConstructor();
331+
332+
$xml->fromArray($this->in1)->toOutput();
333+
334+
$this->expectException(RuntimeException::class);
335+
$this->expectExceptionMessage('XML document is made already.');
336+
$xml->fromArray($this->in1);
337+
}
338+
339+
/**
340+
* @since 2.0.3
341+
*/
342+
public function testDoubleFromArray3()
343+
{
344+
$xml = new XmlConstructor();
345+
346+
$xml->toOutput();
347+
348+
$this->expectException(RuntimeException::class);
349+
$this->expectExceptionMessage('The constructor is closed. You have to create new one to create an XML '
350+
. 'document again.');
351+
$xml->fromArray($this->in1);
331352
}
332353
}

0 commit comments

Comments
 (0)