-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
532 lines (446 loc) · 37 KB
/
atom.xml
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Lars Tesmer's Blog]]></title>
<link href="http://www.lars-tesmer.com/atom.xml" rel="self"/>
<link href="http://www.lars-tesmer.com/"/>
<updated>2014-07-28T13:32:26+02:00</updated>
<id>http://www.lars-tesmer.com/</id>
<author>
<name><![CDATA[Lars Tesmer]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[What My Co-Workers and I Learned When Trying to Write Unit Tests for PHPUnit]]></title>
<link href="http://www.lars-tesmer.com/blog/2011/09/08/what-my-co---workers-and-i-learned-when-trying-to-write-unit-tests-for-phpunit/"/>
<updated>2011-09-08T23:06:00+02:00</updated>
<id>http://www.lars-tesmer.com/blog/2011/09/08/what-my-co—workers-and-i-learned-when-trying-to-write-unit-tests-for-phpunit</id>
<content type="html"><![CDATA[<p>Today a bunch of co-workers and me got together right after work to hone our skills, more specifically, our unit testing skills.<br/>
A couple of weeks before, I had discovered that the code coverage of <a href="https://github.com/sebastianbergmann/phpunit/">PHPUnit</a> is only at ~55%, so I thought it would be a great exercise for our after work hacking to help increase its code coverage by writing unit tests for it.</p>
<p>The plan was to try and write as many tests as we could for the <a href="https://github.com/sebastianbergmann/phpunit/tree/master/PHPUnit/Framework/Constraint">Constraint classes</a> PHPUnit uses to implement its assertions.<br/>
Those Constraint classes are pretty small, fairly easy to understand and not entirely covered by tests – in short, very well suited for our group, a mix of programmers having quite some experience in unit testing as well as others just having started to learn unit testing.</p>
<p>Well, our plan didn’t work out that way, we didn’t really succeed in writing a considerable amount of unit tests.<br/>
However, it still was a valuable experience, as it turned out the unit tests of the Constraints are a good example of how not to unit test.</p>
<p>So here’s what we learned (or were reminded of):</p>
<!-- more -->
<h3>1. Don’t use one single test case class to test several different classes</h3>
<p>The unit tests for the constraint classes of PHPUnit are lumped together <a href="https://github.com/sebastianbergmann/phpunit/blob/3.5/Tests/Framework/ConstraintTest.php">in one large file</a>. I think the reasoning behind that might have been that all constraints derive from the same parent class, <em>PHPUnit_Framework_Constraint</em>.<br/>
Don’t do that.<br/>
It renders it quite hard to find your way to the tests for one of the constraints, it’s quite annoying to navigate through that class.<br/>
If each class has its own test case, it’s much easier to find its test, too.</p>
<h3>2. Name your tests well</h3>
<p>You should name your tests such that they convey the intent of the test, describe what the test is about and how they’re different from other tests. <br/>
The tests in <a href="https://github.com/sebastianbergmann/phpunit/blob/3.5/Tests/Framework/ConstraintTest.php">ConstraintTest.php</a> wear names like:</p>
<ul>
<li>testConstraintIsEmpty</li>
<li>testConstraintPCREMatch</li>
<li>testConstraintClassNotHasStaticAttribute</li>
<li>testConstraintStringMatches</li>
<li>testConstraintStringMatches2</li>
<li>testConstraintStringMatches3</li>
<li>testConstraintStringMatches4</li>
<li>testConstraintStringMatches5</li>
<li>testConstraintStringMatches6 (sic!)</li>
</ul>
<p>It’s really hard to understand what’s going on in tests named in such a way.</p>
<h3>3. Avoid to test more than one behaviour in one single test</h3>
<p>The tests in ConstraintTest.php test a lot of behavior in one single test, here’s an example:</p>
<figure class='code'><figcaption><span>Whoa! A lot of stuff going on in here! </span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="sd">/**</span>
</span><span class='line'><span class="sd"> * @covers PHPUnit_Framework_Constraint_IsType</span>
</span><span class='line'><span class="sd"> * @covers PHPUnit_Framework_Assert::isType</span>
</span><span class='line'><span class="sd"> * @covers PHPUnit_Framework_Constraint::count</span>
</span><span class='line'><span class="sd"> * @covers PHPUnit_Framework_TestFailure::exceptionToString</span>
</span><span class='line'><span class="sd"> */</span>
</span><span class='line'><span class="k">public</span> <span class="k">function</span> <span class="nf">testConstraintIsType</span><span class="p">()</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="nv">$constraint</span> <span class="o">=</span> <span class="nx">PHPUnit_Framework_Assert</span><span class="o">::</span><span class="na">isType</span><span class="p">(</span><span class="s1">'string'</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="nv">$this</span><span class="o">-></span><span class="na">assertFalse</span><span class="p">(</span><span class="nv">$constraint</span><span class="o">-></span><span class="na">evaluate</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="s1">''</span><span class="p">,</span> <span class="k">TRUE</span><span class="p">));</span>
</span><span class='line'> <span class="nv">$this</span><span class="o">-></span><span class="na">assertTrue</span><span class="p">(</span><span class="nv">$constraint</span><span class="o">-></span><span class="na">evaluate</span><span class="p">(</span><span class="s1">''</span><span class="p">,</span> <span class="s1">''</span><span class="p">,</span> <span class="k">TRUE</span><span class="p">));</span>
</span><span class='line'> <span class="nv">$this</span><span class="o">-></span><span class="na">assertEquals</span><span class="p">(</span><span class="s1">'is of type "string"'</span><span class="p">,</span> <span class="nv">$constraint</span><span class="o">-></span><span class="na">toString</span><span class="p">());</span>
</span><span class='line'> <span class="nv">$this</span><span class="o">-></span><span class="na">assertEquals</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">count</span><span class="p">(</span><span class="nv">$constraint</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'> <span class="k">try</span> <span class="p">{</span>
</span><span class='line'> <span class="nv">$constraint</span><span class="o">-></span><span class="na">evaluate</span><span class="p">(</span><span class="k">new</span> <span class="k">stdClass</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">catch</span> <span class="p">(</span><span class="nx">PHPUnit_Framework_ExpectationFailedException</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nv">$this</span><span class="o">-></span><span class="na">assertEquals</span><span class="p">(</span><span class="s"><<<EOF</span>
</span><span class='line'><span class="s">Failed asserting that stdClass Object () is of type "string".</span>
</span><span class='line'>
</span><span class='line'><span class="s">EOF</span>
</span><span class='line'> <span class="p">,</span>
</span><span class='line'> <span class="nx">self</span><span class="o">::</span><span class="na">trimnl</span><span class="p">(</span><span class="nx">PHPUnit_Framework_TestFailure</span><span class="o">::</span><span class="na">exceptionToString</span><span class="p">(</span><span class="nv">$e</span><span class="p">))</span>
</span><span class='line'> <span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="nv">$this</span><span class="o">-></span><span class="na">fail</span><span class="p">();</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>Especially in conjunction with #2, this makes it really hard to figure out what a given test is supposed to test. In our practice group we sat in pairs in front of our laptops and yet it still took us quite some time to find out what those tests were actually doing.</p>
<p>If you find yourself writing more than one assertion in a single test, take your hands from the keyboard and find out whether you’re about to test more than a single behavior.<br/>
Otherwise you will probably curse yourself when you come back to that test code in, say, 6 months…</p>
<h3>Conclusion</h3>
<p>If you plan to introduce others to unit testing by letting them loose on a real world codebase, I’d suggest not to pick PHPUnit, as ironic as it may sound.<br/>
PHPUnit itself is a very valuable tool and I’m very grateful for its existence, I’m using it daily at work. Yet, its test are no good example of how to write good unit tests, working on those tests will cause quite some confusion especially among programmers new to unit testing.</p>
<p>Anyways, for our next practice group meeting we’ll choose something more appropriate, we’re thinking about trying our luck on <a href="https://github.com/kriswallsmith/assetic">Symfony’s Assetic</a>.</p>
<p>However, we will very probably come back to PHPUnit at some point in the future in order to try and refactor its tests.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Review: The Clean Coder - a Code of Conduct for Professional Programmers]]></title>
<link href="http://www.lars-tesmer.com/blog/2011/09/04/review-the-clean-coder-a-code-of-conduct-for-professional-programmers/"/>
<updated>2011-09-04T14:08:00+02:00</updated>
<id>http://www.lars-tesmer.com/blog/2011/09/04/review-the-clean-coder-a-code-of-conduct-for-professional-programmers</id>
<content type="html"><![CDATA[<p><img class="left" src="http://lars-tesmer.com/images/books/the_clean_coder.jpg">
Robert C. Martin aka Uncle Bob, a programmer with decades of experience under his belt, lets us in about what he has learned in his professional life and, more importantly, how he has failed, sometimes failed miserably.
<a href="http://www.amazon.com/Clean-Coder-Conduct-Professional-Programmers/dp/0137081073">The Clean Coder</a> is an important book.</p>
<p>In short, the author helps you to prevent you from making the same mistakes that he committed, giving orientation in one’s quest to become a true professional, a software master.</p>
<p>The book covers several topics, e.g. how to say no (and yes!), how to write clean code, how to test, how to become more effective and efficient.</p>
<p>And while all those topics are different in content, there’s one big <em>leitmotif</em>:</p>
<!-- more -->
<p>Self Responsibility.</p>
<p>Especially when talking about his failures, this <em>leitmotif</em> becomes evident:<br/>
Neither is Robert C. Martin blaming other people, nor the environment, nor bad luck, nor lack of time – no, the essence of his stories is that he himself failed, failed to be responsible, failed to act professionally.</p>
<p>What a tough realization!</p>
<p>I mean, it’s a little bit depressing to see it this way, think about it:<br/>
Your project is a mess, you’re under constant pressure, haven’t seen your family for a long time, you hate your co-workers – why, it’s <strong>your</strong> fault, according to the author.</p>
<p>At the same time, this is also an optimistic way to view things:<br/>
If it’s <em>your</em> fault, <em>your</em> responsibility to having got into such a situation, then it’s also <strong>you</strong> who can change it! You don’t depend on others, you only depend on yourself.</p>
<p>Now, I’m not sure if I entirely follow this conclusion, it’s a thin line to walk between a successful career and depression/burnout. You have to be very self-observant and tread carefully when adapting this way of thinking.</p>
<p>However, I still think it’s a very useful and effective way of thinking, it renders it harder to simply get used to bad situations:<br/>
The code you work on sucks? – Well, then start to write better code!</p>
<p>Your company doesn’t allow for much time to practice, to hone your skills? – Well, go ahead and change it! Find some like-minded co-workers, practice with them after work, go to your bosses and pester them to allocate at least a few hours per month for practice!</p>
<p>Summing up, <a href="http://www.amazon.com/Clean-Coder-Conduct-Professional-Programmers/dp/0137081073">The Clean Coder</a> is an opinionated, yet important book, which you should read (with a critical mind) in order to become a better, a more professional programmer.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Review: Eloquent Ruby by Russ Olsen]]></title>
<link href="http://www.lars-tesmer.com/blog/2011/09/04/review-eloquent-ruby-by-russ-olsen/"/>
<updated>2011-09-04T13:17:00+02:00</updated>
<id>http://www.lars-tesmer.com/blog/2011/09/04/review-eloquent-ruby-by-russ-olsen</id>
<content type="html"><![CDATA[<p><img class="left" src="http://lars-tesmer.com/images/books/eloquent_ruby.jpg">
If you want to learn Ruby and you already know another programming language, <a href="http://www.amazon.com/Eloquent-Ruby-Addison-Wesley-Professional/dp/0321584104">Eloquent Ruby by Russ Olsen</a> is a book you absolutely should read. Heck, even if you do <em>not</em> want to learn Ruby, you might want to read it because, who knows, it might give you <a href="http://lars-tesmer.com/blog/2011/08/29/phpunit-better-syntax-for-expecting-exceptions/">ideas for the programming language you currently use</a>.</p>
<p>So, why do I think this way?</p>
<!-- more -->
<p>For starters, this book is really well written, it never gets boring, it isn’t a dry explanation of yet another programming language – it’s simply an enjoyable read.</p>
<p>More importantly, it doesn’t suffer from the mistake of oh so many other books:<br/>
As an experienced programmer it’s really painful having to suffer through chapter after chapter explaining, for example, what an if-statement is, or that there are such things as, gasp!, loops.</p>
<p>In <a href="http://www.amazon.com/Eloquent-Ruby-Addison-Wesley-Professional/dp/0321584104">Eloquent Ruby</a> there’s none of that. Or when there is, it’s written in a way that’s not treating you like a programmer on his first day.<br/>
<em>Eloquent Ruby</em> does not only teach you the syntax but teaches you <em>idiomatic</em> Ruby, so you’ll be able to write Ruby that actually looks like Ruby.</p>
<p>The book consists of four large sections. The first section introduces you to the basics of Ruby, the second deals about the possibilities offered by classes, modules and blocks in Ruby.<br/>
The third section is where it gets <em>really</em> meaty, you’re introduced to the power (and dangers) of metaprogramming. The final section is about pulling everything you’ve learned so far together, e.g. by showing how to build both internal and external DSLs with Ruby.</p>
<p>I really like the way each chapter is structured:<br/>
Every chapter first teaches you to the basic concepts of the topic at hand, giving lots of code examples.<br/>
More importantly, each chapters closes with the same three subsections:</p>
<ul>
<li>In the Wild</li>
<li>Staying Out of Trouble and</li>
<li>Wrapping up</li>
</ul>
<p><em>In the Wild</em> features real world examples, using the concepts you’ve been familiarized with in the previous subsections of the chapter code taken from actually existing software.<br/>
<em>Staying Out of Trouble</em> helps you to avoid any pitfalls you could fall into when applying the stuff you’ve just learned. I’ve found those sections to be especially useful.<br/>
<em>Wrapping up</em> does what its name suggests: Summing up what’s been covered in the current chapter in a mere paragraph, allowing you to review what you’ve just learned.</p>
<p>In conclusion, if you’re an aspiring Ruby programmer, do yourself a favour and <a href="http://www.amazon.com/Eloquent-Ruby-Addison-Wesley-Professional/dp/0321584104">get this most excellent book</a>.
And while you’re at it, check out the equally excellent <a href="http://www.amazon.com/Ruby-Best-Practices-Gregory-Brown/dp/0596523009/">Ruby Best Practices</a> by Gregory T. Brown, too! :D</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[PHPUnit: Better Syntax for Expecting Exceptions]]></title>
<link href="http://www.lars-tesmer.com/blog/2011/08/29/phpunit-better-syntax-for-expecting-exceptions/"/>
<updated>2011-08-29T18:32:00+02:00</updated>
<id>http://www.lars-tesmer.com/blog/2011/08/29/phpunit-better-syntax-for-expecting-exceptions</id>
<content type="html"><![CDATA[<p>Recently, while starting to learn Ruby, I’ve stumbled upon the following code snippet
which demonstrates how you can assert that a piece of code throws as exception in Ruby’s Test::Unit:</p>
<figure class='code'><figcaption><span>Expecting exceptions in Test::Unit </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">test_some_important_method</span><span class="p">()</span>
</span><span class='line'> <span class="n">some_obj</span> <span class="o">=</span> <span class="kp">new</span> <span class="no">SomeObj</span>
</span><span class='line'> <span class="c1"># Potentially large amount of code</span>
</span><span class='line'>
</span><span class='line'> <span class="n">assert_raise</span> <span class="no">InvalidArgumentException</span> <span class="k">do</span>
</span><span class='line'> <span class="n">some_obj</span><span class="o">.</span><span class="n">someMethod</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>Check out lines 5-7 – isn’t this a really elegant and readable way of expecting an exception?
Now let’s contrast this with how it’s done in PHPUnit:</p>
<!--more-->
<figure class='code'><figcaption><span>Current way of expecting exceptions in PHPUnit </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="o"><?</span><span class="nx">php</span>
</span><span class='line'><span class="sd">/**</span>
</span><span class='line'><span class="sd"> * @expectedException InvalidArgumentException</span>
</span><span class='line'><span class="sd"> */</span>
</span><span class='line'><span class="k">public</span> <span class="k">function</span> <span class="nf">testSomeImportantMethod</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="nv">$some_obj</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">SomeObj</span><span class="p">();</span>
</span><span class='line'> <span class="c1">// Potentially large amount of code</span>
</span><span class='line'>
</span><span class='line'> <span class="nv">$some_obj</span><span class="o">-></span><span class="na">someMethod</span><span class="p">();</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>Well, that isn’t too horrible but I’ve never been really happy with this.<br/>
My main issues with this way of expecting exceptions are:</p>
<p>The expectation is pretty far away from the location you’d normally expect to find an assertion.
Usually, an assertion can be found at the bottom of each test function, whereas with the current method PHPUnit uses,
it’s at the <em>top</em> of the test-function.</p>
<p>Additionally, it’s an annotation “buried” in a comment which is easy to miss.</p>
<p>Finally, PHPUnit will watch for an exception thrown by <em>any</em> of the code inside the test-function. Normally,
it’s the last line of the test-function, so it isn’t hard to find – but what if the expected exception is thrown in
a line <em>before</em> the last line, maybe due to a bug?</p>
<p>So, is there a way to mimic the method of Test::Unit in PHPUnit?<br/>
As you may have guessed, there is one, using anonymous functions and closures, which are available since PHP 5.3!</p>
<p>Here’s the code for a simple function we can use to check if a given piece of code throws an exception:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="o"><?</span><span class="nx">php</span>
</span><span class='line'><span class="k">function</span> <span class="nf">assertThrowsException</span><span class="p">(</span><span class="nv">$exception_name</span><span class="p">,</span> <span class="nv">$code</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nv">$e</span> <span class="o">=</span> <span class="k">null</span><span class="p">;</span>
</span><span class='line'> <span class="k">try</span><span class="p">{</span>
</span><span class='line'> <span class="nv">$code</span><span class="p">();</span>
</span><span class='line'> <span class="p">}</span><span class="k">catch</span> <span class="p">(</span><span class="nx">Exception</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="c1">// No more code, we only want to catch the exception in $e</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span><span class="p">(</span><span class="nv">$e</span> <span class="o">&&</span> <span class="nv">$e</span> <span class="nx">instanceof</span> <span class="nv">$exception_name</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">echo</span> <span class="s2">"</span><span class="se">\n</span><span class="s2">Correct exception thrown!</span><span class="se">\n</span><span class="s2">"</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span><span class="k">else</span><span class="p">{</span>
</span><span class='line'> <span class="k">echo</span> <span class="s2">"</span><span class="se">\n</span><span class="s2">Incorrect exception thrown!</span><span class="se">\n</span><span class="s2">"</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>Confused?<br/>
Well, the most important line of that function is line 5. <br/>
There, the anonymous function that was passed into the function as the second argument gets executed, which allows
us to catch any exception thrown by it.</p>
<p>Still confused? Let’s check out an example:</p>
<figure class='code'><figcaption><span>Let’s throw an exception, yay! </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="o"><?</span><span class="nx">php</span>
</span><span class='line'><span class="k">class</span> <span class="nc">ExceptionThrower</span> <span class="p">{</span>
</span><span class='line'> <span class="k">public</span> <span class="k">function</span> <span class="nf">execute</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">throw</span> <span class="k">new</span> <span class="nx">\InvalidArgumentException</span><span class="p">(</span><span class="s2">"I'm an exception"</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$subject</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ExceptionThrower</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'><span class="nx">assertThrowsException</span><span class="p">(</span><span class="s1">'InvalidArgumentException'</span><span class="p">,</span> <span class="k">function</span> <span class="p">()</span> <span class="k">use</span> <span class="p">(</span><span class="nv">$subject</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nv">$subject</span><span class="o">-></span><span class="na">execute</span><span class="p">();</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>
<p>The magic happens in lines 10-13:<br/>
The <em>execute</em>-method of ExceptionThrower doesn’t get executed immediately because it’s wrapped in an anonymous function, i.e.
<em>execute</em> will only be invoked when we call the anonymous function contained in <em>$code</em> in line 5 of <em>assertThrowsException()</em>.</p>
<p>Now one question remains – can this be done in PHPUnit with a “proper” assertion?<br/>
Yes, it can!</p>
<p>I won’t post the full code into this blog, it would be a bit too much but you can find the code adding a new assertion
<em>assertThrowsException()</em> in the following commit to my fork of PHPUnit 3.5:<br/>
<a href="https://github.com/kiltec/phpunit/commit/dd5c7bd71d6eb8d4b58ce79b5ae069fbb0734354">Link to Github commit</a><br/>
Note that this code is not production-ready, it’s a proof-of-concept with just enough code to get PHPUnit to run the
new assertion without throwing a fit, just so I could find out whether such a new assertion would be possible at all.</p>
<p>Anyway, with the new assertion the PHPUnit test-method from the beginning of this blog would look like this:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="o"><?</span><span class="nx">php</span>
</span><span class='line'><span class="k">public</span> <span class="k">function</span> <span class="nf">testSomeImportantMethod</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="nv">$someClass</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">SomeClass</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'> <span class="nv">$this</span><span class="o">-></span><span class="na">assertThrowsException</span><span class="p">(</span><span class="s1">'InvalidArgumentException'</span><span class="p">,</span> <span class="k">function</span> <span class="p">()</span> <span class="k">use</span><span class="p">(</span><span class="nv">$someClass</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nv">$someClass</span><span class="o">-></span><span class="na">someMethod</span><span class="p">();</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>Granted, you have more to type than in the current way but in my opinion, this offers more control and is more expressive and I’d prefer
expressiveness over terseness any day.</p>
<p>What do you think, should I turn this into a real patch for PHPUnit?<br/>
Or am I just crazy? ;)</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Colored Diffs in Thunderbird]]></title>
<link href="http://www.lars-tesmer.com/blog/2010/10/01/colored-diffs-in-thunderbird/"/>
<updated>2010-10-01T00:00:00+02:00</updated>
<id>http://www.lars-tesmer.com/blog/2010/10/01/colored-diffs-in-thunderbird</id>
<content type="html"><![CDATA[<p>
At work we do code review by getting emails with the diff of every commit to our SVN repository. One day I wondered whether there was an addon for Thunderbird to get colored diffs - and sure enough, there was:
</p>
<!--more-->
<p>
<a href="http://code.google.com/p/colorediffs/">colorediffs by Vadim Atlygin</a>
</p>
<p>
It features various display modes, e.g. simple coloring of unified diffs:
</p>
<img src="http://www.lars-tesmer.com/images/unified.png" /><br />
<p>
Or side-by-side diffs:
</p>
<img src="http://www.lars-tesmer.com/images/side-by-side.png" /> <br />
<p>
As you can see it will also display whitespace characters, if you want to.<br />
Moreover, you can set the tab width to your preferred value.<br />
You can find more screenshots <a href="http://code.google.com/p/colorediffs/wiki/Screenshots">in the wiki of colorediffs.</a>
</p>
<p>
Yet again, another useful Thunderbird-addon making my life a little bit easier.
</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[No Time to Read That Blog - Read It Later!]]></title>
<link href="http://www.lars-tesmer.com/blog/2010/09/29/no-time-to-read-that-blog---read-it-later/"/>
<updated>2010-09-29T00:00:00+02:00</updated>
<id>http://www.lars-tesmer.com/blog/2010/09/29/no-time-to-read-that-blog—read-it-later</id>
<content type="html"><![CDATA[<p>
In order to keep up-to-date with what’s going on in the tech world, I read a lot of blogs, visit <a href="http://news.ycombinator.com/">Hacker News</a> regularly etc. during my lunch break for example.
</p>
<p>
Frequently, I will stumble upon seemingly interesting articles but either they’re too long to be read during the break or there’s no time left to read them.<br />
What I used to do until a year ago was to move those articles into a bookmark folder called ‘toread’, in order to remember to read it when I would have more time. But it was always a bit of a hassle, so I was never really satisfied with that solution.<br />
</p>
<p>
Then I discovered <a href="http://readitlaterlist.com/">Read It Later</a>.</p>
<p>
<!--more-->
It’s a Firefox Addon that with the click on a button or hitting a keyboard shortcut will save the page you’re currently viewing into a list for later access.<br />
When you have time, you can check out said list and with one click or shortcut, you remove the page from the list and <a href="http://readitlaterlist.com/">Read It Later</a> will get you to the next page you’ve saved in chronological order.<br />
Not only that, it also integrates with Google Reader, allowing you to directly save articles without having to leave it.<br />
Additionally, if you use several computers, you can optionally save the list remotely, so you can access the same list from different computers.<br />
And there are <a href="http://readitlaterlist.com/firefox/">many more features</a>, for example an API you can use to code your own app.
</p>
<p>
The way I use it is that I’ll put articles into it until I hit the threshold of 100 saved items and when I have time, e.g. during the weekend, I’ll go through those items and read them - or rather, not read them.<br />
That’s the beauty about this, often it will turn out that the article or blog I’m getting to isn’t that interesting after all and I can quickly move to the next article in the list.<br />
And that decision is really based on my thinking it isn’t interesting and not by feeling under pressure due to lack of time.
</p>
<p>
This is absolutely one of those Firefox addons I don’t want to live without anymore.
</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Git - How to Get Better Diffs for Images]]></title>
<link href="http://www.lars-tesmer.com/blog/2010/09/20/git---how-to-get-better-diffs-for-images/"/>
<updated>2010-09-20T00:00:00+02:00</updated>
<id>http://www.lars-tesmer.com/blog/2010/09/20/git—how-to-get-better-diffs-for-images</id>
<content type="html"><![CDATA[Have you ever wondered if there's something that would make Git to show better diffs for images?<br />
Well, here’s a really useful addition to one’s Git config that will tell Git to use <i>exiftool</i>
to create diffs of changes made to images:
<!--more-->
<pre>
cd /path_to_project
echo '*.png diff=exif' >> .gitattributes
echo '*.jpg diff=exif' >> .gitattributes
echo '*.gif diff=exif' >> .gitattributes
git config diff.exif.textconv exiftool
</pre>
From now on, diffs for images will look something like this:<br />
<pre>
--- a/img.png
+++ b/img.png
@@ -1,7 +1,7 @@
ExifTool Version Number : 7.89
-File Size : 8.9 kB
+File Size : 8.3 kB
File Modification Date/Time : 2010:09:20 16:55:41+02:00
File Type : PNG
MIME Type : image/png
-Image Width : 58
-Image Height : 80
+Image Width : 60
+Image Height : 87
</pre>
Nothing spectacular but much more useful than the default.<br />
Originally, I found this in some slides I read many moons ago.
I tried to find those slides again, unsuccessfully.<br />
Luckily, I’ve found another source explaining how to do it:<br />
<a href="http://progit.org/book/ch7-2.html">Chapter 7 of “Pro Git”</a><br />
Thanks, Scott Chacon!<br />
]]></content>
</entry>
</feed>