EventTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\EventDispatcher;
  11. use Symfony\Component\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->all(), '->all() returns the event parameters');
  29. $this->assertEquals('bar', $event->get('foo'), '->get() returns the value of a parameter');
  30. $event->set('foo', 'foo');
  31. $this->assertEquals('foo', $event->get('foo'), '->set() changes the value of a parameter');
  32. $this->assertTrue($event->has('foo'), '->has() returns true if the parameter is defined');
  33. $this->assertFalse($event->has('oof'), '->has() returns false if the parameter is not defined');
  34. try {
  35. $event->get('foobar');
  36. $this->fail('->get() throws an \InvalidArgumentException exception when the parameter does not exist');
  37. } catch (\Exception $e) {
  38. $this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws an \InvalidArgumentException exception when the parameter does not exist');
  39. $this->assertEquals('The event "name" has no "foobar" parameter.', $e->getMessage(), '->get() 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. protected function createEvent()
  58. {
  59. $this->subject = new \stdClass();
  60. $this->parameters = array('foo' => 'bar');
  61. return new Event($this->subject, 'name', $this->parameters);
  62. }
  63. }