Zend_Test_PHPUnit_Db(日本語)データ・アクセスとドメインモデルを組み合わせると、 目標をテストするために、データベースを使う必要がしばしばあります。 しかし、データベースはそれぞれのテスト全体で永続的です。 そして、それは互いに影響を及ぼすことができるテスト結果に至ります。 さらにまた、テストが動作できるようにするためにデータベースを準備することは、 相当な作業です。 PHPUnitデータベース機能拡張では、 それぞれのテストの間でデータベースを準備したり、取り外したりするための、 きわめて単純な手法を提供することにより、 データベースを用いたテストを単純化します。 Zend Frameworkアプリケーションに対する データベース・テストを書くことが単純化されるように、 このコンポーネントは、Zend Frameworkに依存したコードでPHPUnitデータベース機能拡張を拡張します。 データベース・テストは、2つの概念上の実体、DataSets及びDataTablesで説明できます。 内部的には、PHPUnitデータベース機能拡張は、データベース、そのテーブルと、 構成ファイルまたは本当のデータベース内容からなる列を含むオブジェクト構造を構築できます。 そこで、この抽象的なオブジェクト・グラフは、位置指定子を使用して比較できます。 データベース・テストの一般的なユース・ケースは、 種となるデータで一部のテーブルを準備し、 それから操作を一部実行して、 データベース階層で操作されたことが、 あらかじめ定義された期待される、とある状態と等しいことを最終的に示すことです。 Zend_Test_PHPUnit_Dbは、 既存のZend_Db_Table_Abstract またはZend_Db_Table_Rowset_Abstract インスタンスからDataSets及びDataTablesを生成できるようにして、 この作業を単純化します。 さらにまた、このコンポーネントは、どんなZend_Db_Adapter_Abstractでも テストのために統合できるようにします。 ところが、本来の機能拡張は、PDOで機能するだけです。 Zend_Db_Adapter_Abstractのためのテスト・アダプタ実装は、 このコンポーネントにも含まれます。 APIメソッドによって使われる SQLと結果スタックの働きをするDBアダプタを データベースを全く必要としないで、 インスタンス化できるようにします。 QuickstartSetup a Database TestCaseWe are now writting some database tests for the Bug Database example in the Zend_Db_Table documentation. First we begin to test that inserting a new bug is actually saved in the database correctly. First we have to setup a test-class that extends Zend_Test_PHPUnit_DatabaseTestCase. This class extends the PHPUnit Database Extension, which in turn extends the basic PHPUnit_Framework_TestCase. A database testcase contains two abstract methods that have to be implemented, one for the database connection and one for the initial dataset that should be used as seed or fixture.
Here we create the database connection and seed some data into the database. Some important details should be noted on this code:
Specify a seed datasetIn the previous setup for the database testcase we have specified a seed file for the database fixture. We now create this file specified in the Flat XML format:
We will work with this four entries in the database table "zfbugs" in the next examples. The required MySQL schema for this example is:
A few initial database testsNow that we have implemented the two required abstract methods of the Zend_Test_PHPUnit_DatabaseTestCase and specified the seed database content, which will be re-created for each new test, we can go about to make our first assertion. This will be a test to insert a new bug.
Now up to the $bugsTable->insert($data); everything looks familiar. The lines after that contain the assertion methodname. We want to verify that after inserting the new bug the database has been updated correctly with the given data. For this we create a Zend_Test_PHPUnit_Db_DataSet_QueryDataSet instance and give it a database connection. We will then tell this dataset that it contains a table "zfbugs" which is given by an SQL statement. This current/actual state of the database is compared to the expected database state which is contained in another XML file "bugsInsertIntoAssertions.xml". This XML file is a slight deviation from the one given above and contains another row with the expected data:
There are other ways to assert that the current database state equals an expected state. The "Bugs" table in the example already knows a lot about its inner state, so why not use this to our advantage? The next example will assert that deleting from the database is possible:
We have created a Zend_Test_PHPUnit_Db_DataSet_DbTableDataSet dataset here, which takes any Zend_Db_Table_Abstract instance and adds it to the dataset with its table name, in this example "zfbugs". You could add several tables more if you wanted using the method addTable() if you want to check for expected database state in more than one table. Here we only have one table and check against an expected database state in "bugsDeleteAssertion.xml" which is the original seed dataset without the row with id 4. Since we have only checked that two specific tables (not datasets) are equal in the previous examples we should also look at how to assert that two tables are equal. Therefore we will add another test to our TestCase which verifies updating behaviour of a dataset.
Here we create the current database state from a Zend_Db_Table_Rowset_Abstract instance in conjunction with the Zend_Test_PHPUnit_Db_DataSet_DbRowset($rowset) instance which creates an internal data-representation of the rowset. This can again be compared against another data-table by using the $this->assertTablesEqual() assertion. Usage, API and Extensions PointsThe Quickstart already gave a good introduction on how database testing can be done using PHPUnit and the Zend Framework. This section gives an overview over the API that the Zend_Test_PHPUnit_Db component comes with and how it works internally.
The Zend_Test_PHPUnit_DatabaseTestCase classThe Zend_Test_PHPUnit_DatabaseTestCase class derives from the PHPUnit_Extensions_Database_TestCase which allows to setup tests with a fresh database fixture on each run easily. The Zend implementation offers some additional convenience features over the PHPUnit Database extension when it comes to using Zend_Db resources inside your tests. The workflow of a database test-case can be described as follows.
The Zend_Test_PHPUnit_DatabaseTestCase class has some convenience functions that can help writing tests that interact with the database and the database testing extension. The next table lists only the new methods compared to the PHPUnit_Extensions_Database_TestCase, whose » API is documented in the PHPUnit Documentation.
Integrating Database Testing with the ControllerTestCaseBecause PHP does not support multiple inheritance it is not possible to use the Controller and Database testcases in conjunction. However you can use the Zend_Test_PHPUnit_Db_SimpleTester database tester in your controller test-case to setup a database enviroment fixture for each new controller test. The Database TestCase in general is only a set of convenience functions which can also be accessed and used without the test case. Example #1 Database integration example This example extends the User Controller Test from the Zend_Test_PHPUnit_ControllerTestCase documentation to include a database setup.
Now the Flat XML dataset "initialUserFixture.xml" is used to set the database into an initial state before each test, exactly as the DatabaseTestCase works internally. データベース・テスト・アダプタの使用アプリケーションの一部を本当のデータベースでテストしたくなくても、 結合度のせいでせざるを得ないときもあります。 Zend_Test_DbAdapter では、 データベース接続を開始する必要なしに、 Zend_Db_Adapter_Abstract の実装を使う便利な方法を提供します。 さらに、それはコンストラクタ引数を必要としないので、 このアダプタではPHPUnitテストスイート内から非常に簡単にモックアップを作れます。 テスト・アダプタは、いろいろなデータベース結果のためのスタックとして動作します。 結果のその順序は実装したユーザー側の担当でなければいけません。 そして、それは多くの異なるデータベース・クエリを呼ぶテストのための退屈な仕事であるかもしれません。 しかし、少数のクエリだけが実行されるヘルパーが、テストにはまさに適切なヘルパーです。 あなたはユーザー担当のコードに返されなければならない結果の正確な順序を知っています。
本当のデータベース・アダプタいずれの振る舞いでも、 できる限り fetchAll() 、 fetchObject() 、 及び fetchColumn などのように、 そのようなメソッドができる限りテスト・アダプタとして動作するように シミュレーションされます。 結果スタックにINSERT、UPDATE及びDELETE命令を入れることもできます。 しかしながらそれらは、 $stmt->rowCount() の結果を指定できる命令だけを返します。 クエリ・プロファイラはデフォルトで有効で、 正しく実行されたか検査するために、 実行した SQL 文とそのバウンドされたパラメータを取得できます。
テスト・アダプタは、指定されたクエリが本当に、 スタックから次に返されるSELECT、DELETE、INSERTまたはUPDATEタイプかどうかは 決して調べません。 データを返す正しい順位は、テスト・アダプタのユーザーによって実装されなければなりません。 テスト・アダプタでは、 listTables() 、 describeTables() 及び lastInsertId() メソッドの使用をシミュレーションするための メソッドの指定も行います。 さらに、 setQuoteIdentifierSymbol() を使用して、 引用符で囲むために使用するべき記号を指定できます。既定では何も使用されません。
|