Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\EventDispatcher\Tests; Chris@0: Chris@0: use PHPUnit\Framework\TestCase; Chris@0: use Symfony\Component\EventDispatcher\GenericEvent; Chris@0: Chris@0: /** Chris@0: * Test class for Event. Chris@0: */ Chris@0: class GenericEventTest extends TestCase Chris@0: { Chris@0: /** Chris@0: * @var GenericEvent Chris@0: */ Chris@0: private $event; Chris@0: Chris@0: private $subject; Chris@0: Chris@0: /** Chris@0: * Prepares the environment before running a test. Chris@0: */ Chris@0: protected function setUp() Chris@0: { Chris@0: parent::setUp(); Chris@0: Chris@0: $this->subject = new \stdClass(); Chris@0: $this->event = new GenericEvent($this->subject, array('name' => 'Event')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Cleans up the environment after running a test. Chris@0: */ Chris@0: protected function tearDown() Chris@0: { Chris@0: $this->subject = null; Chris@0: $this->event = null; Chris@0: Chris@0: parent::tearDown(); Chris@0: } Chris@0: Chris@0: public function testConstruct() Chris@0: { Chris@0: $this->assertEquals($this->event, new GenericEvent($this->subject, array('name' => 'Event'))); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests Event->getArgs(). Chris@0: */ Chris@0: public function testGetArguments() Chris@0: { Chris@0: // test getting all Chris@0: $this->assertSame(array('name' => 'Event'), $this->event->getArguments()); Chris@0: } Chris@0: Chris@0: public function testSetArguments() Chris@0: { Chris@0: $result = $this->event->setArguments(array('foo' => 'bar')); Chris@0: $this->assertAttributeSame(array('foo' => 'bar'), 'arguments', $this->event); Chris@0: $this->assertSame($this->event, $result); Chris@0: } Chris@0: Chris@0: public function testSetArgument() Chris@0: { Chris@0: $result = $this->event->setArgument('foo2', 'bar2'); Chris@0: $this->assertAttributeSame(array('name' => 'Event', 'foo2' => 'bar2'), 'arguments', $this->event); Chris@0: $this->assertEquals($this->event, $result); Chris@0: } Chris@0: Chris@0: public function testGetArgument() Chris@0: { Chris@0: // test getting key Chris@0: $this->assertEquals('Event', $this->event->getArgument('name')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @expectedException \InvalidArgumentException Chris@0: */ Chris@0: public function testGetArgException() Chris@0: { Chris@0: $this->event->getArgument('nameNotExist'); Chris@0: } Chris@0: Chris@0: public function testOffsetGet() Chris@0: { Chris@0: // test getting key Chris@0: $this->assertEquals('Event', $this->event['name']); Chris@0: Chris@0: // test getting invalid arg Chris@0: $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException'); Chris@0: $this->assertFalse($this->event['nameNotExist']); Chris@0: } Chris@0: Chris@0: public function testOffsetSet() Chris@0: { Chris@0: $this->event['foo2'] = 'bar2'; Chris@0: $this->assertAttributeSame(array('name' => 'Event', 'foo2' => 'bar2'), 'arguments', $this->event); Chris@0: } Chris@0: Chris@0: public function testOffsetUnset() Chris@0: { Chris@0: unset($this->event['name']); Chris@0: $this->assertAttributeSame(array(), 'arguments', $this->event); Chris@0: } Chris@0: Chris@0: public function testOffsetIsset() Chris@0: { Chris@0: $this->assertTrue(isset($this->event['name'])); Chris@0: $this->assertFalse(isset($this->event['nameNotExist'])); Chris@0: } Chris@0: Chris@0: public function testHasArgument() Chris@0: { Chris@0: $this->assertTrue($this->event->hasArgument('name')); Chris@0: $this->assertFalse($this->event->hasArgument('nameNotExist')); Chris@0: } Chris@0: Chris@0: public function testGetSubject() Chris@0: { Chris@0: $this->assertSame($this->subject, $this->event->getSubject()); Chris@0: } Chris@0: Chris@0: public function testHasIterator() Chris@0: { Chris@0: $data = array(); Chris@0: foreach ($this->event as $key => $value) { Chris@0: $data[$key] = $value; Chris@0: } Chris@0: $this->assertEquals(array('name' => 'Event'), $data); Chris@0: } Chris@0: }