EventTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. require_once __DIR__.'/../../bootstrap.php';
  11. use Symfony\Components\EventDispatcher\Event;
  12. class EventTest extends \PHPUnit_Framework_TestCase
  13. {
  14. protected $subject;
  15. protected $parameters;
  16. public function testGetSubject()
  17. {
  18. $event = $this->createEvent();
  19. $this->assertEquals($this->subject, $event->getSubject(), '->getSubject() returns the event subject');
  20. }
  21. public function testGetName()
  22. {
  23. $this->assertEquals('name', $this->createEvent()->getName(), '->getName() returns the event name');
  24. }
  25. public function testParameters()
  26. {
  27. $event = $this->createEvent();
  28. $this->assertEquals($this->parameters, $event->getParameters(), '->getParameters() returns the event parameters');
  29. $this->assertEquals('bar', $event->getParameter('foo'), '->getParameter() returns the value of a parameter');
  30. $event->setParameter('foo', 'foo');
  31. $this->assertEquals('foo', $event->getParameter('foo'), '->setParameter() changes the value of a parameter');
  32. $this->assertTrue($event->hasParameter('foo'), '->hasParameter() returns true if the parameter is defined');
  33. unset($event['foo']);
  34. $this->assertTrue(!$event->hasParameter('foo'), '->hasParameter() returns false if the parameter is not defined');
  35. try
  36. {
  37. $event->getParameter('foobar');
  38. $this->fail('->getParameter() throws an \InvalidArgumentException exception when the parameter does not exist');
  39. }
  40. catch (\InvalidArgumentException $e)
  41. {
  42. }
  43. $event = new Event($this->subject, 'name', $this->parameters);
  44. }
  45. public function testSetGetReturnValue()
  46. {
  47. $event = $this->createEvent();
  48. $event->setReturnValue('foo');
  49. $this->assertEquals('foo', $event->getReturnValue(), '->getReturnValue() returns the return value of the event');
  50. }
  51. public function testSetIsProcessed()
  52. {
  53. $event = $this->createEvent();
  54. $event->setProcessed(true);
  55. $this->assertEquals(true, $event->isProcessed(), '->isProcessed() returns true if the event has been processed');
  56. $event->setProcessed(false);
  57. $this->assertEquals(false, $event->isProcessed(), '->setProcessed() changes the processed status');
  58. }
  59. public function testArrayAccessInterface()
  60. {
  61. $event = $this->createEvent();
  62. $this->assertEquals('bar', $event['foo'], 'Event implements the ArrayAccess interface');
  63. $event['foo'] = 'foo';
  64. $this->assertEquals('foo', $event['foo'], 'Event implements the ArrayAccess interface');
  65. try
  66. {
  67. $event['foobar'];
  68. $this->fail('::offsetGet() throws an \InvalidArgumentException exception when the parameter does not exist');
  69. }
  70. catch (\InvalidArgumentException $e)
  71. {
  72. }
  73. $this->assertTrue(isset($event['foo']), 'Event implements the ArrayAccess interface');
  74. unset($event['foo']);
  75. $this->assertTrue(!isset($event['foo']), 'Event implements the ArrayAccess interface');
  76. }
  77. protected function createEvent()
  78. {
  79. $this->subject = new \stdClass();
  80. $this->parameters = array('foo' => 'bar');
  81. return new Event($this->subject, 'name', $this->parameters);
  82. }
  83. }