Skip to content

Commit accfc1c

Browse files
committed
Merge pull request #5 from fusionspim/hotfix/catch-domdocument-warnings
Convert DOMDocument warnings to exception
2 parents 54edd12 + 6ab7459 commit accfc1c

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
.DS_Store
44
out
55
vendor
6+
tests/output/*
7+
.gitkeep

src/Document.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,11 @@ public function save($path)
142142
{
143143
$this->build();
144144

145-
$this->dom->save($path);
145+
$results = $this->dom->save($path);
146146

147147
Xml::validate($path);
148+
149+
return $results > 0;
148150
}
149151

150152
/**

src/Xml.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace DemandwareXml;
33

44
use \DOMDocument;
5+
use \DemandwareXml\XmlException;
56

67
class Xml
78
{
@@ -38,6 +39,10 @@ public static function validate($filePath)
3839

3940
libxml_use_internal_errors(true);
4041

42+
set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) {
43+
throw new XmlException($errstr, $errno);
44+
});
45+
4146
// possibly more efficient to pass a $dom object rather than save/reload, but cleaner to assume already saved
4247
$dom = new DOMDocument('1.0', 'UTF-8');
4348
$dom->preserveWhiteSpace = false;
@@ -46,6 +51,8 @@ public static function validate($filePath)
4651
$dom->normalizeDocument();
4752
$dom->save($filePath);
4853

54+
restore_error_handler();
55+
4956
if (! $dom->schemaValidate(realpath(__DIR__ . '/../xsd/catalog.xsd'))) {
5057
throw new XmlException(static::errorSummary());
5158
}

tests/ProductsTest.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
class ProductsTest extends AbstractTest
1010
{
11-
public function testProductsXml()
11+
protected $document;
12+
13+
public function setUp()
1214
{
1315
$document = new Document('TestCatalog');
1416

@@ -50,9 +52,37 @@ public function testProductsXml()
5052
$document->addObject($element);
5153
}
5254

55+
$this->document = $document;
56+
}
57+
58+
public function tearDown()
59+
{
60+
$this->document = null;
61+
}
62+
63+
public function testProductsXml()
64+
{
5365
$sampleXml = $this->loadFixture('products.xml');
54-
$outputXml = $document->getDomDocument();
66+
$outputXml = $this->document->getDomDocument();
5567

5668
$this->assertEqualXMLStructure($sampleXml->firstChild, $outputXml->firstChild);
5769
}
70+
71+
/**
72+
* @expectedException \DemandwareXml\XmlException
73+
* @expectedExceptionMessageRegExp /Entity 'bull' not defined/
74+
*/
75+
public function testProductsInvalidEntitiesException()
76+
{
77+
$element = new Product('product123');
78+
$element->setName('product number 123 •');
79+
80+
$this->document->addObject($element);
81+
$this->document->save(__DIR__ . '/output/products.xml');
82+
}
83+
84+
public function testProductsSaveXml()
85+
{
86+
$this->assertTrue($this->document->save(__DIR__ . '/output/products.xml'));
87+
}
5888
}

tests/output/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)