Skip to content

Commit

Permalink
online-from and online-to cannot be empty (#82)
Browse files Browse the repository at this point in the history
* online-from/to

* Remove unnecessary test and amend other fixtures

* Add nilEmptyWriter and amend product tests/fixtures

* Add test and remove an unnecessary method

* Categories test and tidy up

* Update CategoriesTest.php

* Amend and fix tests

* Swap params over - maybe I had gone mad yesterday

* Add more tests

* Amend test

Co-authored-by: maxakropolis <andy@andystafford.co.uk>
  • Loading branch information
maxakropolis and maxakropolis authored Feb 3, 2021
1 parent 1f6efc4 commit 55099d7
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/Writer/EntityWriter/CategoryXmlWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public function write(): void
$this->writer->ifNotEmpty()->writeAttribute('category-id', $this->category->id);
$this->writer->ifNotEmpty()->writeElementWithAttributes('display-name', $this->category->displayName, ['xml:lang' => 'x-default']);
$this->writer->ifNotEmpty()->writeElement('online-flag', XmlFormatter::fromBoolean($this->category->onlineFlag));
$this->writer->ifNotEmpty()->writeElement('online-from', XmlFormatter::fromDateTime($this->category->onlineFrom));
$this->writer->ifNotEmpty()->writeElement('online-to', XmlFormatter::fromDateTime($this->category->onlineTo));
$this->writer->nilIfEmpty()->writeElement('online-from', XmlFormatter::fromDateTime($this->category->onlineFrom));
$this->writer->nilIfEmpty()->writeElement('online-to', XmlFormatter::fromDateTime($this->category->onlineTo));
$this->writer->ifNotEmpty()->writeElement('parent', $this->category->parentId);
$this->writeTemplate();
$this->writer->ifNotEmpty()->writeElement('sitemap-included-flag', XmlFormatter::fromBoolean($this->category->sitemapIncludedFlag));
Expand Down
4 changes: 2 additions & 2 deletions src/Writer/EntityWriter/ProductXmlWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public function write(): void
$this->writer->ifNotEmpty()->writeElementWithAttributes('display-name', $this->product->displayName, ['xml:lang' => 'x-default']);
$this->writer->ifNotEmpty()->writeElementWithAttributes('long-description', XmlFormatter::sanitise($this->product->longDescription), ['xml:lang' => 'x-default']);
$this->writer->ifNotEmpty()->writeElement('online-flag', XmlFormatter::fromBoolean($this->product->onlineFlag));
$this->writer->writeElement('online-from', XmlFormatter::fromDateTime($this->product->onlineFrom));
$this->writer->writeElement('online-to', XmlFormatter::fromDateTime($this->product->onlineTo));
$this->writer->nilIfEmpty()->writeElement('online-from', XmlFormatter::fromDateTime($this->product->onlineFrom));
$this->writer->nilIfEmpty()->writeElement('online-to', XmlFormatter::fromDateTime($this->product->onlineTo));
$this->writer->ifNotEmpty()->writeElement('available-flag', XmlFormatter::fromBoolean($this->product->availableFlag));
$this->writer->ifNotEmpty()->writeElement('searchable-flag', XmlFormatter::fromBoolean($this->product->searchableFlag));
$this->writer->ifNotEmpty()->writeElement('searchable-if-unavailable-flag', XmlFormatter::fromBoolean($this->product->searchableIfUnavailableFlag));
Expand Down
46 changes: 46 additions & 0 deletions src/Writer/Xml/NilEmptyWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace DemandwareXml\Writer\Xml;

class NilEmptyWriter
{
private $writer;

public function __construct(XmlWriter $writer)
{
$this->writer = $writer;
}

public function writeElement($name, $content = null): bool
{
if (XmlFormatter::isEmptyValue($content)) {
return $this->writeElementWithAttributes($name);
}

return $this->writer->writeElement($name, $content);
}

public function writeAttribute($name, $value): bool
{
return $this->writer->writeAttribute($name, $value);
}

public function writeElementWithAttributes($name, $content = null, array $attributes = []): bool
{
$this->writer->startElement($name);

foreach ($attributes as $attrName => $attrContent) {
$this->writeAttribute($attrName, $attrContent);
}

if (XmlFormatter::isEmptyValue($content)) {
$this->writeAttribute('xsi:nil', 'true');
$this->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
} else {
$this->writer->text($content);
}

$this->writer->endElement();

return true;
}
}
10 changes: 10 additions & 0 deletions src/Writer/Xml/XmlWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class XmlWriter extends PhpXmlWriter
public const INDENT_SPACE = ' ';

private $notEmptyWriter;
private $nilEmptyWriter;
private $bufferLimit = 100;
private $entityCount = 0;

Expand Down Expand Up @@ -75,6 +76,15 @@ public function ifNotEmpty(): NotEmptyWriter
return $this->notEmptyWriter;
}

public function nilIfEmpty(): NilEmptyWriter
{
if (! $this->nilEmptyWriter) {
$this->nilEmptyWriter = new NilEmptyWriter($this);
}

return $this->nilEmptyWriter;
}

public function writeElementWithAttributes($name, $content = null, array $attributes = []): bool
{
$this->startElement($name);
Expand Down
40 changes: 32 additions & 8 deletions tests/Writer/CategoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use DateTimeImmutable;
use DemandwareXml\Test\FixtureHelper;
use DemandwareXml\Writer\Entity\{Assignment, Category, CustomAttribute, DeletedAssignment, DeletedCategory};
use DemandwareXml\Writer\Entity\{Assignment, Category, DeletedAssignment, DeletedCategory};
use DemandwareXml\Writer\Xml\XmlWriter;
use PHPUnit\Framework\TestCase;

Expand All @@ -19,22 +19,46 @@ public function test_categories_xml(): void
$xml->startDocument();
$xml->startCatalog('TestCatalog');

foreach (['Socks', 'Death Stars', 'Donuts'] as $index => $example) {
$element = new Category('CAT' . $index);
$element->setDisplayName($example);
$categories = [
'Socks' => [
'index' => 0,
'dates' => [
'from' => new DateTimeImmutable('2018-01-01 01:01:01'),
'to' => new DateTimeImmutable('2018-02-02 02:02:02'),
],
],
'Death Stars' => [
'index' => 1,
'dates' => [
'from' => null,
'to' => new DateTimeImmutable('2018-02-02 02:02:02'),
],
],
'Donuts' => [
'index' => 2,
'dates' => [
'from' => new DateTimeImmutable('2018-01-01 01:01:01'),
'to' => null,
],
],
];

foreach ($categories as $title => $data) {
$element = new Category('CAT' . $data['index']);
$element->setDisplayName($title);
$element->setParent('CAT0');
$element->setTemplate('cat-listings.html');
$element->setOnlineFlag(true);
$element->setSitemap(0.2);
$element->setPageAttributes($example, 'Buy ' . $example, mb_strtolower($example), '/' . $example);
$element->setPageAttributes($title, 'Buy ' . $title, mb_strtolower($title), '/' . $title);
$element->setOnlineFromTo(
new DateTimeImmutable('2018-01-01 01:01:01'),
new DateTimeImmutable('2018-02-02 02:02:02')
$data['dates']['from'],
$data['dates']['to']
);

$element->addCustomAttributes([
'itemsPerPage' => 30,
'promoMast' => 'cat' . $index . '-banner.png',
'promoMast' => 'cat' . $data['index'] . '-banner.png',
'hasOffers' => true,
]);

Expand Down
45 changes: 37 additions & 8 deletions tests/Writer/Xml/XmlWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,34 @@ public function test_write_empty_element_with_attributes(): void
$this->assertXmlStringEqualsXmlString('<test foo="bar"/>', $xml->outputMemory(true));
}

public function test_write_nil_element(): void
{
$xml = $this->getMemoryXmlWriter();
$xml->nilIfEmpty()->writeElement('test');
$this->assertXmlStringEqualsXmlString('<test xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>', $xml->outputMemory(true));
}

public function test_write_nil_element_with_empty_string(): void
{
$xml = $this->getMemoryXmlWriter();
$xml->nilIfEmpty()->writeElement('test', '');
$this->assertXmlStringEqualsXmlString('<test xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>', $xml->outputMemory(true));
}

public function test_write_nil_element_with_string(): void
{
$xml = $this->getMemoryXmlWriter();
$xml->nilIfEmpty()->writeElement('test', 'content');
$this->assertXmlStringEqualsXmlString('<test>content</test>', $xml->outputMemory(true));
}

public function test_write_nil_element_with_attributes(): void
{
$xml = $this->getMemoryXmlWriter();
$xml->nilIfEmpty()->writeElementWithAttributes('test', null, ['other' => 'value']);
$this->assertXmlStringEqualsXmlString('<test other="value" xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>', $xml->outputMemory(true));
}

public function test_write_flushable_entity(): void
{
$output = TEST_OUTPUT_DIR . '/catalog_auto_flush.xml';
Expand All @@ -92,15 +120,16 @@ public function test_write_flushable_entity(): void

$xml->writeFlushableEntity(new Product('PRD000001'));
$xml->writeFlushableEntity(new Product('PRD000002'));

$this->assertStringContainsString(
<<<'XML'
<product product-id="PRD000001">
<online-from></online-from>
<online-to></online-to>
<online-from xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<online-to xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</product>
<product product-id="PRD000002">
<online-from></online-from>
<online-to></online-to>
<online-from xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<online-to xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</product>
XML,
trim(file_get_contents($output))
Expand All @@ -111,12 +140,12 @@ public function test_write_flushable_entity(): void
$this->assertStringContainsString(
<<<'XML'
<product product-id="PRD000003">
<online-from></online-from>
<online-to></online-to>
<online-from xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<online-to xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</product>
<product product-id="PRD000004">
<online-from></online-from>
<online-to></online-to>
<online-from xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<online-to xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</product>
XML,
trim(file_get_contents($output))
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/categories.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<category category-id="CAT1">
<display-name xml:lang="x-default">Death Stars</display-name>
<online-flag>true</online-flag>
<online-from>2018-01-01T01:01:01</online-from>
<online-from xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<online-to>2018-02-02T02:02:02</online-to>
<parent>CAT0</parent>
<template>cat-listings.html</template>
Expand All @@ -48,7 +48,7 @@
<display-name xml:lang="x-default">Donuts</display-name>
<online-flag>true</online-flag>
<online-from>2018-01-01T01:01:01</online-from>
<online-to>2018-02-02T02:02:02</online-to>
<online-to xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<parent>CAT0</parent>
<template>cat-listings.html</template>
<sitemap-included-flag>true</sitemap-included-flag>
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/products.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
<product product-id="PRD12340000">
<display-name xml:lang="x-default">Minimal Product</display-name>
<long-description xml:lang="x-default">This minimal product tests how empty fields are output</long-description>
<online-from></online-from>
<online-to></online-to>
<online-from xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<online-to xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</product>
<product product-id="SET123">
<upc>500000000001</upc>
Expand Down

0 comments on commit 55099d7

Please sign in to comment.