I am using mockery/mockery to mock laravel db facade on my unit test. But I don't know how to create a stub for the when method. So here is my class that I want to test.
<?php
namespace App;
use Illuminate\Support\Facades\DB;
class TestRepo
{
public function testQb()
{
DB::table('users')
->when(true, function($query) {
$query->where('email_verified_at', null);
})
->get();
}
}
and I want to make sure that the querybuilder runs the when
method including the clousure.
so far I have this test without the stub for when
method
public function test_example()
{
DB::shouldReceive('table')->once()->with('users')->andReturnSelf();
DB::shouldReceive('get')->once()->andReturn(collect(new User()));
(new TestRepo())->testQb();
$this->assertTrue(true);
}
this will test will fail because I dont have a stub for laravel db facade when
method.
can somebody tells me how can I achieve this? thank you in advance.