我(我们)正在创建一个作为未来CMS核心组件的包,当然这个包需要一些单元测试。 当包注册时,它首先设置后端/前端上下文,如下所示:
class FoundationServiceProvider extends ServiceProvider
{
// ... stuff ...
public function register()
{
// Switch the context.
// Url's containing '/admin' will get the backend context
// all other urls will get the frontend context.
$this->app['build.context'] = request()->segment(1) === 'admin'
? Context::BACKEND
: Context::FRONTEND;
}
}
当我访问 /admin 的url时,app('build.context')变量将被设置为
backend
,否则它将被设置为 `frontend。为了测试这个问题,我创建了以下测试:class ServiceProviderTest extends \TestCase
{
public function test_that_we_get_the_backend_context()
{
$this->visit('admin');
$this->assertEquals(Context::BACKEND, app('build.context'));
}
}
当我在浏览器中运行代码时(导航到 /admin),上下文将被捕获,并调用
app('build.context')
将返回 backend
,但是当运行此测试时,我总是得到“frontend”。请问我有没有注意到什么问题或在使用 phpunit 时出现了错误的代码?
提前致谢。