<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[PHPUnit的模拟对象getMock()函数用法示例！]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Tue, 14 Oct 2008 07:49:38 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	由于在我们代码开发中，网网接口调用平凡，但是现实资源往往也不足，为此，在我们的测试过程中也出现了困难，一方面是时间的对等，另一方面来自于开发进度和先关准备资源的不充分，为此，phpunit库早已为我们准备好了，getMock函数来跳过或者模拟一些接口方法：<br/>以下假设update函数是掉外网的一个接口，但是这个时候，外网的接口还没有写好，我们怎么办才好呢？那就getMock吧！<br/><br/>helloworld.php<br/><div class="code">&lt;?php<br/>class helloworld<br/>&#123;<br/>&nbsp;&nbsp;public function helloworld()<br/>&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;hello the world!&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;public function update($arra)<br/>&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return $arra;<br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;public function update_next_do($arra)<br/>&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if($this-&gt;update($arra)==&#039;foos&#039;)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return foos;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;else&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;<br/>&#125;<br/>?&gt;</div><br/><br/>自动生成helloworldTest.php<br/><br/><div class="code"><br/>&lt;?php<br/><br/>require_once &#039;helloworld.php&#039;;<br/><br/>require_once &#039;PHPUnit/Framework/TestCase.php&#039;;<br/><br/>/**<br/> * helloworld test case.<br/> */<br/>class helloworldTest extends PHPUnit_Framework_TestCase &#123;<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;/**<br/>&nbsp;&nbsp; * @var helloworld<br/>&nbsp;&nbsp; */<br/>&nbsp;&nbsp;private $helloworld;<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;/**<br/>&nbsp;&nbsp; * Prepares the environment before running a test.<br/>&nbsp;&nbsp; */<br/>&nbsp;&nbsp;protected function setUp() &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;parent::setUp ();<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// TODO Auto-generated helloworldTest::setUp()<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;helloworld = new helloworld(/* parameters */);<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;/**<br/>&nbsp;&nbsp; * Cleans up the environment after running a test.<br/>&nbsp;&nbsp; */<br/>&nbsp;&nbsp;protected function tearDown() &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// TODO Auto-generated helloworldTest::tearDown()<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;helloworld = null;<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;parent::tearDown ();<br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;/**<br/>&nbsp;&nbsp; * Constructs the test case.<br/>&nbsp;&nbsp; */<br/>&nbsp;&nbsp;public function __construct() &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// TODO Auto-generated constructor<br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;/**<br/>&nbsp;&nbsp; * Tests helloworld-&gt;helloworld()<br/>&nbsp;&nbsp; */<br/>&nbsp;&nbsp;public function testHelloworld() &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// TODO Auto-generated helloworldTest-&gt;testHelloworld()<br/>&nbsp;&nbsp;&nbsp;&nbsp;//$this-&gt;markTestIncomplete ( &quot;helloworld test not implemented&quot; );<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;//$this-&gt;helloworld-&gt;helloworld(/* parameters */);<br/>&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;assertTrue($this-&gt;helloworld-&gt;update(&#039;foo&#039;)==&#039;foo&#039;); ////假如接口真实存在直接可以调用，来什么返回什么<br/>&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;assertFalse($this-&gt;helloworld-&gt;update(&#039;foo&#039;)==&#039;foos&#039;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;//以下开始构造虚拟接口class helloworld&nbsp;&nbsp; function update(&#039;foo&#039;) return foos;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;$observer = $this-&gt;getMock(&#039;helloworld&#039;, array(&#039;update&#039;));<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 设定update()方法的期望值为只被以字符串“something”为参数调用一次。<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$observer-&gt;expects($this-&gt;any())<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;method(&#039;update&#039;)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;with($this-&gt;equalTo(&#039;foo&#039;))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;will($this-&gt;returnValue(&#039;foos&#039;));<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 创建一个Subject对象并附上模拟的Observer对象。<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;assertTrue($observer-&gt;update(&#039;foo&#039;)==&#039;foos&#039;);//虚拟接口变为：输入foo返回foos了<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result = $observer-&gt;update(&#039;foo&#039;);//虚拟接口变为：输入foo返回foos了<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;assertTrue($this-&gt;helloworld-&gt;update_next_do($result)==&quot;foos&quot;);//其余真实方法去调用(虚拟接口)以达到预期结果<br/>&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/><br/>&#125;<br/><br/></div><br/><br/>注意：在里面多次执行了getMock模拟函数update(),我们用了any()，如果我们用once()的话，在调用第二次这个虚拟函数肯定会出错，因为once()限定了，相关匹配如下：<br/><br/><br/>表 11.1. 匹配器<br/>匹配器&nbsp;&nbsp;含义<br/>PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount any()&nbsp;&nbsp;返回一个匹配器，当它评估的方法被执行0或多次时匹配。<br/>PHPUnit_Framework_MockObject_Matcher_InvokedCount never()&nbsp;&nbsp;返回一个匹配器，当它评估的方法被从未被执行时匹配。<br/>PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastOnce atLeastOnce()&nbsp;&nbsp;返回一个匹配器，当它评估的方法被至少执行一次时匹配。<br/>PHPUnit_Framework_MockObject_Matcher_InvokedCount once()&nbsp;&nbsp;返回一个匹配器，当它评估的方法恰好被执行一次时匹配。<br/>PHPUnit_Framework_MockObject_Matcher_InvokedCount exactly(int $count)&nbsp;&nbsp;返回一个匹配器，当它评估的方法正好被执行$count次时匹配。<br/>PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex at(int $index)&nbsp;&nbsp;返回一个匹配器，当它评估的方法在特定的$index上被调用时匹配。<br/><br/><br/>关于：<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;->with($this->equalTo('foo'))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ->will($this->returnValue('foos'));<br/>你要是传入多个变量的参数如（这样传）：<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ->with($this->equalTo('foo'),$this->equalTo('foo2'),$this->equalTo(array("one","two","three")))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ->will($this->returnValue(array("one return array","two&nbsp;&nbsp;return array","three return array")));<br/><br/>这儿的with参数和will关系：<br/>只有传入with的几个对应的参数，方能得到will的结果，但是你要是有引用什么的函数需要Mock的话：<br/><br/><br/><div class="code"> &nbsp;&nbsp;&nbsp;&nbsp;$friendObj = new Friend();<br/>&nbsp;&nbsp;&nbsp;&nbsp;$friendObj-&gt;getList($fuids,$uid,0,1,1000);<br/>&nbsp;&nbsp;&nbsp;&nbsp;var_dump($fuids); //你mock它，但是你传入的$fuids（假如一个数组），运行这儿是得不到的，注意啊！<br/>&nbsp;&nbsp;&nbsp;&nbsp;if(empty($fuids&#91;&#039;uids&#039;&#93;))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;</div><br/>为此：在需要通过mock的函数，请不要写为引用的格式，通过return 返回即可。<br/>但你会说，要是有错误怎么办?<br/>我建议通过php5的try catch 来实现，定义错误编号来解决这个问题，Tks.转帖请注明来自向东的博客：www.xiangdong.org/blog<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite:xiangdong2&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2009-4-13<br/><br/><br/><br/><br/><br/><br/>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] PHPUnit的模拟对象getMock()函数用法示例！]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>