Silex - 从测试中分派时不执行侦听器

Silex - Listeners not executed when dispatching from a test

我写了一个 class 来在每次执行功能测试时在数据库中加载一些固定装置。这些固定装置是通过调用服务创建的,该服务在创建一些数据后调度一个事件,以便从侦听器创建其他数据。问题是在加载灯具时没有执行监听器。

我是否需要模拟所有事件分派以使侦听器被执行?我的意思是,从 fixtures load 方法手动调度所需的事件?为什么不执行侦听器?

abstract class APITest extends WebTestCase
{
    protected $app;

    public function createApplication()
    {
        $app = require __DIR__ . '/../../app.php';
        require __DIR__ . '/../../controllers.php';
        require __DIR__ . '/../../routing.php';
        $app['debug'] = true;
        unset($app['exception_handler']);
        $app['session.test'] = true;

        return $app;
    }

    public function setUp()
    {
        parent::setUp();
        /* @var $app Application */
        $app = $this->app;
        $fixtures = new TestingFixtures($app);
        $fixtures->load();
    }

    ...
}

订阅者是从 app.php 注册的:

$app->register(new SubscribersServiceProvider());

SubscribersServiceProvider中:

use Silex\ServiceProviderInterface;

class SubscribersServiceProvider implements ServiceProviderInterface
{

    /**
     * { @inheritdoc }
     */
    public function register(Application $app)
    {

    }

    /**
     * { @inheritdoc }
     */
    public function boot(Application $app)
    {

        /* @var $dispatcher EventDispatcher */
        $dispatcher = $app['dispatcher'];

        $dispatcher->addSubscriber(new CustomSubscriber($app['foo'], $app['mailer'], $app['monolog']));
        //... more subscribers
    }
}

您只需在测试中调用 $app->boot(),因为订阅者会在应用程序启动时添加。