Questions tagged [mockery]

Mockery is a PHP library used to create Mocks of Objects for testing purpose.

Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its core goal is to offer a test double framework with a succint API capable of clearly defining all possible object operations and interactions using a human readable Domain Specific Language (DSL).

https://github.com/padraic/mockery

529 questions
35
votes
9 answers

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

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 =…
mtmacdonald
  • 14,216
  • 19
  • 63
  • 99
34
votes
2 answers

PHPUnit Mock an object's properties

I'm looking for a way to mock an object and populate its properties. Here's an example of a method who uses a property of another object: class MyClass { private $_object; public function methodUnderTest($object) { $this->_object =…
Riccardo Cedrola
  • 1,270
  • 1
  • 10
  • 16
24
votes
1 answer

Mockery specifying expected arguments for multiple calls

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…
Tim Strijdhorst
  • 1,539
  • 3
  • 14
  • 29
21
votes
1 answer

Mockery throw on first then return value on second call

$client = Mockery::mock(); $client->shouldReceive('send')->andThrow($error)->andReturn(true); Unfortunately it only returns true but not throw the exception first. How do I throw an exception on first call then return value on second call of the…
Zhianc
  • 1,411
  • 3
  • 20
  • 37
20
votes
3 answers

Mockery and Laravel constructor injection

I am using laravel 5 with php unit to create a laravel package. I have a Repository.. namespace Myname\Myapp\Repositories; use Myname\Myapp\Models\PersonModel; class PersonRepository { protected $personModel; public function…
myol
  • 8,857
  • 19
  • 82
  • 143
20
votes
2 answers

Mockery no matching handler for closure

I can't figure out why I'm getting this error during this test. My test appears to be matching the rest of the code exactly. What am I overlooking? In my test I have: $passwordBroker = m::mock('Illuminate\Auth\Reminders\PasswordBroker'); …
Ben
  • 60,438
  • 111
  • 314
  • 488
18
votes
1 answer

How to mock static methods of a Laravel Eloquent model?

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 =…
Zed
  • 5,683
  • 11
  • 49
  • 81
18
votes
2 answers

Is it possible to mock protected properties and methods

Is it possible to mock a protected property with PHP Mockery? I got a class with a method, I will call it `a, that does some magic on an array that is retrieved from a protected property from the same class. That protected property is filled by…
Ilyes512
  • 2,627
  • 5
  • 23
  • 27
17
votes
3 answers

Mockery shouldReceive()->once() doesn't seem to work

I'm trying to get Mockery to assert that a given method is called at least once. My test class is: use \Mockery as m; class MyTest extends \PHPUnit_Framework_TestCase { public function testSetUriIsCalled() { $uri =…
Jez
  • 2,384
  • 1
  • 17
  • 31
16
votes
1 answer

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

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 { …
anglinb
  • 975
  • 1
  • 8
  • 15
14
votes
5 answers

How to Test Laravel Socialite

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…
George
  • 3,757
  • 9
  • 51
  • 86
12
votes
3 answers

What is the difference between overload and alias in Mockery?

I am new to using Mockery and confused with the terminology alias and overload. Can anyone please explain to me when to use which?
Hyder B.
  • 10,900
  • 5
  • 51
  • 60
12
votes
4 answers

Laravel Response::download() test

I have the following code in one of my routes: return Response::download('cv.pdf'); Any idea how to test this? I've tried to use shouldReceive() but that doesn't seem to work ('shouldReceive() undefined function....').
sb89
  • 353
  • 1
  • 6
  • 23
12
votes
1 answer

Class 'Mockery' not found

I use laravel (4.1) framework and i read "Laravel-testing-decoded", it's a ebook by Jeffrey Wey. I want to test my modal User and my method setPasswordAttribute($password) My unit-testing :
timothylhuillier
  • 451
  • 1
  • 8
  • 20
11
votes
1 answer

Mock Laravel's Config facade to return a value only for a certain key

I would like to mock Config::get('specific_key') to return a 'specific_value' in my test. So I wrote the following code: Config::shouldReceive('get') ->with('specific_key') ->andReturn('specific_value'); Config::makePartial(); This will…
Armin Sam
  • 903
  • 9
  • 23
1
2 3
35 36