blob: ccd49f0492bb450d8edc5e0fa7c2549982ee4d40 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="generator" content="Adobe RoboHelp 8">
<title>PHPUnit_Example</title>
<link rel="StyleSheet" href="default.css" type="text/css">
<style type="text/css">
<!--
body { border-left-style:None;
border-right-style:None;
border-bottom-style:None;
background-color:Transparent;
border-top-style:None; }
-->
</style>
<style type="text/css">
<!--
body { border-left-style:None;
border-right-style:None;
border-bottom-style:None;
background-color:Transparent;
border-top-style:None; }
-->
</style>
<script type="text/javascript" language="JavaScript">
//<![CDATA[
function reDo() {
if (innerWidth != origWidth || innerHeight != origHeight)
location.reload();
}
if ((parseInt(navigator.appVersion) == 4) && (navigator.appName == "Netscape")) {
origWidth = innerWidth;
origHeight = innerHeight;
onresize = reDo;
}
onerror = null;
//]]>
</script>
<style type="text/css">
<!--
div.WebHelpPopupMenu { position:absolute;
left:0px;
top:0px;
z-index:4;
visibility:hidden; }
p.WebHelpNavBar { text-align:right; }
-->
</style>
<script type="text/javascript" language="javascript1.2" src="whmsg.js"></script>
<script type="text/javascript" language="javascript" src="whver.js"></script>
<script type="text/javascript" language="javascript1.2" src="whproxy.js"></script>
<script type="text/javascript" language="javascript1.2" src="whutils.js"></script>
<script type="text/javascript" language="javascript1.2" src="whlang.js"></script>
<script type="text/javascript" language="javascript1.2" src="whtopic.js"></script>
<script type="text/javascript" language="javascript1.2">//<![CDATA[
<!--
if (window.gbWhTopic)
{
var strUrl = document.location.href;
var bc = 0;
var n = strUrl.toLowerCase().indexOf("bc-");
if(n != -1)
{
document.location.href = strUrl.substring(0, n);
bc = strUrl.substring(n+3);
}
if (window.setRelStartPage)
{
}
document.write("<p style=\"text-align:left\"> ");
AddMasterBreadcrumbs("ide_help.htm", "", "&gt;", "Home", "pdt_table_of_contents.htm");
document.write("PHPUnit_Example<\/p>");
if (window.setRelStartPage)
{
setRelStartPage("ide_help.htm");
autoSync(0);
sendSyncInfo();
sendAveInfoOut();
}
}
else
if (window.gbIE4)
document.location.reload();
//-->
//]]></script>
</head>
<body leftmargin="5" topmargin="5" rightmargin="5" bottommargin="5"><script type="text/javascript" language="javascript1.2">//<![CDATA[
<!--
if (window.writeIntopicBar)
writeIntopicBar(4);
//-->
//]]></script>
<div style="width: 100%; position: relative;" id="header">
<p align="right"><a
title="Documentation Survey" href="http://spreadsheets.google.com/viewform?formkey=dC1scEswTjFia2tDQ3Y5TVJBbkNQcWc6MQ">Documentation
Survey</a>
| <a href="javascript:crlf=&quot;&quot;;h=location.href;t=document.title;e = &quot;&quot; + (window.getSelection ? window.getSelection() : document.getSelection ? document.getSelection() : document.selection.createRange().text); if (e!=null) location=&quot;mailto:documentation@zend.com&quot; + &quot;?Subject= Feedback on PDT for page &quot; + escape(t) + &quot;&amp;Body=&quot; + escape(e).replace(/ /g, &quot;+&quot;) + crlf + &quot; &quot;; void 0"><span
style="background-color: #ffffff;">Submit Documentation</span><span
style="background-color: #ffffff;"> Feedback</span></a><span
style="background-color: #ffffff;"></span><span style="background-color: #ffffff;">
</span><span style="background-color: #ffffff;">|
</span><span
style="background-color: #ffffff;"><a title="View Latest Version and Get PDF Online"
href="http://www.zend.com/en/resources/zend-documentation/"
target="_blank">View Latest
Version and Get PDF Online</a></span></p>
</div>
<h1>PHPUnit_Example
</h1>
<p>This test will automatically enter parameters (a and b) and check that a + b equals b + a and a * b equals b * a, but that a - b does not equal b - a, and a / b does NOT equal b / a.</p>
<p>To run this test, copy-paste the following code into your new unit test case:</p>
<p class="code" style="margin-left: 40px;">&lt;?php</p>
<p class="code" style="margin-left: 40px;">require_once('Calculator.php');</p>
<p class="code" style="margin-left: 40px;">require_once('PHPUnit\Framework\TestCase.php');</p>
<p class="code" style="margin-left: 40px;">/**</p>
<p class="code" style="margin-left: 40px;">&#160;* Calculator test case.</p>
<p class="code" style="margin-left: 40px;">&#160;*/</p>
<p class="code" style="margin-left: 40px;">class CalculatorTest2 extends PHPUnit_Framework_TestCase</p>
<p class="code" style="margin-left: 40px;">{</p>
<p class="code" style="margin-left: 40px;">/**</p>
<p class="code" style="margin-left: 40px;">&#160;* Constructs the test case.</p>
<p class="code" style="margin-left: 40px;">&#160;*/</p>
<p class="code" style="margin-left: 40px;">public function __construct() {</p>
<p class="code" style="margin-left: 40px;">// TODO Auto-generated constructor</p>
<p class="code" style="margin-left: 40px;">}</p>
<p class="code" style="margin-left: 40px;">&#160;</p>
<p class="code" style="margin-left: 40px;">/**</p>
<p class="code" style="margin-left: 40px;">&#160;* @var Calculator</p>
<p class="code" style="margin-left: 40px;">&#160;*/</p>
<p class="code" style="margin-left: 40px;">private $Calculator;</p>
<p class="code" style="margin-left: 40px;">&#160;</p>
<p class="code" style="margin-left: 40px;">private $a;</p>
<p class="code" style="margin-left: 40px;">private $b;</p>
<p class="code" style="margin-left: 40px;">&#160;</p>
<p class="code" style="margin-left: 40px;">/**</p>
<p class="code" style="margin-left: 40px;">&#160;* Prepares the environment before running a test.</p>
<p class="code" style="margin-left: 40px;">&#160;*/</p>
<p class="code" style="margin-left: 40px;">protected function setUp()</p>
<p class="code" style="margin-left: 40px;">{</p>
<p class="code" style="margin-left: 40px;">parent::setUp();</p>
<p class="code" style="margin-left: 40px;">$this-&gt;Calculator = new Calculator();</p>
<p class="code" style="margin-left: 40px;">&#160;</p>
<p class="code" style="margin-left: 40px;">$this-&gt;a = 2;</p>
<p class="code" style="margin-left: 40px;">$this-&gt;b = 4;</p>
<p class="code" style="margin-left: 40px;">&#160;</p>
<p class="code" style="margin-left: 40px;">}</p>
<p class="code" style="margin-left: 40px;">/**</p>
<p class="code" style="margin-left: 40px;">&#160;* Cleans up the environment after running a test.</p>
<p class="code" style="margin-left: 40px;">&#160;*/</p>
<p class="code" style="margin-left: 40px;">protected function tearDown()</p>
<p class="code" style="margin-left: 40px;">{</p>
<p class="code" style="margin-left: 40px;">// TODO Auto-generated CalculatorTest2::tearDown()</p>
<p class="code" style="margin-left: 40px;">&#160;</p>
<p class="code" style="margin-left: 40px;">$this-&gt;Calculator = null;</p>
<p class="code" style="margin-left: 40px;">&#160;</p>
<p class="code" style="margin-left: 40px;">parent::tearDown();</p>
<p class="code" style="margin-left: 40px;">}</p>
<p class="code" style="margin-left: 40px;">&#160;</p>
<p class="code" style="margin-left: 40px;">public function test_addCommutativity()</p>
<p class="code" style="margin-left: 40px;">{</p>
<p class="code" style="margin-left: 40px;">$this-&gt;assertEquals($this-&gt;Calculator-&gt;add($this-&gt;a, $this-&gt;b), $this-&gt;Calculator-&gt;add($this-&gt;a, $this-&gt;b));</p>
<p class="code" style="margin-left: 40px;">}</p>
<p class="code" style="margin-left: 40px;">&#160;</p>
<p class="code" style="margin-left: 40px;">public function test_multiplyCommutativity()</p>
<p class="code" style="margin-left: 40px;">{</p>
<p class="code" style="margin-left: 40px;">$this-&gt;assertEquals($this-&gt;Calculator-&gt;multiply($this-&gt;a, $this-&gt;b), $this-&gt;Calculator-&gt;multiply($this-&gt;b, $this-&gt;a));</p>
<p class="code" style="margin-left: 40px;">}</p>
<p class="code" style="margin-left: 40px;">public function test_subtractCommutativity()</p>
<p class="code" style="margin-left: 40px;">{</p>
<p class="code" style="margin-left: 40px;">$this-&gt;assertNotEquals($this-&gt;Calculator-&gt;subtract($this-&gt;a, $this-&gt;b), $this-&gt;Calculator-&gt;subtract($this-&gt;b, $this-&gt;a));</p>
<p class="code" style="margin-left: 40px;">}</p>
<p class="code" style="margin-left: 40px;">public function test_divideCommutativity()</p>
<p class="code" style="margin-left: 40px;">{</p>
<p class="code" style="margin-left: 40px;">$this-&gt;assertNotEquals($this-&gt;Calculator-&gt;divide($this-&gt;a, $this-&gt;b), $this-&gt;Calculator-&gt;divide($this-&gt;b, $this-&gt;a));</p>
<p class="code" style="margin-left: 40px;">}</p>
<p class="code" style="margin-left: 40px;">&#160;</p>
<p class="code" style="margin-left: 40px;">}</p>
<p>Back to "Creating and Running a PHPUnit Test Suite"</p>
<div style="width: 100%; position: relative;" id="footer">
<p style="text-decoration: underline; "
align="center">PHP Development
Tools</p>
</div>
<script type="text/javascript" language="javascript1.2">//<![CDATA[
<!--
if (window.writeIntopicBar)
writeIntopicBar(0);
highlightSearch();
//-->
//]]></script>
</body>
</html>