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: $this->subject = new \stdClass(); Chris@17: $this->event = new GenericEvent($this->subject, ['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: Chris@0: public function testConstruct() Chris@0: { Chris@17: $this->assertEquals($this->event, new GenericEvent($this->subject, ['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@17: $this->assertSame(['name' => 'Event'], $this->event->getArguments()); Chris@0: } Chris@0: Chris@0: public function testSetArguments() Chris@0: { Chris@17: $result = $this->event->setArguments(['foo' => 'bar']); Chris@17: $this->assertAttributeSame(['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@17: $this->assertAttributeSame(['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@17: $this->assertAttributeSame(['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@17: $this->assertAttributeSame([], 'arguments', $this->event); Chris@0: } Chris@0: Chris@0: public function testOffsetIsset() Chris@0: { Chris@14: $this->assertArrayHasKey('name', $this->event); Chris@14: $this->assertArrayNotHasKey('nameNotExist', $this->event); 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@17: $data = []; Chris@0: foreach ($this->event as $key => $value) { Chris@0: $data[$key] = $value; Chris@0: } Chris@17: $this->assertEquals(['name' => 'Event'], $data); Chris@0: } Chris@0: }