Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZendTest\EventManager;
use Zend\EventManager\Event;
use Zend\EventManager\EventManager;
use Zend\EventManager\Exception\RuntimeException;
class TestFoo
{
public static $isTriggered = false;
public function __invoke()
{
self::$isTriggered = true;
}
}
class EventManagerTest extends \PHPUnit_Framework_TestCase
{
public function testLazyInstantiator()
{
TestFoo::$isTriggered = false;
$eventManager = new EventManager(function ($requestedListener) {
// The method name isn't being passed here (I defined as __invoke, but I don't think it matters??)
$this->assertSame('ZendTest\EventManager\TestFoo', $requestedListener);
return new TestFoo();
});
$eventManager->attach('MyEvent', [TestFoo::class, '__invoke']);
$event = new Event();
$eventManager->trigger('MyEvent', $event);
$this->assertTrue(TestFoo::$isTriggered);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment