Skip to content

Commit

Permalink
Write any remaining attributes to the urlset element before writing s…
Browse files Browse the repository at this point in the history
…ub-elements (#34)

* Write any remaining attributes to the urlset element before writing sub-elements.

Fixes #33.

* Fix tests
  • Loading branch information
lushc authored and ThePixelDeveloper committed Feb 20, 2017
1 parent 5a1d8dd commit bfb14b6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 21 deletions.
17 changes: 0 additions & 17 deletions spec/UrlSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Thepixeldeveloper\Sitemap\Subelements\Image;
use XMLWriter;

class UrlSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -38,19 +36,4 @@ function it_should_have_a_priority()
{
$this->getPriority()->shouldReturn(null);
}

function it_should_only_append_attributes_once_for_each_subelement_type(XMLWriter $xmlWriter, Image $image)
{
$xmlWriter->startElement('url')->shouldBeCalled();
$xmlWriter->writeElement('loc', 'http://www.example.com/')->shouldBeCalled();
$xmlWriter->endElement()->shouldBeCalled();

$this->addSubElement($image);
$this->addSubElement($image);

$image->appendAttributeToCollectionXML($xmlWriter)->shouldBeCalled();
$image->generateXML($xmlWriter)->shouldBeCalled();

$this->generateXML($xmlWriter);
}
}
25 changes: 25 additions & 0 deletions spec/UrlsetSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Thepixeldeveloper\Sitemap\Url;
use Thepixeldeveloper\Sitemap\Subelements\Image;
use Thepixeldeveloper\Sitemap\Subelements\Video;
use XMLWriter;

class UrlsetSpec extends ObjectBehavior
{
Expand All @@ -24,4 +27,26 @@ function it_should_return_the_urls_added(Url $url)

$this->getUrls()->shouldReturn([$url]);
}

function it_should_only_append_attributes_once_for_each_subelement_type(XMLWriter $xmlWriter, Url $url, Image $image, Video $video)
{
$xmlWriter->startElement('urlset')->shouldBeCalled();
$xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance')->shouldBeCalled();
$xmlWriter->writeAttribute('xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 ' . 'http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd')->shouldBeCalled();
$xmlWriter->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9')->shouldBeCalled();

$url->getSubelementsThatAppend()->willReturn([$image, $video]);
$this->appendSubelementAttribute($xmlWriter, $image)->shouldReturn(true);
$this->appendSubelementAttribute($xmlWriter, $image)->shouldReturn(false);
$this->appendSubelementAttribute($xmlWriter, $video)->shouldReturn(true);
$this->appendSubelementAttribute($xmlWriter, $video)->shouldReturn(false);

$image->appendAttributeToCollectionXML($xmlWriter)->shouldBeCalled();
$video->appendAttributeToCollectionXML($xmlWriter)->shouldBeCalled();
$url->generateXML($xmlWriter)->shouldBeCalled();
$xmlWriter->endElement()->shouldBeCalled();

$this->addUrl($url);
$this->generateXML($xmlWriter);
}
}
4 changes: 0 additions & 4 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ public function __construct($loc)
*/
public function generateXML(XMLWriter $XMLWriter)
{
foreach ($this->getSubelementsThatAppend() as $subelement) {
$subelement->appendAttributeToCollectionXML($XMLWriter);
}

$XMLWriter->startElement('url');
$XMLWriter->writeElement('loc', $this->getLoc());

Expand Down
33 changes: 33 additions & 0 deletions src/Urlset.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class Urlset implements OutputInterface
*/
protected $urls = [];

/**
* Sub-elements that have been appended to the collection attributes.
*
* @var AppendAttributeInterface[]
*/
protected $appendedSubelements = [];

/**
* Add a new URL object.
*
Expand Down Expand Up @@ -47,6 +54,12 @@ public function generateXML(XMLWriter $XMLWriter)

$XMLWriter->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');

foreach ($this->getUrls() as $url) {
foreach ($url->getSubelementsThatAppend() as $subelement) {
$this->appendSubelementAttribute($XMLWriter, $subelement);
}
}

foreach ($this->getUrls() as $url) {
$url->generateXML($XMLWriter);
}
Expand All @@ -63,4 +76,24 @@ public function getUrls()
{
return $this->urls;
}

/**
* Appends the sub-element to the collection attributes if it has yet to be visited.
*
* @param XMLWriter $XMLWriter
* @param OutputInterface $subelement
*
* @return boolean
*/
public function appendSubelementAttribute(XMLWriter $XMLWriter, OutputInterface $subelement)
{
if (array_key_exists(get_class($subelement), $this->appendedSubelements)) {
return false;
}

$subelement->appendAttributeToCollectionXML($XMLWriter);
$this->appendedSubelements[get_class($subelement)] = $subelement;

return true;
}
}

0 comments on commit bfb14b6

Please sign in to comment.