2010-03-18 07:48:03 +00:00
< ? php
/*
* This file is part of the symfony package .
* ( c ) Fabien Potencier < fabien . potencier @ symfony - project . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Tests\Components\EventDispatcher ;
require_once __DIR__ . '/../../bootstrap.php' ;
use Symfony\Components\EventDispatcher\Event ;
use Symfony\Components\EventDispatcher\EventDispatcher ;
class EventDispatcherTest extends \PHPUnit_Framework_TestCase
{
public function testConnectAndDisconnect ()
{
$dispatcher = new EventDispatcher ();
$dispatcher -> connect ( 'bar' , 'listenToBar' );
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( array ( 'listenToBar' ), $dispatcher -> getListeners ( 'bar' ), '->connect() connects a listener to an event name' );
2010-03-18 07:48:03 +00:00
$dispatcher -> connect ( 'bar' , 'listenToBarBar' );
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( array ( 'listenToBar' , 'listenToBarBar' ), $dispatcher -> getListeners ( 'bar' ), '->connect() can connect several listeners for the same event name' );
2010-03-18 07:48:03 +00:00
$dispatcher -> connect ( 'barbar' , 'listenToBarBar' );
$dispatcher -> disconnect ( 'bar' , 'listenToBarBar' );
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( array ( 'listenToBar' ), $dispatcher -> getListeners ( 'bar' ), '->disconnect() disconnects a listener for an event name' );
$this -> assertEquals ( array ( 'listenToBarBar' ), $dispatcher -> getListeners ( 'barbar' ), '->disconnect() disconnects a listener for an event name' );
2010-03-18 07:48:03 +00:00
$this -> assertTrue ( $dispatcher -> disconnect ( 'foobar' , 'listen' ) === false , '->disconnect() returns false if the listener does not exist' );
}
public function testGetHasListeners ()
{
$dispatcher = new EventDispatcher ();
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( false , $dispatcher -> hasListeners ( 'foo' ), '->hasListeners() returns false if the event has no listener' );
2010-03-18 07:48:03 +00:00
$dispatcher -> connect ( 'foo' , 'listenToFoo' );
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( true , $dispatcher -> hasListeners ( 'foo' ), '->hasListeners() returns true if the event has some listeners' );
2010-03-18 07:48:03 +00:00
$dispatcher -> disconnect ( 'foo' , 'listenToFoo' );
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( false , $dispatcher -> hasListeners ( 'foo' ), '->hasListeners() returns false if the event has no listener' );
2010-03-18 07:48:03 +00:00
$dispatcher -> connect ( 'bar' , 'listenToBar' );
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( array ( 'listenToBar' ), $dispatcher -> getListeners ( 'bar' ), '->getListeners() returns an array of listeners connected to the given event name' );
$this -> assertEquals ( array (), $dispatcher -> getListeners ( 'foobar' ), '->getListeners() returns an empty array if no listener are connected to the given event name' );
2010-03-18 07:48:03 +00:00
}
public function testNotify ()
{
$listener = new Listener ();
$dispatcher = new EventDispatcher ();
$dispatcher -> connect ( 'foo' , array ( $listener , 'listenToFoo' ));
$dispatcher -> connect ( 'foo' , array ( $listener , 'listenToFooBis' ));
$e = $dispatcher -> notify ( $event = new Event ( new \stdClass (), 'foo' ));
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( 'listenToFoolistenToFooBis' , $listener -> getValue (), '->notify() notifies all registered listeners in order' );
$this -> assertEquals ( $event , $e , '->notify() returns the event object' );
2010-03-18 07:48:03 +00:00
$listener -> reset ();
$dispatcher = new EventDispatcher ();
$dispatcher -> connect ( 'foo' , array ( $listener , 'listenToFooBis' ));
$dispatcher -> connect ( 'foo' , array ( $listener , 'listenToFoo' ));
$dispatcher -> notify ( new Event ( new \stdClass (), 'foo' ));
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( 'listenToFooBislistenToFoo' , $listener -> getValue (), '->notify() notifies all registered listeners in order' );
2010-03-18 07:48:03 +00:00
}
public function testNotifyUntil ()
{
$listener = new Listener ();
$dispatcher = new EventDispatcher ();
$dispatcher -> connect ( 'foo' , array ( $listener , 'listenToFoo' ));
$dispatcher -> connect ( 'foo' , array ( $listener , 'listenToFooBis' ));
$e = $dispatcher -> notifyUntil ( $event = new Event ( new \stdClass (), 'foo' ));
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( 'listenToFoolistenToFooBis' , $listener -> getValue (), '->notifyUntil() notifies all registered listeners in order and stops if it returns true' );
$this -> assertEquals ( $event , $e , '->notifyUntil() returns the event object' );
2010-03-18 07:48:03 +00:00
$listener -> reset ();
$dispatcher = new EventDispatcher ();
$dispatcher -> connect ( 'foo' , array ( $listener , 'listenToFooBis' ));
$dispatcher -> connect ( 'foo' , array ( $listener , 'listenToFoo' ));
$e = $dispatcher -> notifyUntil ( $event = new Event ( new \stdClass (), 'foo' ));
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( 'listenToFooBis' , $listener -> getValue (), '->notifyUntil() notifies all registered listeners in order and stops if it returns true' );
2010-03-18 07:48:03 +00:00
}
public function testFilter ()
{
$listener = new Listener ();
$dispatcher = new EventDispatcher ();
$dispatcher -> connect ( 'foo' , array ( $listener , 'filterFoo' ));
$dispatcher -> connect ( 'foo' , array ( $listener , 'filterFooBis' ));
$e = $dispatcher -> filter ( $event = new Event ( new \stdClass (), 'foo' ), 'foo' );
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( '-*foo*-' , $e -> getReturnValue (), '->filter() filters a value' );
$this -> assertEquals ( $event , $e , '->filter() returns the event object' );
2010-03-18 07:48:03 +00:00
$listener -> reset ();
$dispatcher = new EventDispatcher ();
$dispatcher -> connect ( 'foo' , array ( $listener , 'filterFooBis' ));
$dispatcher -> connect ( 'foo' , array ( $listener , 'filterFoo' ));
$e = $dispatcher -> filter ( $event = new Event ( new \stdClass (), 'foo' ), 'foo' );
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( '*-foo-*' , $e -> getReturnValue (), '->filter() filters a value' );
2010-03-18 07:48:03 +00:00
}
}
class Listener
{
protected
$value = '' ;
function filterFoo ( Event $event , $foo )
{
return " * $foo * " ;
}
function filterFooBis ( Event $event , $foo )
{
return " - $foo - " ;
}
function listenToFoo ( Event $event )
{
$this -> value .= 'listenToFoo' ;
}
function listenToFooBis ( Event $event )
{
$this -> value .= 'listenToFooBis' ;
return true ;
}
function getValue ()
{
return $this -> value ;
}
function reset ()
{
$this -> value = '' ;
}
}