1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64:
<?php
namespace Guzzle\Tests\Common;
use Guzzle\Common\Event;
use Guzzle\Common\AbstractHasDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcher;
class AbstractHasAdapterTest extends \Guzzle\Tests\GuzzleTestCase
{
public function testDoesNotRequireRegisteredEvents()
{
$this->assertEquals(array(), AbstractHasDispatcher::getAllEvents());
}
public function testAllowsDispatcherToBeInjected()
{
$d = new EventDispatcher();
$mock = $this->getMockForAbstractClass('Guzzle\Common\AbstractHasDispatcher');
$this->assertSame($mock, $mock->setEventDispatcher($d));
$this->assertSame($d, $mock->getEventDispatcher());
}
public function testCreatesDefaultEventDispatcherIfNeeded()
{
$mock = $this->getMockForAbstractClass('Guzzle\Common\AbstractHasDispatcher');
$this->assertInstanceOf('Symfony\Component\EventDispatcher\EventDispatcher', $mock->getEventDispatcher());
}
public function testHelperDispatchesEvents()
{
$data = array();
$mock = $this->getMockForAbstractClass('Guzzle\Common\AbstractHasDispatcher');
$mock->getEventDispatcher()->addListener('test', function(Event $e) use (&$data) {
$data = $e->getIterator()->getArrayCopy();
});
$mock->dispatch('test', array(
'param' => 'abc'
));
$this->assertEquals(array(
'param' => 'abc',
), $data);
}
public function testHelperAttachesSubscribers()
{
$mock = $this->getMockForAbstractClass('Guzzle\Common\AbstractHasDispatcher');
$subscriber = $this->getMockForAbstractClass('Symfony\Component\EventDispatcher\EventSubscriberInterface');
$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
->setMethods(array('addSubscriber'))
->getMock();
$dispatcher->expects($this->once())
->method('addSubscriber');
$mock->setEventDispatcher($dispatcher);
$mock->addSubscriber($subscriber);
}
}