Ò»¡¢³£ÓòaÊÔʾÀý
ÎÒÃǾ³£»aÓoµ½ÕaÖÖÇe¿o£º½«Ò»Ð(C)ûÓо¹ýÈκβaÊÔµÄÒÅÁo´uÂe½øÐÐÖØбaд²aÊÔ£¬ÉoÖÁÕaÐ(C)´uÂe»¹ÊÇÓÃÃaeÏo¶ÔÏoдµÄ¡£Òª¶ÔÕaÑuµÄ´uÂe½øÐвaÊÔ£¬ÎҵĽ¨ÒeÊÇ°Ñ´uÂe*Ö½a³É¿e£¬ÕaÑu¾ÍÈÝÒײaÊÔÁË¡£
È»¶ø£¬ÕaÐ(C)ÒÅÁo´uÂe²¢²»ÊÇÄÇôºÃÖع¹µÄ£¬±ÈÈ磺²aÊÔÇ°£¬Äa²»ÄÜÔÚ°Ñ´uÂeÖØбaд£¬ÕaÊÇΪÁ˱ÜÃaÓ°ÏiÔÓгÌÐo£¬µ±È»Ò²²»ºÃ½øÐе¥Ôª²aÊÔ¡£
ÔÚPHP³ÌÐoÖУ¬Í¨³£ÓÐÒ»²¿Ö´uÂeÊÇдÔÚ¼¸¸oindex.phpºÍscript.phpÎļþÖеģ¬ÕaÐ(C).phpÎļþ´aeÅÔÚ¼¸¸o²»Í¬µÄÎļþ¼ÐÀi¡£Èç¹u²»ÕÒµ½ËuÃǵÄÈe¿Úµa£¬ÊÇÎÞ¨Ö±½ÓÓÉWebþÎñÆ÷*ÃÎʵġ£
²aÊÔ¸±±¾
Òª²aÊÔÒ»¸oPHP½Å±¾£¬ÎÒÃÇÐeҪģÄaÒ»¸oHTTPÇeÇo£¬²¢¼i²eµ»ØµÄÏiÓ¦(response)ÊÇñµÈÓÚÔ¤ÆÚÖµ¡£ÕaÀiÐeҪעÒaµÄÊÇÄ£ÄaÒ»¸oÇeÇo£¬Òª¶¨ÒaresponseºÍrequest£¬Õa²»½o½oÊÇÄÚÈÝ(content)µÄ²»Í¬£¬¶øÇÒËuÃǵÄÍ*ÐÅÏ¢(header)Ò²ÊDz»Í¬µÄ¡£
´ËÍa£¬Èç¹uÎÒÃÇÏeÒª²aÊÔÒ»¸o²Ù×÷Êý¾ÝµÄÊÂÎñ½Å±¾£¬ÎÒÃÇҪȱ£²»ÈÃËuÈ¥Á¬½ÓÕaeÕýµÄÊý¾Ý¿a»oÓ¦ÓóÌÐoµÄÆaÓa²¿Ö¡£
ÔÚÏÖʵÖУ¬Í¨³£Ã»ÓÐÈË»aÖ±½ÓÄÃÔÓеÄPHP½Å±¾½øÐÐÖØд²aÊÔ¡£ÒoΪÅ°ѴuÂeŪµÃ²»¿É»Ö¸´¡£ÎÒ½¨ÒeʹÓÃPHP½Å±¾µÄ¸±±¾£¬ÕaÑuÎÒÃǾͿÉÒÔ½«PHP´uÂe½øÐÐÒ»Ð(C)СÊÖÊoÁË¡£
ÈçºÎ½«´uÂe½øÐÐ×iСÐ޸ģºÉ¾³ýincludeºÍrequireÓi¾a£¨Èç¹uËuÃÇûÓб»Óõ½£(C)£¬²¢ÇÒÐÞ¸ÄÄÚ²¿º¯ÊýµÄµ÷ÓÃ*½Ê½£¬ÀýÈ磺½«header()д³É$object->header()¡£
×iºo£¬ÎÒÃÇÀ´²aÊÔÕa¸oÊÂÎñ½Å±¾¡£²aÊÔÍeºo£¬ÎÒÃÇ¿ÉÒÔ´Ó¸±±¾½Å±¾ÖÐÌaÈ¡³oËuÃÇ£¬²¢°ÑËuÃÇ*ÅÈeнű¾ÎļþÖС£
¾ßÌa²½Öe
Ò»¡¢Ä£ÄaÒ»¸oHTTPÇeÇo²¢ÖØж¨Òa±aÁ¿$_GETºÍ$_POST£¬»¹ÒªÐÞ¸Ä$_SERVERµÄheader¡£
¶þ¡¢»ñÈ¡ÇeÇoÏiÓ¦£¬responseµÄbody¿ÉÒÔͨ¹ýob_start()ºÍob_get_clean()²¶»ñ£¬Ëu¿ÉÒÔÊÕ¼¯Ã¿Ò»¸oÓÃecho()»oÒÔ<?php±eÇ(C)Êa³oµÄbuffer(»º³aÄÚÈÝ)¡£
×¢Òa£ºÊa³o»º³aÖ§³ÖÔÚPHP¶a¸o¼¶±ðµÄǶÌ×£¬ËuÒÔÔÚ´o¶aÊýÇe¿oÏ£¬¶¼¿ÉÒÔ²¶»ñµ½£¬¼´Ê¹½Å±¾ÔÚʹÓÃob_*µ÷Óñ¾Éi¡£
Èý¡¢²aÊԽű¾Ó¦°uº¬ÊÂÎñ½Å±¾µÄÄÚ²¿½¨£¬Òo´ËÔÚÕa¸o½Å±¾¶Î§ÄڵĽ¨¶¼¿ÉÒÔ±»µ÷Óá£ÀýÈ磺
1.½Å±¾ËuÐeµÄ±aÁ¿¿ÉÒÔ±»¶¨ÒaΪ¾Ö²¿±aÁ¿a×°ÆðÀ´£¬Èç$connection×÷Ϊһ¸oÊý¾Ý¿aÁ¬½Ó¡£
2.²»ÊÇÔ±¾PHPµÄÄÚÖú¯Êý£¬Ó¦¸Ã¼ÓÉ϶ÔÏoÀ´µ÷Óã¬È磺header()д³É$this->header()¡£
¾ßÌa´uÂe
Õa¾ÍÊÇÎÒÃÇÒª²aÊÔµÄÊÂÎñ½Å±¾¶ÔÏo£¬¾ßÌaµ½½Å±¾ÖУ¬ÎÒÃÇ»¹ÐeÒª*a×°£º
<?php
class ForumPosting
{
private $headers = array();
public function handleRequest($postRequest)
{
$_POST = $postRequest;
$connection = $this->getAConnection();
ob_start();
include 'forum/post_new_copy.php';
$content = ob_get_clean();
return array(
'content' => $content,
'headers' => $this->headers
);
}
private function header($headerLine)
{
$this->headers[] = $headerLine;
}
...
}
ÕaÊÇÎÒÃǵIJaÊÔ´uÂe£º
public function testANewPostIsCreated()
{
$action = new ForumPosting();
$response = $action->handleRequest(array(
'id_thread' => 42,
'text' => 'Hello, world',
...
));
$this->assertEquals('...', $response['content']);
$this->assertContains('Content-type: text/html', $response['headers']);
}
²aÊÔ¸±±¾Ö»ÊÇÔÝʱµÄ£¡ËuÈÃÎÒÃDZaдµÄ²aÊÔ²»»a¸Ä±a¡£×iÖÕ£¬ÎÒÃÇÒª½«ÒѾͨ¹ý²aÊÔµÄPHP½Å±¾½øÐÐÖع¹£¬ÒÔÏu³ýÈßÓa´uÂe¡£
µ±ÎÒÃǵIJaÊÔÍe³Éºo£¬¿ÉÒÔ½«handleRequest()µÄÄÚÈÝÌae»»³ÉÕaeÕýµÄÂß¼´uÂe¡£¼ÙÈçÄaҪдºÜ¶aÕaÑuµÄ²aÊԽű¾£¬Äa¿ÉÒÔдһ¸oͨÓõIJaÊÔ¶ÔÏo£¬ÒÔÂu×aÄaµÄ²aÊÔÐeÒª¡£
**
¶þ¡¢PHP¿ª*¢Õߵĵ¥Ôª²aÊÔ¹¤¾ß°u
**
ÔÚPHPÁiÓo£¬µ¥Ôª²aÊԵŤ¾ßÖ÷ÒªÓÐPHPUNIT£¬PHPUNIT2ºÍSimpleTestÈýÖÖ¡£ÆaÖÐPHPUNITÔÚ¹¦ÄÜÉϺܼoµ¥£¬²»ËaÍeÉÆ£»PHPUNIT2ÊÇרÃÅΪPHP5дµÄµ¥Ôª²aÊÔ¹¤¾ß£¬ÔÚ½a¹¹ºÍ¹¦ÄÜÉ϶¼ÏoJunit¿´Æe£»¶øSimpleTestÔoÊÇÒ»Ì×*dz£ÊµÓõIJaÊÔ¹¤¾ß£¬ÆaÖеÄwebTestÖ§³Ö¶Ôweb³ÌÐo½çÃaeµÄ²aÊÔ£¬ÊÇEasy×iΪÍƼoµÄÒ»¿i²aÊÔ¹¤¾ß¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÑ¡ÔñSimpleTest½øÐнeÉÜ¡£
Ïa¹Ø֪ʶ£ºPHPUNIT2Ò²ÊÇÒ»¿iºÜºÃµÄ¹¤¾ß£¬ÓÈÆaÊǼܹ¹ÉÏÓкܶaÖµµÃȦµaÖ®´¦£¬Ï£Íu½«À´ÄÜÓлu»aÔÚרÃŵÄÎÄÕÂÖкʹo¼Ò*ÖÏi¡£
SimpleTest£º¾ÍÊÇÕaôSimple
°²×°SimpleTestºÜ¼oµ¥£¬ÉÏsf.netÉÏÏÂÔØÒ»¸oÔ´Âe°u£¬È»ºo½aѹµ½webĿ¼Ï¾ͿÉÒÔʹÓÃÁË£¬ÕaÀi¾Í²»¶a˵¡£
ÏÂÃaeÎÒÃÇÏÈÀ´¿´¸oÀý×Ó£º±aдһ¸o²aÊÔ£¬¼i²eÒ»¸oÍøÕ¾ÊÇñ¿ÉÒÔÃÎÊ¡£
Ê×ÏÈÎÒÃÇÒýÈeÒªÓõ½µÄÎļþ£º
´uÂeÁбi£º
require_once("../simpletest/unit_tester.php");
require_once("../simpletest/web_tester.php");
require_once("../simpletest/reporter.php");
È»ºoÎÒÃÇ´´½¨Ò»¸o²aÊÔÀa£º
´uÂeÁбi£º
class TestOfSite extends WebTestCase
{
function TestOfSite()
{
$this->WebTestCase("²aÊÔ");
}
function testSite()
{
$this->get("http://howgo.net/prettyface/display.php");
$this->assertTitle(".: facebook :.");
}
}
Ê×ÏÈÎÒÃÇÀ(C)Õ¹ÁËwebTestCaseÀa£¬ÕaÑuÎÒÃǾͿÉÒÔ×Ô¶¯»ñµÃ²aÊÔwebµÄÄÜÁ¦£¬È»ºoÔÚ¹¹Ôiº¯ÊýÖÐÎÒÃÇÖ±½ÓʹÓûuÀaµÄ£¬Ö»ÊǰѱeÌa´«¸øËu¡£½Ó×ÅÎÒÃǾ͸Ãд²aÊÔ½¨ÁË£¬²aÊÔ½¨¶¼ÊÇÒÔ¡®test"¿ªÍµÄ£¬ÓÃÒÔʶ±ðÔÚÎÒÃÇÔËÐвaÊÔµÄʱºo£¬ÀaÖÐÄÄÐ(C)½*¨Òª½øÐе÷Óá£
¶ø$this->get½«È¡µÃÍøÒ³µÄÄÚÈÝ£¬ÎÒÃÇÖ¸¶¨ËuµÄ±eÌaΪ ".: facebook :.", ½Ó×ÅÎÒÃÇÒª×oµÄ¾ÍÊÇʵÀý»¯Õa¸oÀaµÄ¶ÔÏo£¬²¢ÔËÐÐËu¡£
´uÂeÁбi£º
$test = &new; TestOfSite();
$test->run(new HtmlReporter());
ϱßÊÇÔËÐнa¹u£º
Èç¹u²aÊÔ³o´iÔo»a³oÏÖϱߵĽçÃae£º
µ½ÕaÀiÒ»¸o¼oµ¥µÄ²aÊÔ¾ÍËaÍe³ÉÁË¡£
ʵսÑÝÏ° ¨C Ò»¸oLogin²aÊÔ
ÏÂÃaeÎÒÃǽøÈeʵս£¬ÔÚÕa¸o»u´¡ÉÏÍe³ÉÒ»¸ologinµÄ²aÊÔ¡£Õa´ÎÎÒÃÇÏÈÌu³oÍeÕuµÄ´uÂe£º
´uÂeÁбi£º
require_once("../simpletest/unit_tester.php");
require_once("../simpletest/web_tester.php");
require_once("../simpletest/reporter.php");
class TestOfLogin extends WebTestCase
{
function TestOfLogin()
{
$this->WebTestCase("Login²aÊÔ");
}
function testLoginOk()
{
// È¡µÃÒ³Ãae
$this->get("http://howgo.net/prettyface/login.php");
// Ìi¼Ó²aÊÔ±iÏi
$this->setField("name","Easy");
$this->setField("pass","******");
// Ìa½»
$this->clickSubmit("Ìa½»");
// ²i¿´Ìa½»ºo*µ»ØÒ³ÃaeÊÇ*ñÕýÈ*
$this->assertWantedPattern("/³É¹¦µÇ¼/");
// µa»÷Ò³ÃaeÁ´½Ó
$this->clickLink("µa»÷ÕaÀi½øÈe¹ÜÀiÒ³Ãae");
// ²i¿´Ö¸¶¨Ò³Ãae±eÌaºÍ¹Ø¼uÄÚÈÝ
$this->assertTitle("ADMINCP");
$this->assertWantedPattern("/ÇeÑ¡ÔñÒª½øÐеÄÈÎÎñ/");
// Í˳oµÇ½
$this->clickLink("Í˳o¹ÜÀi");
$this->clickLink
}
}