模拟Laravel配置缓存

时间:2018-12-20 20:33:01

标签: php laravel phpunit artisan mockery

我正在编写一个新的artisan命令,如果配置被缓存(通过config:cache),我不想运行它:

class NewCommand extends Command
{
    public function handle()
    {
        if (app()->configurationIsCached())
        {
            $this->error('Configuration is cached.  Unable to run command');
            return 1;
        }
    }
}

我正试图编写一个单元测试来解决这个问题,但这失败了:

public function test_command_not_run_if_config_cached()
{
    App::shouldReceive('configurationIsCached')
        ->once()
        ->andReturn(true);

    $this->artisan('new:command')
        ->expectsOutput('Configuration is cached.  Unable to run command');
        ->assertExitCode(1);
}

结果:Method configurationIsCached() from Mockery_0_Illuminate_Foundation_Application should be called exactly 1 times but called 0 times

还有另一种方法可以模拟配置在单元测试中缓存吗?

1 个答案:

答案 0 :(得分:0)

结果是configurationIsCached()App外观上可用。目前尚未记录,但是将命令中的handle函数更新为

public function handle()
{
    if (App::configurationIsCached())
    {
        $this->error('Configuration is cached.  Unable to run command');
        return 1;
    }
}

允许测试通过。感谢@Namosheck指出了这一点