It becomes easier to mock any class.
use Twilio\Rest\Client;
class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->bind(Client::class, function ($app) {
return new Client('', '');
});
}
}
use Twilio\Rest\Client;
$twilio = app(Client::class);
$message = $twilio->messages->create()
//
use Mockery;
use Mockery\MockInterface;
use Twilio\Rest\Api\V2010\Account\MessageInstance;
use Twilio\Rest\Client;
use Twilio\Rest\Api\V2010\Account\MessageList;
public function test_twilio(): void
{
$this->mock(Client::class, function (MockInterface $mock) {
$message = Mockery::mock(MessageInstance::class);
$message->sid = 0;
$messages = Mockery::mock(MessageList::class);
$messages->shouldReceive('create')->andReturn($message);
$mock->messages = $messages;
});
$response = $this->get('/twilio');
$response->assertOk();
}
However, in the example of this question, it is difficult to mock because it is called with $twilio->messages. Older code or code that is automatically generated by OpenAPI often uses properties. When creating packages for Laravel, it is better to consider the ease of testing for users and use the method call form $twilio->messages()->create().
https://stackoverflow.com/questions/78889243/how-to-mock-the-twilio-library-in-laravel/78890240