0

On Laravel 8, I've created a facade and registered the service provider in Laravel. In my Pest test I've added \App\Facades\Twilio::shouldReceive('AsAdmin');.

Running the test results in:

PHP Fatal error:  Cannot redeclare Mockery_2_App_Facades_Twilio::shouldReceive() in /app/vendor/mockery/mockery/library/Mockery/Loader/EvalLoader.php(34) : eval()'d code on line 1032

There's nothing special in my Facade:

class Twilio extends Facade
{
    protected Client $adminCli;
    protected PendingRequest $http;

    protected static function getFacadeAccessor()
    {
        return 'twilio'; // same as bind method in service provider
    }

    public function AsAdmin()
    {
        $username = config('twilio.account_sid');
        $password = config('twilio.auth_token');

        $this->adminCli = new Client($username, $password);

        $this->http = Http::withBasicAuth($username, $password);

        return $this;
    }

Any guidance would be much appreciated.

tylersDisplayName
  • 1,603
  • 4
  • 24
  • 42

1 Answers1

0

The problem was that I had a circular reference to the Twilio facade.

The getFacadeAccessor() instructs the Facade on which service to use.

# Twilio Facade

protected static function getFacadeAccessor()
{
    return 'twilio'; // same as bind method in service provider
}

In my Twilio service provider I had the following, which calls back to the Facade that is calling the service provider. The service provider needs to provide access to a service (SDK for example), not the facade.

    public function register()
    {
        $this->app->bind('twilio',function(){
            # Wrong:
            #   return new \App\Facades\Twilio();
          
            # Correct:
            return new \Twilio\Rest\Client(config('twilio.username'), config('twilio.password')
        });
    }
tylersDisplayName
  • 1,603
  • 4
  • 24
  • 42