我有一个报告未定义变量的测试类,我似乎无法理解问题所在。
基本上,下面的监听器应该监听下面类中记录的应用程序启动事件:
<?php
namespace Colleen\Core\Event\Application;
use Symfony\Component\EventDispatcher\Event;
use Colleen\Core\Application;
/**
* The application.booted event is dispatched each time
* an application instance is created in the system.
*
*/
class ApplicationBootedEvent extends Event
{
protected $app;
public function __construct(Application $app)
{
$this->app = $app;
}
public function getApplication()
{
return $app;
}
}
我的事件类如下所示,它接收应用程序本身的实例。
<?php
namespace Colleen\Core\Event\Application\Listener;
use Colleen\Core\Event\Application\ApplicationBootedEvent;
class ApplicationBootedListener
{
public function onBoot(ApplicationBootedEvent $event)
{
$container = $event->getApplication()->getContainer();
$container->set('class.dispatcher', '\\Symfony\\Component\\EventDispatcher\\EventDispatcher');
}
}
根据Symfony关于Event Dispatcher Component的文档,这两个课程看起来很完美。以下是侦听类,它可以监听 ApplicationBootedEvents :: APP_BOOTED 事件。
<?php
namespace Colleen\Qa\Core\Event\Application\Listener;
use Colleen\Core\Event\Application\Listener\ApplicationBootedListener;
use Colleen\Core\Event\Application\ApplicationBootedEvents;
use Colleen\Core\Event\Application\ApplicationBootedEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Colleen\Core\Container\Container;
use Colleen\Core\Application;
class AppliocationBootedListenerTest extends \PHPUnit_Framework_TestCase
{
public function testApplicationBootListener()
{
$dispatcher = new EventDispatcher();
$dispatcher->addListener(
ApplicationBootedEvents::APP_BOOTED, array(
new ApplicationBootedListener(), 'onBoot'
));
$app = $dispatcher->dispatch(ApplicationBootedEvents::APP_BOOTED, new ApplicationBootedEvent(new Application(new Container())))->getApplication();
$expected = '\\Symfony\\Component\\EventDispatcher\\EventDispatcher';
$actual = $app->getContainer()->get('class.dispatcher');
$this->assertSame($expected, $actual);
}
}
Listener类目前什么都不做,我的测试用例是测试我的容器上是否存在“class.dispatcher”键,这个键简单地扩展了Pimple并通过Application Object提供。
以下是我的测试,显示了这些最终将如何用于我的前端控制器或它们与前端控制器之间的任何类。
fuas
我们的想法是测试是否调用Listener,以及它是否能够为我们的应用程序对象的容器提供所需的所有必要对象,以使我们的Web框架能够工作。
以下是运行此测试用例时得到的结果。
答案 0 :(得分:2)
您的ApplicationBootedEvent.php文件中出现错误,在第24行显示堆栈跟踪建议。
更改
public function getApplication()
{
return $app;
}
要
public function getApplication()
{
return $this->app;
}