/
index.html
411 lines (388 loc) · 13.5 KB
/
index.html
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<html>
<head>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/blood.css" id="theme">
<!-- Code syntax highlighting -->
<link class="codestyle" rel="stylesheet" href="lib/css/zenburn.css">
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>Phake it 'til you make it</h1>
<p>
A Presentation On The Phake Mocking Framework And How It
Compares To PHPUnit Mock Objects
</p>
<br/>
<h3>By Steven Blowers</h3>
<aside class="notes">
<p>Who am I?</p>
<p>What this presentation is about.</p>
<p>Hey Look At Look At This Cool Thing I Found! Presentation</p>
</aside>
</section>
<section>
<section>
<h3>Some Questions To Start Off</h3>
<h4>Hands up please...</h4>
<br/>
<p>When mocking object in PHP...</p>
<ul>
<li>Who uses Phake?</li>
<li>Who uses the PHPUnit Mock Objects?</li>
<li>Who is not familiar with mocking code?</li>
</ul>
<aside class="notes">
<p>Phake: refresher and validate your smart decision</p>
<p>PHPUnit: Primarily aimed at those using PHPUnit Mock Objects.</p>
<p>No Mock: some slides to get you up to speed.</p>
</aside>
</section>
<section>
<h3>Unit Testing</h3>
<p>Testing a small part of the codebase in isolation.</p>
<aside class="notes">
<p>Usually Unit = Class in OOP.</p>
<p>Use it to confirm code behaves as expected after changes made.</p>
</aside>
</section>
<section>
<h3>Test Doubles</h3>
</section>
<section data-background="class_disaster/1.png"></section>
<section data-background="class_disaster/2.png"></section>
<section data-background="class_disaster/3.png"></section>
<section data-background="class_disaster/4.png"></section>
<section data-background="class_disaster/5.png"></section>
</section>
<section>
<h2>Creating Mocks</h2>
</section>
<section>
<pre><code class="php">
class Fisherman
{
private $name;
public function __construct(string $name)
{
$this->name = $name;
}
public function sayHello(): string
{
return 'Hello! My name is ' . $this->name . '.';
}
}
</code></pre>
</section>
<section>
<h3>Creating Mocks</h3>
<p>Phake</p>
<pre><code class="php">
$fishermanMock = Phake::mock(Fisherman::class);
</code></pre>
<p>PHPUnit</p>
<pre><code class="php">
$fishermanMock = $this->getMockBuilder(Fisherman::class)
->disableOriginalConstructor()
->getMock();
</code></pre>
<aside class="notes">
<p>Class constant introduced in 5.5</p>
<p>Static function in Phake class.</p>
<p>Do not inherit from the Phake class.</p>
<p>PHP is single inheritance per class.</p>
<p>Phake not dependent on any particular testing framework.</p>
</aside>
</section>
<section>
<h3>Creating Partial Mocks</h3>
<p>Phake</p>
<pre><code class="php">
$fishermanMock = Phake::partialMock(Fisherman::class, 'Herb');
$fisherManMock->sayHello();
// -> Hello! My name is Herb.
</code></pre>
<p>PHPUnit</p>
<pre><code class="php">
$fishermanMock = $this->getMockBuilder(Fisherman::class)
->setConstructorArgs(['Herb'])
->setMethods(null)
->getMock();
$fisherManMock->sayHello();
// -> Hello! My name is Herb.
</code></pre>
<aside class="notes">
<p>What are partial mocks?</p>
</aside>
</section>
<section>
<h2>Method Stubbing</h2>
<aside class="notes">
<p>What is method stubbing?</p>
</aside>
</section>
<section>
<pre><code class="php">
class Fisherman
{
/** @var Lake */
private $lake;
...
public function fish(string $bait, string $time)
{
$fishCaught = $this->lake->fish($bait, $time);
return $fishCaught === 'red herring' ? $fishCaught : null;
}
}
</code></pre>
<pre><code class="php">
class Lake
{
public function fish(string $bait, string $timeOfDay = 'morning')
{
...
}
}
</code></pre>
</section>
<section>
<h3>Method Stubbing</h3>
<p>Phake</p>
<pre><code class="php">
$lakeMock = Phake::mock(Lake::class);
Phake::when($lakeMock)->fish('maggots', 'afternoon')->thenReturn('trout');
</code></pre>
<p>PHPUnit</p>
<pre><code class="php">
$lakeMock = $this->getMockBuilder(Lake::class)->getMock();
$lakeMock->method('fish')
->with('maggots', 'afternoon')
->willReturn('trout');
</code></pre>
<aside class="notes">
<p>Phake: Method being used like a method, not string</p>
</aside>
</section>
<section>
<h3>Stubbing Multiple Calls</h3>
<p>Phake</p>
<pre><code class="php">
Phake::when($lakeMock)->fish('red maggots', 'morning')->thenReturn('roach');
Phake::when($lakeMock)->fish('grasshoppers', 'afternoon')->thenReturn('bream');
Phake::when($lakeMock)->fish('worms', 'evening')->thenReturn('trout');
</code></pre>
<p>PHPUnit</p>
<pre><code class="php">
$lakeMock->method('fish')
->will($this->returnValueMap([
['red maggots', 'morning', 'roach'],
['grasshoppers', 'late afternoon', 'bream'],
['worms', 'evening', 'trout']
]));
</code></pre>
<aside class="notes">
<p>PHPUnit will overwrite previous stub even if params are different</p>
<p>Internally Phake is using a stack.</p>
</aside>
</section>
<section>
<h3>Stubbing Consecutive Calls</h3>
<p>Phake</p>
<pre><code class="php">
Phake::when($lakeMock)->fish('no bait', 'afternoon')
->thenReturn('pike')
->thenReturn('boot')
->thenReturn('tench');
</code></pre>
<p>PHPUnit</p>
<pre><code class="php">
$lakeMock->method('fish')
->with('no bait', 'afternoon')
->will($this->onConsecutiveCalls('pike', 'boot', 'tench'));
</code></pre>
<aside class="notes">
<p>Phake: chain together thenReturns</p>
<p>PHPUnit will return null after 3rd time.</p>
</aside>
</section>
<section>
<h3>Default Stubs</h3>
<p>Phake</p>
<pre><code class="php">
$lakeMock = Phake::mock(Lake::class, Phake::ifUnstubbed()->thenReturn(42));
</code></pre>
<p>PHPUnit</p>
<pre><code class="php">
</code></pre>
<aside class="notes">
<p>PHPUnit no implementation.</p>
</aside>
</section>
<section>
<h2>Method Verification</h2>
<aside class="notes">
<p>What is method verification?</p>
</aside>
</section>
<section>
<pre><code class="php">
class Lake
{
public function fish(string $bait, string $timeOfDay = 'morning')
{
...
}
public function chumTheWaters()
{
...
}
}
</code></pre>
</section>
<section>
<h3>Basic Verification</h3>
<p>Phake</p>
<pre><code class="php">
$lakeMock = Phake::mock(Lake::class);
$lakeMock->chumTheWaters();
Phake::verify($lakeMock)->chumTheWaters();
</code></pre>
<p>PHPUnit</p>
<pre><code class="php">
$lakeMock = $this->getMockBuilder(Lake::class)->getMock();
$lakeMock->expects($this->once())
->method('chumTheWaters');
$lakeMock->chumTheWaters();
</code></pre>
<aside class="notes">
<p>Keeps a record of all interactions with mock from creation.</p>
<p>Stubbing and verification MUST be declared together.</p>
</aside>
</section>
<section>
<h3>Verification</h3>
<p>Phake</p>
<pre><code class="php">
$lakeMock = Phake::mock(Lake::class);
$lakeMock->fish('ragworms', 'morning');
$lakeMock->fish('ragworms', 'morning');
Phake::verify($lakeMock, Phake::times(2))
->fish('ragworms', $this->isType('string'));
</code></pre>
<p>PHPUnit</p>
<pre><code class="php">
$lakeMock = $this->getMockBuilder(Lake::class)->getMock();
$lakeMock->expects($this->exactly(2))
->method('fish')
->with('ragworms', $this->isType('string'));
$lakeMock->fish('ragworms', 'morning');
$lakeMock->fish('ragworms', 'morning');
</code></pre>
<aside class="notes">
</aside>
</section>
<section>
<h3>Verifying Multiple Invocations</h3>
<p>Phake</p>
<pre><code class="php">
$lakeMock->fish('sweetcorn', 'evening');
$lakeMock->fish('spinner', 'morning');
Phake::verify($lakeMock)->fish('sweetcorn', 'evening');
Phake::verify($lakeMock)->fish('spinner', 'morning');
</code></pre>
<p>PHPUnit</p>
<pre><code class="php">
$lakeMock->expects($this->at(0))->method('fish')->with('sweetcorn', 'evening');
$lakeMock->expects($this->at(1))->method('fish')->with('spinner', 'morning');
$lakeMock->fish('sweetcorn', 'evening');
$lakeMock->fish('spinner', 'morning');
</code></pre>
<aside class="notes">
</aside>
</section>
<section>
<h3>Verification In Order</h3>
<p>Phake</p>
<pre><code class="php">
$lakeMock->fish('sweetcorn', 'evening');
$lakeMock->fish('spinner', 'morning');
Phake::inOrder(
Phake::verify($lakeMock)->fish('sweetcorn', 'evening');
Phake::verify($lakeMock)->fish('spinner', 'morning');
);
</code></pre>
</section>
<section>
<h3>Verify No Other Interaction</h3>
<p>Phake</p>
<pre><code class="php">
$lakeMock->fish('sweetcorn', 'evening');
$lakeMock->fish('spinner', 'morning');
Phake::verify($lakeMock)->fish('sweetcorn', 'evening');
Phake::verify($lakeMock)->fish('spinner', 'morning');
Phake::verifyNoOtherInteractions($lakeMock);
</code></pre>
</section>
<section>
<h3>More About Phake...</h3>
<br />
<h4>The Docs</h4>
<a>phake.readthedocs.org</a>
<br />
<br />
<br />
<h4>The Source Code</h4>
<a>github.com/mlively/Phake</a>
</section>
<section>
<h3>Getting Phake</h3>
<p>Add this to you <code>composer.json</code></p>
<pre><code class="php">
{
"require-dev": {
"phake/phake": "@stable"
}
}
</code></pre>
<p>Or run this in your project's directory with composer installed</p>
<pre><code>composer require phake/phake:@stable --dev</code></pre>
<aside class="notes">
<p>getcomposer.org</p>
</aside>
</section>
<section data-background="shia_applaud.gif">
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<h2>Thank You for listening</h2>
</section>
</div>
</div>
<script src="js/reveal.js"></script>
<script src="lib/js/head.min.js"></script>
<script>
Reveal.initialize({
dependencies: [
{
src: 'plugin/highlight/highlight.js', async: true, callback: function () {
hljs.initHighlightingOnLoad();
}
},
{
src: 'plugin/notes/notes.js', async: true
}
]
});
Reveal.configure({slideNumber: true});
Reveal.configure({slideNumber: 'c/t'});
</script>
</body>
</html>