mockery

Mocking Laravel Eloquent models - how to set a public property with Mockery

孤人 提交于 2019-12-03 04:36:58
I want to use a mock object (Mockery) in my PHPUnit test. The mock object needs to have both some public methods and some public properties set. The class is a Laravel Eloquent model. I tried this: $mock = Mockery::mock('User'); $mock->shouldReceive('hasRole')->once()->andReturn(true); //works fine $mock->roles = 2; //how to do this? currently returns an error $this->assertTrue(someTest($mock)); ... but setting the public property returns this error: BadMethodCallException: Method Mockery_0_User::setAttribute() does not exist on this mock object This error is not returned when mocking a simple

stringContains argument matching within Laravel Log facade shouldReceive

自闭症网瘾萝莉.ら 提交于 2019-12-01 13:15:16
问题 I saw in the Laravel docs that it's possible to set a test expectation like this: Cache::shouldReceive('get') ->once() ->with('key') ->andReturn('value'); Then I saw in the PHPunit docs that flexible argument matching is possible like this: $this->stringContains('Something') . But when I edit my test to have this: Log::shouldReceive('error') ->once(); ->with($this->stringContains('Contact does not exist')); ...then I get the following error: Mockery\Exception\NoMatchingExpectationException:

Mocking a call with chained methods and arguments

北慕城南 提交于 2019-11-30 22:41:42
Im learning how to use mockery in order to run some unit test and Im not sure what to do to mock my database class. It consists of separate methods that can be chained like these two examples: $db->select('someTblName',['fieldName']) ->where('fieldName', 'someValue') ->runQuery() ->fetch(); //returns array or null Another use could be like: $db->select('someTblName') ->where('fieldName', 'someValue') ->where('fieldName', array('>=' , 'someValue') ->runQuery() ->fetch(); //returns array or null From reading some of the documentation I see that I can do something like:(for the first case) $db =

Mockery specifying expected arguments for multiple calls

本小妞迷上赌 提交于 2019-11-30 18:33:14
I am trying to mock an object that gets two calls to the same function but with different arguments. It's pretty straight forward to give back different return values for multiple calls but I can't find anywhere how to do it with the argument validation. I tried: $this->eventDispatcher ->shouldReceive('dispatch') ->twice() ->with(Events::SELECT,\Mockery::type('\Not\Really\A\Namespace\Event')) ->with(Events::ACTIVITY,\Mockery::type('\Not\Really\A\Namespace\Event'); And $this->eventDispatcher ->shouldReceive('dispatch') ->twice() ->with( [Events::SELECT,\Mockery::type('\Not\Really\A\Namespace

How to Test Laravel Socialite

核能气质少年 提交于 2019-11-30 09:09:52
I have an application that makes use of socialite, I want to create test for Github authentication, So I used Socialite Facade to mock call to the Socialite driver method, but when I run my test it tells me that I am trying to get value on null type. Below is the test I have written public function testGithubLogin() { Socialite::shouldReceive('driver') ->with('github') ->once(); $this->call('GET', '/github/authorize')->isRedirection(); } Below is the implementation of the test public function authorizeProvider($provider) { return Socialite::driver($provider)->redirect(); } I understand why it

Laravel Dependency Injection: When do you have to? When can you mock Facades? Advantages of either method?

寵の児 提交于 2019-11-30 01:58:30
I've been using Laravel for a while now and I have been reading a lot about Dependency Injection an testable code. I've come to a point of confusion when talking about Facades and Mocked Objects. I see two patterns: class Post extends Eloquent { protected $guarded = array(); public static $rules = array(); } This is my Post Model. I could run Post::all(); to get all the posts from my blog. Now I want to incorporate it into my controller. Option #1: Dependency Injection My first instinct would be to inject the Post model as a dependecy: class HomeController extends BaseController { public

How to Test Laravel Socialite

旧街凉风 提交于 2019-11-29 13:45:43
问题 I have an application that makes use of socialite, I want to create test for Github authentication, So I used Socialite Facade to mock call to the Socialite driver method, but when I run my test it tells me that I am trying to get value on null type. Below is the test I have written public function testGithubLogin() { Socialite::shouldReceive('driver') ->with('github') ->once(); $this->call('GET', '/github/authorize')->isRedirection(); } Below is the implementation of the test public function

How to mock static methods of a Laravel Eloquent model?

╄→尐↘猪︶ㄣ 提交于 2019-11-29 13:38:23
I have in my code a line like this: ModelName::create($data); where ModelName is just an Eloquent model. Is there a way to mock this call inside a unit test? I tried with: $client_mock = \Mockery::mock('Eloquent','App\Models\ModelName'); $client_mock->shouldReceive('create') ->with($data)->andReturns($returnValue); but it doesn't work. Marcin Nabiałek You should do something like this: $client_mock = \Mockery::mock('overload:App\Models\ModelName'); $client_mock->shouldReceive('create')->with($data)->andReturn($returnValue); We are using overload: because you don't want to pass mock to some

Laravel Dependency Injection: When do you have to? When can you mock Facades? Advantages of either method?

半世苍凉 提交于 2019-11-28 22:49:51
问题 I've been using Laravel for a while now and I have been reading a lot about Dependency Injection an testable code. I've come to a point of confusion when talking about Facades and Mocked Objects. I see two patterns: class Post extends Eloquent { protected $guarded = array(); public static $rules = array(); } This is my Post Model. I could run Post::all(); to get all the posts from my blog. Now I want to incorporate it into my controller. Option #1: Dependency Injection My first instinct would

How to mock static methods of a Laravel Eloquent model?

社会主义新天地 提交于 2019-11-28 07:17:37
问题 I have in my code a line like this: ModelName::create($data); where ModelName is just an Eloquent model. Is there a way to mock this call inside a unit test? I tried with: $client_mock = \Mockery::mock('Eloquent','App\Models\ModelName'); $client_mock->shouldReceive('create') ->with($data)->andReturns($returnValue); but it doesn't work. 回答1: You should do something like this: $client_mock = \Mockery::mock('overload:App\Models\ModelName'); $client_mock->shouldReceive('create')->with($data)-