Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EventDispatcher] Added generic event class.
- Loading branch information
Drak
committed
Apr 20, 2012
1 parent
f8407dd
commit 4ff92b9
Showing
2 changed files
with
357 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\EventDispatcher; | ||
|
||
/** | ||
* Event encapsulation class. | ||
* | ||
* Encapsulates events thus decoupling the observer from the subject they encapsulate. | ||
* | ||
* @author Drak <drak@zikula.org> | ||
*/ | ||
class GenericEvent extends Event implements \ArrayAccess | ||
{ | ||
/** | ||
* Observer pattern subject. | ||
* | ||
* @var mixed usually object or callable | ||
*/ | ||
protected $subject; | ||
|
||
/** | ||
* Array of arguments. | ||
* | ||
* @var array | ||
*/ | ||
protected $arguments; | ||
|
||
/** | ||
* Storage for any process type events. | ||
* | ||
* @var mixed | ||
*/ | ||
protected $data; | ||
|
||
/** | ||
* Encapsulate an event with $subject, $args, and $data. | ||
* | ||
* @param mixed $subject The subject of the event, usually an object. | ||
* @param array $arguments Arguments to store in the event. | ||
* @param mixed $data Convenience argument of data for optional processing. | ||
*/ | ||
public function __construct($subject = null, array $arguments = array(), $data = null) | ||
{ | ||
$this->subject = $subject; | ||
$this->arguments = $arguments; | ||
$this->data = $data; | ||
} | ||
|
||
/** | ||
* Getter for subject property. | ||
* | ||
* @return mixed $subject The observer subject. | ||
*/ | ||
public function getSubject() | ||
{ | ||
return $this->subject; | ||
} | ||
|
||
/** | ||
* Get argument by key. | ||
* | ||
* @param string $key Key. | ||
* | ||
* @throws \InvalidArgumentException If key is not found. | ||
* | ||
* @return mixed Contents of array key. | ||
*/ | ||
public function getArgument($key) | ||
{ | ||
if ($this->hasArgument($key)) { | ||
return $this->arguments[$key]; | ||
} | ||
|
||
throw new \InvalidArgumentException(sprintf('%s not found in %s', $key, $this->getName())); | ||
} | ||
|
||
/** | ||
* Add argument to event. | ||
* | ||
* @param string $key Argument name. | ||
* @param mixed $value Value. | ||
* | ||
* @return GenericEvent | ||
*/ | ||
public function setArgument($key, $value) | ||
{ | ||
$this->arguments[$key] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Getter for all arguments. | ||
* | ||
* @return array | ||
*/ | ||
public function getArguments() | ||
{ | ||
return $this->arguments; | ||
} | ||
|
||
/** | ||
* Set args property. | ||
* | ||
* @param array $args Arguments. | ||
* | ||
* @return GenericEvent | ||
*/ | ||
public function setArguments(array $args = array()) | ||
{ | ||
$this->arguments = $args; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Has argument. | ||
* | ||
* @param string $key Key of arguments array. | ||
* | ||
* @return boolean | ||
*/ | ||
public function hasArgument($key) | ||
{ | ||
return array_key_exists($key, $this->arguments); | ||
} | ||
|
||
/** | ||
* Getter for Data property. | ||
* | ||
* @return mixed Data property. | ||
*/ | ||
public function getData() | ||
{ | ||
return $this->data; | ||
} | ||
|
||
/** | ||
* Set data. | ||
* | ||
* @param mixed $data Data to be saved. | ||
* | ||
* @return GenericEvent | ||
*/ | ||
public function setData($data) | ||
{ | ||
$this->data = $data; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* ArrayAccess for argument getter. | ||
* | ||
* @param string $key Array key. | ||
* | ||
* @throws \InvalidArgumentException If key does not exist in $this->args. | ||
* | ||
* @return mixed | ||
*/ | ||
public function offsetGet($key) | ||
{ | ||
return $this->getArgument($key); | ||
} | ||
|
||
/** | ||
* ArrayAccess for argument setter. | ||
* | ||
* @param string $key Array key to set. | ||
* @param mixed $value Value. | ||
* | ||
* @return void | ||
*/ | ||
public function offsetSet($key, $value) | ||
{ | ||
$this->setArgument($key, $value); | ||
} | ||
|
||
/** | ||
* ArrayAccess for unset argument. | ||
* | ||
* @param string $key Array key. | ||
* | ||
* @return void | ||
*/ | ||
public function offsetUnset($key) | ||
{ | ||
if ($this->hasArgument($key)) { | ||
unset($this->arguments[$key]); | ||
} | ||
} | ||
|
||
/** | ||
* ArrayAccess has argument. | ||
* | ||
* @param string $key Array key. | ||
* | ||
* @return boolean | ||
*/ | ||
public function offsetExists($key) | ||
{ | ||
return $this->hasArgument($key); | ||
} | ||
} |
144 changes: 144 additions & 0 deletions
144
src/Symfony/Component/EventDispatcher/Tests/GenericEventTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\EventDispatcher\Tests; | ||
|
||
use Symfony\Component\EventDispatcher\GenericEvent; | ||
|
||
/** | ||
* Test class for Event. | ||
*/ | ||
class GenericEventTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
/** | ||
* @var GenericEvent | ||
*/ | ||
private $event; | ||
|
||
private $subject; | ||
|
||
/** | ||
* Prepares the environment before running a test. | ||
*/ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->subject = new \StdClass(); | ||
$this->event = new GenericEvent($this->subject, array('name' => 'Event'), 'foo'); | ||
} | ||
|
||
/** | ||
* Cleans up the environment after running a test. | ||
*/ | ||
protected function tearDown() | ||
{ | ||
$this->subject = null; | ||
$this->event = null; | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
public function test__construct() | ||
{ | ||
$this->assertEquals($this->event, new GenericEvent($this->subject, array('name' => 'Event'), 'foo')); | ||
} | ||
|
||
/** | ||
* Tests Event->getArgs() | ||
*/ | ||
public function testGetArgs() | ||
{ | ||
// test getting all | ||
$this->assertSame(array('name' => 'Event'), $this->event->getArguments()); | ||
} | ||
|
||
public function testSetArgs() | ||
{ | ||
$result = $this->event->setArguments(array('foo' => 'bar')); | ||
$this->assertAttributeSame(array('foo' => 'bar'), 'arguments', $this->event); | ||
$this->assertSame($this->event, $result); | ||
} | ||
|
||
public function testSetArg() | ||
{ | ||
$result = $this->event->setArgument('foo2', 'bar2'); | ||
$this->assertAttributeSame(array('name' => 'Event', 'foo2' => 'bar2'), 'arguments', $this->event); | ||
$this->assertEquals($this->event, $result); | ||
} | ||
|
||
public function testGetArg() | ||
{ | ||
// test getting key | ||
$this->assertEquals('Event', $this->event->getArgument('name')); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
*/ | ||
public function testGetArgException() | ||
{ | ||
$this->event->getArgument('nameNotExist'); | ||
} | ||
|
||
public function testOffsetGet() | ||
{ | ||
// test getting key | ||
$this->assertEquals('Event', $this->event['name']); | ||
|
||
// test getting invalid arg | ||
$this->setExpectedException('InvalidArgumentException'); | ||
$this->assertFalse($this->event['nameNotExist']); | ||
} | ||
|
||
public function testOffsetSet() | ||
{ | ||
$this->event['foo2'] = 'bar2'; | ||
$this->assertAttributeSame(array('name' => 'Event', 'foo2' => 'bar2'), 'arguments', $this->event); | ||
} | ||
|
||
public function testOffsetUnset() | ||
{ | ||
unset($this->event['name']); | ||
$this->assertAttributeSame(array(), 'arguments', $this->event); | ||
} | ||
|
||
public function testOffsetIsset() | ||
{ | ||
$this->assertTrue(isset($this->event['name'])); | ||
$this->assertFalse(isset($this->event['nameNotExist'])); | ||
} | ||
|
||
public function testHasArg() | ||
{ | ||
$this->assertTrue($this->event->hasArgument('name')); | ||
$this->assertFalse($this->event->hasArgument('nameNotExist')); | ||
} | ||
|
||
public function testGetSubject() | ||
{ | ||
$this->assertSame($this->subject, $this->event->getSubject()); | ||
} | ||
|
||
|
||
public function testGetData() | ||
{ | ||
$this->event->setData("Don't drink and drive."); | ||
$this->assertEquals("Don't drink and drive.", $this->event->getData()); | ||
} | ||
|
||
public function testSetData() | ||
{ | ||
$this->event->setData("Don't drink and drive."); | ||
$this->assertEquals("Don't drink and drive.", $this->event->getData()); | ||
} | ||
} |