<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LAMPlights &#187; testing</title>
	<atom:link href="http://www.hermanradtke.com/blog/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hermanradtke.com</link>
	<description>Personal anecdotes from my experiences using the LAMP stack</description>
	<lastBuildDate>Wed, 25 Jan 2012 18:14:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Unit Testing and the Law of Demeter</title>
		<link>http://www.hermanradtke.com/blog/unit-testing-and-the-law-of-demeter/</link>
		<comments>http://www.hermanradtke.com/blog/unit-testing-and-the-law-of-demeter/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 16:34:39 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unittest]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=287</guid>
		<description><![CDATA[I was writing some code today and not using Test-Driven development.  The reason was that I did not have a good understanding of what I was writing, so I decided to write some of the guts before writing the tests.  In the process of writing the guts, I recognized that I was paying very close [...]]]></description>
			<content:encoded><![CDATA[<p>I was writing some code today and not using <a class="zem_slink" title="Test-driven development" rel="wikipedia" href="http://en.wikipedia.org/wiki/Test-driven_development">Test-Driven development</a>.  The reason was that I did not have a good understanding of what I was writing, so I decided to write some of the guts before writing the tests.   In the process of writing the guts, I recognized that I was paying very close attention to how I was going to later test each of the methods I was writing.  I was paying especially close attention to the <a class="zem_slink" title="Law of Demeter" rel="wikipedia" href="http://en.wikipedia.org/wiki/Law_of_Demeter">Law of Demeter</a>.   The idea behind the Law of Demeter is to keep units of code distinct from one another.  So how did this relate to my code?   To put it simply, my business logic methods did not use get methods.<br />
<span id="more-287"></span></p>
<p>Assume we have a person class.  The class constructor takes the persons full name and we have a getter and setter for the full name.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Person
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$fullName</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fullName</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFullName</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fullName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setFullName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fullName</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_fullName <span style="color: #339933;">=</span> <span style="color: #000088;">$fullName</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFullName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_fullName<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There is also a method that parses apart the persons full name into separate parts.  The naive approach:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> parseFullName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$nameParts</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFullName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #339933;">...</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This code works fine, but think about how we will write a test for this function.  We have to first set the full name using a setter so the parseFullName method can retrieve that value with a getter.  This violates one of the principle rules of unit testing: testing individual units of code.  If there is a bug in the getter or setter functions, they may inadvertently affect my test.  This is a very real issue when using the magic __get and __set methods.  It also means more work to setup your tests because you have to keep in mind an order of operations when testing.</p>
<p>The better approach:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> parseFullName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fullName</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$nameParts</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fullName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #339933;">...</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Notice how the parseFullName function is being <em>told</em> what to parse rather than <em>asking</em> what to parse.  This subtle change now allows us to truly test this individual unit of code with the least amount of outside environment interaction.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/6314f3a0-e1d8-4018-a07a-9b9cb50489c9/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/reblog_e.png?x-id=6314f3a0-e1d8-4018-a07a-9b9cb50489c9" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/unit-testing-and-the-law-of-demeter/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

