2010-03-18 07:48:03 +00:00
< ? php
/*
2010-04-25 16:06:54 +01:00
* This file is part of the Symfony package .
2011-01-15 13:29:43 +00:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-03-18 07:48:03 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2010-08-20 22:09:55 +01:00
namespace Symfony\Tests\Component\EventDispatcher ;
2010-03-18 07:48:03 +00:00
2010-08-20 22:09:55 +01:00
use Symfony\Component\EventDispatcher\Event ;
2010-03-18 07:48:03 +00:00
class EventTest extends \PHPUnit_Framework_TestCase
{
2010-05-06 12:25:53 +01:00
protected $subject ;
protected $parameters ;
public function testGetSubject ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$event = $this -> createEvent ();
$this -> assertEquals ( $this -> subject , $event -> getSubject (), '->getSubject() returns the event subject' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testGetName ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 'name' , $this -> createEvent () -> getName (), '->getName() returns the event name' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testParameters ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$event = $this -> createEvent ();
2010-11-23 08:42:19 +00:00
$this -> assertEquals ( $this -> parameters , $event -> all (), '->all() returns the event parameters' );
$this -> assertEquals ( 'bar' , $event -> get ( 'foo' ), '->get() returns the value of a parameter' );
$event -> set ( 'foo' , 'foo' );
$this -> assertEquals ( 'foo' , $event -> get ( 'foo' ), '->set() changes the value of a parameter' );
$this -> assertTrue ( $event -> has ( 'foo' ), '->has() returns true if the parameter is defined' );
$this -> assertFalse ( $event -> has ( 'oof' ), '->has() returns false if the parameter is not defined' );
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
try {
2010-11-23 08:42:19 +00:00
$event -> get ( 'foobar' );
$this -> fail ( '->get() throws an \InvalidArgumentException exception when the parameter does not exist' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-11-23 08:42:19 +00:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->get() throws an \InvalidArgumentException exception when the parameter does not exist' );
$this -> assertEquals ( 'The event "name" has no "foobar" parameter.' , $e -> getMessage (), '->get() throws an \InvalidArgumentException exception when the parameter does not exist' );
2010-05-06 12:25:53 +01:00
}
$event = new Event ( $this -> subject , 'name' , $this -> parameters );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testSetIsProcessed ()
{
$event = $this -> createEvent ();
2011-01-26 06:50:06 +00:00
$this -> assertFalse ( $event -> isProcessed (), '->isProcessed() returns false by default' );
$event -> setProcessed ();
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( $event -> isProcessed (), '->isProcessed() returns true if the event has been processed' );
}
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
protected function createEvent ()
{
$this -> subject = new \stdClass ();
$this -> parameters = array ( 'foo' => 'bar' );
return new Event ( $this -> subject , 'name' , $this -> parameters );
}
2010-03-18 07:48:03 +00:00
}