Skip to content

Commit

Permalink
Modified the plugin iterator to include a workaround for what appears…
Browse files Browse the repository at this point in the history
… to be a bug in PHP and added a corresponding test case
  • Loading branch information
elazar committed Aug 7, 2010
1 parent 6085403 commit 84fb294
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
16 changes: 16 additions & 0 deletions Phergie/Plugin/Iterator.php
Expand Up @@ -46,6 +46,22 @@ class Phergie_Plugin_Iterator extends FilterIterator
*/
protected $methods = array();

/**
* Overrides the parent constructor to reset the internal iterator's
* pointer to the current item, which the parent class errantly does not
* do.
*
* @param Iterator $iterator Iterator to filter
*
* @return void
* @link http://bugs.php.net/bug.php?id=52560
*/
public function __construct(Iterator $iterator)
{
parent::__construct($iterator);
$this->rewind();
}

/**
* Adds to a list of plugins to exclude when iterating.
*
Expand Down
28 changes: 14 additions & 14 deletions Tests/Phergie/Plugin/HandlerTest.php
Expand Up @@ -714,23 +714,23 @@ public function testGetPlugins()
}

/**
* Tests the plugin receiving and using a predefined iterator instance.
* Tests that multiple plugin iterators can be used concurrently.
*
* @depends testGetPlugins
* @return void
*/
public function testSetIterator()
public function testUseMultiplePluginIteratorsConcurrently()
{
$plugin = $this->getMockPlugin('TestPlugin');
$this->handler->addPlugin($plugin);
$plugins = $this->handler->getPlugins();
$iterator = new ArrayIterator($plugins);
$this->handler->setIterator($iterator);
$this->assertSame($this->handler->getIterator(), $iterator);
$iterated = array();
foreach ($this->handler as $plugin) {
$iterated[strtolower($plugin->getName())] = $plugin;
}
$this->assertEquals($iterated, $plugins);
$plugin1 = $this->getMockPlugin('TestPlugin1');
$this->handler->addPlugin($plugin1);

$plugin2 = $this->getMockPlugin('TestPlugin2');
$this->handler->addPlugin($plugin2);

$iterator1 = $this->handler->getIterator();
$iterator1->next();
$this->assertSame($plugin2, $iterator1->current());

$iterator2 = $this->handler->getIterator();
$this->assertSame($plugin1, $iterator2->current());
}
}

0 comments on commit 84fb294

Please sign in to comment.