Created
September 4, 2011 12:28
-
-
Save nissuk/1192791 to your computer and use it in GitHub Desktop.
PHPUnit 3.5.15: データベースのテストをする単純な例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ExampleDbTest extends PHPUnit_Extensions_Database_TestCase { | |
protected $conn = null; | |
/** | |
* (non-PHPdoc) | |
* @see PHPUnit_Extensions_Database_TestCase::setUp() | |
*/ | |
public function setUp() { | |
// setUp()をオーバーライドするときは parent::setUp() をしないと | |
// テーブルのクリーンアップとフィクスチャの読み込みがされません… | |
parent::setUp(); | |
} | |
/** | |
* (non-PHPdoc) | |
* @see PHPUnit_Extensions_Database_TestCase::getConnection() | |
*/ | |
public function getConnection() { | |
if (is_null($this->conn)) { | |
// 定数はphpunit.xmlの /phpunit/php/const 要素で定義できます(@nameが定数名、@valueが値)。 | |
$pdo = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD); | |
$this->conn = $this->createDefaultDBConnection($pdo); | |
} | |
return $this->conn; | |
} | |
/** | |
* (non-PHPdoc) | |
* @see PHPUnit_Extensions_Database_TestCase::getDataSet() | |
*/ | |
public function getDataSet() { | |
// getConnection()とgetDataSet()を定義することによって、 | |
// テーブルのクリーンアップとフィクスチャの読み込みが行われます。 | |
// この例では、1行目が列名、2行目以降がデータ(行)形式のCSVから、 | |
// usersテーブルへテストデータを読み込むよう定義します。 | |
$dataset = new PHPUnit_Extensions_Database_DataSet_CsvDataSet(); | |
$dataset->addTable('users', dirname(__FILE__) . '/fixtures/users.csv'); | |
return $dataset; | |
} | |
/** | |
* DataSetが読み込めているかテストします(必須ではありません)。 | |
*/ | |
public function testInitDataset() { | |
$this->assertDataSetsEqual( | |
$this->getDataSet(), | |
$this->getConnection()->createDataSet(array('users'))); | |
} | |
// 以降はgetConnection()で指定したデータベースと同じデータベースを使用してテストが行えます。 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" ?> | |
<phpunit> | |
<php> | |
<const name="DB_DSN" value="mysql:host=test.localhost;dbname=test" /> | |
<const name="DB_USERNAME" value="test" /> | |
<const name="DB_PASSWORD" value="test" /> | |
</php> | |
</phpunit> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id | name | |
---|---|---|
1 | one | |
2 | two | |
3 | three | |
4 | four | |
5 | five |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment