# Mockery: A Better Way

This post is part of a series called Test-Driven PHP.
Evolving Toward a Persistence Layer

Mockery is a PHP extension that offers a superior mocking experience, particularly when compared to PHPUnit. While PHPUnit's mocking framework is powerful, Mockery offers a more natural language with a Hamcrest-like set of matchers. In this article, I'll compare the two mocking frameworks and highlight the best features of Mockery.

Mockery offers a set of mocking-related matchers that are very similar to a Hamcrest dictionary, offering a very natural way to express mocked expectations. Mockery does not override or conflict with PHPUnit's built-in mocking functions; in fact, you can use them both at the same time (and even in the same test method).

## Installing Mockery

There are multiple ways to install Mockery; here are the most common methods.

### Use Composer

Create a file called composer.json in you project's root folder, and add the following code to that file:

 1 {  2  "require": {  3  "Mockery/Mockery": ">=0.7.2"  4  }  5 } 

Next, simply install Composer in your project's root folder using the following command:

 1 curl -s http://getcomposer.org/installer | php 

Finally, install any required dependencies (including Mockery) with this command:

 1 php composer.phar install 

With everything installed, let's ensure that our Mockery installation works. For the sake of simplicity, I'll assume that you have a folder, called Test in your project's root directory. All of the examples in this tutorial will reside in this folder. Here's the code I've used to ensure that Mockery works with my project:

 1 //Filename: JustToCheckMockeryTest.php  2 3 require_once '../vendor/autoload.php';  4 5 class JustToCheckMockeryTest extends PHPUnit_Framework_TestCase {  6 7  protected function tearDown() {  8  \Mockery::close();  9  }  10 11 12  function testMockeryWorks() {  13  $mock = \Mockery::mock('AClassToBeMocked');  14 $mock->shouldReceive('someMethod')->once();  15 16  $workerObject = new AClassToWorkWith;  17 $workerObject->doSomethingWit($mock);  18  }  19 }  20 21 class AClassToBeMocked {}  22 23 class AClassToWorkWith {  24 25  function doSomethingWit($anotherClass) {  26  return $anotherClass->someMethod();  27  }  28 29 }  ### Linux Users: Use Your Distro's Packages Some Linux distributions make it easy to install Mockery, but only a handful provide a Mockery package for their system. The following list are the only distros I'm aware of: • Sabayon: equo install Mockery • Fedora / RHE: yum install Mockery ### Use PEAR PEAR fans can install Mockery by issuing the following commands:  1 sudo pear channel-discover pear.survivethedeepend.com  2 sudo pear channel-discover hamcrest.googlecode.com/svn/pear  3 sudo pear install --alldeps deepend/Mockery  ### Installing from Source Installing from GitHub is for the real geeks! You can always grab the latest version of Mockery through its GitHub repository.  1 git clone git://github.com/padraic/Mockery.git  2 cd Mockery  3 sudo pear channel-discover hamcrest.googlecode.com/svn/pear  4 sudo pear install --alldeps package.xml  ## Creating our First Mocked Object Let's mock some objects before we define any expectations. The following code will modify the previous example to include both PHPUnit and Mockery examples:  1 //Filename: MockeryABetterWayOfMockingTest.php  2 require_once '../vendor/autoload.php';  3 4 class MockeryVersusPHPUnitGetMockTest extends PHPUnit_Framework_TestCase {  5 6  function testCreateAMockedObject() {  7  // With PHPUnit  8 $phpunitMock = $this->getMock('AClassToBeMocked');  9 10  // With Mockery  11 $mockeryMock = \Mockery::mock('AClassToBeMocked');  12  }  13 14 }  15 16 class AClassToBeMocked {  17 18 } 

Mockery allows you to define mocks for classes that do not exist.

The first line ensures that we have access to Mockery. Next, we create a test class, called MockeryVersusPHPUnitGetMockTest, which has a method, testCreateAMockedObject(). The mocked class, AClassToBeMocked, is completely empty at this time; in fact, you could completely remove the class without causing the test to fail.

