LinuxÒÁµéÔ°ÂÛ̳'s Archiver

Roc.Ken ·¢±íÓÚ 2005-11-13 15:08

¡¾×ªÌû¡¿PHPµ¥Ôª²âÊÔ¹¤¾ßPHPUnit³õÌåÑé

z33 <newbdez33 at gmail.com>;
2005.10.27

½ñÌì½Óµ½Á˸öÈÎÎñ£¬ÐèÒª¶ÔÊý×Ö½øÐмÆË㣬ÒòÎªÉæ¼°µ½ÕûÊý£¬Ð¡Êý£¬ºÍ¿ÆÑ§¼ÆÊý·¨µÈºÜ¶àÌõ¼þ£¬ËùÒÔÈ˹¤²âÊԷdz£Âé·³£¬ÓÚÊÇÏëµ½ÁËPHPµÄµ¥Ôª²âÊÔ¹¤¾ßPHPUnit£¬ËùÒÔд¸öÎĵµ±¸²é¡£

¿´ÁËPHPUnitµÄÎĵµÖ®ºó»ù±¾ÓÐÁËһЩÁ˽⣬
[url]http://pear.php.net/manual/en/packages.php.phpunit.intro.php[/url]

¹¤×÷Á÷³ÌÈçÏ£º
1.Éè¼ÆÄãµÄclass/API
2.´´½¨²âÊÔ³ÌÐò¼¯
3.ʵÏÖclass/API
4.ÔËÐвâÊÔ
5.ÐÞÕý²âÊÔʧ°Ü»ò´íÎ󣬻ص½µÚ4²½¡£

ÎÒÃÇÀ´¾Ù¸öÀý×Ó£º
ÏÂÃæÊÇÄãÒª²âÊÔµÄclass£¬ÆäÖÐformatnº¯ÊýÒ»¸öÈ¡ÈÎÒâÊý×ÖµÄ5λÓÐЧÊý×ֵĺ¯Êý¡£
[php]
----------format_number.php-----------
class fo {

        function fo() {
        }

        function formatn($num) {
                $num = rtrim($num,"0");
                $pos = strpos($num,".");
                $num = str_replace(".","",$num);
                $count1 = strlen($num);
                $num = ltrim($num,"0");
                $count2 = strlen($num);
                $zeroc = $count1 - $count2;
                $num = substr($num,0,6);
                $num = round($num/10);
                //$num = str_pad($num, 5, "0");
                if ($pos !== false) {
                        $num = str_pad($num, (strlen($num)+$zeroc), "0", STR_PAD_LEFT);
                        $dotl = substr($num,0,$pos);
                        $dotr = substr($num,$pos);
                        $num = $dotl.".".$dotr;
                }
                return $num;
        }

}
[/php]
½Ó×Å´´½¨TestCase£¬¼Ì³Ð×ÔPHPUnit_TestCase
[php]
----------testcase.php-----------
<?php

require_once 'format_number.php';
require_once 'PHPUnit.php';

class foTest extends PHPUnit_TestCase {

        //Õâ¸ö³ÉÔ±±äÁ¿ÊÇ´æ·ÅÒª²âÊÔµÄÀàÒýÓÃ
        var $abc;

        //¹¹Ô캯Êý
        function foTest($name) {
                $this->;PHPUnit_TestCase($name);
        }

        //newÒ»¸öÒª²âÊÔµÄÀàΪ³ÉÔ±±äÁ¿abc¸³Öµ
        function setUp() {
                $this->;abc = new fo;
        }

        //unsetÒª²âÊÔµÄÀà
        function tearDown() {
                unset($this->;abc);
        }

        //×Ô¶¨ÒåµÄtestcase
        function testFormatn1() {
                //µ÷ÓÃÒª²âÊÔµÄÀàµÄ·½·¨£¬½á¹û·Åµ½$result±äÁ¿
                $result = $this->;abc->;formatn("100.234");
                //ÆÚÍû½á¹û
                $expected = "100.23";
                //ÅжÏÊÇ·ñÏàµÈ£¬ÕâÀïʹÓÃassertTrue·½·¨À´Åжϲ¼¶øÖµÊÇ·ñΪtrue¡£
                $this->;assertTrue($result == $expected);
        }

