<?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>the fancy part of the web &#187; phpunit</title>
	<atom:link href="http://blog.tolleiv.de/tag/phpunit/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tolleiv.de</link>
	<description>is elsewhere - this is just about all sorts of web related work with a small factor of fanciness</description>
	<lastBuildDate>Fri, 03 Sep 2010 15:52:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>multiple arguments for mocked functions&#8230;.</title>
		<link>http://blog.tolleiv.de/2009/08/multiple-arguments-for-mocked-functions/</link>
		<comments>http://blog.tolleiv.de/2009/08/multiple-arguments-for-mocked-functions/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 21:38:45 +0000</pubDate>
		<dc:creator>tolleiv</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phpunit]]></category>

		<guid isPermaLink="false">http://blog.tolleiv.de/?p=221</guid>
		<description><![CDATA[If you look into the PHPUnit documentation ( http://www [...]]]></description>
			<content:encoded><![CDATA[<p>If you look into the PHPUnit documentation (<a href="http://www.phpunit.de/manual/3.3/en/test-doubles.html#test-doubles.mock-objects"> http://www.phpunit.de/manual/3.3/en/test-doubles.html#test-doubles.mock-objects</a> ) you&#8217;ll see some nice examples but non of them shows how to translate a function with multiple arguments into a mock-assumption.</p>
<p>Within the examples you&#8217;ll find:<br />
<code>$observer-&gt;expects($this-&gt;once())-&gt;method('update')<br/>-&gt;with($this-&gt;equalTo('something'));<br />
</code></p>
<p>So what if the &#8220;update&#8221; function has two arguments, pretty easy:<br />
<code>$observer-&gt;expects($this-&gt;once())-&gt;method('update')<br/>-&gt;with($this-&gt;equalTo('something'), $this-&gt;equalTo('something else'));<br />
</code></p>
<p>I guess Sebastian though that that there&#8217;s no need for documentation if something is so obvious <img src='http://blog.tolleiv.de/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
 <p>Feel free to Flattr this post at <a href="http://flattr.com/" title="Flattr" target="_blank">flattr.com</a>, if you like it.</p> <p><a href="http://flattr.com/" title="Flattr" target="_blank"><img src="http://blog.tolleiv.de/wp-content/plugins/flattrss/button-compact-static-100x17.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.tolleiv.de/2009/08/multiple-arguments-for-mocked-functions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Mocks within Tests</title>
		<link>http://blog.tolleiv.de/2008/05/using-mocks-within-tests/</link>
		<comments>http://blog.tolleiv.de/2008/05/using-mocks-within-tests/#comments</comments>
		<pubDate>Sat, 17 May 2008 20:02:00 +0000</pubDate>
		<dc:creator>tolleiv</dc:creator>
				<category><![CDATA[mock]]></category>
		<category><![CDATA[phpunit]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tests]]></category>

		<guid isPermaLink="false">http://blog.tolleiv.de/2008/05/using-mocks-within-tests/</guid>
		<description><![CDATA[Let's say you have some kind of process which creates c [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you have some kind of process which creates cookies and which requires the cookies to bake before you deliver them to the customers. This process somehow touches two kinds of objects the cookie itself and a object which performs the process &#8211; the cookie oven.<br />
When you start to develop the oven how do you check whether the cookies are really baked or not? Since you can&#8217;t get rid of the dependency between the oven and the cookie, you have to simulate a real object and do the checks by hand &#8211; but wait there&#8217;s already a way to resolve it:<br />
PHPUnit ships with a very nice and comfortable function to create and check mock-objects and that&#8217;s exactly what we need in this situation where we somehow need to find out if our cookie really gets baked.<br />
The testcase for this scenario could look like this:</p>
<pre name="code" class="php">
class TestCookieOven extends PHPUnit_Framework_TestCase {
	public function testIfCookieIsBakenWithinFinishing() {
		/* create the mock and expect the
		bakeIt method to be called at least once */
		$cookiemock = $this->getMock(‘Cookie’, array(‘bakeIt’));
		$cookiemock->expects($this->once())->method(‘bakeIt’);

		/* create the observed object and perform the required steps */
		$cookieoven = new CookieOven();
		$cookieoven->finishCookie($cookiemock);
	}
}
</pre>
<p>This first creates a mock and assigns the expectation that the &#8220;bakeIt&#8221;-function is called exactly once. [ beside the <span style="font-style: italic;">-&gt;once()</span>-call there are some alternatives: <span style="font-style: italic;">-&gt;any()</span>, -&gt;<span style="font-style: italic;">never()</span>, -&gt;<span style="font-style: italic;">atLeastOnce()</span>, -&gt;<span style="font-style: italic;">exactly(int $count)</span> and -&gt;<span style="font-style: italic;">at(int $index) ]. Then it passes the mock to the oven and performs the method we want to check. To make this test green you need at least this:</p>
<pre name="code" class="php">
CookieOven {
	/**
	* Takes a cookie and prepares it to be ready
	* for a customer
	*
	* @param Cookie $cookie
	*/
	public function finishCookie(Cookie $cookie) {
		$cookie->bakeIt();
	}
}
</pre>
<p>So as soon you have these lines you have a green test and that&#8217;s great because there is no need to have a real &#8220;cookie&#8221; class &#8211; someone else can take care of the cookies &#8211; oh wait &#8230; I&#8217;d better do this myself <img src='http://blog.tolleiv.de/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<script type="text/javascript">
var flattr_wp_ver = '0.9.14';
var flattr_uid = '23199';
var flattr_url = 'http://blog.tolleiv.de';
var flattr_lng = 'en_GB';
var flattr_cat = 'text';
var flattr_tag = 'blog,wordpress,rss,feed';
var flattr_btn = 'large';
var flattr_tle = 'the fancy part of the web';
var flattr_dsc = 'is elsewhere - this is just about all sorts of web related work with a small factor of fanciness';
</script>
<script src="https://api.flattr.com/js/0.5.0/load.js?mode=auto" type="text/javascript"></script> <p>Feel free to Flattr this post at <a href="http://flattr.com/" title="Flattr" target="_blank">flattr.com</a>, if you like it.</p> <p><a href="http://flattr.com/" title="Flattr" target="_blank"><img src="http://blog.tolleiv.de/wp-content/plugins/flattrss/button-compact-static-100x17.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.tolleiv.de/2008/05/using-mocks-within-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
