tjtjtjのメモ

自分のためのメモです

yiiカイドを横断してみる リレーション(3/3) - テスト

今回は作成したテーブルにデータを登録し、モデルからDBのデータにアクセス可能かユニットテストを使って検証します。とりあえずテスト概要を読んでおいてください。PHPUnitをインストールしていなければしといてください。

テスト計画

まずどんなテストをするのか決めておきます。今回はモデル間のリレーションが想定どおり働くかどうかを検証します。

  • UserのProfileが取得できること
  • UserからPost(複数)を取得できること
  • Postのauthor(User)を取得できること
  • PostのCategory(複数)を取得できること

テストデータ作成 - フィクスチャマネージャの準備

フィクスチャを参照しつつ、main.php を変更します。毎回変更してますね。
/webapp/protrected/config/main.php

<?php
return array(
	'components'=>array(
		'fixture'=>array(  // components に fixture を加える
			'class'=>'system.test.CDbFixtureManager',
		),
	),
);

テストデータ作成 - フィクスチャを定義する

フィクスチャを定義するを見つつ、テストデータをフィクスチャファイルに書き出します。フィクスチャファイルはprefixを省略せずテーブル名と一致させます。tablePrefixを適用する方法はわからなかった。

/webapp/protected/tests/fixtures/tbl_user.php

<?php
return array(
	'user1'=>array(
		'username'=>'user1',
		'password'=>'user1password',
		'email'=>'user1@test.test',
	),
	'user2'=>array(
		'username'=>'user2',
		'password'=>'user2password',
		'email'=>'user2@test.test',
	),
);

/webapp/protected/tests/fixtures/tbl_profile.php

<?php
return array(
	'user1'=>array(
		'owner_id'=>'1',
		'photo'=>NULL, //file_get_contents
		'website'=>'user1 website',
	),
	'user2'=>array(
		'owner_id'=>'2',
		'photo'=>NULL,  //file_get_contents
		'website'=>'user2 website',
	),
);

/webapp/protected/tests/fixtures/tbl_post.php

<?php
return array(
	'post1'=>array(
		'title'=>'post1 title',
		'content'=>'post1 content',
		'create_time'=>getdate(),
		'author_id'=>1,
	),
	'post2'=>array(
		'title'=>'post2 title',
		'content'=>'post2 content',
		'create_time'=>getdate(),
		'author_id'=>1,
	),
);

/webapp/protected/tests/fixtures/tbl_post_category.php

<?php
return array(
	'post_category_1_1'=>array(
		'post_id'=>'1',
		'category_id'=>'1',
	),
	'post_category_1_2'=>array(
		'post_id'=>'1',
		'category_id'=>'2',
	),
);

/webapp/protected/tests/fixtures/tbl_category.php

<?php
return array(
	'category1'=>array(
		'name'=>'category1',
	),
	'category2'=>array(
		'name'=>'category2',
	),
);

テストケース作成

ユニットテストを参考にしつつ、テストケースを書きます。

/webapp/protected/tests/unit/models/UserTest.php

<?php
class UserTest extends CDbTestCase
{
	public $fixtures=array();

	public function testProfile()
	{
		$user = User::model()->findByPk(1);

		$this->assertNotNull($user);

		// UserのProfileが取得できること
		$this->assertNotNull($user->profile);
		$this->assertEquals("user1 website", $user->profile->website);


	}

	public function testPosts()
	{
		$user = User::model()->findByPk(1);

		$this->assertNotNull($user);

		// Userが登録したPost(複数)を取得できること
		$this->assertEquals(2, count($user->posts));
		$this->assertEquals("post1 title", $user->posts[0]->title);
		$this->assertEquals("post2 title", $user->posts[1]->title);

	}
}

/webapp/protected/tests/unit/models/PostTest.php

<?php
class PostTest extends CDbTestCase
{
	public $fixtures=array();

	public function testAuthor()
	{
		$post = Post::model()->findByPk(1);

		$this->assertNotNull($post);

		// Postのauthor(User)を取得できること
		$this->assertNotNull($post->author);
		$this->assertEquals("user1", $post->author->username);

	}

	public function testCategories()
	{
		$post = Post::model()->findByPk(1);

		$this->assertNotNull($post);

		// PostのCategory(複数)を取得できること
		$this->assertEquals(2, count($post->categories));
		$this->assertEquals("category1", $post->categories[0]->name);
		$this->assertEquals("category2", $post->categories[1]->name);

	}
}

テスト

テストです。次のようにユニットテストを実行しOKとなれば成功です。

>cd webapp\protected\tests
>phpunit unit\models\PostTest.php
PHPUnit 3.5.14 by Sebastian Bergmann.

..

Time: 2 seconds, Memory: 7.00Mb

OK (2 tests, 7 assertions)
>cd webapp\protected\tests
>phpunit unit\models\UserTest.php
PHPUnit 3.5.14 by Sebastian Bergmann.

..

Time: 1 second, Memory: 7.00Mb

OK (2 tests, 7 assertions)