        function testFormatn2() {
                $result = $this->;abc->;formatn("0.100234");
                $expected = "0.10023";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn3() {
                $result = $this->;abc->;formatn("0.100235");
                $expected = "0.10024";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn4() {
                $result = $this->;abc->;formatn("0.000100235");
                $expected = "0.00010024";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn5() {
                $result = $this->;abc->;formatn("0.000100232");
                $expected = "0.00010023";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn6() {
                $result = $this->;abc->;formatn("1343");
                $expected = "1343";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn7() {
                $result = $this->;abc->;formatn("1343.01");
                $expected = "1343";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn8() {
                $result = $this->;abc->;formatn("1343.05");
                $expected = "1343.1";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn9() {
                $result = $this->;abc->;formatn("0");
                $expected = "0";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn10() {
                $result = $this->;abc->;formatn("105.2342");
                $expected = "105.23";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn11() {
                $result = $this->;abc->;formatn("105.2375");
                $expected = "105.24";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn12() {
                $result = $this->;abc->;formatn("0.000523751");
                $expected = "0.00052375";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn13() {
                $result = $this->;abc->;formatn("0.000523755");
                $expected = "0.00052376";
                $this->;assertTrue($result == $expected);
        }

}
[/php]
×îºó»¹ÐèÒªÒ»¸öÔËÐвâÊԵijÌÐò
[code]
----------runtest.php-----------
<?php
require_once 'testcase.php';
require_once 'PHPUnit.php';

$suite = new PHPUnit_TestSuite("foTest");
$result = PHPUnit::run($suite);

echo $result->;toString();
?>;
[/code]

       
·¢±íÓÚ: 2005-10-27 11:33             [ÒýÓÃ]  [ͶËß] [¿ìËٻظ´]
PHPµ¥Ôª²âÊÔ¹¤¾ßPHPUnit³õÌåÑé

z33 <newbdez33 at gmail.com>;
2005.10.27

½ñÌì½Óµ½Á˸öÈÎÎñ£¬ÐèÒª¶ÔÊý×Ö½øÐмÆË㣬ÒòÎªÉæ¼°µ½ÕûÊý£¬Ð¡Êý£¬ºÍ¿ÆÑ§¼ÆÊý·¨µÈºÜ¶àÌõ¼þ£¬ËùÒÔÈ˹¤²âÊԷdz£Âé·³£¬ÓÚÊÇÏëµ½ÁËPHPµÄµ¥Ôª²âÊÔ¹¤¾ßPHPUnit£¬ËùÒÔд¸öÎĵµ±¸²é¡£

¿´ÁËPHPUnitµÄÎĵµÖ®ºó»ù±¾ÓÐÁËһЩÁ˽⣬
[url]http://pear.php.net/manual/en/packages.php.phpunit.intro.php[/url]

¹¤×÷Á÷³ÌÈçÏ£º
1.Éè¼ÆÄãµÄclass/API
2.´´½¨²âÊÔ³ÌÐò¼¯
3.ʵÏÖclass/API
4.ÔËÐвâÊÔ
5.ÐÞÕý²âÊÔʧ°Ü»ò´íÎ󣬻ص½µÚ4²½¡£

ÎÒÃÇÀ´¾Ù¸öÀý×Ó£º
ÏÂÃæÊÇÄãÒª²âÊÔµÄclass£¬ÆäÖÐformatnº¯ÊýÒ»¸öÈ¡ÈÎÒâÊý×ÖµÄ5λÓÐЧÊý×ֵĺ¯Êý¡£

CODE:
[Copy to clipboard]
----------format_number.php-----------
class fo {

        function fo() {
        }

        function formatn($num) {
                $num = rtrim($num,"0");
                $pos = strpos($num,".");
                $num = str_replace(".","",$num);
                $count1 = strlen($num);
                $num = ltrim($num,"0");
                $count2 = strlen($num);
                $zeroc = $count1 - $count2;
                $num = substr($num,0,6);
                $num = round($num/10);
                //$num = str_pad($num, 5, "0");
                if ($pos !== false) {
                        $num = str_pad($num, (strlen($num)+$zeroc), "0", STR_PAD_LEFT);
                        $dotl = substr($num,0,$pos);
                        $dotr = substr($num,$pos);
                        $num = $dotl.".".$dotr;
                }
                return $num;
        }

}
½Ó×Å´´½¨TestCase£¬¼Ì³Ð×ÔPHPUnit_TestCase

CODE:
[Copy to clipboard]
----------testcase.php-----------
<?php

require_once 'format_number.php';
require_once 'PHPUnit.php';

class foTest extends PHPUnit_TestCase {

        //Õâ¸ö³ÉÔ±±äÁ¿ÊÇ´æ·ÅÒª²âÊÔµÄÀàÒýÓÃ
        var $abc;

        //¹¹Ô캯Êý
        function foTest($name) {
                $this->;PHPUnit_TestCase($name);
        }

        //newÒ»¸öÒª²âÊÔµÄÀàΪ³ÉÔ±±äÁ¿abc¸³Öµ
        function setUp() {
                $this->;abc = new fo;
        }

        //unsetÒª²âÊÔµÄÀà
        function tearDown() {
                unset($this->;abc);
        }

        //×Ô¶¨ÒåµÄtestcase
        function testFormatn1() {
                //µ÷ÓÃÒª²âÊÔµÄÀàµÄ·½·¨£¬½á¹û·Åµ½$result±äÁ¿
                $result = $this->;abc->;formatn("100.234");
                //ÆÚÍû½á¹û
                $expected = "100.23";
                //ÅжÏÊÇ·ñÏàµÈ£¬ÕâÀïʹÓÃassertTrue·½·¨À´Åжϲ¼¶øÖµÊÇ·ñΪtrue¡£
                $this->;assertTrue($result == $expected);
        }

        function testFormatn2() {
                $result = $this->;abc->;formatn("0.100234");
                $expected = "0.10023";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn3() {
                $result = $this->;abc->;formatn("0.100235");
                $expected = "0.10024";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn4() {
                $result = $this->;abc->;formatn("0.000100235");
                $expected = "0.00010024";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn5() {
                $result = $this->;abc->;formatn("0.000100232");
                $expected = "0.00010023";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn6() {
                $result = $this->;abc->;formatn("1343");
                $expected = "1343";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn7() {
                $result = $this->;abc->;formatn("1343.01");
                $expected = "1343";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn8() {
                $result = $this->;abc->;formatn("1343.05");
                $expected = "1343.1";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn9() {
                $result = $this->;abc->;formatn("0");
                $expected = "0";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn10() {
                $result = $this->;abc->;formatn("105.2342");
                $expected = "105.23";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn11() {
                $result = $this->;abc->;formatn("105.2375");
                $expected = "105.24";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn12() {
                $result = $this->;abc->;formatn("0.000523751");
                $expected = "0.00052375";
                $this->;assertTrue($result == $expected);
        }

        function testFormatn13() {
                $result = $this->;abc->;formatn("0.000523755");
                $expected = "0.00052376";
                $this->;assertTrue($result == $expected);
        }

}
×îºó»¹ÐèÒªÒ»¸öÔËÐвâÊԵijÌÐò

CODE:
[Copy to clipboard]
----------runtest.php-----------
<?php
require_once 'testcase.php';
require_once 'PHPUnit.php';

$suite = new PHPUnit_TestSuite("foTest");
$result = PHPUnit::run($suite);

echo $result->;toString();
?>;
ÏÖÔھͿÉÒÔͨ¹ýÃüÁîÐÐÔËÐÐÕâ¸ötestcase
php runtest.php

µÃµ½½á¹ûÈçÏ£º
[php]
TestCase foTest->;testFormatn1() passed
TestCase foTest->;testFormatn2() passed
TestCase foTest->;testFormatn3() passed
TestCase foTest->;testFormatn4() passed
TestCase foTest->;testFormatn5() passed
TestCase foTest->;testFormatn7() passed
TestCase foTest->;testFormatn8() passed
TestCase foTest->;testFormatn9() passed
TestCase foTest->;testFormatn10() passed
TestCase foTest->;testFormatn11() passed
TestCase foTest->;testFormatn12() passed
TestCase foTest->;testFormatn13() passed
TestCase foTest->;testFormatn6() failed: expected TRUE, actual FALSE[/php]

ÆäÖÐtestFormatn6µÄ²âÊÔʧ°Ü£¬
ÎÒÃǾͿÉÒÔÈ¥¼ì²éÒ»ÏÂÎÒÃǵĴúÂëÔÚʲôµØ·½³öÎÊÌâÁË¡£

²¹³äÒ»µã
Ò²¿ÉÒÔ°ÑassertTrue·½·¨»»assertEquals£¬ÈçÏ£º
[php]        function testFormatn6() {
                $result = $this->;abc->;formatn("1343");
                $expected = "1343";
                $this->;assertEquals($expected, $result);
        }[/php]
Èç¹ûʧ°ÜµÃµ½¶ÔÓ¦µÄ½á¹û»áÖ±¹ÛһЩ£¨¿ÉÒÔÏÔʾ´íÎóµÄ½á¹û£©£º
[code]TestCase foTest->;testFormatn8() failed: expected 1343 , actual 134.[/code]

Roc.Ken ·¢±íÓÚ 2005-11-13 15:12

ÓÐÒ»¶ÎÖØ¸´ÁË, ±à¼­Ê±¾ÓÈ»ÕÒ²»³öÀ´

Ò³: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.