The testCreateAMockedObject() test method defines two objects. The first is a PHPUnit mock, and the second is created with Mockery. Mockery's syntax is:

 1 $mockedObject = \Mockery::mock('SomeClassToBeMocked');  ## Assign Simple Expectations Mocks are commonly used to verify an object's behavior (primarily its methods) by specifying what are called expectations. Let's set up a few simple expectations. ### Expect a Method to be Called Probably the most common expectation is one that expects a specific method call. Most mocking frameworks allow you to specify the amount of calls you expect a method to receive. We'll begin with a simple single call expectation:  1 //Filename: MockeryABetterWayOfMockingTest.php  2 require_once '../vendor/autoload.php';  3 4 class MockeryVersusPHPUnitGetMockTest extends PHPUnit_Framework_TestCase {  5 6  protected function tearDown() {  7  \Mockery::close();  8  }  9 10  function testExpectOnce() {  11 $someObject = new SomeClass();  12 13  // With PHPUnit  14  $phpunitMock =$this->getMock('AClassToBeMocked');  15  $phpunitMock->expects($this->once())->method('someMethod');  16  // Exercise for PHPUnit  17  $someObject->doSomething($phpunitMock);  18 19  // With Mockery  20  $mockeryMock = \Mockery::mock('AnInexistentClass');  21 $mockeryMock->shouldReceive('someMethod')->once();  22  // Exercise for Mockery  23  $someObject->doSomething($mockeryMock);  24  }  25 26 }  27 28 class AClassToBeMocked {  29  function someMethod() {}  30 }  31 32 class SomeClass {  33  function doSomething($anotherObject) {  34 $anotherObject->someMethod();  35  }  36 } 

This code configures an expectation for both PHPUnit and Mockery. Let's start with the former.

Some Linux distributions make it easy to install Mockery.

We use the expects() method to define an expectation to call someMethod() once. But in order for PHPUnit to work correctly, we must define a class called AClassToBeMocked, and it must have a someMethod() method.

This is a problem. If you are mocking a lot of objects and developing using TDD principles for a top-down design, you would not want to create all the classes and methods before your test. Your test should fail for the right reason, that the expected method was not called, instead of a critical PHP error with no relation to the real implementation. Go ahead and try to remove the someMethod() definition from AClassToBeMocked, and see what happens.

Mockery, on the other hand, allows you to define mocks for classes that do not exist.

