Last active
December 26, 2015 14:29
-
-
Save masayuki5160/7165646 to your computer and use it in GitHub Desktop.
PHPUnitをテストしたときのメモをずらずら。
This file contains 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 | |
/** | |
* Created by JetBrains PhpStorm. | |
* User: masayuki5160 | |
* Date: 2013/10/26 | |
* Time: 13:19 | |
* To change this template use File | Settings | File Templates. | |
*/ | |
require_once 'DB.php'; | |
class Register { | |
protected $age, $name; | |
public function save() | |
{ | |
$dsn = 'mysql://root:@localhost/PHP'; | |
$db = DB::connect($dsn); | |
if (PEAR::isError($db)) { | |
die($db->getMessage()); | |
} | |
$sql = "INSERT INTO REGIST_LOG_TBL (USER_ID) VALUES (?)"; | |
$stmt = $db->prepare($sql); | |
$data = array($this->name); | |
$db->execute($stmt, $data); | |
$db->disconnect(); | |
return true; | |
} | |
public function setAge($age) | |
{ | |
$this->age = $age; | |
} | |
public function setName($name) | |
{ | |
$this->name = $name; | |
} | |
} |
This file contains 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
/* DB作成 */ | |
CREATE DATABASE PHP DEFAULT CHARACTER SET utf8; | |
/* テスト用のtable作成 */ | |
CREATE TABLE IF NOT EXISTS `REGIST_LOG_TBL` ( | |
`LOG_ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '主キー', | |
`USER_ID` varchar(16) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL COMMENT 'ユーザID', | |
`UPD_DATE` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新日時', | |
PRIMARY KEY (`LOG_ID`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED COMMENT='PHP Unit Testログテーブル' AUTO_INCREMENT=1 ; | |
This file contains 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 | |
/** | |
* Created by JetBrains PhpStorm. | |
* User: masayuki5160 | |
* Date: 2013/10/26 | |
* Time: 13:11 | |
* To change this template use File | Settings | File Templates. | |
*/ | |
/** PHPUnitのヘッダファイルををインクルード */ | |
require_once 'PHPUnit/Autoload.php'; | |
/** テスト対象のPHPファイル */ | |
require_once 'Register.php'; | |
class RegisterTest extends PHPUnit_Framework_TestCase{ | |
public function setUp() | |
{ | |
$this->Register = new Register; | |
} | |
public function testRegistUser(){ | |
// プロパティをセット | |
$this->Register->setName('MASAYUKI TANAKA'); | |
$this->Register->setAge(27); | |
// 保存の結果テスト | |
$result = $this->Register->save(); | |
$this->assertTrue($result); | |
} | |
} |
This file contains 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
【テスト用環境】 | |
cent os 6.4 | |
php 5.3 | |
PHPUnit 3.7.28 | |
【いろいろ準備】 | |
# yum install -y php php-mysql php-pear | |
# yum install php-dom –enablerepo=epel –enablerepo=remi | |
# pear config-set auto_discover 1 | |
# pear install pear.phpunit.de/PHPUnit | |
# pear install DB | |
※php-mysqlとpear DBはテストようにインストール | |
このあとDBの方の準備。 | |
使用したSQLは別ファイルに記載してます。 | |
【テストの実行&結果】 | |
# ls -l | |
-rw-r--r-- 1 root root 817 10月 26 05:18 2013 Register.php | |
-rw-r--r-- 1 root root 770 10月 26 04:52 2013 RegisterTest.php | |
# phpunit RegisterTest.php | |
PHPUnit 3.7.28 by Sebastian Bergmann. | |
. | |
Time: 14 ms, Memory: 4.50Mb | |
OK (1 test, 1 assertion) | |
【参考】 | |
PHPUnitでユニットテスト http://www.atmarkit.co.jp/ait/articles/0911/12/news105.html | |
PHPUnitでテスト http://d.hatena.ne.jp/hateka/20121017/1350486293 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment