今回は,
既存クラスからテストを作成する
さて,
<?php
require_once 'Cart.php';
class Checkout {
private $cart;
public function __construct(Cart $cart) {
$this->cart = $cart;
}
public function getSubTotal() {
return $this->cart->getTotal();
}
public function getShippingCharge() {
if ($this->cart->getTotal() > 1500) {
return 0;
} else {
return 315;
}
}
public function getTotal() {
return $this->cart->getTotal() + $this->getShippingCharge();
}
public function getCart() {
return $this->cart;
}
}
しかし,
PHPUnit3には既存クラスから自動的にテストケースを生成するジェネレータが用意されています。今回はこれを使ってテストケースを作ってみましょう。
既存クラスからテストケースを生成するには,
$ phpunit --skeleton Checkout PHPUnit 3.1.7 by Sebastian Bergmann. Wrote test class skeleton for "Checkout" to "CheckoutTest.php". $
生成されたCheckoutTest.
<?php
// Call CheckoutTest::main() if this source file is executed directly.
if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'CheckoutTest::main');
}
require_once 'PHPUnit/Framework.php';
require_once 'Checkout.php';
/**
* Test class for Checkout.
* Generated by PHPUnit on 2007-08-23 at 15:27:59.
*/
class CheckoutTest extends PHPUnit_Framework_TestCase {
/**
* Runs the test methods of this class.
*
* @access public
* @static
*/
public static function main() {
require_once 'PHPUnit/TextUI/TestRunner.php';
$suite = new PHPUnit_Framework_TestSuite('CheckoutTest');
$result = PHPUnit_TextUI_TestRunner::run($suite);
}
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*
* @access protected
*/
protected function setUp() {
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*
* @access protected
*/
protected function tearDown() {
}
/**
* @todo Implement testGetSubTotal().
*/
public function testGetSubTotal() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testGetShippingCharge().
*/
public function testGetShippingCharge() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testGetTotal().
*/
public function testGetTotal() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testGetCart().
*/
public function testGetCart() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
}
// Call CheckoutTest::main() if this source file is executed directly.
if (PHPUnit_MAIN_METHOD == 'CheckoutTest::main') {
CheckoutTest::main();
}
?>
生成されたCheckoutTest.
$ phpunit CheckoutTest PHPUnit 3.1.7 by Sebastian Bergmann. IIII Time: 0 seconds OK, but incomplete or skipped tests! Tests: 4, Incomplete: 4. $