【问题标题】:Lithium PHP integration testing - routes.php not included?锂 PHP 集成测试 - 不包括 routes.php?
【发布时间】:2013-12-23 14:05:00
【问题描述】:

我正在基于 Union of 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 向加载 routes.php 的 Dispatcher 的“run”方法添加了一个过滤器。

当然,我可能完全错过了这里的重点!我很感激你能给我的任何指导!

【问题讨论】:

    标签: php testing lithium


    【解决方案1】:

    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);
    });
    

    【讨论】:

      猜你喜欢
      • 2013-09-30
      • 1970-01-01
      • 2020-11-01
      • 2011-03-15
      • 2011-01-20
      • 2012-05-21
      • 1970-01-01
      • 1970-01-01
      • 2019-10-03
      相关资源
      最近更新 更多