Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
.DS_Store
out
vendor
tests/output/*
.gitkeep
4 changes: 3 additions & 1 deletion src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,11 @@ public function save($path)
{
$this->build();

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

Xml::validate($path);

return $results > 0;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace DemandwareXml;

use \DOMDocument;
use \DemandwareXml\XmlException;

class Xml
{
Expand Down Expand Up @@ -38,6 +39,10 @@ public static function validate($filePath)

libxml_use_internal_errors(true);

set_error_handler(function ($severity, $message, $file, $line) {
throw new XmlException($message, $severity, $severity, $file, $line);
});

// possibly more efficient to pass a $dom object rather than save/reload, but cleaner to assume already saved
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->preserveWhiteSpace = false;
Expand All @@ -46,6 +51,8 @@ public static function validate($filePath)
$dom->normalizeDocument();
$dom->save($filePath);

restore_error_handler();

if (! $dom->schemaValidate(realpath(__DIR__ . '/../xsd/catalog.xsd'))) {
throw new XmlException(static::errorSummary());
}
Expand Down
35 changes: 33 additions & 2 deletions tests/ProductsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

class ProductsTest extends AbstractTest
{
public function testProductsXml()
protected $document;

public function setUp()
{
$document = new Document('TestCatalog');

Expand Down Expand Up @@ -50,9 +52,38 @@ public function testProductsXml()
$document->addObject($element);
}

$this->document = $document;
}

public function tearDown()
{
$this->document = null;
}

public function testProductsXml()
{
$sampleXml = $this->loadFixture('products.xml');
$outputXml = $document->getDomDocument();
$outputXml = $this->document->getDomDocument();

$this->assertEqualXMLStructure($sampleXml->firstChild, $outputXml->firstChild);
}

/**
* @expectedException \DemandwareXml\XmlException
* @expectedExceptionMessageRegExp /Entity 'bull' not defined/
*/
public function testProductsInvalidEntitiesException()
{
$element = new Product('product123');
$element->setName('product number 123 •');

$this->document->addObject($element);

$this->assertTrue($this->document->save(__DIR__ . '/output/products.xml'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, do we need this assert?

}

public function testProductsSaveXml()
{
$this->assertTrue($this->document->save(__DIR__ . '/output/products.xml'));
}
}
Empty file added tests/output/.gitkeep
Empty file.