Trying to mock Twilio in phpunit, first mock works, but the second doesn’t

  laravel, mockery, phpunit, twilio, unit-testing

I am using phpunit and Laravel. I need to test the routes of a controller that uses TwilioRestClient and I am stuck at the create function!

This is the general code of the create function that gives me problems:

public function create(Request $request)
   //some code

   $twilioAccount = $this->twilioClient->api->v2010->accounts->create($account_options);
   $this->twilioClient = new Client($twilioAccount->sid, $twilioAccount->auth_token);
   $apiKey = $this->twilioClient->newKeys->create($api_key_options);
   //Some more code

And this is what my most promising code for the test looks like:

public function testSuccessfullyCreate()
   //----------------------- Here is the part that works fine -----------------------------
   //mock for the $this->twilioClient->api->v2010->accounts->create()
   self::$mockTwilioAccountList = Mockery::mock('overload:TwilioRestApiV2010AccountList');
   //mocked AccountInstance that will be returned by create()   
   self::$mockTwilioAccount = Mockery::mock(overload:TwilioRestApiV2010AccountInstance');
   self::$mockTwilioAccount->sid = 'validAccountSid';
   self::$mockTwilioAccount->auth_token = 'validAccountAuthToken';


   //----------- Here is the mocking that doesn't want to work no matter what I try è_é -------------

   //mock for the $this->twilioClient->newKeys->create()
   self::$mockTwilioNewKey = Mockery::mock('overload:TwilioRestApiV2010AccountNewKeyList');
   //mocked NewKeyInstance that will be returned by create()   
   self::$mockTwilioApiKey = Mockery::mock(overload:TwilioRestApiV2010AccountNewKeyInstance');
   self::$mockTwilioApiKey->sid = 'validApiKeySid';

   self::$mockTwilioNewKey ->shouldReceive('create')->andReturn(self::$mockTwilioApiKey);

   //---------------------------- And here is my call -----------------------------
   $this->json('POST', $uri, [], $authentication_token);

Now, as I said, the first part that mock the create for TwilioAccount work just fine, I have no problem at all. Then comes the second mocking for the newKey and whether the "new Client()" part is commented or not, when I use dd() this is the message I get:
"Return value of TwilioRestApiV2010AccountContext::__get() must be an instance of TwilioListResource, instance of TwilioRestApiV2010AccountNeyKeyList returned"

The "funny" thing is that NewKeyList AND AccountList both extends from TwilioListResource, but "ok" I told myself "I’ll just add TwilioListResource to my NewKeyList mocking"…

So I tried these :

  • self::$mockTwilioNewKey = Mockery::mock(‘overload:TwilioRestApiV2010AccountNewKeyList, TwilioListResource’);
  • self::$mockTwilioNewKey = Mockery::mock(‘TwilioRestApiV2010AccountNewKeyList, TwilioListResource’);
  • self::$mockTwilioNewKey = Mockery::mock(‘TwilioRestApiV2010AccountNewKeyList’);

They all returned "[HTTP 404] Unable to create record: The requested resource was not found" meaning the mock didn’t work.

I also tried (among other things) mocking TwilioClient after reading this

So the expectations I used were:

but it didn’t work, for both.

Does anyone have any insight as to why NewKeyList doesn’t work? Or any ideas on another way to go around testing this? Because I’ve searched and tried possible solutions for over a week now and it’s beginning to drive me crazy. Please can anyone help me???

Source: Laravel

Leave a Reply