PHPUnit ã¨ã㧠assertTrue ã¨ãé¢åã ãã at ã¨ãã«ããã©ã¤ãããã¯
ããï¼å¤åããã¯ãã¦ãã®ã¯ã©ã¤ããããªãã¨ããããã§ããã©ï¼ã¾ã¼ã©ã¤ãããã¯ãã¤ã¤ããã¯ããã£ã¦ãããã§ï¼
ã¾ã¼ãã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ã£ã¦ããããããã¨ããããã§ããã©ï¼PHPUnitããªããã«ãã³ã¤ã¤ãã使ã£ã¦ã¿ã¦ããæè¿ã§ãï¼
ã§ï¼ãã£ã±ãè²ã
é¢åãªã®ã¯ï¼ assertTrue ã¨ãã§ãããï¼
æå°ã®ãã¹ãã³ã¼ãã£ã¦ä»¥ä¸ã®ããã«ãªã£ã¦ãã¨æããã§ããï¼(マニュアルããæç²)
<?php require_once 'PHPUnit/Framework.php'; class StackTest extends PHPUnit_Framework_TestCase { public function testPushAndPop() { $stack = array(); $this->assertEquals(0, count($stack)); array_push($stack, 'foo'); $this->assertEquals('foo', $stack[count($stack)-1]); $this->assertEquals(1, count($stack)); $this->assertEquals('foo', array_pop($stack)); $this->assertEquals(0, count($stack)); } }
$this->assertEquels ã¨ãçç¥ããã
ã§ï¼ãã® assertEquals ã¨ããã¡ãã¡æã£ã¦ãããªãããï¼ã¨ãããã ae ã¨ãã«ã¨ã¤ãªã¢ã¹ãä½ãããã¨æãã¾ãï¼
<?php require_once 'PHPUnit/Framework.php'; class TestBase extends PHPUnit_Framework_TestCase { protected $aliases = array( 'at' => 'assertTrue', 'af' => 'assertFalse', 'ae' => 'assertEquals', // ... ); public function __call($name, $args) { if (array_key_exists($name, $this->aliases)) { call_user_func_array(array($this, $this->aliases[$name]), $args); } } }
ãããªãã®ãç¨æãã¦ããã¦ï¼ãããç¶æ¿ãã¦ãã¹ãã±ã¼ã¹ãã¤ããã°ï¼ä»¥ä¸ã®ããã«ã§ãã¾ããï¼alias ã é£æ³é åã§å®ç¾©ãã¦ãããã®ã¯æåã§ããã© ... ã¾ãä¸åº¦æ¸ãã¦ãã¾ãã°ããã¡ãã§ããï¼
<?php class StackTest extends TestBase { public function testPushAndPop() { $stack = array(); $this->ae(0, count($stack)); array_push($stack, 'foo'); $this->ae('foo', $stack[count($stack)-1]); $this->ae(1, count($stack)); $this->ae('foo', array_pop($stack)); $this->ae(0, count($stack)); } }
ããï¼å°ããã·ã«ãªãã¾ããï¼
ãã£ã¨çç¥ããã
ã§ãããã使ã£ã¦ãã¨ï¼ $this->ae ã¨ãã鬱é¶ãããªã£ã¦ãã¾ãï¼$this-> ã¨ããããªãã§ããããï¼
ã¨ããããã§ï¼ããã«ä»¥ä¸ã®ããã«ï¼é¢æ°ãå®ç¾©ãã¾ãï¼debug_backtrace ã§ãªãã¸ã§ã¯ããã¨ããã®ãç´ æµã§ããï¼
<?php function ae ($v1, $v2) { $bt = debug_backtrace(); $bt[1]['object']->assertEquals($v1, $v2); } function at ($v) { $bt = debug_backtrace(); $bt[1]['object']->assertTrue($v); } function af ($v) { $bt = debug_backtrace(); $bt[1]['object']->assertFalse($v); }
ãããããããasserté¢æ°åç¨æããã®ã¯ããã©ãããããã¡ã¿ããã°ã©ãã³ã°ãããã¨ããã§ããï¼ï¼ï¼
ã¨æã£ããã©ã¾ã¼ãããä¸åº¦æ¸ãã¦ãã¾ãã°ãã話ãªã®ã§ï¼ã¨ã·ã¨ãã¾ãããï¼
ããã«ãã£ã¦ï¼ãã¹ãã±ã¼ã¹ã§ã¯ï¼ä»¥ä¸ã®ããã«ãããããã«ãªãã¾ããï¼
<?php class StackTest extends TestBase { public function testPushAndPop() { $stack = array(); ae(0, count($stack)); array_push($stack, 'foo'); ae('foo', $stack[count($stack)-1]); ae(1, count($stack)); ae('foo', array_pop($stack)); ae(0, count($stack)); } }
ããï¼ãããªããã£ã±ããã¹ãæ¸ããããªãã¾ããï¼
DocTest 使ãã°ããã¨æãã
ã¨ãã£ã¦ãã¾ã£ããããè¿ãè¨èããªãï¼
ããï¼ã§ã PHPUnit çµæ§ããã§ãã
ã¢ããã¼ã·ã§ã³ã¤ããã¦ï¼ã¹ã¼ãã¼ã°ãã¼ãã«ã®ããã¯ã¢ãããã¨ã£ããï¼ä¾åã®å®ç¾©ãç°¡åã«ã§ãããï¼
ã¨ãããã㧠Let's Testing!