Skip to content

Commit

Permalink
General cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisLandry committed Nov 9, 2012
1 parent 193de2d commit 24a225b
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 142 deletions.
8 changes: 5 additions & 3 deletions libraries/joomla/feed/factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ public function getFeed($uri)
// Open the URI within the stream reader.
if (!$reader->open($uri, null, LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_NOWARNING))
{
// @codeCoverageIgnoreStart
throw new RuntimeException('Unable to open the feed.');
// @codeCoverageIgnoreEnd
}

try
{
// Skip ahead to the root node.
while ($reader->read() && ($reader->nodeType !== XMLReader::ELEMENT));
do
{
$reader->read();
}
while ($reader->nodeType !== XMLReader::ELEMENT);
}
catch (Exception $e)
{
Expand Down
39 changes: 4 additions & 35 deletions libraries/joomla/feed/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ abstract class JFeedParser
*/
protected $stream;

/**
* @var DOMDocument
* @since 12.3
*/
private $_node;

/**
* Constructor.
*
Expand All @@ -51,7 +45,6 @@ abstract class JFeedParser
*/
public function __construct(XMLReader $stream)
{
$this->_node = new DOMDocument;
$this->stream = $stream;
}

Expand All @@ -73,13 +66,14 @@ public function parse()
do
{
// Expand the element for processing.
$el = $this->expandToSimpleXml();
$el = new SimpleXMLElement($this->stream->readOuterXml());

// Get the list of namespaces used within this element.
$ns = $el->getNamespaces(true);

// Get an array of available namespace objects for the element.
$namespaces = array();

foreach ($ns as $prefix => $uri)
{
// Ignore the empty namespace prefix.
Expand All @@ -90,6 +84,7 @@ public function parse()

// Get the necessary namespace objects for the element.
$namespace = $this->fetchNamespace($prefix);

if ($namespace)
{
$namespaces[] = $namespace;
Expand Down Expand Up @@ -189,31 +184,6 @@ protected function processElement(JFeed $feed, SimpleXMLElement $el, array $name
}
}

/**
* Method to expand the current reader node into a SimpleXML node for more detailed reading
* and manipulation.
*
* @return SimpleXMLElement
*
* @since 12.3
* @throws RuntimeException
*/
protected function expandToSimpleXml()
{
// Whizbang! And now we have a SimpleXMLElement element from the current stream node. **MAGIC** :-)
$el = simplexml_import_dom($this->_node->importNode($this->stream->expand(), true));

// Let's take care of some sanity checking.
if (!($el instanceof SimpleXMLElement))
{
// @codeCoverageIgnoreStart
throw new RuntimeException('Unable to expand node to SimpleXML element.');
// @codeCoverageIgnoreEnd
}

return $el;
}

/**
* Method to get a namespace object for a given namespace prefix.
*
Expand All @@ -231,6 +201,7 @@ protected function fetchNamespace($prefix)
}

$className = get_class($this) . ucfirst($prefix);

if (class_exists($className))
{
$this->namespaces[$prefix] = new $className;
Expand Down Expand Up @@ -301,8 +272,6 @@ protected function moveToClosingElement()
}
}

// @codeCoverageIgnoreStart
throw new RuntimeException('Unable to find the closing XML node.');
// @codeCoverageIgnoreEnd
}
}
3 changes: 3 additions & 0 deletions libraries/joomla/feed/parser/rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ protected function handleWebmaster(JFeed $feed, SimpleXMLElement $el)

// This is really cheap parsing. Probably need to create a method to do this more robustly.
$name = null;

if (isset($tmp[1]))
{
$name = trim($tmp[1], ' ()');
Expand Down Expand Up @@ -367,6 +368,7 @@ protected function processFeedEntry(JFeedEntry $entry, SimpleXMLElement $el)

// Add the feed entry author if available.
$author = (string) $el->author;

if (!empty($author))
{
$entry->author = $this->processPerson($author);
Expand Down Expand Up @@ -410,6 +412,7 @@ protected function processPerson($data)

// This is really cheap parsing, but so far good enough. :)
$data = explode(' ', $data, 2);

if (isset($data[1]))
{
$person->name = trim($data[1], ' ()');
Expand Down
20 changes: 12 additions & 8 deletions tests/suites/unit/joomla/feed/JFeedEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,12 @@ public function testAddContributor()
$properties = TestReflection::getValue($this->_instance, 'properties');

// Make sure the contributor we added actually exists.
$this->assertTrue(in_array(
new JFeedPerson('Dennis Ritchie', 'dennis.ritchie@example.com'),
$properties['contributors']
));
$this->assertTrue(
in_array(
new JFeedPerson('Dennis Ritchie', 'dennis.ritchie@example.com'),
$properties['contributors']
)
);

$this->_instance->addContributor('Dennis Ritchie', 'dennis.ritchie@example.com');

Expand All @@ -230,10 +232,12 @@ public function testAddLink()
$properties = TestReflection::getValue($this->_instance, 'properties');

// Make sure the link we added actually exists.
$this->assertTrue(in_array(
$expected,
$properties['links']
));
$this->assertTrue(
in_array(
$expected,
$properties['links']
)
);

$this->_instance->addLink($expected);

Expand Down
128 changes: 49 additions & 79 deletions tests/suites/unit/joomla/feed/JFeedParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ public function testParse()
$this->_reader->XML($xml);

// Advance the reader to the first <tag1> element.
while ($this->_reader->read() && ($this->_reader->name != 'tag1'));
do
{
$this->_reader->read();
}
while ($this->_reader->name != 'tag1');

$parser->parse();
}
Expand Down Expand Up @@ -139,27 +143,26 @@ public function testProcessElementWithElement()
{
$el = new SimpleXMLElement('<element1></element1>');

// process element has a few dependencies that we need to pass:
// a JFeed object, an element, and namespaces
// Process element has a few dependencies that we need to pass: a JFeed object, an element, and namespaces.
$feed = $this->getMockBuilder('JFeed')
->disableOriginalConstructor()
->getMock();
->disableOriginalConstructor()
->getMock();

$mock = $this->getMockBuilder('JFeedParserProcessElementMock')
->disableOriginalConstructor()
->setMethods(array('processFeedEntry', 'handleElement1'))
->getMock();
->disableOriginalConstructor()
->setMethods(array('processFeedEntry', 'handleElement1'))
->getMock();

$mock->expects($this->once())
->method('handleElement1')
->with($feed, $el);
->method('handleElement1')
->with($feed, $el);

$namespace = $this->getMockBuilder('JFeedParserNamespace')
->getMock();
->getMock();

$namespace->expects($this->once())
->method('processElementForFeed')
->with($feed, $el);
->method('processElementForFeed')
->with($feed, $el);

$mock->processElement($feed, $el, array($namespace));
}
Expand All @@ -176,83 +179,34 @@ public function testProcessElementWithEntry()
{
$el = new SimpleXMLElement('<myentry></myentry>');

// process element has a few dependencies that we need to pass:
// a JFeed object, an element, and namespaces
// Process element has a few dependencies that we need to pass: a JFeed object, an element, and namespaces
$feed = $this->getMockBuilder('JFeed')
->disableOriginalConstructor()
->getMock();
->disableOriginalConstructor()
->getMock();

$feed->expects($this->once())
->method('addEntry')
->with($this->isInstanceOf('JFeedEntry'));
->method('addEntry')
->with($this->isInstanceOf('JFeedEntry'));

$mock = $this->getMockBuilder('JFeedParserProcessElementMock')
->disableOriginalConstructor()
->setMethods(array('processFeedEntry'))
->getMock();
->disableOriginalConstructor()
->setMethods(array('processFeedEntry'))
->getMock();

$mock->expects($this->once())
->method('processFeedEntry')
->with($this->isInstanceOf('JFeedEntry'), $el);
->method('processFeedEntry')
->with($this->isInstanceOf('JFeedEntry'), $el);

$namespace = $this->getMockBuilder('JFeedParserNamespace')
->getMock();
->getMock();

$namespace->expects($this->once())
->method('processElementForFeedEntry')
->with($this->isInstanceOf('JFeedEntry'), $el);
->method('processElementForFeedEntry')
->with($this->isInstanceOf('JFeedEntry'), $el);

$mock->processElement($feed, $el, array($namespace));
}

/**
* Tests JFeedParser::expandToSimpleXml()
*
* @return void
*
* @covers JFeedParser::expandToSimpleXml
* @since 12.3
*/
public function testExpandToSimpleXml()
{
// Set the XML for the internal reader and move the stream to the first <node> element.
$this->_reader->XML('<node foo="bar"><child>foobar</child></node>');
$this->_reader->next('node');

// Execute the 'expandToSimpleXml' method.
$el = TestReflection::invoke($this->_instance, 'expandToSimpleXml');

$this->assertInstanceOf(
'SimpleXMLElement',
$el,
'The expanded return value should be a SimpleXMLElement instance.'
);

$this->assertEquals(
'node',
$el->getName(),
'The element should be named "node".'
);

$this->assertEquals(
'bar',
(string) $el['foo'],
'The element should have an attribute "foo" with a value "bar".'
);

$this->assertInstanceOf(
'SimpleXMLElement',
$el->child[0],
'The expanded return value should have a child element which is a SimpleXMLElement instance.'
);

$this->assertEquals(
'foobar',
(string) $el->child[0],
'The child element should have a value of "foobar".'
);
}

/**
* Tests JFeedParser::fetchNamespace()
*
Expand Down Expand Up @@ -376,7 +330,11 @@ public function testMoveToClosingElementWithInternalElements()
$this->_reader->XML('<root><node test="first"><child>foobar</child></node><node test="second"></node></root>');

// Advance the reader to the first <node> element.
while ($this->_reader->read() && ($this->_reader->name != 'node'));
do
{
$this->_reader->read();
}
while ($this->_reader->name != 'node');

// Ensure that the current node is <node test="first">.
$this->assertEquals(XMLReader::ELEMENT, $this->_reader->nodeType);
Expand All @@ -389,7 +347,11 @@ public function testMoveToClosingElementWithInternalElements()
$this->assertEquals('node', $this->_reader->name);

// Advance the reader to the next element.
while ($this->_reader->read() && ($this->_reader->nodeType != XMLReader::ELEMENT));
do
{
$this->_reader->read();
}
while ($this->_reader->nodeType != XMLReader::ELEMENT);

// Ensure that the current node is <node test="first">.
$this->assertEquals(XMLReader::ELEMENT, $this->_reader->nodeType);
Expand All @@ -416,7 +378,11 @@ public function testMoveToClosingElementWithSelfClosingTag()
$this->_reader->XML('<root><node test="first" /><node test="second"></node></root>');

// Advance the reader to the first <node> element.
while ($this->_reader->read() && ($this->_reader->name != 'node'));
do
{
$this->_reader->read();
}
while ($this->_reader->name != 'node');

// Ensure that the current node is <node test="first">.
$this->assertEquals(XMLReader::ELEMENT, $this->_reader->nodeType);
Expand All @@ -429,7 +395,11 @@ public function testMoveToClosingElementWithSelfClosingTag()
$this->assertEquals('node', $this->_reader->name);

// Advance the reader to the next element.
while ($this->_reader->read() && ($this->_reader->nodeType != XMLReader::ELEMENT));
do
{
$this->_reader->read();
}
while ($this->_reader->nodeType != XMLReader::ELEMENT);

// Ensure that the current node is <node test="first">.
$this->assertEquals(XMLReader::ELEMENT, $this->_reader->nodeType);
Expand Down
Loading

0 comments on commit 24a225b

Please sign in to comment.