Notice that the above example creates a mock for AnInexistentClass, which as its name implies, does not exist (nor does its someMethod() method).

 1 function testExpectMultiple() {  2  $someObject = new SomeClass();  3 4  // With PHPUnit 2 times  5 $phpunitMock = $this->getMock('AClassToBeMocked');  6 $phpunitMock->expects($this->exactly(2))->method('someMethod');  7  // Exercise for PHPUnit  8 $someObject->doSomething($phpunitMock);  9 $someObject->doSomething($phpunitMock);  10 11  // With Mockery 2 times  12 $mockeryMock = \Mockery::mock('AnInexistentClass');  13  $mockeryMock->shouldReceive('someMethod')->twice();  14  // Exercise for Mockery  15 $someObject->doSomething($mockeryMock);  16 $someObject->doSomething($mockeryMock);  17 18  // With Mockery 3 times  19 $mockeryMock = \Mockery::mock('AnInexistentClass');  20  $mockeryMock->shouldReceive('someMethod')->times(3);  21  // Exercise for Mockery  22 $someObject->doSomething($mockeryMock);  23 $someObject->doSomething($mockeryMock);  24 $someObject->doSomething($mockeryMock);  25 }  Mockery provides two different methods to better suit your needs. The first method, twice(), expects two method calls. The other method is times(), which lets you specify an amount. Mockery's approach is much cleaner and easier to read. ## Returning Values Another common use for mocks is to test a method's return value. Naturally, both PHPUnit and Mockery have the means to verify return values. Once again, let's start with something simple. ### Simple Return Values The following code contains both PHPUnit and Mockery code. I also updated SomeClass to provide a testable return value.  1 class MockeryVersusPHPUnitGetMockTest extends PHPUnit_Framework_TestCase {  2 3  protected function tearDown() {  4  \Mockery::close();  5  }  6 7  // [...] //  8 9  function testSimpleReturnValue() {  10 $someObject = new SomeClass();  11  $someValue = 'some value';  12 13  // With PHPUnit  14 $phpunitMock = $this->getMock('AClassToBeMocked');  15 $phpunitMock->expects($this->once())->method('someMethod')->will($this->returnValue($someValue));  16  // Expect the returned value  17 $this->assertEquals($someValue,$someObject->doSomething($phpunitMock));  18 19 20  // With Mockery  21 $mockeryMock = \Mockery::mock('AnInexistentClass');  22  $mockeryMock->shouldReceive('someMethod')->once()->andReturn($someValue);  23  // Expect the returned value  24  $this->assertEquals($someValue, $someObject->doSomething($mockeryMock));  25  }  26 27 }  28 29 class AClassToBeMocked {  30 31  function someMethod() {  32 33  }  34 35 }  36 37 class SomeClass {  38 39  function doSomething($anotherObject) {  40  return$anotherObject->someMethod();  41  }  42 43 } 

Both PHPUnit's and Mockery's API is straight-forward and easy to use, but I still find Mockery to be cleaner and more readable.

### Returning Different Values

Frequent unit testers can testify to complications with methods that return different values. Unfortunately, PHPUnit's limited $this->at($index) method is the only way to return different values from the same method. The following code demonstrates the at() method:

 1 function testDemonstratePHPUnitCallIndexing() {  2  $someObject = new SomeClass();  3 $firstValue = 'first value';  4  $secondValue = 'second value';  5 6  // With PHPUnit  7 $phpunitMock = $this->getMock('AClassToBeMocked');  8 $phpunitMock->expects($this->at(0))->method('someMethod')->will($this->returnValue($firstValue));  9 $phpunitMock->expects($this->at(1))->method('someMethod')->will($this->returnValue($secondValue));  10  // Expect the returned value  11 $this->assertEquals($firstValue,$someObject->doSomething($phpunitMock));  12 $this->assertEquals($secondValue,$someObject->doSomething($phpunitMock));  13 14 }  This code defines two separate expectations and makes two different calls to someMethod(); so, this test passes. But let's introduce a twist and add a double call in the tested class:  1 2 // [...] //  3 function testDemonstratePHPUnitCallIndexingOnTheSameClass() {  4 $someObject = new SomeClass();  5  $firstValue = 'first value';  6 $secondValue = 'second value';  7 8  // With PHPUnit  9  $phpunitMock =$this->getMock('AClassToBeMocked');  10  $phpunitMock->expects($this->at(0))->method('someMethod')->will($this->returnValue($firstValue));  11  $phpunitMock->expects($this->at(1))->method('someMethod')->will($this->returnValue($secondValue));  12  // Expect the returned value  13  $this->assertEquals('first value second value',$someObject->concatenate($phpunitMock));  14 15 }  16 17 class SomeClass {  18 19  function doSomething($anotherObject) {  20  return $anotherObject->someMethod();  21  }  22 23  function concatenate($anotherObject) {  24  return $anotherObject->someMethod() . ' ' .$anotherObject->someMethod();  25  }  26 27 } 

The test still passes. PHPUnit expects two calls to someMethod() that happen inside the tested class when performing the concatenation via the concatenate() method. The first call returns the first value, and the second call returns the second value. But, here's the catch: what would happen if you double the assertion? Here's the code:

 1 function testDemonstratePHPUnitCallIndexingOnTheSameClass() {  2  $someObject = new SomeClass();  3 $firstValue = 'first value';  4  $secondValue = 'second value';  5 6  // With PHPUnit  7 $phpunitMock = $this->getMock('AClassToBeMocked');  8 $phpunitMock->expects($this->at(0))->method('someMethod')->will($this->returnValue($firstValue));  9 $phpunitMock->expects($this->at(1))->method('someMethod')->will($this->returnValue($secondValue));  10  // Expect the returned value  11 $this->assertEquals('first value second value', $someObject->concatenate($phpunitMock));  12  $this->assertEquals('first value second value',$someObject->concatenate($phpunitMock));  13 14 }  It returns the following error:  1 Failed asserting that two strings are equal.  2 --- Expected  3 +++ Actual  4 @@ @@  5 -'first value second value'  6 +' '  PHPUnit continues counting between distinct calls to concatenate(). By the time the second call in the last assertion occurs, $index is at the values 2 and 3. You can make the test pass by modifying your expectations to consider the two new steps, like this:

 1 function testDemonstratePHPUnitCallIndexingOnTheSameClass() {  2  $someObject = new SomeClass();  3 $firstValue = 'first value';  4  $secondValue = 'second value';  5 6  // With PHPUnit  7 $phpunitMock = $this->getMock('AClassToBeMocked');  8 $phpunitMock->expects($this->at(0))->method('someMethod')->will($this->returnValue($firstValue));  9 $phpunitMock->expects($this->at(1))->method('someMethod')->will($this->returnValue($secondValue));  10 $phpunitMock->expects($this->at(2))->method('someMethod')->will($this->returnValue($firstValue));  11 $phpunitMock->expects($this->at(3))->method('someMethod')->will($this->returnValue($secondValue));  12  // Expect the returned value  13 $this->assertEquals('first value second value', $someObject->concatenate($phpunitMock));  14  $this->assertEquals('first value second value',$someObject->concatenate($phpunitMock));  15 16 }  You can probably live with this code, but Mockery makes this scenario trivial. Don't believe me? Take a look:  1 function testMultipleReturnValuesWithMockery() {  2 $someObject = new SomeClass();  3  $firstValue = 'first value';  4 $secondValue = 'second value';  5 6  // With Mockery  7  $mockeryMock = \Mockery::mock('AnInexistentClass');  8 $mockeryMock->shouldReceive('someMethod')->andReturn($firstValue,$secondValue, $firstValue,$secondValue);  9 10  // Expect the returned value  11  $this->assertEquals('first value second value',$someObject->concatenate($mockeryMock));  12 $this->assertEquals('first value second value', $someObject->concatenate($mockeryMock));  13 } 

Like PHPUnit, Mockery uses index counting, but we don't have to worry about indices. Instead, we simply list all the expected values, and Mockery returns them in order.

Additionally, PHPUnit returns NULL for unspecified indexes, but Mockery always returns the last specified value. That's a nice touch.

### Try Multiple Methods with Indexing

Let's introduce a second method into our code, the concatWithMinus() method:

 1 class SomeClass {  2 3  function doSomething($anotherObject) {  4  return$anotherObject->someMethod();  5  }  6 7  function concatenate($anotherObject) {  8  return$anotherObject->someMethod() . ' ' . $anotherObject->someMethod();  9  }  10 11  function concatWithMinus($anotherObject) {  12  return $anotherObject->anotherMethod() . ' - ' .$anotherObject->anotherMethod();  13  }  14 15 } 

This method behaves similarly to concatenate(), but it concatenates the string values with " - " as opposed to a single space. Because these two methods perform similar tasks, it makes sense to to test them inside the same test method to avoid duplicate testing.

As demonstrated in the above code, the second function uses a different mocked method called anotherMethod(). I made this change to force us to mock both methods in our tests. Our mockable class now looks like this:

 1 class AClassToBeMocked {  2 3  function someMethod() {  4 5  }  6 7  function anotherMethod() {  8 9  }  10 11 } 

Testing this with PHPUnit might look like the following:

 1 function testPHPUnitIndexingOnMultipleMethods() {  2  $someObject = new SomeClass();  3 $firstValue = 'first value';  4  $secondValue = 'second value';  5 6  // With PHPUnit  7 $phpunitMock = $this->getMock('AClassToBeMocked');  8 9  // First and second call on the semeMethod:  10 $phpunitMock->expects($this->at(0))->method('someMethod')->will($this->returnValue($firstValue));  11 $phpunitMock->expects($this->at(1))->method('someMethod')->will($this->returnValue($secondValue));  12  // Expect the returned value  13 $this->assertEquals('first value second value', $someObject->concatenate($phpunitMock));  14 15  // First and second call on the anotherMethod:  16  $phpunitMock->expects($this->at(0))->method('anotherMethod')->will($this->returnValue($firstValue));  17  $phpunitMock->expects($this->at(1))->method('anotherMethod')->will($this->returnValue($secondValue));  18  // Expect the returned value  19  $this->assertEquals('first value - second value',$someObject->concatWithMinus($phpunitMock));  20 }  The logic is sound. Define two different expectations for each method and specify the return value. This works only with PHPUnit 3.6 or newer. Please Note: PHPunit 3.5 and older had a bug which did not reset the index for each method, resulting in unexpected return values for mocked methods. Let's look at the same scenario with Mockery. Once again, we get much cleaner code. See for yourself:  1 function testMultipleReturnValuesForDifferentFunctionsWithMockery() {  2 $someObject = new SomeClass();  3  $firstValue = 'first value';  4 $secondValue = 'second value';  5 6  // With Mockery  7  $mockeryMock = \Mockery::mock('AnInexistentClass');  8 $mockeryMock->shouldReceive('someMethod')->andReturn($firstValue,$secondValue);  9  $mockeryMock->shouldReceive('anotherMethod')->andReturn($firstValue, $secondValue);  10 11  // Expect the returned value  12 $this->assertEquals('first value second value', $someObject->concatenate($mockeryMock));  13  $this->assertEquals('first value - second value',$someObject->concatWithMinus($mockeryMock));  14 }  ## Return Values Based on Given Parameter Honestly, this is something PHPUnit simply cannot do. At the time of this writing, PHPUnit does not permit you to return different values from the same function based on the function's parameter. Therefore, the following test fails:  1 2 // [...] //  3 function testPHUnitCandDecideByParameter() {  4 $someObject = new SomeClass();  5 6  // With PHPUnit  7  $phpunitMock =$this->getMock('AClassToBeMocked');  8  $phpunitMock->expects($this->any())->method('getNumber')->with(2)->will($this->returnValue(2));  9 $phpunitMock->expects($this->any())->method('getNumber')->with(3)->will($this->returnValue(3));  10 11  $this->assertEquals(4,$someObject->doubleNumber($phpunitMock, 2));  12 $this->assertEquals(6, $someObject->doubleNumber($phpunitMock, 3));  13 14 }  15 16 class AClassToBeMocked {  17 18 // [...] //  19  function getNumber($number) {  20  return$number;  21  }  22 }  23 24 class SomeClass {  25 26  // [...] //  27 28  function doubleNumber($anotherObject,$number) {  29  return $anotherObject->getNumber($number) * 2;  30  }  31 } 

Please ignore the fact that there is no logic in this example; it would fail even if it was present. This code, however, does help illustrate the idea.

This test fails because PHPUnit cannot differentiate between the two expectations in the test. The second expectation, expecting parameter 3, simply overrides the first expecting parameter 2. If you attempt to run this test, you get the following error:

 1 Expectation failed for method name is equal to when invoked zero or more times  2 Parameter 0 for invocation AClassToBeMocked::getNumber(2) does not match expected value.  3 Failed asserting that 2 matches expected 3. 

Mockery can do this, and the code below works exactly as you would expect it to work. The method returns different values based on its provided parameters:

 1 function testMockeryReturningDifferentValuesBasedOnParameter() {  2  $someObject = new SomeClass();  3 4  // Mockery  5 $mockeryMock = \Mockery::mock('AnInexistentClass');  6  $mockeryMock->shouldReceive('getNumber')->with(2)->andReturn(2);  7 $mockeryMock->shouldReceive('getNumber')->with(3)->andReturn(3);  8 9  $this->assertEquals(4,$someObject->doubleNumber($mockeryMock, 2));  10 $this->assertEquals(6, $someObject->doubleNumber($mockeryMock, 3));  11 } 

## Partial Mocks

Sometimes, you want to mock only specific methods on your object (as opposed to mocking an entire object). The following Calculator class already exists; we want to only mock certain methods:

 1 class Calculator {  2  function add($firstNo,$secondNo) {  3  return $firstNo +$secondNo;  4  }  5 6  function subtract($firstNo,$secondNo) {  7  return $firstNo -$secondNo;  8  }  9 10  function multiply($value,$multiplier) {  11  $newValue = 0;  12  for($i=0;$i<$multiplier;$i++)  13 $newValue = $this->add($newValue, $value);  14  return$newValue;  15  }  16 } 

This Calculator class has three methods: add(), subtract(), and multiply(). Multiply uses a loop to perform the multiplication by calling the add() for a specified amount of times (e.g. 2 x 3 is really 2 + 2 + 2).

Let's assume that we want to test multiply() in total isolation; so, we'll mock add() and check for specific behavior on multiply(). Here are some possible tests:

 1 function testPartialMocking() {  2  $value = 3;  3 $multiplier = 2;  4  $result = 6;  5 6  // PHPUnit  7 $phpMock = $this->getMock('Calculator', array('add'));  8 $phpMock->expects($this->exactly(2))->method('add')->will($this->returnValue($result));  9 10 $this->assertEquals($result,$phpMock->multiply($value,$multiplier));  11 12  // Mockery  13  $mockeryMock = \Mockery::mock(new Calculator);  14 $mockeryMock->shouldReceive('add')->andReturn($result);  15 16 $this->assertEquals($result,$mockeryMock->multiply($value,$multiplier));  17 18  // Mockery extended test checking parameters  19  $mockeryMock2 = \Mockery::mock(new Calculator);  20 $mockeryMock2->shouldReceive('add')->with(0,3)->andReturn(3);  21  $mockeryMock2->shouldReceive('add')->with(3,3)->andReturn(6);  22 23 $this->assertEquals($result,$mockeryMock2->multiply($value,$multiplier));  24 } 

Mockery offers...a very natural way to express mocked expectations.

The first PHPUnit test is anemic; it simply tests that the method add() is called twice and it returns the final value on each call. It gets the job done, but it's also a little complicated. PHPUnit forces you to pass the list of methods that you want to mock as second parameter to $this->getMock(). Otherwise, PHPUnit would mock all methods, each returning NULL by default. This list must be kept in concordance with the expectations you define on your mocked object. For example, if I add a second expectation to $phpMock's substract() method, PHPUnit would ignore it and call the original substract() method. That is, unless I explicitly specify the name of the method (substract) in the $this->getmock() statement. Of course, Mockery is different by allowing you to provide a real object to \Mockery::mock(), and it automatically creates a partial mock. It achieves this by implementing a proxy-like solution for mocking. All the expectations you define are used, but Mockery falls back to the original method if you do not specify an expectation for that method. Please Note: Mockery's approach is very simple, but internal method calls do not pass through the mocked object. This example is misleading, but it illustrates how not to use Mockery's partial mocks. Yes, Mockery creates a partial mock if you pass a real object, but it only mocks only external calls. For example, based on the previous code, the multiply() method calls the real add() method. Go ahead and try to change the last expectation from ...->andReturn(6) to ...->andReturn(7). The test should obviously fail, but it doesn't because the real add() executes instead of the mocked add() method. But we can circumvent this issue by creating mocks like this:  1 //Instead of  2 $mockeryMock = \Mockery::mock(new Calculator);  3 // Create the mock like this  4 $mockeryMock = \Mockery::mock('Calculator[add]');  While syntactically different, the concept is similar to PHPUnit's approach: you have to list the mocked methods in two places. But for any other test, you can just simply pass the real object, which is much easier--especially when dealing with constructor parameters. ## Dealing with Constructor Parameters Let's add a constructor with two parameters to the Calculator class. The revised code:  1 class Calculator {  2  public$myNumbers = array();  3 4  function __construct($firstNo,$secondNo) {  5  $this->myNumbers[]=$firstNo;  6  $this->myNumbers[]=$secondNo;  7  }  8  // [...] //  9 } 

Every test in this article will fail after adding this constructor. More precisely, the testPartialMock() test results in the following error:

 1 Missing argument 1 for Calculator::__construct(),  2 called in /usr/share/php/PHPUnit/Framework/MockObject/Generator.php  3 on line 224 and defined 

PHPUnit tries to mock the real object by automatically calling the constructor, expecting to have the parameters correctly set. There are two ways around this problem: either set the parameters, or don't call the constructor.

 1 //Specify Constructor Parameters  2 $phpMock =$this->getMock('Calculator', array('add'), array(1,2));  3 4 //Do not call original constructor  5 $phpMock =$this->getMock('Calculator', array('add'), array(), '', false); 

Mockery automagically works around this problem. It's okay not to specify a constructor parameter; Mockery will simply not call the constructor. But you can specify a list of constructor parameters for Mockery to use. For example:

 1 function testMockeryConstructorParameters() {  2  $result = 6;  3  // Mockery  4  // Do not call constructor  5 $noConstrucCall = \Mockery::mock('Calculator[add]');  6  $noConstrucCall->shouldReceive('add')->andReturn($result);  7 8  // Use constructor parameters  9  $withConstructParams = \Mockery::mock('Calculator[add]', array(1,2));  10 $withConstructParams->shouldReceive('add')->andReturn($result);  11 12  // User real object with real values and mock over it  13 $realCalculator = new Calculator(1,2);  14  $mockRealObj = \Mockery::mock($realCalculator);  15  $mockRealObj->shouldReceive('add')->andReturn($result);  16 } 

## Technical Considerations

Mockery is another library that integrates your tests, and you may want to consider what technical implications this may have.

• Mockery uses a lot of memory. You will have to increase the maximum memory to 512MB if you want to run many tests(say over 1000 tests with more than 3000 assertions). See php.ini documentation for further details.
• You have to organize your tests to run in separate processes, when mocking static methods and static method calls.
• You can auto-load Mockery into every test by using PHPUnit's bootstrap functionality (helpful when you have many tests and you don't want to repeat yourself).
• You can automate the call to \Mockery::close() in each test's tearDown() by editing phpunit.xml.

## Final Conclusions

PHPUnit certainly has its issues, especially when it comes to functionality and expressiveness. Mockery can greatly improve your mocking experience by making your tests easy to write and understand - but it's not perfect (there's no such thing!).

This tutorial has highlighted many key aspects of Mockery, but, honestly, we've barely scratched the surface. Be sure to explore the project's Github repository to learn more.