<?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>Sat, 21 Jan 2012 12:58:05 +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>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.phpunit.de/manual/3.3/en/test-doubles.html#test-doubles.mock-objects ) you'll see some nice examples but non of them shows how to translate a function with multiple arguments into a mock-assumption.

Within the examples you'll find:
$observer-&#62;expects($this-&#62;once())-&#62;method('update')-&#62;with($this-&#62;equalTo('something'));


So what if the "update" function has two arguments, pretty easy:
$observer-&#62;expects($this-&#62;once())-&#62;method('update')-&#62;with($this-&#62;equalTo('something'), $this-&#62;equalTo('something else'));


I guess Sebastian though that that there's no ...]]></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 class="wp-flattr-button"></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 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 - the cookie oven.
When you start to develop the oven how ...]]></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>
<p class="wp-flattr-button"></p> <p><a href="http://blog.tolleiv.de/?flattrss_redirect&amp;id=561&amp;md5=89def8cfcb8f5e48adae0d3b46b1a4c3" title="Flattr" target="_blank"><img src="http://blog.tolleiv.de/wp-content/plugins/flattr/img/flattr-badge-large.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>

