dao コマンド
説明
S2Dao で必要な dao インタフェース、entity クラス、テストクラスを生成します。
実行例
dao コマンドの説明では、S2DaoのExampleで使用されているサンプルデータを用いて説明します。
mysql> desc CD; +---------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+--------------+------+-----+---------+-------+ | ID | int(11) | NO | PRI | | | | TITLE | varchar(100) | YES | | | | | CONTENT | varchar(200) | YES | | | | +---------+--------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> select * from CD; +----+----------+---------+ | ID | TITLE | CONTENT | +----+----------+---------+ | 1 | S2Dao!!! | hello!! | +----+----------+---------+ 1 row in set (0.00 sec) mysql>
s2base.php5 ディレクトリで s2base コマンドを実行します。
% s2base
[INFO ] s2base directory : /seasar.php/workspace/s2base.php5/lib/S2Base/build/s2base.php5
[INFO ] project directory : /seasar.php/workspace/s2base.php5
[INFO ] project type : command
[INFO ] command type : command
[ Command list ]
0 : (exit)
1 : dao
2 : dicon
3 : entity
4 : service & dao
5 : interceptor
6 : module
7 : service
choice ? : 1 <--- 1 : dao を選択
[ Module list ]
0 : (exit)
1 : Default
choice ? : 1 <--- 1 : Default モジュールを選択
use database ? (y/n) : n <--- データベースに接続してテーブル情報を取得
するかどうかを選択します。
dao interface name ? : CdDao <--- 英数字 [_a-zA-Z0-9] が使用可能です。
entity class name ? : CdEntity <--- 英数字 [_a-zA-Z0-9] が使用可能です。
table name ? [CdEntity] : CD <--- データベースのテーブル名を設定。
columns ? [id,name,--, , ] : id,title,content <--- カンマ区切りでカラム名を入力
[ generate information ]
module name : Default
dao interface name : CdDao
dao test class name : CdDaoTest
entity class name : CdEntity
table name : CD
columns : id, title, content
confirm ? (y/n) : y <--- 表示される情報を確認
[INFO ] create : /seasar.php/workspace/s2base.php5/app/modules/Default/dao/CdDao.class.php
[INFO ] create : /seasar.php/workspace/s2base.php5/test/modules/Default/dao/CdDaoTest.class.php
[INFO ] create : /seasar.php/workspace/s2base.php5/app/modules/Default/entity/CdEntity.class.php
[ Command list ]
0 : (exit)
1 : command
・・・
%
daoコマンドにより、PHPUnitを用いたUnitTestクラスが作成されます。s2base コマンドに test 引数を付けて実行すると、UnitTestが実行されます。
% s2base test .+Test <--- 第3引数に正規表現で実行したいテストを指定できます。 [INFO ] s2base directory : /seasar.php/workspace/s2base.php5/lib/S2Base/build/s2base.php5 [INFO ] project directory : /seasar.php/workspace/s2base.php5 [INFO ] project type : command [INFO ] command type : test [INFO ] test target : .+Test PHPUnit 3.1.7 by Sebastian Bergmann. CdDaoTest::testA . Time: 0 seconds OK (1 test) %dao コマンドにより生成されるDaoインタフェースは、デフォルトでいくつかのメソッドが定義済みです。
% more app/modules/Default/dao/CdDao.php
<?php
interface CdDao {
const BEAN = "CdEntity";
public function findAllList();
//public function findAllArray();
//public function update(CdEntity $entity);
//public function insert(CdEntity $entity);
//public function delete(CdEntity $entity);
}
%
例として、findAllListメソッドを実行してみます。まず、テストクラスにテストメソッドを追加します。
% more test/modules/Default/dao/CdDaoTest.php
<?php
class CdDaoTest extends PHPUnit_Framework_TestCase {
private $module = "Default";
private $container; <--- 各テストの前処理でS2Containerオブジェクトが設定されます。
private $dao; <--- 各テストの前処理でDaoオブジェクトが設定されます。
public function __construct($name) {
parent::__construct($name);
S2ContainerApplicationContext::init();
}
public function testFindAllList() { <--- 追加テストメソッド
$rows = $this->dao->findAllList();
$entity = $rows[0];
$this->assertTrue($entity instanceof CdEntity);
$this->assertEquals($entity->getId(),'1');
$this->assertEquals($entity->getTitle(),'S2Dao!!!');
$this->assertEquals($entity->getContent(),'hello!!');
print_r($entity);
}
public function setUp(){
print __CLASS__ . '::' . $this->getName() . PHP_EOL;
$moduleDir = S2BASE_PHP5_ROOT . "/app/modules/{$this->module}";
require_once($moduleDir . "/{$this->module}.inc.php");
$this->container = S2ContainerApplicationContext::create();
$this->dao = $this->container->getComponent("CdDao");
}
public function tearDown() {
print PHP_EOL;
$this->container = null;
$this->dao = null;
}
}
%
テストを実行すると、データベースから取得したデータを格納したEntitiyオブジェクトが表示されます。
% s2base test .+Test
[INFO ] s2base directory : /seasar.php/workspace/s2base.php5/lib/S2Base/build/s2base.php5
[INFO ] project directory : /seasar.php/workspace/s2base.php5
[INFO ] project type : command
[INFO ] command type : test
[INFO ] test target : .+Test
PHPUnit 3.1.7 by Sebastian Bergmann.
CdDaoTest::testFindAllList
CdEntity Object <--- print_r($entity) でデバッグとして表示しています。
(
[id:protected] => 1
[title:protected] => S2Dao!!!
[content:protected] => hello!!
)
.
Time: 0 seconds
OK (1 test)
%
dicon コマンド
説明
XML形式のdiconファイルを生成します。
実行例
s2base.php5 ディレクトリで s2baseコマンド を実行します。
% s2base [INFO ] s2base directory : /seasar.php/workspace/s2base.php5/lib/S2Base/build/s2base.php5 [INFO ] project directory : /seasar.php/workspace/s2base.php5 [INFO ] project type : command [INFO ] command type : command [ Command list ] 0 : (exit) 1 : dao 2 : dicon 3 : entity 4 : service & dao 5 : interceptor 6 : module 7 : service choice ? : 2 <--- 2 : dicon を選択 [ Module list ] 0 : (exit) 1 : Default choice ? : 1 <--- 1 : Default モジュールを選択 dicon name ? : sample <--- 英数字 [_a-zA-Z0-9] が使用可能です。 [ generate information ] module name : Default dicon file name : sample.dicon confirm ? (y/n) : y <--- 表示される情報を確認 [INFO ] create : /seasar.php/workspace/s2base.php5/app/modules/Default/dicon/sample.dicon [ Command list ] 0 : (exit) 1 : command ・・・ %sample.dicon は空のダイコンファイルです。
% more app/modules/Default/dicon/sample.dicon <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container//EN" "http://www.seasar.org/dtd/components21.dtd"> <components> </components> %
entity コマンド
説明
S2Dao で用いる Entity クラスを作成します。columns でカンマ区切りのカラム名を入力すると、対応するプロパティとアクセッサメソッドを追加します。
実行例
s2base.php5 ディレクトリで s2baseコマンド を実行します。
% s2base
[INFO ] s2base directory : /seasar.php/workspace/s2base.php5/lib/S2Base/build/s2base.php5
[INFO ] project directory : /seasar.php/workspace/s2base.php5
[INFO ] project type : command
[INFO ] command type : command
[ Command list ]
0 : (exit)
1 : dao
2 : dicon
3 : entity
4 : service & dao
5 : interceptor
6 : module
7 : service
choice ? : 3 <--- 3 : entity を選択
[ Module list ]
0 : (exit)
1 : Default
choice ? : 1 <--- 1 : Default モジュールを選択
use database ? (y/n) : n <--- データベースに接続してテーブル情報を取得
するかどうかを選択します。
entity class name ? : DvdEntity <--- 英数字 [_a-zA-Z0-9] が使用可能です。
table name ? [DvdEntity] : DVD <--- データベースのテーブル名を設定
columns ? [id,name,--, , ] : id,title,content <--- カンマ区切りでカラム名を入力
[ generate information ]
module name : Default
entity class name : DvdEntity
table name : DVD
columns : id, title, content
confirm ? (y/n) : y <--- 表示される情報を確認
[INFO ] create : /seasar.php/workspace/s2base.php5/app/modules/Default/entity/DvdEntity.php
[ Command list ]
0 : (exit)
1 : command
・・・
%
columns で、id、title、content を指定したので、3つのプロパティとそれぞのアクセッサメソッドが実装されます。
% more app/modules/Default/entity/DvdEntity.php
<?php
class DvdEntity {
const TABLE = "DVD";
public function __construct(){}
protected $id;
const id_COLUMN = "id";
public function setId($val){$this->id = $val;}
public function getId(){return $this->id;}
protected $title;
const title_COLUMN = "title";
public function setTitle($val){$this->title = $val;}
public function getTitle(){return $this->title;}
protected $content;
const content_COLUMN = "content";
public function setContent($val){$this->content = $val;}
public function getContent(){return $this->content;}
public function __toString() {
$buf = array();
$buf[] = 'id => ' . $this->getId();
$buf[] = 'title => ' . $this->getTitle();
$buf[] = 'content => ' . $this->getContent();
return '{' . implode(', ',$buf) . '}';
}
}
%
service & dao コマンド
説明
service & dao コマンドはサービス名を決めると、命名規則に従ってサービス、dao、enitity、ダイコンファイル、テストクラスを生成します。
service コマンドと dao コマンドを同時に実行することとほぼ同じ処理になります。
例えば、サービス名を「 emp 」とすると、サービスは EmpService、dao は EmpDao、entity は EmpEntitiy となります。
実行例
s2base.php5 ディレクトリで s2baseコマンド を実行します。
% s2base
[INFO ] s2base directory : /seasar.php/workspace/s2base.php5/lib/S2Base/build/s2base.php5
[INFO ] project directory : /seasar.php/workspace/s2base.php5
[INFO ] project type : command
[INFO ] command type : command
[ Command list ]
0 : (exit)
1 : dao
2 : dicon
3 : entity
4 : service & dao
5 : interceptor
6 : module
7 : service
choice ? : 4 <--- 4 : service & dao を選択
[ Module list ]
0 : (exit)
1 : Default
choice ? : 1 <--- 1 : Default モジュールを選択
service name ? : emp <--- 英数字 [_a-zA-Z0-9] が使用可能です。
use database ? (y/n) : n <--- データベースに接続してテーブル情報を取得
するかどうかを選択します。
table name ? [emp] : EMP <--- データベースのテーブル名を設定
columns ? [id,name,--, , ] : id,name,dept <--- カンマ区切りでカラム名を入力
[ generate information ]
module name : Default
service name : emp
service class name : EmpService
service test class name : EmpServiceTest
dao interface name : EmpDao
dao test class name : EmpDaoTest
entity class name : EmpEntity
entity class extends : none
table name : EMP
columns : id, name, dept
confirm ? (y/n) : y <--- 表示される情報を確認
[INFO ] create : /seasar.php/workspace/s2base.php5/app/modules/Default/service/EmpService.php
[INFO ] create : /seasar.php/workspace/s2base.php5/test/modules/Default/service/EmpServiceTest.php
[INFO ] create : /seasar.php/workspace/s2base.php5/test/modules/Default/dao/EmpDaoTest.php
[INFO ] create : /seasar.php/workspace/s2base.php5/app/modules/Default/dao/EmpDao.php
[INFO ] create : /seasar.php/workspace/s2base.php5/app/modules/Default/entity/EmpEntity.php
[ Command list ]
0 : (exit)
1 : command
・・・
%
EmpServiceクラスでは EmpDao へのセッターメソッドが定義済みなので、サービスに dao が自動インジェクションされます。
% more app/modules/Default/service/EmpService.php
<?php
class EmpService {
private $empDao;
public function __construct(){}
public function setEmpDao(EmpDao $dao){
$this->empDao = $dao;
}
}
%
interceptor コマンド
説明
S2Aop の MethodInterceptor を実装するクラスを生成します。
実行例
s2base.php5 ディレクトリで s2base コマンドを実行します。
% s2base [INFO ] s2base directory : /seasar.php/workspace/s2base.php5/lib/S2Base/build/s2base.php5 [INFO ] project directory : /seasar.php/workspace/s2base.php5 [INFO ] project type : command [INFO ] command type : command [ Command list ] 0 : (exit) 1 : dao 2 : dicon 3 : entity 4 : service & dao 5 : interceptor 6 : module 7 : service choice ? : 5 <--- 5 : interceptor を選択 [ Module list ] 0 : (exit) 1 : Default choice ? : 1 <--- 1 : Default モジュールを選択 interceptor class name ? : HogeInterceptor <--- 英数字 [_a-zA-Z0-9] が使用可能です。 [ generate information ] module name : Default interceptor class name : HogeInterceptor confirm ? (y/n) : y <--- 表示される情報を確認 [INFO ] create : /seasar.php/workspace/s2base.php5/app/modules/Default/interceptor/HogeInterceptor.php [ Command list ] 0 : (exit) 1 : dao ・・・ %S2Container_AbstractInterceptor を継承した interceptor を生成します。参照:独自実装によるInterceptor
% more app/modules/Default/interceptor/HogeInterceptor.php
<?php
class HogeInterceptor
extends S2Container_AbstractInterceptor {
/**
* @param S2Container_MethodInvocation $invocation
* - $invocation->getThis() : return target object
* - $invocation->getMethod() : return ReflectionMethod of target method
* - $invocation->getArguments() : return array of method arguments
*/
public function invoke(S2Container_MethodInvocation $invocation) {
return $invocation->proceed();
}
}
%
module コマンド
説明
app/modules ディレクトリと test/modules ディレクトリにモジュールディレクトリを作成します。 app/modules/モジュールディレクトリには、dao、dicon、entity、interceptor、serviceディレクトリが作成されます。
実行例
s2base.php5 ディレクトリで s2baseコマンド を実行します。
% s2base [INFO ] s2base directory : /seasar.php/workspace/s2base.php5/lib/S2Base/build/s2base.php5 [INFO ] project directory : /seasar.php/workspace/s2base.php5 [INFO ] project type : command [INFO ] command type : command [ Command list ] 0 : (exit) 1 : dao 2 : dicon 3 : entity 4 : service & dao 5 : interceptor 6 : module 7 : service choice ? : 6 <--- 6 : module を選択 module name ? : Default <--- モジュール名を入力。英数字 [_a-zA-Z0-9] が使用可能です。 [ generate information ] module name : Default <--- モジュール情報を確認 confirm ? (y/n) : y [INFO ] create : /seasar.php/workspace/s2base.php5/app/modules/Default [INFO ] create : /seasar.php/workspace/s2base.php5/app/modules/Default/dao [INFO ] create : /seasar.php/workspace/s2base.php5/app/modules/Default/dicon [INFO ] create : /seasar.php/workspace/s2base.php5/app/modules/Default/entity [INFO ] create : /seasar.php/workspace/s2base.php5/app/modules/Default/interceptor [INFO ] create : /seasar.php/workspace/s2base.php5/app/modules/Default/service [INFO ] create : /seasar.php/workspace/s2base.php5/test/modules/Default [INFO ] create : /seasar.php/workspace/s2base.php5/test/modules/Default/dao [INFO ] create : /seasar.php/workspace/s2base.php5/test/modules/Default/service [INFO ] create : /seasar.php/workspace/s2base.php5/app/modules/Default/Default.inc.php [ Command list ] 0 : (exit) 1 : command ・・・ %app ディレクトリと test ディレクトリ以下にDefaultモジュールが作成されます。
% ls app/modules/Default Default.inc.php dao dicon entity interceptor service % ls test/modules/Default dao service %
service コマンド
説明
サービスクラス、テストクラスを生成します。
実行例
s2base.php5 ディレクトリで s2base コマンドを実行します。
% s2base [INFO ] s2base directory : /seasar.php/workspace/s2base.php5/lib/S2Base/build/s2base.php5 [INFO ] project directory : /seasar.php/workspace/s2base.php5 [INFO ] project type : command [INFO ] command type : command [ Command list ] 0 : (exit) 1 : dao 2 : dicon 3 : entity 4 : service & dao 5 : interceptor 6 : module 7 : service choice ? : 7 <--- 7 : service を選択 [ Module list ] 0 : (exit) 1 : Default choice ? : 1 <--- 1 : Default モジュールを選択 service interface name ? : CulcService <--- 英数字 [_a-zA-Z0-9] が使用可能です。 [ generate information ] module name : Default service class name : CulcService service test class name : CulcServiceTest confirm ? (y/n) : y <--- 表示される情報を確認 [INFO ] create : /seasar.php/workspace/s2base.php5/app/modules/Default/service/CulcService.php [INFO ] create : /seasar.php/workspace/s2base.php5/test/modules/Default/service/CulcServiceTest.php [ Command list ] 0 : (exit) 1 : command ・・・ %service コマンドにより PHPUnit を用いた UnitTest クラスが作成されます。 s2base コマンドに test 引数を付けて実行すると UnitTest を実行できます。
% s2base test CulcService [INFO ] s2base directory : /seasar.php/workspace/s2base.php5/lib/S2Base/build/s2base.php5 [INFO ] project directory : /seasar.php/workspace/s2base.php5 [INFO ] project type : command [INFO ] command type : test [INFO ] test target : CulcService PHPUnit 3.1.7 by Sebastian Bergmann. CulcServiceTest::testA . Time: 0 seconds OK (1 test) %例として、サービスクラスに add メソッドを追加します。
% more app/modules/Default/service/CulcService.php
<?php
class CulcService {
public function __construct(){}
public function add($a, $b) { <--- 足し算メソッドを追加
return $a + $b;
}
}
テストクラスに add メソッドをテストするテストメソッドを追加します。
% more test/modules/Default/service/CulcServiceTest.php
<?php
class CulcServiceImplTest extends PHPUnit_Framework_TestCase {
private $module = "Default";
private $container; <--- 各テストの前処理でS2Containerオブジェクトが設定されます。
private $service; <--- 各テストの前処理でサービスオブジェクトが設定されます。
public function __construct($name) {
parent::__construct($name);
}
public function testAdd() { <--- テストメソッドを追加
$a = 2;
$b = 3;
$c = $this->service->add($a,$b);
$this->assertEquals($c,5);
print "$a + $b = $c" . PHP_EOL;
}
public function setUp(){
print __CLASS__ . '::' . $this->getName() . PHP_EOL;
$moduleDir = S2BASE_PHP5_ROOT . "/app/modules/{$this->module}";
include_once($moduleDir . "/{$this->module}.inc.php");
$this->container = S2ContainerApplicationContext::create();
$this->service = $this->container->getComponent('CulcService');
}
public function tearDown() {
$this->container = null;
$this->service = null;
}
}
%
s2base コマンドに test 引数を付けて実行すると UnitTest を実行できます。
% s2base test CulcService [INFO ] s2base directory : /seasar.php/workspace/s2base.php5/lib/S2Base/build/s2base.php5 [INFO ] project directory : /seasar.php/workspace/s2base.php5 [INFO ] project type : command [INFO ] command type : test [INFO ] test target : CulcService PHPUnit 3.1.7 by Sebastian Bergmann. CulcServiceTest::testAdd 2 + 3 = 5 <--- デバッグとして、足し算式を表示します。 . Time: 0 seconds OK (1 test) %