Lithium PHP集成测试 - 不包括routes.php?

时间:2013-12-23 14:05:00

标签: php testing lithium

我正在基于RAD的框架项目联盟在Lithium(PHP框架)中构建一个玩具应用程序。它在浏览器中运行良好,但在运行集成测试时,没有加载routes.php,因此路由不起作用。

这是我测试的代码:

class StaffController extends \lithium\action\Controller {
    public function add() {
        $staff = Staff::create();
        if (($this->request->data) && $staff->save($this->request->data)) {
            return $this->redirect(array('Staff::view', 'args' => array($staff->id)));
        }
        return compact('staff');
}

我的测试:

    public function testAdd() {
            //Router::connect('/{:controller}/{:action}/{:args}');
        $request = new Request();
        $request->data = array('name' => 'Brand new user');
        $controller = new StaffController(array('request' => $request));
        /* @var $response \lithium\action\Response */
        $response = $controller->add();
        $this->assertEqual(302, $response->status['code']);
    }

注意注释掉的行--Router :: connect(' / {:controller} / {:action} / {:args}'); - 如果我取消注释,那一切都很好。

我感到困惑的是,为什么在单元测试中运行时,app / config / routes.php(我定义我的路线)没有被加载。根据我的判断,app / config / bootstrap / action.php为" run"添加了一个过滤器。加载routes.php。

的Dispatcher方法

当然,我可能完全忽略了这一点!我很感激你能给我的任何指导!

1 个答案:

答案 0 :(得分:2)

Lithium的lithium\action\Dispatcher用于http请求,lithium\console\Dispatcher用于控制台命令。

我假设您正在从命令行运行测试。我正在查看“框架”项目的app/config/bootstrap/action.php文件(here on github)。

它只包含未从命令行加载的lithium\action\Dispatcher的routes.php文件。 app/config/bootstrap/console.php也不包括控制台的routes.php。

我的建议是编辑console.php文件并将过滤器更改为:

Dispatcher::applyFilter('run', function($self, $params, $chain) {
    Environment::set($params['request']);
    foreach (array_reverse(Libraries::get()) as $name => $config) {
        if ($name === 'lithium') {
            continue;
        }
        $file = "{$config['path']}/config/routes.php";
        file_exists($file) ? call_user_func(function() use ($file) { include $file; }) : null;
    }
    return $chain->next($self, $params, $chain);
});