1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\EventDispatcher\Tests;
13
14use PHPUnit\Framework\TestCase;
15use Symfony\Component\EventDispatcher\GenericEvent;
16
17/**
18 * Test class for Event.
19 */
20class GenericEventTest extends TestCase
21{
22    /**
23     * @var GenericEvent
24     */
25    private $event;
26
27    private $subject;
28
29    /**
30     * Prepares the environment before running a test.
31     */
32    protected function setUp()
33    {
34        parent::setUp();
35
36        $this->subject = new \stdClass();
37        $this->event = new GenericEvent($this->subject, array('name' => 'Event'));
38    }
39
40    /**
41     * Cleans up the environment after running a test.
42     */
43    protected function tearDown()
44    {
45        $this->subject = null;
46        $this->event = null;
47
48        parent::tearDown();
49    }
50
51    public function testConstruct()
52    {
53        $this->assertEquals($this->event, new GenericEvent($this->subject, array('name' => 'Event')));
54    }
55
56    /**
57     * Tests Event->getArgs().
58     */
59    public function testGetArguments()
60    {
61        // test getting all
62        $this->assertSame(array('name' => 'Event'), $this->event->getArguments());
63    }
64
65    public function testSetArguments()
66    {
67        $result = $this->event->setArguments(array('foo' => 'bar'));
68        $this->assertAttributeSame(array('foo' => 'bar'), 'arguments', $this->event);
69        $this->assertSame($this->event, $result);
70    }
71
72    public function testSetArgument()
73    {
74        $result = $this->event->setArgument('foo2', 'bar2');
75        $this->assertAttributeSame(array('name' => 'Event', 'foo2' => 'bar2'), 'arguments', $this->event);
76        $this->assertEquals($this->event, $result);
77    }
78
79    public function testGetArgument()
80    {
81        // test getting key
82        $this->assertEquals('Event', $this->event->getArgument('name'));
83    }
84
85    /**
86     * @expectedException \InvalidArgumentException
87     */
88    public function testGetArgException()
89    {
90        $this->event->getArgument('nameNotExist');
91    }
92
93    public function testOffsetGet()
94    {
95        // test getting key
96        $this->assertEquals('Event', $this->event['name']);
97
98        // test getting invalid arg
99        $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
100        $this->assertFalse($this->event['nameNotExist']);
101    }
102
103    public function testOffsetSet()
104    {
105        $this->event['foo2'] = 'bar2';
106        $this->assertAttributeSame(array('name' => 'Event', 'foo2' => 'bar2'), 'arguments', $this->event);
107    }
108
109    public function testOffsetUnset()
110    {
111        unset($this->event['name']);
112        $this->assertAttributeSame(array(), 'arguments', $this->event);
113    }
114
115    public function testOffsetIsset()
116    {
117        $this->assertArrayHasKey('name', $this->event);
118        $this->assertArrayNotHasKey('nameNotExist', $this->event);
119    }
120
121    public function testHasArgument()
122    {
123        $this->assertTrue($this->event->hasArgument('name'));
124        $this->assertFalse($this->event->hasArgument('nameNotExist'));
125    }
126
127    public function testGetSubject()
128    {
129        $this->assertSame($this->subject, $this->event->getSubject());
130    }
131
132    public function testHasIterator()
133    {
134        $data = array();
135        foreach ($this->event as $key => $value) {
136            $data[$key] = $value;
137        }
138        $this->assertEquals(array('name' => 'Event'), $data);
139    }
140}
141