<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[@TheKeyboard]]></title>
  <link href="http://www.littlehart.net/atthekeyboard/atom.xml" rel="self"/>
  <link href="http://www.littlehart.net/atthekeyboard/"/>
  <updated>2012-05-16T23:10:51-04:00</updated>
  <id>http://www.littlehart.net/atthekeyboard/</id>
  <author>
    <name><![CDATA[Chris Hartjes]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Test All The Things?]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/05/16/test-all-the-things/"/>
    <updated>2012-05-16T00:00:00-04:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/05/16/test-all-the-things</id>
    <content type="html"><![CDATA[<p>I was asked on Twitter today for my thoughts on the topic of whether or not
there are things you should NOT test. Of course, I think that you should
<a href="http://www.littlehart.net/test-all-the-things.jpg">test all the things</a>
but let&#8217;s be honest in what we&#8217;re trying to do.</p>

<p>Let&#8217;s start out with the premise that every single bit of code in your
application. By this I mean we have 100% code coverage for your application.
This is difficult but achievable with lots of tests and proper use of
mock objects.</p>

<p>Even with 100% code coverage, it still might not be enough. So much
depends on your actual test cases. Have you anticipated edge cases that
could cause your code grief? This is the one area of web applications that
I think leads to the greatest amount of problems: determining edge cases
when your code accepts user-generated content. So even if 100% code coverage
can&#8217;t save you from every problem, what is a good compromise?</p>

<p>If you obsessively run PHPUnit code coverage reports like I do, you will be familiar
with the <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=210575">CRAP index</a> values
for your code. Go read the article about it. I&#8217;ll wait.</p>

<p>I think that if you want to do effective testing, you can probably limit
your code coverage efforts to class methods or functions that have a CRAP index
of 5 of more. It&#8217;s a low enough value that you will end up testing a very large
amount of your application&#8217;s code base and will be testing non-trivial code.</p>

<p>You don&#8217;t really need to test pass-through methods or functions (functions that are one-liners
that return the response from another methods or functions) or getter / setter
functions (if you&#8217;re into that sort of thing). Chances are they will get
coverage in tests that call those methods.</p>

<p>In essence, don&#8217;t waste a lot of time writing tests that test the trivial
or totally expected. You will have a much greater impact on your application
by writing tests for the complicated parts of your application.</p>

<p>Finally, I encourage you to pursue another very impactful testing practice:
whenever a bug is reported in your application, write a test to verify
the bug. Then fix your application until the bug goes away. By doing things
that way, you end up with a collection of tests that reflect the various
ways that the users of your application actually, you know, use your
application.</p>

<p>Good code coverage results in a feeling that only weird edge cases are going
to bring your application crashing down. That frees you up to keep on
building out the application instead of worrying if a minor change is going
to wreak everything.</p>

<p>So the ultimate goal is TEST ALL THE THINGS THAT MATTER. Aim for that, and
the rest will all fall into place.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How The Testing Sausage Gets Made]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/05/16/how-the-testing-sausage-gets-made/"/>
    <updated>2012-05-16T00:00:00-04:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/05/16/how-the-testing-sausage-gets-made</id>
    <content type="html"><![CDATA[<p>So how does the Grumpy Programmer write code, TDD style? It starts with
using a set of tools that reduce the amount of friction required.</p>

<p>First, I do almost all my work from a terminal. On my Macbook I use
<a href="http://www.iterm2.com/#/section/home">iTerm 2</a> instead of the stock
Terminal.app. Until recently you could not get 256 colours without
some external hacking and since I like my editor to have purty colours
I needed a terminal to match it. I might take a look at Terminal.app
again, but iTerm 2 fits the bill.</p>

<p>Why the terminal? PHPUnit is a CLI application. Yes, many popular
editors and IDE&#8217;s can do things like execute PHPUnit for you, but they
are limited in what flexibility they provide. At Kaplan we have several
different configurations under which we can run our tests. Again, reduce
the friction between the tools and you using them.</p>

<p>Then we have the <a href="http://www.vim.org">One True Editor</a>. While I do use
other editors from time to time (I do like <a href="http://www.sublimetext.com/2">Sublime Text 2</a>
a lot because it has Vim bindings), I stick with Vim. Why? The plugins.</p>

<p>Here&#8217;s what I use:</p>

<ul>
<li><a href="https://github.com/tpope/vim-pathogen">Pathogen</a> to make loading other plugins easy</li>
<li><a href="http://www.vim.org/scripts/script.php?script_id=102">DiffDir</a> for making code review and test reviews easier</li>
<li><a href="http://www.vim.org/scripts/script.php?script_id=2423">Gist.vim</a></li>
<li><a href="http://www.vim.org/scripts/script.php?script_id=13">closetag</a></li>
<li><a href="https://github.com/AndrewRadev/inline_edit.vim">inline_edit.vim</a></li>
<li><a href="https://github.com/scrooloose/nerdtree">nerdtree</a></li>
<li><a href="https://github.com/ervandew/supertab">supertab</a></li>
<li><a href="https://github.com/vim-scripts/surround.vim">surround.vim</a> - it has ruined me for using other editors</li>
<li><a href="http://vimcasts.org/episodes/aligning-text-with-tabular-vim/">Tabular.vim</a></li>
<li><a href="https://github.com/majutsushi/tagbar">tagbar</a> - INDISPENSABLE</li>
<li><a href="https://github.com/tpope/vim-repeat">vim-repeat</a></li>
<li><a href="https://github.com/thisivan/vim-taglist">vim-taglist</a></li>
<li><a href="https://github.com/tpope/vim-unimpaired">vim-unimpaired</a></li>
</ul>


<p>There is nothing specific to doing TDD in these plugins, but what they
do for me is allow me to slice and dice text as quickly as possible, along
with providing me with easy ways to navigate through the code base. Remember,
reducing friction is a big thing to me. If the tool makes it tough for me
to do what I want, it will be the target of grumpy ranitng.</p>

<p>One of the reasons I like Sublime Text 2 is that you can jump all over the
place in the code base in similar ways. If you editor doesn&#8217;t allow you to
do that, you need to get rid of that editor and use something that does.
NO EXCUSES.</p>

<p>A typical coding session has me using two terminal windows. One has Vim running
in it, the other is used to run PHPUnit or other testing framework of choice as
I venture in the world of BDD and testing in other languages. Then I do the
following:</p>

<ul>
<li>figure out HOW to test the code I&#8217;m writing</li>
<li>write the tests required</li>
<li>write code until the tests pass</li>
<li>refactor code as required</li>
<li>refactor tests as required.</li>
</ul>


<p>That&#8217;s really all their is to it. The actual work flow of when you are using
TDD isn&#8217;t magical. I will admit that experience is the only teacher for how
to actually write the tests, but luckily I have an awesome <a href="https://twitter.com/FreshWillisms">co-worker</a>
who bellows obscenities at me while guiding me down the path to total testing
mastery. I know Will, you only hate me because you love me.</p>

<p>Every time I make a change to a test or the code, I re-run the tests. I find that
this lets me get more done while actually slowing down the pace. You end up
more focussed on the bit of code that really needs your attention, instead of
getting drawn into blind alleys.</p>

<p>I also frequently run code-coverage reports to make sure that I&#8217;m getting
maximum coverage for my efforts, while ignoring writing tests specifically
for methods with very low CRAP indexes. Again, I&#8217;m making sure that I am
targeting the parts of my code that need to be tested.</p>

<p>I know I&#8217;ve written about this before, but when you approach things from a
TDD perspective, you find that using things like dependency injection
become second nature.</p>

<p>If there is a lesson to be learned from this, it&#8217;s to make sure that every
tool that you use reduces the friction that is generated when building
your application using TDD.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Have A 'Strategy']]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/05/15/have-a-strategy/"/>
    <updated>2012-05-15T00:00:00-04:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/05/15/have-a-strategy</id>
    <content type="html"><![CDATA[<p>Yesterday on Twitter I got into a nerdfight with a blogger who posted a
blog entry about <a href="http://odino.org/use-the-strategy-to-avoid-the-switch-case-antipattern/">using the Strategy pattern to avoid switch-case</a>.
At first glance, I was convinced that he was using <a href="http://ca.php.net/manual/en/language.variables.variable.php">variable variables</a>
which I feel is a big <a href="https://en.wikipedia.org/wiki/Code_smell">code smell</a>.</p>

<p>So natually I started arguing. I was rude. He was rude, slapping me in the twitter face with a blog link that seemed
to indicate he thought I had no idea about variables in PHP. To be fair I earned that by not paying attention.
Once I realized what he was doing, I realized that the Strategy pattern was applicable in this case&#8230;but his
chosen example was dumb and one that I wouldn&#8217;t have used to demonstrate things. As expected, he told me to supply
a sample of a better way. I did, telling him that the sample would be better if he didn&#8217;t mash the logging level
together with the message.</p>

<p>Anyway, I&#8217;m not going to do that bit of work here. Instead, I want to show that it is not enough to teach an idea
to someone. You have to show them how to test their idea once it&#8217;s been refactored to make sure nothing has changed.
First, let&#8217;s start with a working example of his logging class, using the dreaded switch-case.</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>
<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>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Sample logger class that uses a switch-case</span>
</span><span class='line'><span class="k">require_once</span> <span class="s1">&#39;LoggingException.php&#39;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">LoggerSwitch</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">public</span> <span class="k">function</span> <span class="nf">logMessage</span><span class="p">(</span><span class="nv">$message</span> <span class="o">=</span> <span class="s2">&quot;CRITICAL::The system encountered a problem&quot;</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="nv">$parts</span> <span class="o">=</span> <span class="nb">explode</span><span class="p">(</span><span class="s1">&#39;::&#39;</span><span class="p">,</span> <span class="nv">$message</span><span class="p">);</span>
</span><span class='line'>      <span class="nv">$level</span> <span class="o">=</span> <span class="nx">strtolower</span><span class="p">(</span><span class="nv">$parts</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span>
</span><span class='line'>      <span class="nv">$logMessage</span> <span class="o">=</span> <span class="nv">$parts</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">switch</span> <span class="p">(</span><span class="nv">$level</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>          <span class="k">case</span> <span class="s1">&#39;notice&#39;</span><span class="o">:</span>
</span><span class='line'>              <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_writeToLog</span><span class="p">(</span><span class="nv">$level</span><span class="p">,</span> <span class="nv">$logMessage</span><span class="p">);</span>
</span><span class='line'>          <span class="k">case</span> <span class="s1">&#39;critical&#39;</span><span class="o">:</span>
</span><span class='line'>              <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_writeToLog</span><span class="p">(</span><span class="nv">$level</span><span class="p">,</span> <span class="nv">$logMessage</span><span class="p">);</span>
</span><span class='line'>              <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_emailOps</span><span class="p">(</span><span class="nv">$message</span><span class="p">);</span>
</span><span class='line'>          <span class="k">case</span> <span class="s1">&#39;catastrophe&#39;</span><span class="o">:</span>
</span><span class='line'>              <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_writeToLog</span><span class="p">(</span><span class="nv">$level</span><span class="p">,</span> <span class="nv">$logMessage</span><span class="p">);</span>
</span><span class='line'>              <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_emailOps</span><span class="p">(</span><span class="nv">$logMessage</span><span class="p">);</span>
</span><span class='line'>              <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_textCeo</span><span class="p">(</span><span class="nv">$message</span><span class="p">);</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">throw</span> <span class="k">new</span> <span class="nx">LoggingException</span><span class="p">(</span><span class="s2">&quot;Unknown logging level </span><span class="si">{</span><span class="nv">$level</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">protected</span> <span class="k">function</span> <span class="nf">_writeToLog</span><span class="p">(</span><span class="nv">$level</span><span class="p">,</span> <span class="nv">$logMessage</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="k">return</span> <span class="s2">&quot;Wrote </span><span class="si">{</span><span class="nv">$logMessage</span><span class="si">}</span><span class="s2"> to </span><span class="si">{</span><span class="nv">$level</span><span class="si">}</span><span class="s2"> file&quot;</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">protected</span> <span class="k">function</span> <span class="nf">_emailOps</span><span class="p">(</span><span class="nv">$logMessage</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="k">return</span> <span class="s2">&quot;Sent email to Ops&quot;</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">protected</span> <span class="k">function</span> <span class="nf">_textCeo</span><span class="p">(</span><span class="nv">$message</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="k">return</span> <span class="s2">&quot;Sent text to CEO&quot;</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>Here&#8217;s the tests for it</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>
<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>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// PHPUnit test for logger-switch.php</span>
</span><span class='line'>
</span><span class='line'><span class="k">require</span> <span class="s1">&#39;../logger-switch.php&#39;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">LoggerSwitchTest</span> <span class="k">extends</span> <span class="nx">PHPUnit_Framework_TestCase</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">public</span> <span class="nv">$logger</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">public</span> <span class="k">function</span> <span class="nf">setUp</span><span class="p">()</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">logger</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">LoggerSwitch</span><span class="p">();</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  
</span><span class='line'>  <span class="sd">/**</span>
</span><span class='line'><span class="sd">  * Data provider for testing our logging object</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">loggerTestingScenarios</span><span class="p">()</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="k">return</span> <span class="k">array</span><span class="p">(</span>
</span><span class='line'>          <span class="k">array</span><span class="p">(</span>
</span><span class='line'>              <span class="s1">&#39;NOTICE::test notice message&#39;</span><span class="p">,</span>
</span><span class='line'>              <span class="s1">&#39;Wrote test notice message to notice file&#39;</span>
</span><span class='line'>          <span class="p">),</span>
</span><span class='line'>          <span class="k">array</span><span class="p">(</span>
</span><span class='line'>              <span class="s1">&#39;CRITICAL::test notice message&#39;</span><span class="p">,</span>
</span><span class='line'>              <span class="s1">&#39;Sent email to Ops&#39;</span>
</span><span class='line'>          <span class="p">),</span>
</span><span class='line'>          <span class="k">array</span><span class="p">(</span>
</span><span class='line'>              <span class="s1">&#39;CATASTROPHE::test notice message&#39;</span><span class="p">,</span>
</span><span class='line'>              <span class="s1">&#39;Sent text to CEO&#39;</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><span class='line'>
</span><span class='line'>  <span class="sd">/**</span>
</span><span class='line'><span class="sd">  * @test</span>
</span><span class='line'><span class="sd">  * @dataProvider loggerTestingScenarios</span>
</span><span class='line'><span class="sd">  * @param $message          string</span>
</span><span class='line'><span class="sd">  * @param $expectedResponse string</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">testReturnsExpectedResponseBasedOnMessage</span><span class="p">(</span>
</span><span class='line'>      <span class="nv">$message</span><span class="p">,</span>
</span><span class='line'>      <span class="nv">$expectedResponse</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="nv">$response</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">logger</span><span class="o">-&gt;</span><span class="na">logMessage</span><span class="p">(</span><span class="nv">$message</span><span class="p">);</span>
</span><span class='line'>      <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">assertEquals</span><span class="p">(</span>
</span><span class='line'>          <span class="nv">$expectedResponse</span><span class="p">,</span>
</span><span class='line'>          <span class="nv">$response</span><span class="p">,</span>
</span><span class='line'>          <span class="s1">&#39;Did not get expected response&#39;</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="sd">/**</span>
</span><span class='line'><span class="sd">  * @test</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">testThrowsExceptionWhenUnknownLevelSubmitted</span><span class="p">()</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">setExpectedException</span><span class="p">(</span><span class="s1">&#39;LoggingException&#39;</span><span class="p">,</span> <span class="s1">&#39;Unknown logging level test&#39;</span><span class="p">);</span>
</span><span class='line'>      <span class="nv">$response</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">logger</span><span class="o">-&gt;</span><span class="na">logMessage</span><span class="p">(</span><span class="s1">&#39;TEST:: test message&#39;</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>These tests give us 100% test coverage, in case you were wondering.</p>

<p>Okay, so the next step is to refactor our incredibly stupid logging class to
use the <a href="https://en.wikipedia.org/wiki/Strategy_pattern">Strategy pattern</a>.</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>
<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>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Logging class that uses the strategy pattern</span>
</span><span class='line'><span class="k">require_once</span> <span class="s1">&#39;LoggingException.php&#39;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">LoggerStrategy</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">public</span> <span class="k">function</span> <span class="nf">logMessage</span><span class="p">(</span><span class="nv">$message</span> <span class="o">=</span> <span class="s2">&quot;CRITICAL::The system encountered a problem&quot;</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="nv">$parts</span> <span class="o">=</span> <span class="nb">explode</span><span class="p">(</span><span class="s1">&#39;::&#39;</span><span class="p">,</span> <span class="nv">$message</span><span class="p">);</span>
</span><span class='line'>      <span class="nv">$level</span> <span class="o">=</span> <span class="nx">strtolower</span><span class="p">(</span><span class="nv">$parts</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span>
</span><span class='line'>      <span class="nv">$method</span> <span class="o">=</span> <span class="nb">sprintf</span><span class="p">(</span><span class="s1">&#39;_log%sMessage&#39;</span><span class="p">,</span> <span class="nx">ucfirst</span><span class="p">(</span><span class="nv">$level</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nb">method_exists</span><span class="p">(</span><span class="nv">$this</span><span class="p">,</span> <span class="nv">$method</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">LoggingException</span><span class="p">(</span><span class="s1">&#39;Unknown logging level &#39;</span> <span class="o">.</span> <span class="nv">$level</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="nv">$this</span><span class="o">-&gt;</span><span class="nv">$method</span><span class="p">(</span><span class="nv">$parts</span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span>
</span><span class='line'>  <span class="p">}</span>    
</span><span class='line'>
</span><span class='line'>  <span class="k">protected</span> <span class="k">function</span> <span class="nf">_logNoticeMessage</span><span class="p">(</span><span class="nv">$message</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_writeToLog</span><span class="p">(</span><span class="s1">&#39;notice&#39;</span><span class="p">,</span> <span class="nv">$message</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">protected</span> <span class="k">function</span> <span class="nf">_logCriticalMessage</span><span class="p">(</span><span class="nv">$message</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_writeToLog</span><span class="p">(</span><span class="s1">&#39;critical&#39;</span><span class="p">,</span> <span class="nv">$message</span><span class="p">);</span>
</span><span class='line'>      <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_emailOps</span><span class="p">(</span><span class="nv">$message</span><span class="p">);</span>   
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">protected</span> <span class="k">function</span> <span class="nf">_logCatastropheMessage</span><span class="p">(</span><span class="nv">$message</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_writeToLog</span><span class="p">(</span><span class="s1">&#39;catastrophe&#39;</span><span class="p">,</span> <span class="nv">$message</span><span class="p">);</span>
</span><span class='line'>      <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_emailOps</span><span class="p">(</span><span class="nv">$message</span><span class="p">);</span>
</span><span class='line'>      <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_textCeo</span><span class="p">(</span><span class="nv">$message</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">protected</span> <span class="k">function</span> <span class="nf">_writeToLog</span><span class="p">(</span><span class="nv">$level</span><span class="p">,</span> <span class="nv">$logMessage</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="k">return</span> <span class="s2">&quot;Wrote </span><span class="si">{</span><span class="nv">$logMessage</span><span class="si">}</span><span class="s2"> to </span><span class="si">{</span><span class="nv">$level</span><span class="si">}</span><span class="s2"> file&quot;</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">protected</span> <span class="k">function</span> <span class="nf">_emailOps</span><span class="p">(</span><span class="nv">$logMessage</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="k">return</span> <span class="s2">&quot;Sent email to Ops&quot;</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">protected</span> <span class="k">function</span> <span class="nf">_textCeo</span><span class="p">(</span><span class="nv">$message</span><span class="p">)</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="k">return</span> <span class="s2">&quot;Sent text to CEO&quot;</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 thing that lept out at me was that this refactor worked, but resulted in
more code. The good characteristic is all the small methods without lots of
code in them. Even better is that we don&#8217;t have to change any of our tests,
everything worked the same! All we had to do was change which object we
were using for $this->logger.</p>

<p>If you want to see the code for this blog post for yourself (it does work
and run using PHPUnit) you can grab it from <a href="https://github.com/chartjes/blog-strategy">Github</a>.
Also, feel free to take a look at the <a href="http://www.grumpy-testing.com/strategy/tests/coverage/">code coverage report</a>
as proof that I did cover everything I should have.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Why I Began Testing]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/05/14/why-i-began-testing/"/>
    <updated>2012-05-14T00:00:00-04:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/05/14/why-i-began-testing</id>
    <content type="html"><![CDATA[<p>In the run-up to <a href="http://tek.phparch.com">php|tek 2012</a> next week I wanted to
do a blog entry every day this week. Since I am supremely lazy I made sure to
ask my followers on Twitter what they would like me to blog about. There were
some interesting suggestions but I will start out with one that I think
explains a lot about me.</p>

<p>I&#8217;ve spent quite a bit of time in the last few years helping to spread the word
that having automated test suites and testable applications is a good thing.
I know it&#8217;s not easy and I&#8217;ve had conversations with many other developers who
still think that writing tests is a waste of time, not appropriate for their
unique situation, and so on and so forth.</p>

<p>If I were to make some sweeping generalizations about those people who feel
that test-driven development is a waste of time, I would call them supremely
overconfident in their own abilities in the face of plenty of evidence that
suggests otherwise. Many developers that I respect immensely still seem to
think that Getting Shit Done Does Not Include Writing Tests.</p>

<p>I will return to that topic later.</p>

<p>I don&#8217;t remember when it was that I exactly discovered <a href="http://simpletest.org">SimpleTest</a> while
working at an online dating site. I do remember it was in 2003. If I had to make an educated guess it was
because I had gotten tired of testing everything through a browser. Loading
the same pages OVER AND OVER AND OVER again, hoping I had covered all the
scenarios for the incredibly crummy code I was writing (this was quite early
in my programming career) in my manual testing. Only to find that I had
missed something and it was causing problems on the live site.</p>

<p>Once I discovered unit testing, it was like a bit was flipped in my brain.
I immediately understood the benefits of a test suite that we could run from
the browser AND provide proof that all was good. No more manual testing unless
absolutely necessary!</p>

<p>It took some time and some lobbying but not too long before I left, the
application architect they had hired decided that we needed to write a bunch
of tests using SimpleTest as part of progressing towards a refactoring and
rewrite of the application itself, since it really was the type of spaghetti
PHP application the language is derided for encouraging.</p>

<p>I don&#8217;t know what happened to all those tests after I left. I would keep
running into people who I worked with there and new hires and would sometimes
ask what was going on. Never did ask about tests though, as I assumed that
they were either still there but not being used, or ripped out under the
guise of Getting Shit Done Does Not Include Writing Tests.</p>

<p>So why am I still in the minority that values testing? I think it&#8217;s because
many people blind themselves to the reality of how much work they are really
getting done. Writing tests as part of the development process, in my opinion,
simply moves the debugging of your application from after you&#8217;ve written the
code to while you are still writing it.</p>

<p>Look, it still takes the same amount of time to write code + debug whether you
use TDD or GSDDNIWT. Be honest with yourself. Every time you are up until the
early hours of the morning trying to fix a problem is an opportunity to be
really honest with yourself and say &#8220;how can I avoid this?&#8221; instead of saying
&#8220;oh well, this is how it really is.&#8221; I believe that having tests will reduce
the amount of time you spend trying to understand why things are not working
the way that you expect them to.</p>

<p>If anything, the rise of startup worship makes the job of people like me
even tougher. Minimum Viable Product goes hand-in-hand with GSDDNIWT. I cringe
when people forward me conversations with their bosses and co-workers that
seem to imply that only inexperienced developers really need tests. &#8220;Rockstars
have the codebase memorized and can intuitively make changes that won&#8217;t break
anything.&#8221; Really?</p>

<p>I can only speak for myself, but testing is my way of not having to remember
the entire code base. It lets me examine discrete modules of code and see
clear examples of how they work and how they are supposed to interact with
other modules. When it&#8217;s crunch time and something absolutely needs to work,
my memory is the last thing I want to be relying on to tell me where to look
for the problem.</p>

<p>I could write a very long ranty blog post about testing but I will save my
energy for those ready to be converted instead of those who have decided to
rely on their immense programming skills and eidetic memory about their
application. Lesser mortals like me will knit themselves a comfy blanket
using tests and simplified application models and hopefully be sound asleep
while my rockstar compatriots are up at 2am debugging their application
after a painful production launch.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Grumpy Programmers Like Pair Programming]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/05/09/grumpy-programmers-like-pair-programming/"/>
    <updated>2012-05-09T00:00:00-04:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/05/09/grumpy-programmers-like-pair-programming</id>
    <content type="html"><![CDATA[<p>Some of you might have a vision of me, the self-described and oft-labeled
Grumpy Programmer as the type who prefers to work in solitude. Uninterrupted,
I crank out code while in The Zone.</p>

<p>I prefer working in collaborative environments where the entire team gets to
work on the entire application. None of this siloing bullshit that I see happen
time and time again. &#8220;Borat is the only guy who understand our reporting system.
We can&#8217;t possibly let anyone else work on it!&#8221; That sort of nonsense doesn&#8217;t
belong on any team and also speaks of ridonkulous communication problems.</p>

<p>At <a href="http://www.kaplanprofessional.com">Kaplan Professional</a> they&#8217;ve chosen to
use <a href="http://en.wikipedia.org/wiki/Pair_programming">pair programming</a> as a normal,
everyday development practice. Our team consists of 3 developers in the main
offices in La Crosse, 7 remote workers spread across 3 time zones, 2 QA staff,
one testing engineer, and our development manager. Pair programming has worked
out awesomely and I want to try and explain why.</p>

<p>One of the biggest hurdles facing a team with in-house and remote workers is
communication issues. Your classic pair programming setup is two developers
sitting together at the same desk, one typing while the other helps out with
suggestions on how to write the tests we&#8217;re going to need and how to hand-crafted
our artisanal code. Sadly we still have people writing the tests AFTER they&#8217;ve
written the code, but grumpiness is not eliminated in one day.</p>

<p>At Kaplan we use the dynamic duo of <a href="http://join.me">Join.me</a> and <a href="http://www.teamspeak.com/">TeamSpeak</a>
for pair programming. Whomever is driving will start a join.me session, share the
link with their partner and away they go. There is much coding and playful banter
as Shit Gets Done. I cannot put it any other way.</p>

<p>Your partner keeps you honest and focussed on the task you&#8217;re trying to solve. For
example, when I was coding a feature I was not 100% familiar with I was able to
get my pair to do research on other parts of the code base while I was creating
the skeletons for some tests. Many times my pair would suggest &#8220;don&#8217;t you think we
could make that bit of code better by&#8230;&#8221;, and more often than not we&#8217;d have a good
discussion about writing easy-to-understand-yet-efficient code or discuss how what
we are doing is out of character with the rest of the code. <a href="http://www.slideshare.net/rdohms/bettercode-phpbenelux212alternate">Object calisthenics</a>
is a favourite topic of mine and has led some of my other fellow developers to rethink
how they have been building things.</p>

<p>See, people have this idea that pair programming means that one guy codes while the
other sullenly watches or surfs the internet instead. Or it means two developers
fucking around instead of working. I cannot speak for other developers, but I don&#8217;t
put up with that shit while paired. I am expecting my pair to devote as much attention
to the task at hand as I am, and I return the favour by being attentive and helpful
towards my pair when I am not driving the session.</p>

<p>Because developers are taking turns solving problems and implementing solutions,
the amount of tribal knowledge (knowledge about the application itself locked up
in people&#8217;s brains instead of shared and/or documented) decreases and everyone&#8217;s
understanding of the system goes up. This transfer of knowledge can only really
happen as one developer shows another just what the hell is going on inside this
application. There is no better way I can think of to do this than while paired.</p>

<p>Pair programming also has the added good side effect of allowing team members to
get to know each other. It&#8217;s only the most anti-social of co-workers who won&#8217;t end
up discussing things outside of work while pair programming. I don&#8217;t see any reason
to work with robots who only care about how many story points we are cranking out
every sprint. We are people first and coders second. Don&#8217;t forget that.</p>

<p>Finally, pair programming results in more than one person understanding how a
particular problem was solved and how the solution was implemented. At Kaplan
we always have a sprint demo day at the end where we demonstrate the various
features that were implemented during the sprint. With everyone having used
pair programming, it&#8217;s much easier for the group as a whole to decide who will
demonstrate what. No need for one developer to hog all the glory!</p>

<p>This also ends up being a great way for those who already understand the
application to teach new hires how things work. Kaplan Professional had
expanded its team quite rapidly when I started and I think that without pairing we would
still be struggling to meet our sprint commitments. Nothing prompts rage-quitting
more than a situation where it is incredibly difficult for new hires to make
positive contributions to the team within a few days of starting.</p>

<p>I understand that not every organization is ready to make a commitment to use
pair programming. Just like not every organization is ready to make a commitment
to writing tests as part of their normal flow. But don&#8217;t harbour any misconceptions
about what pair programming <em>really</em> is: two developers working together towards
solving problems, alternating who is mashing keys on a keyboard. Both developers
<em>are</em> working, they are just working <em>together</em>.</p>

<p>The goal is to solve problems within a unit of time that you have committed to.
Kaplan Professional is no different in that it expects you to honour your commitments.
Pair programming has been a way for problems to get solved faster than if
individual programmers had been trying to solve them. Communication is up, non-working
features are done, and the amount of work actually getting done has increased
noticeably in the two months since I started working in pairs.</p>

<p>I cannot recommend enough giving pair programming an honest try in your work
environment. You might be surprised to find out that you are Getting More Shit Done
than you realize.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Build PHP 5.4 on CentOS with Vagrant]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/04/15/build-php-54-on-centos-with-vagrant/"/>
    <updated>2012-04-15T00:00:00-04:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/04/15/build-php-54-on-centos-with-vagrant</id>
    <content type="html"><![CDATA[<p>I like the idea of using <a href="http://vagrantup.com">Vagrant</a> to create virtual
machines for my development work. Doing things this way I think keeps the
host machine cleaner and allows you the ability to distribute those VM&#8217;s
to other people as well.</p>

<p>My old boss Ben Ramsey did a very informative post on <a href="http://benramsey.com/blog/2012/03/build-php-54-on-centos-62/">getting PHP 5.4 configured on CentOS</a>
so I decided to one-up him by taking his instructions and creating a <a href="http://puppetlabs.com/">Puppet</a>
manifest so you could do this using Vagrant or on any server that you can
provision using Puppet.</p>

<p>First, you will need a CentOS base box to play with. I used <a href="http://www.vagrantbox.es/37/">this box</a>
but I think any CentOS 6.0 (or above) base box will do. If you are feeling really
adventurous you could grab an ISO and use <a href="https://github.com/jedi4ever/veewee">Vewee</a>
and follow <a href="http://www.ducea.com/2011/08/15/building-vagrant-boxes-with-veewee/">these instructions</a>
to create your own.</p>

<p>Whatever box you choose, the next step is to create the directory where your VM
will run and run &#8216;vagrant init&#8217; with the name of the box and then edit the Vagrantfile.
Here&#8217;s a condensed version of my Vagrantfile</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>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">Vagrant</span><span class="o">::</span><span class="no">Config</span><span class="o">.</span><span class="n">run</span> <span class="k">do</span> <span class="o">|</span><span class="n">config</span><span class="o">|</span>
</span><span class='line'>  <span class="n">config</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">box</span> <span class="o">=</span> <span class="s2">&quot;centos-60-x86_64&quot;</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">config</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">forward_port</span> <span class="mi">80</span><span class="p">,</span> <span class="mi">8080</span>
</span><span class='line'>  <span class="n">config</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">forward_port</span> <span class="mi">8000</span><span class="p">,</span> <span class="mi">8000</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">config</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">provision</span> <span class="ss">:puppet</span> <span class="k">do</span> <span class="o">|</span><span class="n">puppet</span><span class="o">|</span>
</span><span class='line'>     <span class="n">puppet</span><span class="o">.</span><span class="n">manifests_path</span> <span class="o">=</span> <span class="s2">&quot;manifests&quot;</span>
</span><span class='line'>     <span class="n">puppet</span><span class="o">.</span><span class="n">manifest_file</span>  <span class="o">=</span> <span class="s2">&quot;default.pp&quot;</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>Those port forwards are identical to the ones that Ben suggested using. You
can get rid of the one that maps port 8000 if you don&#8217;t feel like playing
around with the new built-in web server introducted in PHP 5.4.</p>

<p>Now, create the manifests directory inside the directory where you are placing
the new VM and add a file called default.pp that looks 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>
<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>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
<span class='line-number'>79</span>
<span class='line-number'>80</span>
<span class='line-number'>81</span>
<span class='line-number'>82</span>
<span class='line-number'>83</span>
<span class='line-number'>84</span>
<span class='line-number'>85</span>
<span class='line-number'>86</span>
<span class='line-number'>87</span>
<span class='line-number'>88</span>
<span class='line-number'>89</span>
<span class='line-number'>90</span>
<span class='line-number'>91</span>
<span class='line-number'>92</span>
<span class='line-number'>93</span>
<span class='line-number'>94</span>
<span class='line-number'>95</span>
<span class='line-number'>96</span>
<span class='line-number'>97</span>
<span class='line-number'>98</span>
<span class='line-number'>99</span>
<span class='line-number'>100</span>
<span class='line-number'>101</span>
<span class='line-number'>102</span>
<span class='line-number'>103</span>
<span class='line-number'>104</span>
<span class='line-number'>105</span>
<span class='line-number'>106</span>
<span class='line-number'>107</span>
<span class='line-number'>108</span>
<span class='line-number'>109</span>
<span class='line-number'>110</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># Puppet manifest for my PHP dev machine</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="n">httpd</span> <span class="p">{</span>
</span><span class='line'>  <span class="nb">exec</span> <span class="p">{</span> <span class="s1">&#39;yum-update&#39;</span><span class="p">:</span>
</span><span class='line'>    <span class="n">command</span> <span class="o">=&gt;</span> <span class="s1">&#39;/usr/bin/yum -y update&#39;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;httpd&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span> <span class="o">=&gt;</span> <span class="n">present</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;httpd-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</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="k">class</span> <span class="n">phpdev</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;libxml2-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;libXpm-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;gmp-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;libicu-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;t1lib-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;aspell-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;openssl-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;bzip2-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;libcurl-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;libjpeg-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;libvpx-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;libpng-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;freetype-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;readline-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;libtidy-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;libxslt-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</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="k">class</span> <span class="n">rpmforge</span> <span class="p">{</span>
</span><span class='line'>  <span class="nb">exec</span> <span class="p">{</span>
</span><span class='line'>    <span class="s2">&quot;/usr/bin/wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm&quot;</span><span class="p">:</span>
</span><span class='line'>      <span class="k">alias</span>   <span class="o">=&gt;</span> <span class="s2">&quot;grab-rpmforge&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="nb">exec</span> <span class="p">{</span>
</span><span class='line'>    <span class="s2">&quot;/bin/rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt&quot;</span><span class="p">:</span>
</span><span class='line'>      <span class="k">alias</span>   <span class="o">=&gt;</span> <span class="s2">&quot;import-key&quot;</span><span class="p">,</span>
</span><span class='line'>      <span class="nb">require</span> <span class="o">=&gt;</span> <span class="no">Exec</span><span class="o">[</span><span class="s2">&quot;grab-rpmforge&quot;</span><span class="o">]</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="nb">exec</span> <span class="p">{</span>
</span><span class='line'>    <span class="s2">&quot;/bin/rpm -i rpmforge-release-0.5.2-2.el6.rf.*.rpm&quot;</span><span class="p">:</span>
</span><span class='line'>      <span class="k">alias</span>   <span class="o">=&gt;</span> <span class="s2">&quot;install-rpmforge&quot;</span><span class="p">,</span>
</span><span class='line'>      <span class="nb">require</span> <span class="o">=&gt;</span> <span class="no">Exec</span><span class="o">[</span><span class="s2">&quot;import-key&quot;</span><span class="o">]</span><span class="p">,</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">package</span> <span class="p">{</span> <span class="s2">&quot;libmcrypt-devel&quot;</span><span class="p">:</span>
</span><span class='line'>    <span class="k">ensure</span>  <span class="o">=&gt;</span> <span class="n">present</span><span class="p">,</span>
</span><span class='line'>    <span class="nb">require</span> <span class="o">=&gt;</span> <span class="no">Exec</span><span class="o">[</span><span class="s2">&quot;install-rpmforge&quot;</span><span class="o">]</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="kp">include</span> <span class="n">httpd</span>
</span><span class='line'><span class="kp">include</span> <span class="n">phpdev</span>
</span><span class='line'><span class="kp">include</span> <span class="n">rpmforge</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now, there <em>appears</em> to be a lot of stuff going on in there, but it&#8217;s actually
quite simple. I broke things down into 3 distinct groups.</p>

<p>The first group (
which I called &#8216;httpd&#8217;) makes sure that we install Apache 2 and the development
libraries and header files that will be needed to compile PHP. Then we open up
the firewall to allow access to the VM on those two ports. By default Vagrant
maps port 22 on the VM to port 2222 on the VM, so no need to add that in.</p>

<p>The next group (phpdev) is installing all the support libraries that Ben recommended
in his own article.</p>

<p>Finally we need to execute a few commands to allows us to also install libmcrypt-devel
because it is not part of the standard CentOS distribution, but is available via
RPMForge.</p>

<p>Save that file (make sure there are no typos either from me or from you!), then type
&#8216;vagrant up&#8217; and it will try and it will try to provision your new server with this
Puppet manifest and add in all your files.</p>

<p>Then you can follow the rest of Ben&#8217;s instructions on how to compile PHP 5.4 (I
installed PHP 5.4.1RC1 just to be living on the edge) and don&#8217;t forget to verify
that your Apache setup is configured to all you to serve up PHP files.</p>

<p>I think that you can have a cutting-edge CentOS VM with the latest PHP installed on
it up and running in under an hour from starting to download your CentOS Vagrant
base box to verifying your test file with phpinfo() in it worked.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Metatesting: Testing Constructors]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/04/05/metatesting-testing-constructors/"/>
    <updated>2012-04-05T00:00:00-04:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/04/05/metatesting-testing-constructors</id>
    <content type="html"><![CDATA[<p>If you have a PHP application that makes heavy use of objects (which is probably
95% of you reading this) then you will have objects with constructor methods
in them. It is also very likely that there is some stuff going on in those
constructors. So how do you test things like this?</p>

<p>Here is the constructor method that we are trying to test:</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>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">Alpha</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">protected</span> <span class="nv">$_foo</span><span class="p">;</span>
</span><span class='line'>    <span class="k">protected</span> <span class="nv">$_bar</span><span class="p">;</span>
</span><span class='line'>    <span class="k">protected</span> <span class="nv">$_baz</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">__construct</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nv">$container</span> <span class="o">=</span> <span class="nx">Grumpy_Container</span><span class="o">::</span><span class="na">getInstance</span><span class="p">();</span>
</span><span class='line'>        <span class="nv">$alphaConfig</span> <span class="o">=</span> <span class="nv">$container</span><span class="o">-&gt;</span><span class="na">alphaConfig</span><span class="p">();</span>
</span><span class='line'>        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_foo</span> <span class="o">=</span> <span class="nv">$alphaConfig</span><span class="o">-&gt;</span><span class="na">foo</span><span class="p">;</span>
</span><span class='line'>        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_bar</span> <span class="o">=</span> <span class="nv">$alphaConfig</span><span class="o">-&gt;</span><span class="na">bar</span><span class="p">;</span>
</span><span class='line'>        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_baz</span> <span class="o">=</span> <span class="nv">$alphaConfig</span><span class="o">-&gt;</span><span class="na">baz</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// the rest of our class is here</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>In this example, Grumpy_Container is an object that is available via
autoloading and it is used as a dependency injection container (DIC).</p>

<p>Okay, so how do we test that the constructor is doing what it is supposed to?
We would want to verify that those protected class attributes have been set
to values we are expecting.</p>

<p>In order to do that we will also need to override the values in our DIC so
when we run the test we have values in there we are expecting.</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>
<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>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">public</span> <span class="nx">testConstructorAssignsExpectedAttributeValues</span><span class="p">()</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="nv">$container</span> <span class="o">=</span> <span class="nx">Grumpy_Container</span><span class="o">::</span><span class="na">getInstance</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Create our mock configuration object</span>
</span><span class='line'>    <span class="nv">$alphaConfig</span> <span class="o">=</span> <span class="k">new</span> <span class="k">stdClass</span><span class="p">();</span>
</span><span class='line'>    <span class="nv">$alphaConfig</span><span class="o">-&gt;</span><span class="na">foo</span> <span class="o">=</span> <span class="s1">&#39;testfoo&#39;</span><span class="p">;</span>
</span><span class='line'>    <span class="nv">$alphaConfig</span><span class="o">-&gt;</span><span class="na">bar</span> <span class="o">=</span> <span class="s1">&#39;testbar&#39;</span><span class="p">;</span>
</span><span class='line'>    <span class="nv">$alphaConfig</span><span class="o">-&gt;</span><span class="na">baz</span> <span class="o">=</span> <span class="s1">&#39;testbaz&#39;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Place this back in the container to override what we have</span>
</span><span class='line'>    <span class="nv">$container</span><span class="o">-&gt;</span><span class="na">alphaConfig</span> <span class="o">=</span> <span class="k">function</span><span class="p">()</span> <span class="k">use</span> <span class="p">(</span><span class="nv">$alphaConfig</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nv">$alphaConfig</span><span class="p">;</span>
</span><span class='line'>    <span class="p">};</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Create our mock object</span>
</span><span class='line'>    <span class="nv">$alphaTest</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">getMockBuilder</span><span class="p">(</span><span class="s1">&#39;Alpha&#39;</span><span class="p">)</span>
</span><span class='line'>        <span class="o">-&gt;</span><span class="na">disableOriginalConstructor</span><span class="p">()</span>
</span><span class='line'>        <span class="o">-&gt;</span><span class="na">getMock</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Manually run the constructor</span>
</span><span class='line'>    <span class="nv">$alphaTest</span><span class="o">-&gt;</span><span class="na">__construct</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Check for values we expect</span>
</span><span class='line'>    <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">assertAttributeEquals</span><span class="p">(</span>
</span><span class='line'>        <span class="nv">$alphaConfig</span><span class="o">-&gt;</span><span class="na">foo</span><span class="p">,</span>
</span><span class='line'>        <span class="s1">&#39;_foo&#39;</span><span class="p">,</span>
</span><span class='line'>        <span class="nv">$alphaTest</span><span class="p">,</span>
</span><span class='line'>        <span class="s1">&#39;Should set expected foo config value&#39;</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">-&gt;</span><span class="na">assertAttributeEquals</span><span class="p">(</span>
</span><span class='line'>        <span class="nv">$alphaConfig</span><span class="o">-&gt;</span><span class="na">bar</span><span class="p">,</span>
</span><span class='line'>        <span class="s1">&#39;_bar&#39;</span><span class="p">,</span>
</span><span class='line'>        <span class="nv">$alphaTest</span><span class="p">,</span>
</span><span class='line'>        <span class="s1">&#39;Should set expected foo config value&#39;</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">-&gt;</span><span class="na">assertAttributeEquals</span><span class="p">(</span>
</span><span class='line'>        <span class="nv">$alphaConfig</span><span class="o">-&gt;</span><span class="na">baz</span><span class="p">,</span>
</span><span class='line'>        <span class="s1">&#39;_baz&#39;</span><span class="p">,</span>
</span><span class='line'>        <span class="nv">$alphaTest</span><span class="p">,</span>
</span><span class='line'>        <span class="s1">&#39;Should set expected bar config value&#39;</span>
</span><span class='line'>    <span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The magic method here is assertAttributeEquals(). It lets you check the value
inside any protected or private class attributes, which is absolutely a
requirement in this example. It is probably better to have some other
publicly-available methods that can return us the protected (or in some
cases private because you don&#8217;t want anyone to manipulate them) attributes.</p>

<p>Hope this helps you understand some ways to test your constructors.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Metatesting: Understanding Mock Objects]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/03/27/metatesting-understanding-mock-objects/"/>
    <updated>2012-03-27T00:00:00-04:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/03/27/metatesting-understanding-mock-objects</id>
    <content type="html"><![CDATA[<p>One of the things that I have learned to quickly appreciate about working at
<a href="http://kaplanprofessional.com">Kaplan Professional</a> is the very large suite
of unit and integration tests. I believe there are 2000+ tests, and although
there has been a dip in code coverage, it is still impressive to see coverage
levels for most things in the 65%+ range. We&#8217;ll get it back up to where it
belongs in no time.</p>

<p>With such an extensive array of tests, I have received an education in what
it really means to write unit tests for live, production-ready code that really
takes unit testing seriously. I think that the biggest problem that most
developers face when getting into Test Driven Development is not understanding
of how to efficiently use mock objects.</p>

<p>If you are really writing your unit tests the way you should, each test is
focusing on testing one bit of functionality in isolation, which means that
you will be heavily relying on mock objects to make things work. I&#8217;ve come
up with an example scenario that I hope goes a long way to explaining how
to effectively use mock objects.</p>

<p>In these examples I am using PHPUnit&#8217;s <a href="http://www.phpunit.de/manual/3.0/en/mock-objects.html">built-in support</a>
for mock objects. At Kaplan our testing engineer told me that he had tried to use
<a href="https://github.com/padraic/mockery">Mockery</a> but there were problems getting
it to play nicely with the dependency injection container that is in use (it&#8217;s
a customized version of <a href="http://pimple.sensiolabs.org">Pimple</a>). Can&#8217;t argue
with that. Not until I experiment with Mockery myself to see if it gains us
anything instead of using what comes with PHPUnit.</p>

<p>So here is our scenario: we have a class called Alpha, that contains a method
called munge(). We wish to test that method in a unit test. So what does
this class look like?</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>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">Alpha</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="c1">// Omit code showing constructor...</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">munge</span><span class="p">(</span><span class="nx">Foo</span> <span class="nv">$foo</span><span class="p">,</span> <span class="nx">Bar</span> <span class="nv">$bar</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nv">$fooDetails</span> <span class="o">=</span> <span class="nv">$foo</span><span class="o">-&gt;</span><span class="na">getDetails</span><span class="p">();</span>
</span><span class='line'>        <span class="nv">$fooId</span> <span class="o">=</span> <span class="nv">$foo</span><span class="o">-&gt;</span><span class="na">getId</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>        <span class="nv">$barStartDate</span> <span class="o">=</span> <span class="nv">$bar</span><span class="o">-&gt;</span><span class="na">getStartDate</span><span class="p">();</span>
</span><span class='line'>        <span class="nv">$barEndDate</span> <span class="o">=</span> <span class="nv">$bar</span><span class="o">-&gt;</span><span class="na">getEndDate</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>        <span class="nv">$source</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">{</span><span class="nv">$fooId</span><span class="si">}</span><span class="s2">|</span><span class="si">{</span><span class="nv">$fooDetails</span><span class="si">}</span><span class="s2">|</span><span class="si">{</span><span class="nv">$barStartDate</span><span class="si">}</span><span class="s2">|</span><span class="si">{</span><span class="nv">$barEndDate</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">return</span> <span class="nb">md5</span><span class="p">(</span><span class="s1">&#39;rocksalt&#39;</span> <span class="o">.</span> <span class="nv">$source</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>My grumpy tester&#8217;s eye looks at that and immediately says &#8220;we will need to
mock those Foo and Bar objects to make this test work.&#8221; First we need to look
at the Foo object</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>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'><span class="k">class</span> <span class="nc">Foo</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="c1">// Omit code showing how we construct a Foo object</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">getId</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_id</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">getDetails</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nv">$tree</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_tree</span><span class="p">;</span>
</span><span class='line'>        <span class="nv">$branch</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_branch</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">return</span> <span class="nv">$tree</span> <span class="o">.</span> <span class="s1">&#39;-&gt;&#39;</span> <span class="o">.</span> <span class="nv">$branch</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>So it looks like this class represents one &#8220;Foo&#8221; object in the system. I omitted
the constructor as it&#8217;s not really important to the lesson I&#8217;m trying to teach
here. Let&#8217;s assume we have a way to generate a Foo object via some sort of
mapper.</p>

<p>Our Alpha->munge() method expects to be passed in a Foo object and to retrieve
info via getDetails() and getId() calls. Let&#8217;s start our test and mock that up.</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>
<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>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="x"> </span>
</span><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'><span class="k">class</span> <span class="nc">Test</span> <span class="k">extends</span> <span class="nx">PHPUnit_Framework_TestCase</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">testAlphaMunge</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nv">$mockFoo</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">getMockBuilder</span><span class="p">(</span><span class="s1">&#39;Foo&#39;</span><span class="p">)</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">disableOriginalConstructor</span><span class="p">()</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">getMock</span><span class="p">();</span>
</span><span class='line'>        <span class="nv">$mockFoo</span><span class="o">-&gt;</span><span class="na">expects</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">atLeastOnce</span><span class="p">())</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">method</span><span class="p">(</span><span class="s1">&#39;getDetails&#39;</span><span class="p">)</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">will</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">returnValue</span><span class="p">(</span><span class="s1">&#39;1-&gt;2&#39;</span><span class="p">));</span>
</span><span class='line'>        <span class="nv">$mockFoo</span><span class="o">-&gt;</span><span class="na">expects</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">atLeastOnce</span><span class="p">())</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">method</span><span class="p">(</span><span class="s1">&#39;getId&#39;</span><span class="p">)</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">will</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">returnValue</span><span class="p">(</span><span class="s1">&#39;1&#39;</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'>        <span class="c1">// More test fun to come</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>When you build a mock for use in your test, you generally have to do two things:</p>

<ul>
<li>create a mock of the object itself</li>
<li>tell it what to do when specific methods are called</li>
</ul>


<p>What I&#8217;ve done here is pretty standard EXCEPT for the use of that disableOriginalConstructor()
method. Why is it used? Well, sometimes when you are creating a mock of an object
it will often have a constructor that accepts parameters. I&#8217;m not testing to see
if we can create the object, I merely am trying to simulate some of the methods
of that object.</p>

<p>Once I&#8217;ve created the mock of the object, I then tell it what
to do when I make certain method calls. Clearly I&#8217;ve just pulled some
arbitrary values out of my ass for this example, but usually you can figure
out what some reasonable return values are supposed to be by, you know, actually
looking at the code you are mocking.</p>

<p>So what does our Bar object look like?</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="cp">&lt;?php</span>
</span><span class='line'><span class="k">class</span> <span class="nc">Bar</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="c1">// Same drill, no need to show constructor details</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">getStartDate</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_startDate</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">getEndDate</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_endDate</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>Time to add a mock Bar to our test</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>
<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>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'><span class="k">class</span> <span class="nc">Test</span> <span class="k">extends</span> <span class="nx">PHPUnit_Framework_TestCase</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">testAlphaMunge</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nv">$mockFoo</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">getMockBuilder</span><span class="p">(</span><span class="s1">&#39;Foo&#39;</span><span class="p">)</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">disableOriginalConstructor</span><span class="p">()</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">getMock</span><span class="p">();</span>
</span><span class='line'>        <span class="nv">$mockFoo</span><span class="o">-&gt;</span><span class="na">expects</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">atLeastOnce</span><span class="p">())</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">method</span><span class="p">(</span><span class="s1">&#39;getDetails&#39;</span><span class="p">)</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">will</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">returnValue</span><span class="p">(</span><span class="s1">&#39;1-&gt;2&#39;</span><span class="p">));</span>
</span><span class='line'>        <span class="nv">$mockFoo</span><span class="o">-&gt;</span><span class="na">expects</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">atLeastOnce</span><span class="p">())</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">method</span><span class="p">(</span><span class="s1">&#39;getId&#39;</span><span class="p">)</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">will</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">returnValue</span><span class="p">(</span><span class="s1">&#39;1&#39;</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'>        <span class="nv">$mockBar</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">getMockBuilder</span><span class="p">(</span><span class="s1">&#39;Bar&#39;</span><span class="p">)</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">disableOriginalConstructor</span><span class="p">()</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">getMock</span><span class="p">();</span>
</span><span class='line'>        <span class="nv">$mockBar</span><span class="o">-&gt;</span><span class="na">expects</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">atLeastOnce</span><span class="p">())</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">method</span><span class="p">(</span><span class="s1">&#39;getStartDate&#39;</span><span class="p">)</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">will</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">returnValue</span><span class="p">(</span><span class="s1">&#39;2012-03-27&#39;</span><span class="p">));</span>
</span><span class='line'>        <span class="nv">$mockBar</span><span class="o">-&gt;</span><span class="na">expects</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">atLeastOnce</span><span class="p">())</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">method</span><span class="p">(</span><span class="s1">&#39;getEndDate&#39;</span><span class="p">)</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">will</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">returnValue</span><span class="p">(</span><span class="s1">&#39;2012-04-09&#39;</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'>        <span class="c1">// More test fun to come</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>A quick note about $this->atLeastOnce(): when you mock the objects you can tell it
how often you expect that method to be called. I had a long discussion about this
with our testing engineer Will Parker after I posted the orginal version of this post.</p>

<p>We have multiple options</p>

<ul>
<li>$this->once() - should be used only when you REALLY want that method to be called once</li>
<li>$this->atLeastOnce() - should be used when you need to do it once but aren&#8217;t sure how many times</li>
<li>$this->any() - you really don&#8217;t care how many times it&#8217;s called</li>
</ul>


<p>If you&#8217;re lazy, you&#8217;ll go with $this->any() so you don&#8217;t have to worry about it.
After a discussion with Will, I have come to like the use of $this->atLeastOnce().
It&#8217;s not a deal-breaker no matter which one you choose, a lot of it comes down
to personal preferences.</p>

<p>So now we have our two objects mocked. Now we can go back to our test and
add in the creation of our Alpha class, inject the two mocks into the
method we are testing, and check for expected results.</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>
<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>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'><span class="k">class</span> <span class="nc">Test</span> <span class="k">extends</span> <span class="nx">PHPUnit_Framework_TestCase</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">testAlphaMunge</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nv">$mockFoo</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">getMockBuilder</span><span class="p">(</span><span class="s1">&#39;Foo&#39;</span><span class="p">)</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">disableOriginalConstructor</span><span class="p">()</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">getMock</span><span class="p">();</span>
</span><span class='line'>        <span class="nv">$mockFoo</span><span class="o">-&gt;</span><span class="na">expects</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">atLeastOnce</span><span class="p">())</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">method</span><span class="p">(</span><span class="s1">&#39;getDetails&#39;</span><span class="p">)</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">will</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">returnValue</span><span class="p">(</span><span class="s1">&#39;1-&gt;2&#39;</span><span class="p">));</span>
</span><span class='line'>        <span class="nv">$mockFoo</span><span class="o">-&gt;</span><span class="na">expects</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">atLeastOnce</span><span class="p">())</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">method</span><span class="p">(</span><span class="s1">&#39;getId&#39;</span><span class="p">)</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">will</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">returnValue</span><span class="p">(</span><span class="s1">&#39;1&#39;</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'>        <span class="nv">$mockBar</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">getMockBuilder</span><span class="p">(</span><span class="s1">&#39;Bar&#39;</span><span class="p">)</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">disableOriginalConstructor</span><span class="p">()</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">getMock</span><span class="p">();</span>
</span><span class='line'>        <span class="nv">$mockBar</span><span class="o">-&gt;</span><span class="na">expects</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">atLeastOnce</span><span class="p">())</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">method</span><span class="p">(</span><span class="s1">&#39;getStartDate&#39;</span><span class="p">)</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">will</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">returnValue</span><span class="p">(</span><span class="s1">&#39;2012-03-27&#39;</span><span class="p">));</span>
</span><span class='line'>        <span class="nv">$mockBar</span><span class="o">-&gt;</span><span class="na">expects</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">atLeastOnce</span><span class="p">())</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">method</span><span class="p">(</span><span class="s1">&#39;getEndDate&#39;</span><span class="p">)</span>
</span><span class='line'>            <span class="o">-&gt;</span><span class="na">will</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">returnValue</span><span class="p">(</span><span class="s1">&#39;2012-04-09&#39;</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'>        <span class="nv">$source</span> <span class="o">=</span> <span class="s2">&quot;1|1-&gt;2|2012-03-27|2012-04-09&quot;</span><span class="p">;</span>
</span><span class='line'>        <span class="nv">$expectedMunge</span> <span class="o">=</span> <span class="nb">md5</span><span class="p">(</span><span class="s1">&#39;rocksalt&#39;</span> <span class="o">.</span> <span class="nv">$source</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="nv">$testAlpha</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Alpha</span><span class="p">();</span>
</span><span class='line'>        <span class="nv">$munge</span> <span class="o">=</span> <span class="nv">$testAlpha</span><span class="o">-&gt;</span><span class="na">getMunge</span><span class="p">(</span><span class="nv">$mockFoo</span><span class="p">,</span> <span class="nv">$mockBar</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">assertEquals</span><span class="p">(</span>
</span><span class='line'>            <span class="nv">$expectedMunge</span><span class="p">,</span>
</span><span class='line'>            <span class="nv">$munge</span><span class="p">,</span>
</span><span class='line'>            <span class="s1">&#39;Munge value should match expected&#39;</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>And there you have it! A test for a specific method that uses mocks to properly
isolate functionality that is required for the test to work.</p>

<p>I hope this blog post makes things a lot clearer when it comes to understanding
the role of mocks when creating unit tests.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Moving on, and a Testing Mini-Manifesto]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/03/09/moving-on-and-a-testing-mini-manifesto/"/>
    <updated>2012-03-09T00:00:00-05:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/03/09/moving-on-and-a-testing-mini-manifesto</id>
    <content type="html"><![CDATA[<p>After 13 months I&#8217;ve decided to move on from <a href="http://moontoast.com">Moontoast</a>
and take a position as a senior web developer with <a href="http://kaplanprofessional.com/">Kaplan Professional</a>
working on their platform team and trying to create all sorts of testable
awesomeness with them. I start on March 12 with two weeks of training in
La Crosse, WI, followed by two weeks at home (wanted to be home for my birthday
and family good times as Easter), followed by 4 weeks in La Crosse and then
back in my basement lair cranking out the work.</p>

<p>With the new position comes more of the stuff I am really passionate about:
testing and automation. Which also got me to thinking about the reasons why I
am so passionate about these things.</p>

<p>I thought I would create my own little testing mini-manifesto here. The ideas
my <a href="http://devhell.info">podcasting</a> <a href="https://twitter.com/funkatron"> partner</a> did with his <a href="http://microphp.org">MicroPHP Manifesto</a>
made me realize that sometimes it is good to write these things down, as it
were.</p>

<h2>I test because the tools are easy to use</h2>

<p>I write tests because the tools that exist today to write them are easy to
install and testing techniques are easily taught.</p>

<h2>I test because I don&#8217;t like surprises </h2>

<p>I write tests to make sure what I am building behaves the way I am expecting.</p>

<h2>I test because I want people to understand what I&#8217;ve done</h2>

<p>I write tests so they can act as documentation for how to use things that I
have built.</p>

<h2>I test because I want to be able to change things fearlessly</h2>

<p>I write tests so that I can experiment and change things, knowing I have a
way of telling myself if I&#8217;ve done something wrong.</p>

<h2>I test because automation is a secret weapon</h2>

<p>I write tests so I can create an automated developer workflows
that will reduce the likelihood that something horribly broken makes it
into production.</p>

<h2>I test because it forces me to focus on design</h2>

<p>I write tests to force me to loosely couple my components and create simple
systems that can display complex behaviour.</p>

<h2>I test because to me it is the right thing to do</h2>

<p>I write tests because applications with a high level of test code coverage
have significantly reduced bug-fix cycles and turn production deployments
into non-events that happen multiple times a day.</p>

<p>I hope my mini-manifesto at least makes you think about where testing fits into
modern web application development.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Simple API testing using Behat]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/02/26/simple-api-testing-with-behat/"/>
    <updated>2012-02-26T00:00:00-05:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/02/26/simple-api-testing-with-behat</id>
    <content type="html"><![CDATA[<p>I&#8217;ve recently started using <a href="http://behat.org">Behat</a> as my testing framework
of choice. Why? I like the idea of writing tests in a format that is as close
to English as I can get. This also makes it easier for non-technical people
to write tests for you as well, making sure they &#8220;have some skin in the game&#8221;
as you build out your automated test suite.</p>

<p>I have been trying to push my JavaScript skills lately by first playing around
with Node.js to get a feel for event-driven server-side coding and by trying
to build something semi-useful using <a href="http://knockout.js">Knockout</a>. Knockout
is a client-side Model-View-View-Model framework that promotes four really
good principles: declarative bindings, automatic UI refreshing, dependency
tracking and templating. Check out the documentation on the Knockout web site
for a great explanation of why those are good thigns.</p>

<p>A typical Knockout-out powered site is one where the client-side code talks
to a server-side API. My experimental site for this is one that uses an
API written in PHP using <a href="http://slimframework.com">Slim</a>. Of course, I cannot
do this without writing some tests. My first set of tests are for the calling
of the API itself.</p>

<p>After working with PHPUnit&#8217;s &#8220;assert that the result matches expectations&#8221;
philosophy, switching to using a behavioural approach has changed how I decide
to approach actually testing something. Let me show you what I mean.</p>

<p>Here&#8217;s a test for an API call to get our &#8220;current&#8221; set of transactions, which
is hard-coded in the system to be all trades that have occurred in the past
3 weeks.</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>
</pre></td><td class='code'><pre><code class=''><span class='line'>Feature: transactions API 
</span><span class='line'>
</span><span class='line'>Scenario: 
</span><span class='line'>  Given I call "/transactions/current"
</span><span class='line'>  Then I get a response
</span><span class='line'>  And the response is JSON
</span><span class='line'>  And the response contains at least one transaction
</span><span class='line'>  And the first transaction contains a transaction ID
</span><span class='line'>  And the first transaction contains two teams
</span><span class='line'>  And the first transaction contains a description</span></code></pre></td></tr></table></div></figure>


<p>If you read that out loud it sounds like how you would describe to a co-worker
what you were expecting to happen on an API call, albeit a little more stilted
and formalized.</p>

<p>When you first write a feature like this and then ask Behat to run all the
tests on it, if there is no code to execute a specific step it will give you
the skeleton of code to make it happen. Then it is up to you to implement
the actual code that will make the tests run.</p>

<p>Here&#8217;s the final product:</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>
<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>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
<span class='line-number'>79</span>
<span class='line-number'>80</span>
<span class='line-number'>81</span>
<span class='line-number'>82</span>
<span class='line-number'>83</span>
<span class='line-number'>84</span>
<span class='line-number'>85</span>
<span class='line-number'>86</span>
<span class='line-number'>87</span>
<span class='line-number'>88</span>
<span class='line-number'>89</span>
<span class='line-number'>90</span>
<span class='line-number'>91</span>
<span class='line-number'>92</span>
<span class='line-number'>93</span>
<span class='line-number'>94</span>
<span class='line-number'>95</span>
<span class='line-number'>96</span>
<span class='line-number'>97</span>
<span class='line-number'>98</span>
<span class='line-number'>99</span>
<span class='line-number'>100</span>
<span class='line-number'>101</span>
<span class='line-number'>102</span>
<span class='line-number'>103</span>
<span class='line-number'>104</span>
<span class='line-number'>105</span>
<span class='line-number'>106</span>
<span class='line-number'>107</span>
<span class='line-number'>108</span>
<span class='line-number'>109</span>
<span class='line-number'>110</span>
<span class='line-number'>111</span>
<span class='line-number'>112</span>
<span class='line-number'>113</span>
<span class='line-number'>114</span>
<span class='line-number'>115</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">use</span> <span class="nx">Behat\Behat\Context\ClosuredContextInterface</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">Behat\Behat\Context\TranslatedContextInterface</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">Behat\Behat\Context\BehatContext</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">Behat\Behat\Exception\PendingException</span><span class="p">;</span>
</span><span class='line'><span class="k">use</span> <span class="nx">Behat\Gherkin\Node\PyStringNode</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">Behat\Gherkin\Node\TableNode</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="c1">//</span>
</span><span class='line'><span class="c1">// Require 3rd-party libraries here:</span>
</span><span class='line'><span class="c1">//</span>
</span><span class='line'><span class="c1">//   require_once &#39;PHPUnit/Autoload.php&#39;;</span>
</span><span class='line'><span class="c1">//   require_once &#39;PHPUnit/Framework/Assert/Functions.php&#39;;</span>
</span><span class='line'><span class="c1">//</span>
</span><span class='line'>
</span><span class='line'><span class="sd">/**</span>
</span><span class='line'><span class="sd"> * Features context.</span>
</span><span class='line'><span class="sd"> */</span>
</span><span class='line'><span class="k">class</span> <span class="nc">FeatureContext</span> <span class="k">extends</span> <span class="nx">BehatContext</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="sd">/**</span>
</span><span class='line'><span class="sd">     * Initializes context.</span>
</span><span class='line'><span class="sd">     * Every scenario gets it&#39;s own context object.</span>
</span><span class='line'><span class="sd">     *</span>
</span><span class='line'><span class="sd">     * @param   array   $parameters     context parameters (set them up through behat.yml)</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">__construct</span><span class="p">(</span><span class="k">array</span> <span class="nv">$parameters</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="c1">// Initialize your context here</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="sd">/**</span>
</span><span class='line'><span class="sd">     * @Given /^I call &quot;([^&quot;]*)&quot;$/</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">iCall</span><span class="p">(</span><span class="nv">$argument1</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nv">$client</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Guzzle\Service\Client</span><span class="p">();</span>
</span><span class='line'>        <span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;http://local.ibl-delphi&#39;</span> <span class="o">.</span> <span class="nv">$argument1</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">();</span>
</span><span class='line'>        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">response</span> <span class="o">=</span> <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">getBody</span><span class="p">(</span><span class="k">true</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="sd">/**</span>
</span><span class='line'><span class="sd">     * @Then /^I get a response$/</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">iGetAResponse</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="k">empty</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">response</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">Exception</span><span class="p">(</span><span class="s1">&#39;Did not get a response from the API&#39;</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="sd">/**</span>
</span><span class='line'><span class="sd">     * @Given /^the response is JSON$/</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">theResponseIsJson</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nv">$data</span> <span class="o">=</span> <span class="nx">json_decode</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">response</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="k">empty</span><span class="p">(</span><span class="nv">$data</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">Exception</span><span class="p">(</span><span class="s2">&quot;Response was not JSON</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">.</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">response</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="sd">/**</span>
</span><span class='line'><span class="sd">     * @Given /^the response contains at least one transaction$/</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">theResponseContainsAtLeastOneTransaction</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nv">$data</span> <span class="o">=</span> <span class="nx">json_decode</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">response</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="nb">count</span><span class="p">(</span><span class="nv">$data</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">1</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">Exception</span><span class="p">(</span><span class="s2">&quot;Response did not contain at least one transaction&quot;</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="sd">/**</span>
</span><span class='line'><span class="sd">     * @Given /^the first transaction contains a transaction ID$/</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">theFirstTransactionContainsATransactionId</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nv">$data</span> <span class="o">=</span> <span class="nx">json_decode</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">response</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span>
</span><span class='line'>        <span class="nv">$transaction</span> <span class="o">=</span> <span class="nv">$data</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nb">isset</span><span class="p">(</span><span class="nv">$transaction</span><span class="p">[</span><span class="s1">&#39;id&#39;</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">Exception</span><span class="p">(</span><span class="s2">&quot;First transaction did not contain a transaction id&quot;</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="sd">/**</span>
</span><span class='line'><span class="sd">     * @Given /^the first transaction contains two teams$/</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">theFirstTransactionContainsTwoTeams</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nv">$data</span> <span class="o">=</span> <span class="nx">json_decode</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">response</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span>
</span><span class='line'>        <span class="nv">$transaction</span> <span class="o">=</span> <span class="nv">$data</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nb">isset</span><span class="p">(</span><span class="nv">$transaction</span><span class="p">[</span><span class="s1">&#39;tradePartner1&#39;</span><span class="p">])</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="nb">isset</span><span class="p">(</span><span class="nv">$transaction</span><span class="p">[</span><span class="s1">&#39;tradePartner2&#39;</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">Exception</span><span class="p">(</span><span class="s2">&quot;First transaction did not contain two teams&quot;</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="sd">/**</span>
</span><span class='line'><span class="sd">     * @Given /^the first transaction contains a description$/</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">theFirstTransactionContainsADescription</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nv">$data</span> <span class="o">=</span> <span class="nx">json_decode</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">response</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span>
</span><span class='line'>        <span class="nv">$transaction</span> <span class="o">=</span> <span class="nv">$data</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nb">isset</span><span class="p">(</span><span class="nv">$transaction</span><span class="p">[</span><span class="s1">&#39;description&#39;</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">Exception</span><span class="p">(</span><span class="s2">&quot;First transaction is missing a description&quot;</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>Notice how Behat uses annotations that describe what type of scenario the
method uses. It also uses regular expressions to extract specific values
that you are wishing to test. Those are typically values inside quotes
in the actual features.</p>

<p>If you are wondering what the skeletons (or stubbs depending on what
terminology you like to use) looked like <em>before</em> I wrote code to implement
them, you can simply cut-and-paste the scenario file at the top of this
post into a feature file (don&#8217;t forget to give a .feature extension) and it
will show you all of them.</p>

<p>I liked this method because it meant I could easily add more tests and
Behat would tell me what code woudl be needed to run it.</p>

<p>So what does the output look like for the test?</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=''><span class='line'>-> % bin/behat
</span><span class='line'>Feature: transactions API
</span><span class='line'>
</span><span class='line'>  Scenario:                                             # features/transactions.feature:3
</span><span class='line'>    Given I call "/transactions/current"                # FeatureContext::iCall()
</span><span class='line'>    Then I get a response                               # FeatureContext::iGetAResponse()
</span><span class='line'>    And the response is JSON                            # FeatureContext::theResponseIsJson()
</span><span class='line'>    And the response contains at least one transaction  # FeatureContext::theResponseContainsAtLeastOneTransaction()
</span><span class='line'>    And the first transaction contains a transaction ID # FeatureContext::theFirstTransactionContainsATransactionId()
</span><span class='line'>    And the first transaction contains two teams        # FeatureContext::theFirstTransactionContainsTwoTeams()
</span><span class='line'>    And the first transaction contains a description    # FeatureContext::theFirstTransactionContainsADescription()
</span><span class='line'>
</span><span class='line'>1 scenario (1 passed)
</span><span class='line'>7 steps (7 passed)
</span><span class='line'>0m0.362s</span></code></pre></td></tr></table></div></figure>


<p>I have obviously only scratched the surface of what can be done with Behat.
I myself have questions about how to prevent the FeatureContext.php file,
which contains all the functionality that drives the tests, from getting
too large. I can also see the lure of endlessly tweaking the scenarios to
reduce the amount of code required to run them all.</p>

<p>What about code-coverage reports? Easily available in PHPUnit but is it
possible using Behat? It&#8217;s still early days for some of the alternatives to
PHPUnit.</p>

<p>The example above is only the first of numerous tests that will be written
for this side project, as it needs to cover displaying archived transactions
to be voted on, user authentication, and the voting process itself.</p>

<p>I think that the BDD approach maps better to a results-oriented methodology
when it comes to testing: you are using real-life scenarios to make sure that
you are getting back the results you are expecting. In the end. that is still
the goal of testing.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Thoughts on testing APIs]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/02/22/thoughts-on-testing-apis/"/>
    <updated>2012-02-22T00:00:00-05:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/02/22/thoughts-on-testing-apis</id>
    <content type="html"><![CDATA[<p>I have been asked a few times on Twitter for some advice on how to use
testing tools in order to test APIs. It just so happens that I am writing a
bunch of tests for an API at work so perhaps this is a very timely blog post.
Rather than show you exactly what I did (I&#8217;m using <a href="http://behat.org">Behat</a>)
I wanted to discuss some common strategies I use when contemplating testing
APIs.</p>

<p>When you are testing an API you main concern is your ability to reproduce
the results of a specific API call. If your API is such that repeating a
call several times will give you different results, you do have some
challenges on your hands.</p>

<p>Testing that an API just works is what I would call a functional test. At
the highest level you are saying &#8220;will this call actually work&#8221;. Since you
will be actually making API calls in your tests they can take quite some
time to run depending on how many you have. Long-running tests are the
type of tests that really should be forming the last line of defense just
before you&#8217;re about to go into production.</p>

<p>Why? Functional tests have a lot of moving parts: HTTP calls, request and
response parsing, and likely communications with some sort of data store.
Not exactly the types of things that lead you to having a test suite that
can run in 10 seconds or less.</p>

<p>In my opinion the only way to have really effective functional tests is
by making sure that the API endpoints you are testing are using a known
data source, one that will not be altered by your tests. That
way it will be easy to verify that a particular call had the expected
response. Remember, at the most basic level testing is about repeatedly
verifying that your code is returning an expected result.</p>

<p>If you are testing someone else&#8217;s API, I hope there is a test mode or a
sandbox that you can use. A lot of APIs are rate-limited or only allow a
limited number of requests a day (I&#8217;m looking at you, Google Analytics).
The last thing you want is for your application to longer be able to access an
API because you went nuts testing things. Remember, the goal of functional
tests in this scenario are to make sure that the API is returning results
that you expect, nothing more.</p>

<p>Integration tests of APIs are a bit of a tricky thing to do in an effective
way. Sometimes 3rd party APIs change. Sometimes you are also lucky enough
to be told ahead of time that it&#8217;s happening. If your application relies
heavily on 3rd party APIs then your integration tests are a key line of
defense. While tests like these shouldn&#8217;t be run as much as the unit
tests for the API, they should be run before a production push. Nobody
likes their application to be broken because of an unannounced change to
an API.</p>

<p>Finally, we have tests designed to make sure that your application is
correctly handling the results of API calls. To make these sorts of tests work,
you will end up either creating mock
objects of the code that will do the calls or data fixtures representing
results of API calls. It is also for this reason I always recommend wrapping
the calls to an API in code, just so you can mock them for testing purposes.
Sure, it might be easier to cheat and do file_get_contents() on a URL but
that approach is pretty difficult to test.</p>

<p>Remember, these tests should not even be calling out to an API in order to
work. Mocks and data fixtures are supposed to provide that data. All you
are doing is testing the code that needs results from the API. Again, if
you have been building your application with testability in mind your
suite of unit tests should be blazing fast.</p>

<p>In the end, it doesn&#8217;t really matter what tools you are using to test either
your own APIs or ones that your application depends on. Be clear in your
mind about what it is you are actually trying to accomplish and don&#8217;t get
create tests that provide very little value to your application.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Organizing Slim Framework Applications]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/02/14/organizing-slim-framework-apps/"/>
    <updated>2012-02-14T00:00:00-05:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/02/14/organizing-slim-framework-apps</id>
    <content type="html"><![CDATA[<p>Influenced by discussion with <a href="https://twitter.com/funkatron">Ed</a> I have
decided to use <a href="http://www.slimframework.com/">Slim</a> as the framework
for a PHP application I am building that is an addition to what is found
on the web site for the <a href="http://www.ibl.org">simulation baseball league</a> I
have belonged to for 14 seasons. I&#8217;ve never really used a microframework
in PHP before. I used <a href="http://flask.pocoo.org">Flask</a> for a Python
<a href="https://github.com/chartjes/liesitoldmykids">project</a> that I did to experiment
with using Google App Engine. The principles seem to be quite similar
(although I will admit that having decorators in PHP would be ineresting)
but the trade-off with a microframework is that you usually have to figure
out an application layout for yourself.</p>

<p>I decided to use the following things for this app:</p>

<ul>
<li>Slim (using latest stable version, 1.5.0 at this writing)</li>
<li><a href="http://pimple.sensiolabs.org">Pimple</a> as a dependency injection container</li>
<li><a href="http://twig.sensiolabs.org">Twig</a> for a templating engine</li>
</ul>


<p>I also decided to use <a href="http://packagist.org/about-composer">Composer</a> as a way
to install the above dependencies. Slim is not in <a href="http://packagist.org">Packagist</a>
so I was forced to do a little reorganizing work after getting some help in
IRC from <a href="https://twitter.com/jakefolio">Jake Smith</a>. Here&#8217;s what I did to get
all three of those things playing nicely together with Composer&#8217;s autoloader:</p>

<ul>
<li>moved the directory that contains Slim into the vendor directory</li>
<li>altered vendor/.composer/autoload_namespaces.php to look for Slim</li>
</ul>


<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>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="x">// autoload_namespace.php generated by Composer</span>
</span><span class='line'>
</span><span class='line'><span class="x">$vendorDir = dirname(__DIR__);</span>
</span><span class='line'>
</span><span class='line'><span class="x">return array(</span>
</span><span class='line'><span class="x">    &#39;Twig_Extensions_&#39; =&gt; $vendorDir . &#39;/twig/extensions/lib/&#39;,</span>
</span><span class='line'><span class="x">    &#39;Twig_&#39; =&gt; $vendorDir . &#39;/twig/twig/lib/&#39;,</span>
</span><span class='line'><span class="x">    &#39;Pimple&#39; =&gt; $vendorDir . &#39;/pimple/pimple/lib/&#39;,</span>
</span><span class='line'><span class="x">    &#39;Slim&#39; =&gt; $vendorDir .&#39;/slim/slim&#39;</span>
</span><span class='line'><span class="x">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>I&#8217;ve come to the opinion that if you are using a tool that provides an autoload
you are probably better off using the one they provide rather then trying to make
those two co-exist. I&#8217;m sure I could&#8217;ve used the autoloader from the <a href="http://phix-project.org">Phix project</a>
but I didn&#8217;t feel like screwing around with it. The composer one works and it was
easy to tell it to use Slim. Once I start creating the models that I will use for
this application I will add their location to that file too.</p>

<p>With that out of the way, it came time to discuss how to actually organize this
application. I have a templates directory for my Twig templates, which totally
makes sense. But what to do with the index.php file that Slim relies on. Like
any front-controller-driven application all our requests, after being folded
and mutilated by a web server rewrite rule, go through index.php. In the sample
Slim app they have something like this in their index.php file:</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>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="x">$app = new Slim();</span>
</span><span class='line'><span class="x">$app-&gt;get(&#39;/hello/:name&#39;, function ($name) {</span>
</span><span class='line'><span class="x">    echo &quot;Hello, $name!&quot;;</span>
</span><span class='line'><span class="x">});</span>
</span><span class='line'><span class="x">$app-&gt;run();</span>
</span></code></pre></td></tr></table></div></figure>


<p>This might be all Kool and the Gang if you only have a bunch of really small
actions. This app will be interacting with models a lot, and I&#8217;m positive that
despite my best efforts to roll with a &#8220;fat models, skinny controllers&#8221; paradigm
that some of those actions will end up being lots of code. Smashing all that stuff
together in one file strikes me as an abomination against best coding practices.
Ed told me how the <a href="http://fictivekin.com/">FictiveKin</a> folks are organizing their
Slim apps, so I have created something similar to it.</p>

<p>All my routes are going to go into a routes directory and then I will
specifically include the routes I want via require calls. One route per file and
I will try and name them something sensible.</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="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">require</span> <span class="s1">&#39;./bootstrap.php&#39;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Load all our routes</span>
</span><span class='line'><span class="k">require</span> <span class="s1">&#39;./routes/main.php&#39;</span><span class="p">;</span>
</span><span class='line'><span class="k">require</span> <span class="s1">&#39;./routes/hello.php&#39;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$app</span><span class="o">-&gt;</span><span class="na">run</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>


<p>Inside each one of these files will be an app->get|post|whatever call</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>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$app</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="k">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">echo</span> <span class="s1">&#39;MAIN PAGE&#39;</span><span class="p">;</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>Yes, that default route sucks but getting the structure in place was more
important to me than having something useful stubbed out.</p>

<p>I&#8217;m sure there is more than one way to organize a Slim application, but this
is one that I think works best for me.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Baby Steps with Node.js]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/02/13/baby-steps-with-node-js/"/>
    <updated>2012-02-13T00:00:00-05:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/02/13/baby-steps-with-node-js</id>
    <content type="html"><![CDATA[<p>In a desire to stay relevant, I have been spending some time taking a look
at <a href="http://nodejs.org">Node.js</a>. Why? I believe it is always a good idea
to take a look at whatever the new hawtness is, if only to evaluate it&#8217;s
suitability for use within your own applications. Sometimes it is also
a good idea to see if you can wrap your brain around it.</p>

<p>So I went and bought a good <a href="http://leanbundle.com/b/node/">bundle of ebooks about Node</a>
and started going through the tutorial code. These two seemed like a good
place to start: an introductory tutorial and then another book that leaps
right into things with the aim of really giving you some hands-on work with
Node.</p>

<p>The crowning glory of my early work is this bit of code from a tutorial that
emits events and then allows you to react to them:</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>
<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>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">EventEmitter</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;events&#39;</span><span class="p">).</span><span class="nx">EventEmitter</span><span class="p">;</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">util</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;util&#39;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">Ticker</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){};</span>
</span><span class='line'>
</span><span class='line'><span class="nx">util</span><span class="p">.</span><span class="nx">inherits</span><span class="p">(</span><span class="nx">Ticker</span><span class="p">,</span> <span class="nx">EventEmitter</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="nx">Ticker</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">tick</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
</span><span class='line'>    <span class="nx">interval</span> <span class="o">=</span> <span class="nx">setInterval</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>        <span class="nx">self</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">&#39;tick&#39;</span><span class="p">);</span>
</span><span class='line'>    <span class="p">},</span> <span class="mi">1000</span><span class="p">);</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">myTicker</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Ticker</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'><span class="nx">myTicker</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;tick&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;TICK&#39;</span><span class="p">);</span>
</span><span class='line'><span class="p">});</span>
</span><span class='line'>
</span><span class='line'><span class="nx">myTicker</span><span class="p">.</span><span class="nx">tick</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now I realize that this code sample is something very trivial but I also
think it shows what someone who has mostly worked in languages without
callbacks is up against when they start using node.</p>

<p>As a PHP guy I am used to my code being executed pretty much in the order
that you read it when looking at the source code: from top to bottom, with
jumps out to other code as required but always returning to this flow.</p>

<p>Once you start getting into callbacks and other evented paradigms it becomes
a lot more difficult to intuitively figure out what&#8217;s going on. Note that
I said &#8220;intuitively&#8221; because I&#8217;m sure that to anyone past the beginner
stage with Node would understand what is going on there.</p>

<p>There is a lot that looks familiar though. We have the importing of modules
via require statements. I also think this a good point to mention the
outstanding job that <a href="http://npmjs.org/">npm</a> does in helping to manage
dependencies. I used to think that the <a href="http://rubygems.org">Ruby gems</a> system
was one that should be aspired to. If you&#8217;re going to ignore <a href="http://www.cpan.org/">CPAN</a>
that is.</p>

<p>Then I create what I can only describe as an empty object, and then we
say that our Ticket object is going to inherit whatever is in EventEmitter.
Okay, so far so good in terms of familiar territory.</p>

<p>Next we have the block of code that lets us create a tick method for our
overall Ticker object, and this is done with a callback. We have scoping
issues to deal with (hence the self = this line) and we are creating something
that will emit a &#8216;tick&#8217; event once a second (1000ms). That looks a little
weird if you&#8217;re not used to callbacks, and we even have an anonymous function
inside an anonymous function.</p>

<p>After that we then grab a Ticker object, tell it that every time there is a
&#8216;tick&#8217; event detected we want to log TICK to the console. Finally, we tell
it to run myTicker.tick() and then it goes on it&#8217;s way emitting TICK once a second.</p>

<p>That&#8217;s not such a mind bender once you break it down the way I did. But what about
this type of thing:</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>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">fs</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;fs&#39;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="nx">fs</span><span class="p">.</span><span class="nx">open</span><span class="p">(</span><span class="s1">&#39;./a.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">fd</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">writeBuffer</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Buffer</span><span class="p">(</span><span class="s1">&#39;7&#39;</span><span class="p">);</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">bufferOffset</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">bufferLength</span> <span class="o">=</span> <span class="nx">writeBuffer</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">filePosition</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="nx">fs</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">fd</span><span class="p">,</span> <span class="nx">writeBuffer</span><span class="p">,</span> <span class="nx">bufferOffset</span><span class="p">,</span> <span class="nx">bufferLength</span><span class="p">,</span> <span class="nx">filePosition</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">written</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>            <span class="k">throw</span> <span class="nx">err</span><span class="p">;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;wrote &#39;</span> <span class="o">+</span> <span class="nx">written</span> <span class="o">+</span> <span class="s1">&#39; bytes&#39;</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>We all know that it is easy to write spaghetti code in PHP because of the fact
that it grew up on the web: it was a templating language with C library
wrappers. Very easy to just mash everything together. I see the same sort of
spaghetti possibilities there in Node, with the added bonus of callbacks
being mixed in there to make certain things difficult.</p>

<p>I understand what I did there: I am opening a file and then writing to it. The
way that I got to do it made me scratch my head. Writing to the file happens
as a callback that is executed as a result of opening up the file for
appending? Like I said before, that does not strike me as very intuitive
for programmers coming to Node with a little understanding of Javascript
and jQuery, which is probably most programmers&#8217; introduction to anonymous
functions.</p>

<p>But I do get it. You cannot get non-blocking IO without doing such gyrations
but I am concerned that sufficiently large Node applications will be, to put
it bluntly, a fucking nightmare to debug. Is this something that cannot be
moved away from if you choose to write programs using tools that suppoort
concurrency and/or evented concepts?</p>

<p>I&#8217;d love to hear what sort of best practices people are using in order to
tame the potential for Node applications degenerating into spaghetti hell.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How Not To Suck At PHP]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/02/03/how-not-to-suck-at-php/"/>
    <updated>2012-02-03T00:00:00-05:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/02/03/how-not-to-suck-at-php</id>
    <content type="html"><![CDATA[<p>Since I hadn&#8217;t blogged for a bit I thought I would ask my Twitter peeps what
they might want to hear my rant about. One of the more interesting suggestions
I got was from <a href="https://twitter.com/tnorthcutt">Travis Northcutt</a> who said
the following:</p>

<blockquote><p>&#8220;Got it. I have no formal CS background, but I&#8217;m trying to learn enough
to build (something). I know enough PHP to kind of, sort of get by
but am trying to solidify my foundation. So if I&#8217;m being selfish, write
a post to someone in my position who wants to not suck at building stuff.&#8221;</p></blockquote>

<p>This is actually a great question. Although the self-centered answer is
to start off by buying my <a href="http://grumpy-testing.com">book</a>, that isn&#8217;t
really the question that is being asked.</p>

<p>I thought about this question for a while and have some thoughts on what it
really means to know how to not suck at building things using PHP. In my
never even remotely humble opinion I think the key is to understand what
PHP is really good at.</p>

<p>I have spoken at length about my opinion that PHP won the web war because
it was so tightly coupled with HTML and very early on had support to run
with Apache via mod_php. However the world has changed, and the web war
itself has changed. I think I can say with a smirk that the web war is now
an asymmetrical one, where the expectations of behaviour by users is totally
different.</p>

<p>But PHP is still in a really good position because of its true nature:
PHP is glue. It&#8217;s glue that binds things together in
a way to quickly produce HTML output. There is a reason the LAMP stack (with
PHP as the &#8220;P&#8221;) was so successful: if you had to get data out of a database
and onto an HTML page there was no easier way.</p>

<p>One of the trends that I see in PHP is that the frameworks that sit on top
of PHP are starting to ask for influence over the development and direction
of PHP itself. I have mixed feelings about this. On the one hand, it is
good that people push for newer features in their chosen programming
language. I think the PHP core is not necessarily against these suggestions
but they move at different pace than the framework people. But my other
concern is that we don&#8217;t end up with a framework driving the language.</p>

<p>When you think of Ruby, unless you are in denial or a really old Ruby hacker,
you think of Rails. Without Rails Ruby does not get the push that it did
and continues to get. It&#8217;s a good language but it would&#8217;ve languished in
obscurity if not for DHH.</p>

<p>So what does this have to do with not sucking at building stuff in PHP? I
think it&#8217;s important to understand that you can build cool stuff without
relying on a framework. Heresy, I know. But if you keep thinking of PHP
as glue it totally makes sense. How many applications are REALLY just PHP?
They are <em>really</em> web server + data source + Javascript + CSS + HTML. Think
about that for a minute. PHP is the glue that binds all that together.</p>

<p>The danger for people like Travis who are starting out with PHP is that they
will get caught up with a mindset that a framework is the solution to their
problems. It is <em>one</em> solution to the problem of building a web application,
but failing to understand how to write PHP that behaves like glue will lead
to a lot of problems later. Because when the time comes for you to do something
that the framework did not anticipate you will be fucked. And you will be
incorrect when you blame the framework.</p>

<p>So my advice to Travis is that he should worry about learning to use PHP like
glue and correctly identify the problems he is trying to solve NOW instead of
worrying about the problems he might have to solve later. There will be time to
fix your problems. Some of those will be solved by using tools that are not
written in PHP, but PHP can still glue them together. NoSQL data sources.
Distributed workers. Queuing systems. Real-time document searching capabilities.
These are all things that are NOT written in PHP but it is extremely likely
that someone has written a library in PHP that can talk to them.</p>

<p>Hope that helps you Travis!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[PHPUnit aborted fix]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/01/17/phpunit-aborted-fix/"/>
    <updated>2012-01-17T00:00:00-05:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/01/17/phpunit-aborted-fix</id>
    <content type="html"><![CDATA[<p>A while back I ran into a problem on a server where PHPUnit kept <a href="http://stackoverflow.com/questions/3065563/phpunit-reporting-aborted-no-matter-what-tests-are-run/8898712">reporting
&#8220;aborted&#8221; no matter what tests were run</a>.
That was a pretty annoying bug. I never did find out what the problem was
as I moved onto other problems and chalked that error up to some undiagnosed
weirdness on that particular server.</p>

<p>From time to time I would get asked on Twitter if I had ever solved the
problem. My answer was always &#8220;no, and if <em>you</em> do solve it please let
met know how you fixed it.&#8221; Today, my friends, was the day.</p>

<p><img src="http://www.littlehart.net/atthekeyboard/images/phpunit-bug.png"></p>

<p>Kudos to <a href="https://twitter.com/demiankatz">Damian Katz</a> for coming up with a
solution. Without digging deeper into it, I would imagine that you could
alter the phpunit.xml file (if you are using one in your setup) to automatically
use the -dzend.enable_gc=0 option every time you run a test.</p>

<p>How did he figure it out? He tracked down <a href="https://bugs.php.net/bug.php?id=53976">this bug</a> that
seemed to be displaying the exact behaviour that I had reported way back when.
It seems odd that garbage collection would cause this problem, but perhaps it
was a perfect storm of lots of small tests causing over-active garbage collection
which in turn causes the PHP interpreter to abort whatever it is running.</p>

<p>This is another case where I would love someone with knowledge of PHP internals
to try and explain why it is happening.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[CodeMash 2012 Review]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/01/16/codemash-2012-review/"/>
    <updated>2012-01-16T00:00:00-05:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/01/16/codemash-2012-review</id>
    <content type="html"><![CDATA[<p>Last week I attended <a href="http://codemash.org">CodeMash 2012</a> as a speaker and
gave my talk on <a href="http://www.slideshare.net/chartjes/building-testable-php-applications">building testable PHP applications</a> (don&#8217;t forget to buy the <a href="http://grumpy-testing.com">book</a> that the talk inspired). Held in Sandusky, Ohio in the
cold of January it is an interesting conference. Unlike most conferences that
I attend, it is multi-disciplinary. There were talks on .NET, Java, Ruby, Python,
and two lonely talks about PHP.</p>

<p>For me, this is somewhat uncomfortable. It is only natural that people who use
certain tools tend to flock together at a conference. I joked that where were
only 3.7 PHP developers (with someone stating that the 0.3 part is part of someone
that I cut off for not writing tests) but the fact was that unless you knew
members of multiple communities you were going to have to be content to feel
a little excluded.</p>

<p>Anyway, I did have a really good time hanging out with my friends and making
some new ones. It&#8217;s a really good idea to talk to as many people as you can,
even they are not using the same tools you are using. You can gain some
perspective.</p>

<p>Let&#8217;s go over the talks I attended and my thoughts on them.</p>

<h1>It&#8217;s the Little Things (Brad Colbow)</h1>

<p><a href="https://twitter.com/bradcolbow">Brad</a> is a freelance graphic designer from the Cleveland area. He also does
an online comic called <a href="http://bradcolbow.com/archive/">The Brads</a> so, as expected,
his presentation was full of all sorts of awesome visuals. Also, I thought
that <em>I</em> talked fast, this guy made me look like one of the Ents from Lord of The
Rings.</p>

<p>The main take-away I got from his talk was that it never hurts to pay attention
to the little things. I realized that this holds true from both an UI/UX perspective
and in your code. A bunch of bad little things always turn out into one huge
bad thing.</p>

<p>His slides describing the blog posting he made about <a href="http://bradcolbow.com/archive/view/the_brads_why_drm_doesnt_work/?p=205">how crazy it was to check out an audio book from his local library</a> were awesome.</p>

<h1>Vagrant: Virtualized Environments Made Simple (Matt Stine)</h1>

<p><a href="https://twitter.com/mstine">Matt</a> took what he said was normally a 3 hour
tutorial and crammed it into a very informative 1 hour talk. It did help
that I had been considering the use of Vagrant anyway, but I got a better
picture of more practical uses of Vagrant. I did not know about <a href="http://www.ducea.com/2011/08/15/building-vagrant-boxes-with-veewee/">veewee</a>
as a tool to help you create your own Vagrant boxes.</p>

<p>This is of interest because I had a hankering to play around with an <a href="http://www.archlinux.org/">Arch Linux</a>
VM but found the existing base boxes out there, well, not working with the
version of Virtualbox I had installed. I think veewee will let me pursue
that further.</p>

<h1>Breaking the Sound Barrier with Node.js on Windows and Azure (Glenn Block)</h1>

<p>While sitting in this talk I took a picture and tweeted about how it was time
to make myself &#8220;personally uncomfortable&#8221; by sitting in a talk involving a
technology that I don&#8217;t use running on a platform I don&#8217;t use. What I was hoping
to find out was some more basic information on Node.js and why it is such a
big deal.</p>

<p>To be perfectly honest, the more I see Node.js the more I start to understand
the allure of it. What really bothers me is that so many people are gambling
their applications on a tool that hasn&#8217;t even reached 1.0 yet. I understand
the power of how Node.js gets things done. Evented + concurrency + JS is a platform that
has lots of appeal to people who maybe haven&#8217;t done that sort of work before.
Not that I&#8217;m including myself in the group that &#8220;gets it&#8221; but at this point
in time I&#8217;m still on the &#8220;I will use Twisted/Tornado if I need to create event-driven,
high-concurrency web servers.&#8221;</p>

<p>Maybe when Node.js gets to 1.0 I will think differently.</p>

<p>Azure itself didn&#8217;t really thrill me that much, but I do see the advantage of
that environment to people who have already made a large investment on the
server side in Microsoft. What is really admirable is that they are making
some great contributions back to the open source community. The SDK for running
Node.js on Windows is open source, and I must give them credit for that.</p>

<h1>Code Kata and Analysis (Jim Weirich)</h1>

<p>This was a talk that I went into expecting to be surrounding by Ruby hipsterism.
After all, this is the guy who created <a href="http://rake.rubyforge.org/">Rake</a> and works
for a well-known <a href="http://edgecase.com/">Ruby consultancy</a>. Once I saw him I
knew I was in for a treat. Older guy. White hair. Unix-style beard. Runs emacs.</p>

<p>I was not disappointed.</p>

<p>A code kata is a programming exercise that serves a few purposes. You do it
in order to learn how to implement common algorithms in different languages
and learn how to solve the easy problems by habit, with the goal of then
freeing up your mind to solve hard problems.</p>

<p>The code kata this time was to create an arabic number to Roman numerals
converter. The language of choice was Ruby (no big surprise there) and I
was even happier to see that it was a test-driven kata using <a href="http://rspec.info/">Rspec</a>.</p>

<p>This brings me around to a topic that makes me super uncomfortable. It seems
to me that testing has become an integral part of the Ruby and Python ecosystems.
It does not appear to be common at all in the PHP and .NET communities. I care
about this sort of stuff so it does pain me to see such opposition in the form
of apathy and excuses amongst PHP programmers. I wish I could surround myself
with developers who feel like I do about the value of automated testing in
delivering solid applications. Maybe in the next life.</p>

<p>It was also good to see someone walk you through the whole TDD practice <em>and</em>
refactor stuff after getting a particular set of tests working. In the end
we had two great examples of code: an easy-to-understand converter and tests
that are easy to understand.</p>

<h1>Dealing with Information Overload (Scott Hanselman)</h1>

<p>One of the more dynamic speakers I saw, <a href="https://twitter.com/shanselman">Scott</a> gave
a talk to a packed room on tips for dealing with information overload. From him
I learned some new techniques I&#8217;m going to be phasing in:</p>

<ul>
<li>No reading email until noon, and then &#8220;office hours&#8221; from a certain time onwards when
anyone can speak to you</li>
<li><a href="http://www.instapaper.com/">Instapaper</a> is awesome because I can flag things to read later <em>and</em> get them
delivered to my Kindle on Friday afternoons for some after-dinner reading</li>
<li><a href="http://www.ifttt.com/">If this, then that</a> is an extremely interesting tool
that can be used to trigger events that help you delegate things into the future</li>
<li>The <a href="http://www.pomodorotechnique.com/">Pomodoro Technique</a> is a very useful
tool to help with short-term focus. Cut out the distractions and concentrate
for 25 minutes. Even I think I can do that.</li>
<li>Keep your email responses very short (he reccomends <a href="http://five.sentenc.es/">Five Sentences</a>) and
instead write a longer response to that issue and post it somewhere others can
find it, whether it is a company wiki or on your own blog. That way, next time
someone asks you that same question you can point them there</li>
</ul>


<p>Excellent talk full of lots of excellent ideas. I knew it was gonna be awesome
when he put <a href="http://www.funnyordie.com/videos/e685c7e4ff/literal-video-total-eclipse-of-the-heart">this video up</a>
to entertain us before his talk.</p>

<h1>A Few of My Favorite [Python] Things (Mike Pirnat)</h1>

<p><a href="http://www.twitter.com/mpirnat">Mike</a> is a long-time Python programmer who
shared with us a &#8220;series of lightning talks crammed into one presentation&#8221;.
While I have some basic Python knowledge I did see a bunch of really interesting
libraries and other tools that I did not know about. He covered so much, so fast
that I was glad he was going to post a link to the slides on his blog.</p>

<p>It also reminded me how, coming from the PHP world, languages like Ruby and Python
really are more terse. You might not think it matters (or even hate significant
whitespace) but for some people the elegance that can be found in Ruby and
Python code often makes it easier for others to understand what you are doing.
I know that I <em>feel</em> like I&#8217;m doing more with less when I write Python code.</p>

<p>Then I gave my <a href="http://www.slideshare.net/chartjes/building-testable-php-applications">own talk</a>.
I was disappointed that I actually ran out of time(!) for it. In the scheme of
things, 90 slides is about 20 too many for a talk on as dense a topic as unit
testing tools, strategies for writing code, and complementary tools to make the
whole thing easier.</p>

<p>I have identified abotu 20 slides that I think I could chop out and turn into
2 slides of the &#8220;and if you&#8217;re interested in some tools that I think are
helpful&#8221; type.</p>

<p>I had a good time (including hanging out at the Kalahari&#8217;s water park with my
kids) at CodeMash 2012 and hope to go back for CodeMash 2013.</p>

<h1>Why so little PHP?</h1>

<p>I was asked about why I felt that there were so few talks about PHP and what
could be done to make it better. Well, when only 7 people even submit PHP
ideas I think there is a problem of visibility.</p>

<p>Given that this conference covers a wide variety of technologies I think it
is safe to say that there was quite a bit of overlap. 31 .NET talks? Really?
There is nothing wrong with .NET but do you <em>really</em> want to exclude an entire
sector of the web programming world at your conference?</p>

<p>Also, there is the issue of speaker reimbursement I know that the official
policy of CodeMash is &#8220;we cannot guarantee speaker reimbursement&#8221;. That&#8217;s cool
but that will also stop talented people who are not willing to travel from
where they are to the Cleveland airport and then hitch a ride to the Kalahari.
Maybe in the PHP world we have gotten spoiled by those who run conferences
since they tend to cover a significant portion of the costs a speaker might
incur to get there.</p>

<p>There is no easy answer to how to get more PHP people to submit talks and to
attend the conference in general. It probably also doesn&#8217;t help that the
conference sold 1200 tickets in 20 minutes, making it that much harder to
get a ticket.</p>

<p>I&#8217;d love for more PHP folks to come to this conference and get their horizons
expanded with a glimpse at what other communities, just as full of energetic
people but with different focusses, are doing.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Better Remote Code Development]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/01/05/better-remote-code-development/"/>
    <updated>2012-01-05T00:00:00-05:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/01/05/better-remote-code-development</id>
    <content type="html"><![CDATA[<p>I am struggling to find a better way to handle a new paradigm in development.
At <a href="http://moontoast.com">Moontoast</a> all our servers are up on EC2, which
includes the two dev servers (one for each project) that I use every day.</p>

<p>I&#8217;ve been playing around with <a href="http://www.sublimetext.com">Sublime Text</a>  (because of the awesome vim bindings)
and was thinking about why do I always have to be logged into the remote
server to do my work. Normally I connect using SSH, then attach to my <a href="http://tmux.sourceforge.net">tmux</a>
session and then fire up vim.</p>

<p>This is okay but it lacks a certain elegance in it&#8217;s approach. As a result
I end up using things like <a href="http://dropbox.com">Dropbox</a> to synchronize my
.vimrc file so I get a consistent experience across environments. That&#8217;s ok
for now, although I&#8217;m having some weirdness due to vim 7.2 vs. vim 7.3 issues
in one of those environments.</p>

<p>I also need to be able to use git to commit changes, create and merge branches, etc.
I need to be connected to the server to make that happen. But that is starting
to feel like a limitation to me.</p>

<p>What I&#8217;m chafing against is having to deal with multiple environments all the time
to get my work done. I want to use ONE editor, tweaked out exactly the way I want
to edit code in multiple locations. I want to use ONE tool for handling git work
in multiple locations.</p>

<p>Any thoughts on how to achieve what I want? There are occasions when I do need to
connect to a server (say to tail a log while testing a Gearman job) but damnit
it&#8217;s the second decade of the 21st century. This should be a SOLVABLE problem.</p>

<p>So for now I&#8217;m trying out SublimeText2 as my main editor and started using Transmit
(sorry Expandrive but I am finding Transmit to be a lot more responsive) so the
missing piece if for me to be able to commit code changes on a remote server
WITHOUT being logged into that remote server.</p>

<p>In a perfect world I am using virtual machines for my dev work, but I haven&#8217;t convinced
our awesome sysadmin to put in the time necessary to make it happen for me.</p>

<p>Let me know potential solutions for my desired setup.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[The Grumpy Programmer's Guide To Building Testable PHP Applications]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2012/01/03/grumpy-testing/"/>
    <updated>2012-01-03T00:00:00-05:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2012/01/03/grumpy-testing</id>
    <content type="html"><![CDATA[<p>After the middling success of my first <a href="http://www.littlehart.net/book">programming book</a>
I had been thinking about writing another book. When the end of October rolled
around and <a href="https://en.wikipedia.org/wiki/National_Novel_Writing_Month">National Novel Writing Month</a>
was about to happen I decided to take the plunge and write something.</p>

<p>It didn&#8217;t turn out to be The Great Canadian Novel but I did collect my thoughts on
what goes into building PHP applications in such a way that they are easily
testable. I called it &#8220;The Grumpy Programmer&#8217;s Guide To Building Testable PHP Applications&#8221;.</p>

<p>When I wrote my first book I did everything in <a href="http://www.apple.com/iwork/pages/">Pages</a> but
that was before the rise of e-readers. This time I wanted to offer my book in
PDF, epub and mobi formats. So I started up using <a href="http://www.methods.co.nz/asciidoc/">AsciiDoc</a>
since I felt it gave me the most options for converting the text into other formats
with a little bit of programming work. I created a Makefile (old school, baby!)
that took the text and turned it into a PDF.</p>

<p>I had thought about doing the book directly in <a href="http://docbook.org/">Docbook</a> as
it provided the best way for conversion purposes, but I found all the XML to
be getting in the way of, you know, actually writing the thing. Plain text
with a little bit of mark-up seemed to be the best fit for my brain.</p>

<p>As my self-imposed deadline got closer I felt like I was in for a lot of late
nights fiddling around with layouts to get things to an acceptable level. Lucky
for me an alternative came along.</p>

<p>I follow Reg Braithwaite (better known as @raganwald) and saw that he had a new
ebook out called <a href="http://leanpub.com/shippingsoftware">&#8220;What I&#8217;ve Learned From Failure&#8221;</a> which he published with some
help from <a href="http://leanpub.com">Leanpub</a>. The system looked awesome: write your
book in Markdown (which I already use for this blog) and then it would create
ebooks in PDF/epub/mobi for you, handling all the distribution and payment
collection for you. Holy fuck, this is what I wanted.</p>

<p>So I took the plunge and signed up for an account. I started tweaking my text
to use the Markdown-specific formats the system requires and little by little
the book is being massaged into it&#8217;s new shape.</p>

<p>I&#8217;ve gotten some great feedback from my technical reviewers and will be
incorporating those changes into the book as soon as I&#8217;ve converted it over
to the Leanpub structure. I&#8217;m giving a talk on the topic the book covers at
<a href="http://codemash.org">Codemash 2012</a> and will launch the book while I&#8217;m
there, offering those who attend my talk a discount as a way of thanking
them for travelling to the shores of Lake Erie in the middle of winter.</p>

<p>So please visit the <a href="http://leanpub.com/grumpy-testing">home of my book online</a>
and until the book is published feel free to add your name to the mailing list.
As soon as it&#8217;s available you will be notified. I&#8217;m still tweaking the pricing
so feel free to suggest to me how much you&#8217;d be willing to pay for such a beast.
It will probably come in between 40 and 50 pages in length.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[The Year of the Struggle]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2011/12/30/the-year-of-the-struggle/"/>
    <updated>2011-12-30T00:00:00-05:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2011/12/30/the-year-of-the-struggle</id>
    <content type="html"><![CDATA[<p>For those who know me outside of the context of my ranty tweets and
occasionally-lucid blog posts you will know that I am an avid simulation
baseball game player. I&#8217;m about to start my 15th season in my current
league and it has taught me a great many lessons about myself and the importance
of having a set of skills that turn independent bits into a cohesive team.</p>

<p>(If you care the league uses a game that it creates itself using freely-available
data from <a href="http://retrosheet.org">Retrosheet</a> and if you&#8217;ve ever come across
<a href="http://www.strat-o-matic.com/products/baseball">Strat-O-Matic-Baseball</a> you might
have an idea of how game-play works)</p>

<p>I&#8217;ve had middling success in that league: some really crappy seasons where I
clearly did not know what I was doing along with making the playoffs 3 years
out of 4 complete with a trip to our league champsionship series. Many times
I&#8217;ve had teams where the skills of the players (who are merely statistical
representations within the confines of the game rules) simply aren&#8217;t a good
fit.</p>

<p>If you have lots of power, a good bullpen, but guys who don&#8217;t get on-base
you will find yourself winning by a lot or losing by a lot because
power comes and goes. If you have great pitching but a bad offense (similar
to what I have for our upcoming 2012 season) then you will find yourself
in many close games but you will probably lose more of them than you win
because your offense will be unable to come through when needed.</p>

<p>I&#8217;ve also learned that I am a decent field manager (in terms of playing the
games and understanding strategies), lousy at evaluating hitters in our
draft (my past 4 first-round picks have been hitters who have been injured
and/or underachieved), great at evaluating pitchers, and very impatient
in the trade market when trying to improve my team.</p>

<p>I went through a terrible season last year due to a combination of bad luck
and impatience in making trades to keep a fringe playoff contender in the
playoff hunt in 2010, then crashing to lose almost 120 games in 2011.
So I struggle to find ways to build a better team, and better identify the
combination of skills I need for my team to achieve success.</p>

<p>So what does all this have to do with programming? I feel that my current
set of skills are a terrible match for today&#8217;s programming environment and
it&#8217;s making me feel a set of emotions I thought I ditched when I left high
school: jealousy, fear, angst and regret.</p>

<p>I&#8217;ve always prided myself in thinking that I have spotted trends in programming
before everyone else. I first saw the value of frameworks in 2003-2004. About
the same time I discovered SimpleTest and thought that it would be a very
valuable tool. It&#8217;s almost 2012 and I still feel the same way, but I feel like
the programming community I belong to still doesn&#8217;t give a shit.</p>

<p>In February of this year I jumped on an opportunity to work with some people
I respect in the PHP community and joined Moontoast. It had been a while
since I had done the startup thing and thought it would be a good change for
me. At my previous employer I had to do everything myself for my projects and
I looked at Moontoast as an opportunity to work with a team again.</p>

<p>Instead I struggled. Struggled to cope with two applications written using
heavily-modified versions of two frameworks. Struggled to understand the
business that relied on these applications. Struggled with the idea that
the things I felt really, REALLY mattered simply don&#8217;t matter to the
business as a whole. It&#8217;s not a lie when I say it hurt. Hurt a lot to miss
deadlines and let down team members. Hurt a lot to struggle to make things
like automated testing and simpler product development FUCKING WORK and not
achieve anything. Struggled while wondering if I was getting honest feedback
from fellow employees who might not want to really say what they feel because
they are not an asshole like I often am.</p>

<p>On top of that, I felt like the volume of tweets and blog posts I cranked
out achieved nothing that I could hang my hat on and make my ego stop
screaming at me in my head. I still care immensely about best practices,
automation, and building applications that can be easily tested. All too often
I feel like I am the only one who wants it so bad I pound my fists on my
desk like a little boy having a fucking temper tantrum.</p>

<p>The feedback loop I want just isn&#8217;t there any more. Perhaps my troll-like
existence in my home office is wearing me down and making it more difficult
for me to actually get along with people. My ego is getting in the way of
success in a team setting and I am struggling to find a way to figure out
how to change things for the better.</p>

<p>PHP is the tool I&#8217;ve been using pretty much non-stop since 1998. It won the
web battle. It really did. But things are changing and expectations are
changing amongst the next wave of programmers. Elegance in code matters.
Ability to support concurrency at the code level matters. Perhaps it shouldn&#8217;t
but it does. I think of all the investment I&#8217;ve made in PHP and I find myself
constantly asking the question &#8220;did you do the right thing?&#8221;</p>

<p>I have found that the PHP community is very supportive of me and my ideas, so
I&#8217;m probably not as terrible a person as I make myself out to be. But the PHP
community as a whole really seems to think that much of the stuff I consider
to be important is just a waste of time, getting in the way of getting things
done so you can go home after work.</p>

<p>The crowd I follow online is a self-selecting one. Most of them care about
the same things (except there are those who disagree with me on the importance
of automated testing as a critical part of development) but what about those
who aren&#8217;t there? How many of them give a shit about clear, concise code? How
many of them have actually created an application for their employer that
had a complete testing suite (in 14 years I&#8217;ve been able to do that ONCE)
for it? How many of them have automated their deployments as a result of
a merge of code in their version control system?</p>

<p>It&#8217;s more likely that I am being selfish and demanding recognition for the
things I&#8217;ve done and don&#8217;t really deserve that recognition. I joke with
my wife that I am a &#8220;big deal on the Internet&#8221; and #internetfamous on Twitter,
but am I really? Or am I just loud and repeat myself over and over again.
Am I just being tolerated or are there people for whom my thoughts are
resonating and they are going out and kicking ass in a way I have been unable
to?</p>

<p>Maybe this old saying is true: those who can, do, and those who can&#8217;t, teach.</p>

<p>2011 was the year of the struggle for me. I don&#8217;t want 2012 to be like that
but I am not sure how because I&#8217;m scared that I&#8217;m becoming useless and
irrelevant and that the things I am passionate about have no place in the
modern PHP community, the only one I&#8217;ve ever really been a part of.</p>

<p>Hope you have a good 2012. I&#8217;ll be here, struggling.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Better HTTP Request/Response in PHP]]></title>
    <link href="http://www.littlehart.net/atthekeyboard/2011/12/12/better-http-request-response-in-php/"/>
    <updated>2011-12-12T00:00:00-05:00</updated>
    <id>http://www.littlehart.net/atthekeyboard/2011/12/12/better-http-request-response-in-php</id>
    <content type="html"><![CDATA[<p>I just started up a podcast called <a href="http://devhell.info">Developer Hell</a> with
my fellow piss-and-moan-driven-development practitioner <a href="http://funkatron.com">Ed Finkler</a>
. It basically consists of Ed and I sitting down and just having an open and
unedited discussion about whatever programming-related topic we feel like
rambling on and on about. Our first episode covered the always-riveting
topic of <a href="http://devhell.info/post/2011-12-10/what-we-hate-about-php/">&#8220;What We Hate About PHP&#8221;</a>.</p>

<p>Now even though it is just us complaining about stuff in PHP that we don&#8217;t
like, regardless of the true merits of our complaints, it sparked a discussion
on Twitter about just what should be done to make handling PUT and DELETE
requests better in PHP.</p>

<p>$_POST and $_GET are workhorses of the PHP world, used in any application
that accepts web requests from somewhere. Sometimes accessing them is wrapped
inside objects or filtered out using the filter() extension of even <a href="https://github.com/funkatron/inspekt">Inspekt</a>
but I think this illustrates the problem.</p>

<p><a href="http://twitter.com/lxt">Laura Thompson</a> made the suggestion that PHP should
look to port the concept of the Request object that Python uses over to PHP.
As a cryptopythonista I think that is an excellent idea. <a href="http://twitter.com./auroraeosrose">Elizabeth Smith</a>
did ask for some constructive ideas on solving this problem. While my C skills
are non-existent, I thought I&#8217;d delve a bit into more higher-level thoughts
about this.</p>

<p>I think the fact that we have $_POST and $_GET lulls some of us into the
false sense that we should have $_PUT and $_DELETE objects, since that
would map to the commonly-desired set of HTTP verbs that REST likes to use.
But what should be inside those things, or should we be moving towards a more
Pythonesque solution where a Request object, as part of core or via a only-really-for-the-brave-
PECL extension?</p>

<p>What I&#8217;m really after is for an easy way to detect that a PUT or DELETE
request has been made. Existing solutions for these are not pretty and (
in my opinion) non-intuitive. With my thoughts that PHP is quickly turning
into a tool used for web service API end points that don&#8217;t require insane
levels of concurrency, the idea that there is no native support for figuring
out if you&#8217;re been given a PUT or DELETE request is disheartening.</p>

<p>The <a href="http://www.php.net/http">PECL HTTP extension</a> is a step in the correct
direction, but it too has some rough edges and would require a lot of
polishing before it could be moved into the core where it really belongs.</p>

<p>Again, what I&#8217;m looking for is an easy way for PHP to be able to know what
type of request it is dealing with so I can make my PHP applications more
aware of what it being asked of them. It seems that pretty much every
framework implements their own Request/Response objects, so that should be an
indication that this kind of code abstraction is a desired core PHP feature.</p>

<p>Share your thoughts with me in the comments.</p>
]]></content>
  </entry>
  
</feed>

