gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:31:35 25 4
gpt4 key购买 nike

我正在基于 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 的“运行”方法添加了一个过滤器。

当然,我可能完全忽略了这里的重点!如果您能给我任何指导,我将不胜感激!

最佳答案

Lithium 有一个用于 http 请求的 lithium\action\Dispatcher 和一个用于控制台命令的 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);
});

关于php - Lithium PHP 集成测试 - 不包括 routes.php?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20745175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com