Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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