<?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; unit testing</title>
	<atom:link href="http://phpprotip.com/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://phpprotip.com</link>
	<description>Together we can defeat the internet</description>
	<lastBuildDate>Sat, 11 Feb 2012 01:39: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>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>tekx &#8211; continuous inspection and integration of PHP projects</title>
		<link>http://phpprotip.com/2010/05/tekx-continuous-inspection-and-integration-of-php-projects/</link>
		<comments>http://phpprotip.com/2010/05/tekx-continuous-inspection-and-integration-of-php-projects/#comments</comments>
		<pubDate>Wed, 19 May 2010 23:06:59 +0000</pubDate>
		<dc:creator>chance</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[phptek]]></category>
		<category><![CDATA[tekx]]></category>
		<category><![CDATA[Build automation]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Enterprise application integration]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://phpprotip.com/?p=251</guid>
		<description><![CDATA["Countinous Integration is about preventing your developers from burning in Integration Hell" - @s_bergmann Integration Hell My code is perfect, yours is pretty good and then Martin...well, he's a fucking idiot. (lost where he was going with this story). D= Team member should integrate their work frequently. This reduces errors when managing multiple team members. [...]]]></description>
			<content:encoded><![CDATA[<p>"Countinous Integration is about preventing your developers from burning in Integration Hell" - @<a class=\"zem_slink\" title=\"Sebastian Bergmann\" rel=\"blog\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3NlYmFzdGlhbi1iZXJnbWFubi5kZS8=">s_bergmann</a></p>
<h2>Integration Hell</h2>
<p>My code is perfect, yours is pretty good and then Martin...well, he's a fucking idiot. (lost where he was going with this story). D=</p>
<p>Team member should integrate their work frequently. This reduces errors when managing multiple team members. The value (ROI) is in:</p>
<ul>
<li>reducing risk</li>
<li>reduce repetitive processes</li>
<li>generate deployable <a class=\"zem_slink freebase/en/computer_software\" title=\"Computer software\" rel=\"wikipedia\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9Db21wdXRlcl9zb2Z0d2FyZQ==">software</a></li>
<li>enable better project visibility</li>
<li>establish greater product confidence</li>
<li>helps with late discovery of defects
<ul>
<li><a class=\"zem_slink freebase/en/regression_testing\" title=\"Regression testing\" rel=\"wikipedia\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9SZWdyZXNzaW9uX3Rlc3Rpbmc=">regression testing</a></li>
<li>test coverage</li>
</ul>
</li>
<li>prevents low quality software
<ul>
<li>coding standard adherance</li>
</ul>
</li>
<li>prevents lack of deployable software</li>
</ul>
<p>This practice focuses on <a class=\"zem_slink freebase/en/software_design\" title=\"Software design\" rel=\"wikipedia\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9Tb2Z0d2FyZV9kZXNpZ24=">software design</a> that uses <a class=\"zem_slink freebase/en/unit_test\" title=\"Unit testing\" rel=\"wikipedia\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9Vbml0X3Rlc3Rpbmc=">unit tests</a> to prevent and detect defects. This all significantly works to provide significantly better quality software.</p>
<p><span id="more-251"></span></p>
<h2>How do we implement it</h2>
<p>We need <a class=\"zem_slink freebase/en/build_automation\" title=\"Build automation\" rel=\"wikipedia\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9CdWlsZF9hdXRvbWF0aW9u">build automation</a>.</p>
<p>You start by identifying repetitive processes (running tests, analyzing source code, packaging, deployment). You want to make these processes a <em>non-event</em>.</p>
<h3>Static code analysis</h3>
<p>Lines of code gives a text-based metric for code size.</p>
<ul>
<li>Lines of code (<a class=\"zem_slink freebase/en/source_lines_of_code\" title=\"Source lines of code\" rel=\"wikipedia\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9Tb3VyY2VfbGluZXNfb2ZfY29kZQ==">loc</a>)</li>
<li>comment lines of code (cloc)</li>
<li>non-comment lines of code (ncloc)</li>
<li>executable lines of code (eloc)</li>
</ul>
<p>Code duplication. Is it textually identical? token for token identical? functionally identical? Dupicate code contradicts code reuse. Co-Evolution of clones hinders maintenance. There is a tool called PHPCPD (a copy-paste detector). Highest they've seen is 16% duplication in a code base of 5 million lines of code.</p>
<h3>code complexity</h3>
<p><a class=\"zem_slink freebase/en/cyclomatic_complexity\" title=\"Cyclomatic complexity\" rel=\"wikipedia\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9DeWNsb21hdGljX2NvbXBsZXhpdHk=">Cyclomatic complexity</a> counts the number of branching points such as if, for,foreach,while,case,catch,&amp;&amp;,"",ternary operatory. NPath Complexity counts the number of execution paths. Higher complexity leads to more errors and makes testing harder.</p>
<p>You can analyze code with "sniffs".</p>
<h2>build automation</h2>
<ul>
<li>apache ant</li>
<li>gnu make</li>
<li>phing</li>
<li>rake</li>
<li>shell scripts</li>
</ul>
<p>Reviewing some of the tools that got covered in another talk I blogged about. If anything new/interesting is added, i'll list it. @s_bergmann recommends Hudson (http://hudson-ci.org). Apparently very easy to use and is used by some big names like Sun.</p>
<h6 class="zemanta-related-title" style="font-size: 1em;">Related articles by Zemanta</h6>
<ul class="zemanta-article-ul">
<li class="zemanta-article-ul-li"><a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5zbGlkZXNoYXJlLm5ldC9zZWJhc3RpYW5fYmVyZ21hbm4vcGhwLWNvZGUtcmV2aWV3LTQxNDI3MTk=">PHP Code Review</a> (slideshare.net)</li>
<li class="zemanta-article-ul-li"><a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5zbGlkZXNoYXJlLm5ldC9zZWJhc3RpYW5fYmVyZ21hbm4vY29udGludW91cy1pbnRlZ3JhdGlvbi1vZi1waHAtcHJvamVjdHMtNDE1OTY5OQ==">Continuous Integration of PHP Projects</a> (slideshare.net)</li>
<li class="zemanta-article-ul-li"><a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5zbGlkZXNoYXJlLm5ldC9wYXNjYWxsb3Vpcy9hcHBsaWVkLWxlYW4tc3RhcnR1cC1pZGVhcy1jb250aW51b3VzLWRlcGxveW1lbnQtYXQta2FjaGluZw==">Applied Lean Startup Ideas: Continuous Deployment at kaChing</a> (slideshare.net)</li>
<li class="zemanta-article-ul-li"><a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5zbGlkZXNoYXJlLm5ldC93YWthbGVvL2hvdy1jYW4teW91LWltcHJvdmUtaGFybW9uaXplLWFuZC1hdXRvbWF0ZS15b3VyLWRldmVsb3BtZW50LXByb2Nlc3MtdXNpbmctdG9vbHMtbGlrZS1tYXZlbi1odWRzb24tYW5kLW5leHVzLXRoaXMtcHJlc2VudGF0aW9uLWlzLWEtaGlnaGxldmVsLW92ZXJ2aWV3LW9mLWphdmEtc29mdHdhcmUtZGV2ZWxvcG1lbnQtcHJvY2Vzcy1pbXByb3ZlbWVudC0zMzIxMDkx">How can you improve, harmonize and automate your development process using tools like Maven, Hudson, and Nexus? This presentation is a high-level overview of Java software development process improvement.</a> (slideshare.net)</li>
</ul>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class=\"zemanta-pixie-a\" title=\"Reblog this post [with Zemanta]\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3JlYmxvZy56ZW1hbnRhLmNvbS96ZW1pZmllZC9mNDQwNThiYi0wOWI4LTQ1NTUtODAzMy04MTQ4Y2Y4Zjk5YzIv"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/reblog_a.png?x-id=f44058bb-09b8-4555-8033-8148cf8f99c2" alt="Reblog this post [with 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=251" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://phpprotip.com/2010/05/tekx-continuous-inspection-and-integration-of-php-projects/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>headers exception with Zend_Session while unit testing</title>
		<link>http://phpprotip.com/2009/06/headers-exception-with-zend_session-while-unit-testing/</link>
		<comments>http://phpprotip.com/2009/06/headers-exception-with-zend_session-while-unit-testing/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 14:57:18 +0000</pubDate>
		<dc:creator>chance</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[phpunit]]></category>
		<category><![CDATA[web dev]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[header exception]]></category>
		<category><![CDATA[headers sent]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[zend_session]]></category>

		<guid isPermaLink="false">http://phpprotip.com/?p=154</guid>
		<description><![CDATA[While the manual for Zend_Session does discuss unit testing and the read-only exception, it has no mention of an exception I encountered recently while unit testing. I admit that the reason I encountered the exception is most likely because I'm Doing It Wrong. However, given reality, I did not have time to properly make the [...]]]></description>
			<content:encoded><![CDATA[<p>While the manual for <a title=\"Zend_Session advanced usage (unit testing)\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2ZyYW1ld29yay56ZW5kLmNvbS9tYW51YWwvZW4vemVuZC5zZXNzaW9uLmFkdmFuY2VkX3VzYWdlLmh0bWwjemVuZC5zZXNzaW9uLmFkdmFuY2VkX3VzYWdlLnRlc3Rpbmc=" target=\"_blank\">Zend_Session</a> does discuss unit testing and the read-only exception, it has no mention of an exception I encountered recently while unit testing. I admit that the reason I encountered the exception is most likely because I'm Doing It Wrong. However, given reality, I did not have time to properly make the class, or at least do it better. I received the following exception:</p>
<blockquote><p>exception 'Zend_Session_Exception' with message 'Session must be started before any output has been sent to the browser; output started...'</p></blockquote>
<p>The problem was, of course, that the feedback from previous tests was already outputted to the screen. I could've solved this by playing with my test suite and just starting sessions at that level but that wouldn't have been the best solution. In fact, I'd qualify that solution under bandaids (solutions that mask a problem instead of fixing them). If you can't see why it is a bandaid, I'm happy to discuss it but for once, I"m going to try to stay on topic and not digress any further than I have.</p>
<p>So, here's the scenario:</p>
<ul>
<li>not enough time to refactor the class so that sessions are only called/created when necessary</li>
<li>sessions are being called because we're testing some auth stuff which relies on session information, we might not be able to do <a title=\"Dependency Injection\" href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9EZXBlbmRlbmN5X2luamVjdGlvbg==" target=\"_blank\">DI</a> (see above)</li>
<li>we want to minimize contributing factors to test failure so session has to be called/destroyed for the tests that need it, all other tests should never have session existing</li>
</ul>
<p>The solution? A little 'undocumented' static variable:</p>
<p><strong>Zend_Session::$_unitTestEnabled = true;</strong></p>
<p>Ok, undocumented is a bit misleading. You won't find out about it by looking at the <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2ZyYW1ld29yay56ZW5kLmNvbS9tYW51YWwv" target=\"_blank\">Manual</a> but will find it when you're digging through the <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2ZyYW1ld29yay56ZW5kLmNvbS9hcGlkb2MvY29yZS8=" target=\"_blank\">API</a>. So it is documented, just not well. I'm guessing because it is rare to encounter the exception. I'm not surprised that I encountered it since my projects always seem to have some degree of weirdness, which makes for an interesting learning curve thats compounded with Zend's curve.</p>
 <img src="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=154" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://phpprotip.com/2009/06/headers-exception-with-zend_session-while-unit-testing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

