Created
May 11, 2014 12:34
-
-
Save designermonkey/d91007c0a282b5e8c262 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <?php | |
| /** | |
| * Symphony CMS | |
| */ | |
| namespace SymphonyCMS; | |
| /** | |
| * Event Listener | |
| */ | |
| class EventListener implements ListenerInterface | |
| { | |
| /** | |
| * The event to listen for | |
| * @var string | |
| */ | |
| protected $event; | |
| /** | |
| * The callback to run at transaction time | |
| * @var Closure | |
| */ | |
| protected $callback; | |
| /** | |
| * Sets the event to listen for, or retrieves the event being listened to | |
| * @param string $event | |
| * @return string | |
| */ | |
| public function event($event = null) | |
| { | |
| if (is_null($event)) { | |
| return $this->event; | |
| } | |
| $this->event = $event; | |
| } | |
| /** | |
| * Sets the callback | |
| * @param Closure $callback | |
| */ | |
| public function callback(\Closure $callback) | |
| { | |
| $this->callback = $callback; | |
| } | |
| /** | |
| * Run the callback on the transaction data | |
| * @param array $data | |
| */ | |
| public function execute(&$data) | |
| { | |
| $callback = $this->callback; | |
| $data = $callback($data); | |
| // var_dump($data);die; | |
| } | |
| } |
This file contains hidden or 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
| <?php | |
| /** | |
| * Symphony CMS Tests | |
| */ | |
| namespace SymphonyCMS\Tests; | |
| use \SymphonyCMS\EventListener; | |
| use \SymphonyCMS\Transaction; | |
| /** | |
| * Listener Stack Test | |
| */ | |
| class EventListenerTest extends \PHPUnit_Framework_TestCase | |
| { | |
| public function setUp() | |
| { | |
| $this->listener = new EventListener; | |
| $this->event = 'my-test-event'; | |
| } | |
| public function getInternal($source) | |
| { | |
| $reflection = new \ReflectionClass($this->listener); | |
| $property = $reflection->getProperty($source); | |
| $property->setAccessible(true); | |
| return $property->getValue($this->listener); | |
| } | |
| public function testSetEvent() | |
| { | |
| $this->listener->event($this->event); | |
| $event = $this->getInternal('event'); | |
| $this->assertEquals($this->event, $event); | |
| } | |
| public function testGetEvent() | |
| { | |
| $reflection = new \ReflectionClass($this->listener); | |
| $property = $reflection->getProperty('event'); | |
| $property->setAccessible(true); | |
| $property->setValue($this->listener, $this->event); | |
| $this->assertEquals($this->event, $this->listener->event()); | |
| } | |
| public function testCallback() | |
| { | |
| $this->listener->callback(function () { | |
| return 'callback returns value'; | |
| }); | |
| $callback = $this->getInternal('callback'); | |
| $this->assertInstanceOf('Closure', $callback); | |
| $this->assertEquals('callback returns value', $callback()); | |
| } | |
| public function testExecute() | |
| { | |
| $data = [ | |
| 'key' => false | |
| ]; | |
| $this->listener->callback(function ($data) { | |
| $data['key'] = true; | |
| return $data; | |
| }); | |
| $this->listener->execute($data); | |
| var_dump($data);die; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment