Skip to content

Commit

Permalink
Merge pull request #185 from alexdebril/issue/184
Browse files Browse the repository at this point in the history
Issue/184
  • Loading branch information
alexdebril authored Jun 22, 2018
2 parents 54cc6d5 + b7f1e6d commit ed66d0f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
19 changes: 16 additions & 3 deletions src/FeedIo/Rule/Atom/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,23 @@ public function setProperty(NodeInterface $node, \DOMElement $element) : void
public function createCategoryElement(\DomDocument $document, CategoryInterface $category) : \DOMElement
{
$element = $document->createElement($this->getNodeName());
$element->setAttribute('scheme', $category->getScheme() ?? '');
$element->setAttribute('term', $category->getTerm() ?? '');
$element->setAttribute('label', $category->getLabel() ?? '');
$this->setNonEmptyAttribute($element, 'scheme', $category->getScheme());
$this->setNonEmptyAttribute($element, 'term', $category->getTerm());
$this->setNonEmptyAttribute($element, 'label', $category->getLabel());

return $element;
}

/**
* Sets the attribute only if the value is not emtpy
* @param DomElement $element
* @param string $name
* @param string $value
*/
protected function setNonEmptyAttribute(\DomElement $element, string $name, string $value = null) : void
{
if (! is_null($value)) {
$element->setAttribute($name, $value);
}
}
}
14 changes: 7 additions & 7 deletions tests/FeedIo/Rule/Atom/CategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function setUp()
{
$this->object = new Category();
}

public function testSetProperty()
{
$item = new Item();
Expand All @@ -36,27 +36,27 @@ public function testSetProperty()
$element->setAttribute('label', 'FooBar');
$element->setAttribute('term', 'foobar');
$this->object->setProperty($item, $element);

$count = 0;
foreach ($item->getCategories() as $category) {
$count++;
$this->assertEquals('foobar', $category->getTerm());
$this->assertEquals('FooBar', $category->getLabel());
$this->assertEquals('http', $category->getScheme());
}

$this->assertEquals(1, $count);
}

public function testCreateCategoryElement()
{
$category = new \FeedIo\Feed\Node\Category();
$category->setLabel('Foo');
$category->setTerm('foo');
$category->setScheme('bar');

$element = $this->object->createCategoryElement(new \DomDocument(), $category);

$this->assertEquals('Foo', $element->getAttribute('label'));
$this->assertEquals('foo', $element->getAttribute('term'));
$this->assertEquals('bar', $element->getAttribute('scheme'));
Expand Down Expand Up @@ -89,6 +89,6 @@ public function testCreateElement()
$this->assertEquals('category', $nextElement->nodeName);

$document->appendChild($rootElement);
$this->assertXmlStringEqualsXmlString('<feed><category scheme="" term="" label="foo"/><category scheme="" term="" label="bar"/></feed>', $document->saveXML());
$this->assertXmlStringEqualsXmlString('<feed><category label="foo"/><category label="bar"/></feed>', $document->saveXML());
}
}

0 comments on commit ed66d0f

Please sign in to comment.