googleapis / google-api-php-client

A PHP client library for accessing Google APIs
Apache License 2.0
9.22k stars 3.52k forks source link

How to mock Google\Client? #2439

Closed atakhan closed 1 year ago

atakhan commented 1 year ago

Hi, thank you for this awesome client, it easy to understand and there is many interesting code tricks in source. My using experience was great while i decide to write a unittest for my job :) My job syncs data from Google analytics service with my local database. And it uses Google_Client. So, i am trying to mock google_client as $client = \Mockery::mock('overload:'. Google_Client::class) but there is an error Could not load mock Google_Client, class already exists There is no other tests and runInSeparateProcess not helps

bshaffer commented 1 year ago

It could be because Google_Client is an alias... try mocking using Google\Client::class instead. If that doesn't work, it's probably because both Google\Client and Google_Client are autoloaded using autoload-files, which means they've been loaded by the time your call to Mockery::mock takes place. I am not sure how Mockery works, but if it relies on a class to not have been autoloaded yet, then this isn't going to work.

I would suggest using prophecy-phpunit instead. This is a great mocking library, and recommended by phpunit.