I have problem.
There is a service class, what I want to mocking for test, but I can not.
My test:
protected function setUp() : void
{
parent::setUp();
Queue::fake();
$this->client = $this->createClient();
}
private function callJob()
{
CreateBrevoContactJob::dispatch($this->client);
}
/** @test */
public function it_calls_create_brevo_contact_api_endpoint()
{
$this->mock(ContactsApi::class, function (MockInterface $mock) {
$mock->shouldReceive('createContact')->once();
});
$this->callJob();
}
My job:
class CreateBrevoContactJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public Client $client;
private ContactsApi $brevo;
/**
* Create a new job instance.
* @param Client $client
* @return void
*/
public function __construct(Client $client)
{
$this->client = $client;
$this->brevo = (new Brevo())->getContactInstance();
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$this->brevo->createContact((new ClientBrevoContactAdapter($this->client))->convert());
// create new contact (api)
// create BrevoContact from brevo api instance
}
}
Brevo.php:
class Brevo extends Model
{
private Configuration $config;
public function __construct()
{
parent::__construct();
$this->config = Configuration::getDefaultConfiguration()->setApiKey('api-key', env('BREVO_API_KEY'));
}
public function getContactInstance(): ContactsApi
{
return new ContactsApi(
new Client(),
$this->config
);
}
}
test result:
- TestsUnitJobsCreateBrevoContactJobTest::it_calls_create_brevo_contact_api_endpoint
MockeryExceptionInvalidCountException: Method createContact() from Mockery_2_SendinBlue_Client_Api_ContactsApi should be called
exactly 1 times but called 0 times.
Maybe someone can help me… 🙂
Thanks,