<?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>Internet Strategy Guide &#187; unittesting</title>
	<atom:link href="http://phpprotip.com/category/php/unittesting/feed/" rel="self" type="application/rss+xml" />
	<link>http://phpprotip.com</link>
	<description>Together we can defeat the internet</description>
	<lastBuildDate>Tue, 08 Nov 2011 06:19:18 +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>expected exceptions annotations, mocked object calls, oh my.</title>
		<link>http://phpprotip.com/2010/12/expected-exceptions-annotations-mocked-object-calls-oh-my/</link>
		<comments>http://phpprotip.com/2010/12/expected-exceptions-annotations-mocked-object-calls-oh-my/#comments</comments>
		<pubDate>Fri, 24 Dec 2010 15:06:11 +0000</pubDate>
		<dc:creator>chance</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phpunit]]></category>
		<category><![CDATA[unittesting]]></category>
		<category><![CDATA[Exception handling]]></category>
		<category><![CDATA[Mock object]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://phpprotip.com/?p=490</guid>
		<description><![CDATA[Note: I have tested this in PHPUnit 3.4.1 and haven't tried it out in 3.5. Anyone who has worked with PHPUnit has most likely worked with expected exceptions and mock objects. The nice thing about working with expected exceptions is that we have access to a handy @expectedException annotation. I've gotten into the habit of [...]]]></description>
			<content:encoded><![CDATA[<p>Note: I have tested this in <a class=\"zem_slink freebase/en/phpunit\" title=\"PHPUnit\" rel=\"homepage nofollow\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5waHB1bml0LmRlLw==">PHPUnit</a> 3.4.1 and haven't tried it out in 3.5.<br />
Anyone who has worked with PHPUnit has most likely worked with expected <a class=\"zem_slink freebase/en/exception_handling\" title=\"Exception handling\" rel=\"wikipedia nofollow\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9FeGNlcHRpb25faGFuZGxpbmc=">exceptions</a> and mock objects. The nice thing about working  with expected exceptions is that we have access to a handy @expectedException annotation. I've gotten into the habit of using this for exceptions my fixtures should throw but also for when I'm using a <a class=\"zem_slink freebase/en/mock_object\" title=\"Mock object\" rel=\"wikipedia nofollow\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9Nb2NrX29iamVjdA==">mock object</a> to verify a method call. So my tests usually expect foo_exception for fixture throws and when i'm testing method calls via a mock, they expect Exception. Therein lies my problem. Because all my custom class exceptions obviously extend the Exception class, I can get some false positives in testing.</p>
<pre class="php">equire_once <span style="color: #ff0000;">'Zend/Loader/Autoloader.php'</span>;
<span style="color: #0000ff;">$loader</span> = Zend_Loader_Autoloader::<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'foo.php'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #006699; font-weight: bold;">class</span> tmpTest <span style="color: #006699; font-weight: bold;">extends</span> PHPUnit_Framework_Testcase
<span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * @expectedException Exception
     */</span>
    <span style="color: #006699; font-weight: bold;">public</span> <span style="color: #006699; font-weight: bold;">function</span> testFooBar<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$foo</span>=<span style="color: #006699; font-weight: bold;">new</span> foo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$foo</span>-&amp;gt;bar<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * @expectedException Exception
     */</span>
    <span style="color: #006699; font-weight: bold;">public</span> <span style="color: #006699; font-weight: bold;">function</span> testBarBaz<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$mock</span>=<span style="color: #0000ff;">$this</span>-&amp;gt;getMock<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'foo'</span>,<a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5waHAubmV0L2FycmF5"><span style="color: #006666;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'baz'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$mock</span>-&amp;gt;expects<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&amp;gt;any<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         -&amp;gt;method<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'baz'</span><span style="color: #66cc66;">&#41;</span>
         -&amp;gt;will<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&amp;gt;throwException<span style="color: #66cc66;">&#40;</span><span style="color: #006699; font-weight: bold;">new</span> Exception<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'baz'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$mock</span>-&amp;gt;barbaz<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<pre class="php"><span style="color: #006699; font-weight: bold;">class</span> foo_exception <span style="color: #006699; font-weight: bold;">extends</span> Exception<span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #006699; font-weight: bold;">class</span> foo
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #006699; font-weight: bold;">public</span> <span style="color: #006699; font-weight: bold;">function</span> bar<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        throw <span style="color: #006699; font-weight: bold;">new</span> foo_exception<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'bar'</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #006699; font-weight: bold;">public</span> <span style="color: #006699; font-weight: bold;">function</span> baz<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5waHAubmV0L2VjaG8="><span style="color: #006666;">echo</span></a> <span style="color: #ff0000;">&quot;bwah<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #006699; font-weight: bold;">public</span> <span style="color: #006699; font-weight: bold;">function</span> barbaz<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$this</span>-&amp;gt;bar<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$this</span>-&amp;gt;baz<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>So here we have an expectation for Exception but if we look at the code, we see that the bar method throws a foo_exception and the testBarBaz test is trying to test for the baz call via a mock that throws an Exception. if we change the annotation to expect foo_exception, the test still passes. This leads me to believe the best way to isolate the behavior we wish to test is to not use annotation for these sorts of tests. Or if you want to use annotation, be sure to use a unique exception for the mock. This means, unfortunately for me, that I'll have to go back through all my tests and ensure there's no false positives.</p>
<p>Lesson learned: be careful using shortcuts (and don't stand in the fire).</p>
<p>On a side note, <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cHM6Ly9naXRodWIuY29tL3NlYmFzdGlhbmJlcmdtYW5uL3BocHVuaXQvYmxvYi8zLjUvUEhQVW5pdC9GcmFtZXdvcmsvVGVzdENhc2UucGhwI0w3NDM=">this part of PHPUnit</a> is why those tests will behave that way. The behavior is completely my fault but I wanted to confirm it was behaving because of how it was verifying the expected exception.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class=\"zemanta-pixie-a\" title=\"Enhanced by Zemanta\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy56ZW1hbnRhLmNvbS8="><img class="zemanta-pixie-img" style="border: none; float: right;" src="http://img.zemanta.com/zemified_c.png?x-id=d53ab02f-d978-4960-8750-b7ee8c46327b" alt="Enhanced by Zemanta" /></a><span class="zem-script more-related more-info pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
 <img src="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=490" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://phpprotip.com/2010/12/expected-exceptions-annotations-mocked-object-calls-oh-my/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>visibility and inheritance.</title>
		<link>http://phpprotip.com/2009/08/visibility-and-inheritance/</link>
		<comments>http://phpprotip.com/2009/08/visibility-and-inheritance/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 15:16:40 +0000</pubDate>
		<dc:creator>chance</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[phpunit]]></category>
		<category><![CDATA[theory crafting]]></category>
		<category><![CDATA[unittesting]]></category>
		<category><![CDATA[web dev]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[inheritance]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[visibility]]></category>

		<guid isPermaLink="false">http://phpprotip.com/?p=164</guid>
		<description><![CDATA[An interesting topic came up in #phpc today. It revolved around some issues I've been encountering in my latest code designs/structures. It also leads into some side topics that I will attempt to explore. From my point of view, the discussion centered around what is the best 'default' visibility to use for methods. Another thing [...]]]></description>
			<content:encoded><![CDATA[<p>An interesting topic came up in #phpc today. It revolved around some issues I've been encountering in my latest code designs/structures. It also leads into some side topics that I will attempt to explore.</p>
<p>From my point of view, the discussion centered around what is the best 'default' visibility to use for methods. Another thing touched upon is the <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9PcGVuL2Nsb3NlZF9wcmluY2lwbGU=">Open/Closed principle</a>, which I think I subscribe to or may subscribe to(this depends on my ability to determine what half of the words in the entry mean).</p>
<p>Out of the whole discussion, here is the points I got (aka understood) out of it. Please correct me in the comments if I'm off base in any way.</p>
<p><strong>Methods should only be public when necessary. This is to help reduce the amount of side-effects that can occur because of method overrides.</strong></p>
<p>K, I can accept that and in thinking about my past code, I use to use protected more than private. I used public very sparingly.<br />
Now I find, since I started unit testing, that I have a large amount of public functions than I've had in the past. That is because I can't figure out how to test private methods. One way that I can think of is to create public methods that allow you to test the private ones. Unfortunately, this makes me wonder why the method isn't public to begin with since it seems redundant and wasteful to have these public methods to access private methods.<br />
Please note the key word methods, property accessors are a different story.<br />
The best solution (I can think of) to testing private methods is Mock Objects. Unfortunately, even though I've started to use mocks/doubles more, I'm unsure if my implementation is correct. Until I'm confident in my understanding of mocks/doubles, I worry about having false positives in test results.</p>
<p><strong>Another assertion that was made in the discussion was that private methods allow you to preserve the class' core functionality.</strong><br />
Unfortunately, no matter what the visibility of the method is, you're able to override it (and potentially mess with the core functionality you were trying to preserve).<br />
Example:</p>
<pre>
class foo{
    private function foobar() {
        echo "foo\n";
        return "foo foo\n";
    }

    public function bar() {
        echo "w00t ";
        return $this->foobar();
    }
}

class bar extends foo{
    private function foobar() {
        return "bar\n";
    }

    public function baz() {
        echo $this->bar();
    }

    public function wut() {
        echo $this->foobar();
    }
}

$f=new bar();
$f->baz();
$f->wut();
</pre>
<p>By running the above code, you get:</p>
<pre>
w00t foo
foo foo
bar
</pre>
<p>If preservation of core functionality is your main concern, then you're better off using <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3VzMi5waHAubmV0L21hbnVhbC9lbi9sYW5ndWFnZS5vb3A1LmZpbmFsLnBocA==">final</a>.</p>
<p>So after all that rambling, you're probably wondering what I'm trying to get to. It still comes down to visibility's effect on inheritance. The way I see it, unless you declare the method as final, you can't lock down the parent functionality because private methods can still be overridden. Trying to figure out what level of visibility for a method is a situational call. There is no correct 'default' visibility. Sure private is safer because of <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2h0dHA6Ly9lbi53aWtpcGVkaWEub3JnL3dpa2kvUHJpbmNpcGxlX29mX2xlYXN0X3ByaXZpbGVnZQ==">least privilege</a> but it makes testing a bear (or at least a bear at my current skill level in testing). Public potentially opens you up for abuse or misuse.</p>
<p>What I would still like to know is, how do you do class method visibility? How does that affect your testing methodology? Can someone give me an example (that isn't a singleton) where private is a better choice over protected? I like protected because it seems less limiting to me and my current coding style appreciates that degree of flexibility.</p>
 <img src="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=164" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://phpprotip.com/2009/08/visibility-and-inheritance/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

