EventTest.php 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Components\EventDispatcher;
  10. use Symfony\Components\EventDispatcher\Event;
  11. class EventTest extends \PHPUnit_Framework_TestCase
  12. {
  13. protected $subject;
  14. protected $parameters;
  15. public function testGetSubject()
  16. {
  17. $event = $this->createEvent();
  18. $this->assertEquals($this->subject, $event->getSubject(), '->getSubject() returns the event subject');
  19. }
  20. public function testGetName()
  21. {
  22. $this->assertEquals('name', $this->createEvent()->getName(), '->getName() returns the event name');
  23. }
  24. public function testParameters()
  25. {
  26. $event = $this->createEvent();
  27. $this->assertEquals($this->parameters, $event->getParameters(), '->getParameters() returns the event parameters');
  28. $this->assertEquals('bar', $event->getParameter('foo'), '->getParameter() returns the value of a parameter');
  29. $event->setParameter('foo', 'foo');
  30. $this->assertEquals('foo', $event->getParameter('foo'), '->setParameter() changes the value of a parameter');
  31. $this->assertTrue($event->hasParameter('foo'), '->hasParameter() returns true if the parameter is defined');
  32. unset($event['foo']);
  33. $this->assertFalse($event->hasParameter('foo'), '->hasParameter() returns false if the parameter is not defined');
  34. try {
  35. $event->getParameter('foobar');
  36. $this->fail('->getParameter() throws an \InvalidArgumentException exception when the parameter does not exist');
  37. } catch (\Exception $e) {
  38. $this->assertInstanceOf('\InvalidArgumentException', $e, '->getParameter() throws an \InvalidArgumentException exception when the parameter does not exist');
  39. $this->assertEquals('The event "name" has no "foobar" parameter.', $e->getMessage(), '->getParameter() throws an \InvalidArgumentException exception when the parameter does not exist');
  40. }
  41. $event = new Event($this->subject, 'name', $this->parameters);
  42. }
  43. public function testSetGetReturnValue()
  44. {
  45. $event = $this->createEvent();
  46. $event->setReturnValue('foo');
  47. $this->assertEquals('foo', $event->getReturnValue(), '->getReturnValue() returns the return value of the event');
  48. }
  49. public function testSetIsProcessed()
  50. {
  51. $event = $this->createEvent();
  52. $event->setProcessed(true);
  53. $this->assertTrue($event->isProcessed(), '->isProcessed() returns true if the event has been processed');
  54. $event->setProcessed(false);
  55. $this->assertFalse($event->isProcessed(), '->setProcessed() changes the processed status');
  56. }
  57. public function testArrayAccessInterface()
  58. {
  59. $event = $this->createEvent();
  60. $this->assertEquals('bar', $event['foo'], 'Event implements the ArrayAccess interface');
  61. $event['foo'] = 'foo';
  62. $this->assertEquals('foo', $event['foo'], 'Event implements the ArrayAccess interface');
  63. try {
  64. $event['foobar'];
  65. $this->fail('::offsetGet() throws an \InvalidArgumentException exception when the parameter does not exist');
  66. } catch (\Exception $e) {
  67. $this->assertInstanceOf('\InvalidArgumentException', $e, '::offsetGet() throws an \InvalidArgumentException exception when the parameter does not exist');
  68. $this->assertEquals('The event "name" has no "foobar" parameter.', $e->getMessage(), '::offsetGet() throws an \InvalidArgumentException exception when the parameter does not exist');
  69. }
  70. $this->assertTrue(isset($event['foo']), 'Event implements the ArrayAccess interface');
  71. unset($event['foo']);
  72. $this->assertFalse(isset($event['foo']), 'Event implements the ArrayAccess interface');
  73. }
  74. protected function createEvent()
  75. {
  76. $this->subject = new \stdClass();
  77. $this->parameters = array('foo' => 'bar');
  78. return new Event($this->subject, 'name', $this->parameters);
  79. }
  80. }