|
|
@@ -0,0 +1,82 @@
|
|
|
+<?xml version="1.0" encoding="UTF-8"?>
|
|
|
+<!-- Reviewed: no -->
|
|
|
+<!-- EN-Revision: 17054 -->
|
|
|
+<sect2 id="zend.test.phpunit.db.adapter">
|
|
|
+ <title>データベース・テスト・アダプタの使用</title>
|
|
|
+
|
|
|
+ <para>
|
|
|
+ アプリケーションの一部を本当のデータベースでテストしたくなくても、
|
|
|
+ 結合度のせいでせざるを得ないときもあります。
|
|
|
+ <classname>Zend_Test_DbAdapter</classname>では、
|
|
|
+ データベース接続を開始する必要なしに、
|
|
|
+ <classname>Zend_Db_Adapter_Abstract</classname>の実装を使う便利な方法を提供します。
|
|
|
+ さらに、それはコンストラクタ引数を必要としないので、
|
|
|
+ このアダプタではPHPUnitテストスイート内から非常に簡単にモックアップを作れます。
|
|
|
+ </para>
|
|
|
+
|
|
|
+ <para>
|
|
|
+ テスト・アダプタは、いろいろなデータベース結果のためのスタックとして動作します。
|
|
|
+ 結果のその順序は実装したユーザー側の担当でなければいけません。
|
|
|
+ そして、それは多くの異なるデータベース・クエリを呼ぶテストのための退屈な仕事であるかもしれません。
|
|
|
+ しかし、少数のクエリだけが実行されるヘルパーが、テストにはまさに適切なヘルパーです。
|
|
|
+ あなたはユーザー担当のコードに返されなければならない結果の正確な順序を知っています。
|
|
|
+ </para>
|
|
|
+
|
|
|
+ <programlisting language="php"><![CDATA[
|
|
|
+$adapter = new Zend_Test_DbAdapter();
|
|
|
+$stmt1Rows = array(array('foo' => 'bar'), array('foo' => 'baz'));
|
|
|
+$stmt1 = Zend_Test_DbStatement::createSelectStatement($stmt1Rows);
|
|
|
+$adapter->appendStatementToStack($stmt1);
|
|
|
+
|
|
|
+$stmt2Rows = array(array('foo' => 'bar'), array('foo' => 'baz'));
|
|
|
+$stmt2 = Zend_Test_DbStatement::createSelectStatement($stmt2Rows);
|
|
|
+$adapter->appendStatementToStack($stmt2);
|
|
|
+
|
|
|
+$rs = $adapter->query('SELECT ...'); // Returns Statement 2
|
|
|
+while ($row = $rs->fetch()) {
|
|
|
+ echo $rs['foo']; // Prints "Bar", "Baz"
|
|
|
+}
|
|
|
+$rs = $adapter->query('SELECT ...'); // Returns Statement 1
|
|
|
+]]></programlisting>
|
|
|
+
|
|
|
+ <para>
|
|
|
+ 本当のデータベース・アダプタいずれの振る舞いでも、
|
|
|
+ できる限り<methodname>fetchAll()</methodname>、<methodname>fetchObject()</methodname>、
|
|
|
+ 及び<methodname>fetchColumn</methodname>などのように、
|
|
|
+ そのようなメソッドができる限りテスト・アダプタとして動作するように
|
|
|
+ シミュレーションされます。
|
|
|
+ </para>
|
|
|
+
|
|
|
+ <para>
|
|
|
+ 結果スタックにINSERT、UPDATE及びDELETE命令を入れることもできます。
|
|
|
+ しかしながらそれらは、
|
|
|
+ <methodname>$stmt->rowCount()</methodname>の結果を指定できる命令だけを返します。
|
|
|
+ </para>
|
|
|
+
|
|
|
+ <programlisting language="php"><![CDATA[
|
|
|
+$adapter = new Zend_Test_DbAdapter();
|
|
|
+$adapter->appendStatementToStack(
|
|
|
+ Zend_Test_DbStatement::createInsertStatement(1)
|
|
|
+);
|
|
|
+$adapter->appendStatementToStack(
|
|
|
+ Zend_Test_DbStatement::createUpdateStatement(2)
|
|
|
+);
|
|
|
+$adapter->appendStatementToStack(
|
|
|
+ Zend_Test_DbStatement::createDeleteStatement(10
|
|
|
+));
|
|
|
+]]></programlisting>
|
|
|
+
|
|
|
+ <para>
|
|
|
+ テスト・アダプタは、指定されたクエリが本当に、
|
|
|
+ スタックから次に返されるSELECT、DELETE、INSERTまたはUPDATEタイプかどうかは
|
|
|
+ 決して調べません。
|
|
|
+ データを返す正しい順位は、テスト・アダプタのユーザーによって実装されなければなりません。
|
|
|
+ </para>
|
|
|
+
|
|
|
+ <para>
|
|
|
+ テスト・アダプタでは、
|
|
|
+ <methodname>listTables()</methodname>や<methodname>describeTables()</methodname>
|
|
|
+ そして<methodname>lastInsertId()</methodname>メソッドを使用をシミュレーションするために、
|
|
|
+ メソッドも指定します
|
|
|
+ </para>
|
|
|
+</sect2>
|