Skip to content

Commit

Permalink
Add new style get methods to Event.
Browse files Browse the repository at this point in the history
Also add specific coverage for getData('key') as it was missing before.

Refs #9974
  • Loading branch information
markstory committed Jan 8, 2017
1 parent 9aa6dbc commit c013f51
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Event/Event.php
Expand Up @@ -125,22 +125,44 @@ public function __set($attribute, $value)
* Returns the name of this event. This is usually used as the event identifier
*
* @return string
* @deprecated 3.4.0 use getName() instead.
*/
public function name()
{
return $this->_name;
}

/**
* Returns the name of this event. This is usually used as the event identifier
*
* @return string
*/
public function getName()
{
return $this->_name;
}

/**
* Returns the subject of this event
*
* @return object
* @deprecated 3.4.0 use getSubject() instead.
*/
public function subject()
{
return $this->_subject;
}

/**
* Returns the subject of this event
*
* @return object
*/
public function getSubject()
{
return $this->_subject;
}

/**
* Stops the event from being used anymore
*
Expand All @@ -165,12 +187,23 @@ public function isStopped()
* The result value of the event listeners
*
* @return mixed
* @deprecated 3.4.0 use getResult() instead.
*/
public function result()
{
return $this->_result;
}

/**
* The result value of the event listeners
*
* @return mixed
*/
public function getResult()
{
return $this->_result;
}

/**
* Listeners can attach a result value to the event.
*
Expand All @@ -190,8 +223,21 @@ public function setResult($value = null)
* @param string|null $key The data payload element to return, or null to return all data.
* @return array|mixed|null The data payload if $key is null, or the data value for the given $key. If the $key does not
* exist a null value is returned.
* @deprecated 3.4.0 use getData() instead.
*/
public function data($key = null)
{
return $this->getData($key);
}

/**
* Access the event data/payload.
*
* @param string|null $key The data payload element to return, or null to return all data.
* @return array|mixed|null The data payload if $key is null, or the data value for the given $key. If the $key does not
* exist a null value is returned.
*/
public function getData($key = null)
{
if ($key !== null) {
return isset($this->_data[$key]) ? $this->_data[$key] : null;
Expand Down
6 changes: 6 additions & 0 deletions tests/TestCase/Event/EventTest.php
Expand Up @@ -37,6 +37,7 @@ public function testName()
{
$event = new Event('fake.event');
$this->assertEquals('fake.event', $event->name());
$this->assertEquals('fake.event', $event->getName());
}

/**
Expand All @@ -50,6 +51,7 @@ public function testSubject()
{
$event = new Event('fake.event', $this);
$this->assertSame($this, $event->subject());
$this->assertSame($this, $event->getSubject());

$event = new Event('fake.event');
$this->assertNull($event->subject());
Expand Down Expand Up @@ -79,6 +81,10 @@ public function testEventData()
{
$event = new Event('fake.event', $this, ['some' => 'data']);
$this->assertEquals(['some' => 'data'], $event->data());
$this->assertEquals(['some' => 'data'], $event->getData());

$this->assertEquals('data', $event->getData('some'));
$this->assertNull($event->getData('undef'));
}

/**
Expand Down

0 comments on commit c013f51

Please sign in to comment.