1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
.. index::
single: Quick Reference
Quick Reference
===============
The purpose of this page is to give a quick and short overview of some of the
most common Mockery features.
Do read the :doc:`../reference/index` to learn about all the Mockery features.
Integrate Mockery with PHPUnit, either by extending the ``MockeryTestCase``:
.. code-block:: php
use \Mockery\Adapter\Phpunit\MockeryTestCase;
class MyTest extends MockeryTestCase
{
}
or by using the ``MockeryPHPUnitIntegration`` trait:
.. code-block:: php
use \PHPUnit\Framework\TestCase;
use \Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
class MyTest extends TestCase
{
use MockeryPHPUnitIntegration;
}
Creating a test double:
.. code-block:: php
$testDouble = \Mockery::mock('MyClass');
Creating a test double that implements a certain interface:
.. code-block:: php
$testDouble = \Mockery::mock('MyClass, MyInterface');
Expecting a method to be called on a test double:
.. code-block:: php
$testDouble = \Mockery::mock('MyClass');
$testDouble->shouldReceive('foo');
Expecting a method to **not** be called on a test double:
.. code-block:: php
$testDouble = \Mockery::mock('MyClass');
$testDouble->shouldNotReceive('foo');
Expecting a method to be called on a test double, once, with a certain argument,
and to return a value:
.. code-block:: php
$mock = \Mockery::mock('MyClass');
$mock->shouldReceive('foo')
->once()
->with($arg)
->andReturn($returnValue);
Expecting a method to be called on a test double and to return a different value
for each successive call:
.. code-block:: php
$mock = \Mockery::mock('MyClass');
$mock->shouldReceive('foo')
->andReturn(1, 2, 3);
$mock->foo(); // int(1);
$mock->foo(); // int(2);
$mock->foo(); // int(3);
$mock->foo(); // int(3);
Creating a runtime partial test double:
.. code-block:: php
$mock = \Mockery::mock('MyClass')->makePartial();
Creating a spy:
.. code-block:: php
$spy = \Mockery::spy('MyClass');
Expecting that a spy should have received a method call:
.. code-block:: php
$spy = \Mockery::spy('MyClass');
$spy->foo();
$spy->shouldHaveReceived()->foo();
Not so simple examples
^^^^^^^^^^^^^^^^^^^^^^
Creating a mock object to return a sequence of values from a set of method
calls:
.. code-block:: php
use \Mockery\Adapter\Phpunit\MockeryTestCase;
class SimpleTest extends MockeryTestCase
{
public function testSimpleMock()
{
$mock = \Mockery::mock(array('pi' => 3.1416, 'e' => 2.71));
$this->assertEquals(3.1416, $mock->pi());
$this->assertEquals(2.71, $mock->e());
}
}
Creating a mock object which returns a self-chaining Undefined object for a
method call:
.. code-block:: php
use \Mockery\Adapter\Phpunit\MockeryTestCase;
class UndefinedTest extends MockeryTestCase
{
public function testUndefinedValues()
{
$mock = \Mockery::mock('mymock');
$mock->shouldReceive('divideBy')->with(0)->andReturnUndefined();
$this->assertTrue($mock->divideBy(0) instanceof \Mockery\Undefined);
}
}
Creating a mock object with multiple query calls and a single update call:
.. code-block:: php
use \Mockery\Adapter\Phpunit\MockeryTestCase;
class DbTest extends MockeryTestCase
{
public function testDbAdapter()
{
$mock = \Mockery::mock('db');
$mock->shouldReceive('query')->andReturn(1, 2, 3);
$mock->shouldReceive('update')->with(5)->andReturn(NULL)->once();
// ... test code here using the mock
}
}
Expecting all queries to be executed before any updates:
.. code-block:: php
use \Mockery\Adapter\Phpunit\MockeryTestCase;
class DbTest extends MockeryTestCase
{
public function testQueryAndUpdateOrder()
{
$mock = \Mockery::mock('db');
$mock->shouldReceive('query')->andReturn(1, 2, 3)->ordered();
$mock->shouldReceive('update')->andReturn(NULL)->once()->ordered();
// ... test code here using the mock
}
}
Creating a mock object where all queries occur after startup, but before finish,
and where queries are expected with several different params:
.. code-block:: php
use \Mockery\Adapter\Phpunit\MockeryTestCase;
class DbTest extends MockeryTestCase
{
public function testOrderedQueries()
{
$db = \Mockery::mock('db');
$db->shouldReceive('startup')->once()->ordered();
$db->shouldReceive('query')->with('CPWR')->andReturn(12.3)->once()->ordered('queries');
$db->shouldReceive('query')->with('MSFT')->andReturn(10.0)->once()->ordered('queries');
$db->shouldReceive('query')->with(\Mockery::pattern("/^....$/"))->andReturn(3.3)->atLeast()->once()->ordered('queries');
$db->shouldReceive('finish')->once()->ordered();
// ... test code here using the mock
}
}
|