DbTestTrait.php 3.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\test;

use Yii;

/**
 * DbTestTrait implements the commonly used methods for setting up and accessing fixture data.
 *
 * To use DbTestTrait, call the [[loadFixtures()]] method in the setup method in a test case class.
 * The specified fixtures will be loaded and accessible through [[getFixtureData()]] and [[getFixtureModel()]].
 *
 * For example,
 *
 * ~~~
 * use yii\test\DbTestTrait;
Qiang Xue committed
22
 * use yii\codeception\TestCase;
23 24 25
 * use app\models\Post;
 * use app\models\User;
 *
Qiang Xue committed
26
 * class PostTestCase extends TestCase
27 28 29
 * {
 *     use DbTestTrait;
 *
Qiang Xue committed
30
 *     protected function setUp()
31
 *     {
Qiang Xue committed
32 33
 *         parent::setUp();
 *
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
 *         $this->loadFixtures([
 *             'posts' => Post::className(),
 *             'users' => User::className(),
 *         ]);
 *     }
 * }
 * ~~~
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
trait DbTestTrait
{
	/**
	 * Loads the specified fixtures.
	 *
	 * This method should typically be called in the setup method of test cases so that
	 * the fixtures are loaded before running each test method.
	 *
	 * This method does the following things:
	 *
	 * - Run [[DbFixtureManager::initScript]] if it is found under [[DbFixtureManager::basePath]].
	 * - Clean up data and models loaded in memory previously.
	 * - Load each specified fixture:
	 *      * Truncate the corresponding table.
	 *      * If a fixture file named `TableName.php` is found under [[DbFixtureManager::basePath]],
	 *        the file will be executed, and the return value will be treated as rows which will
	 *        then be inserted into the table.
	 *
	 * @param array $fixtures a list of fixtures (fixture name => table name or AR class name) to be loaded.
	 * Each array element can be either a table name (with schema prefix if needed), or a fully-qualified
	 * ActiveRecord class name (e.g. `app\models\Post`). An element can be optionally associated with a key
	 * which will be treated as the fixture name. For example,
	 *
	 * ~~~
	 * [
	 *     'tbl_comment',
	 *     'users' => 'tbl_user',   // 'users' is the fixture name, 'tbl_user' is a table name
	 *     'posts' => 'app\models\Post,  // 'app\models\Post' is a model class name
	 * ]
	 * ~~~
	 *
	 * @return array the loaded fixture data (fixture name => table rows)
	 */
	public function loadFixtures(array $fixtures = [])
	{
		return $this->getFixtureManager()->load($fixtures);
	}

	/**
	 * Returns the DB fixture manager.
	 * @return DbFixtureManager the DB fixture manager
	 */
	public function getFixtureManager()
	{
		return Yii::$app->getComponent('fixture');
	}

	/**
	 * Returns the table rows of the named fixture.
	 * @param string $fixtureName the fixture name.
	 * @return array the named fixture table rows. False is returned if there is no such fixture data.
	 */
	public function getFixtureRows($fixtureName)
	{
		return $this->getFixtureManager()->getRows($fixtureName);
	}

	/**
	 * Returns the named AR instance corresponding to the named fixture.
	 * @param string $fixtureName the fixture name.
	 * @param string $modelName the name of the fixture data row
	 * @return \yii\db\ActiveRecord the named AR instance corresponding to the named fixture.
	 * Null is returned if there is no such fixture or the record cannot be found.
	 */
	public function getFixtureModel($fixtureName, $modelName)
	{
		return $this->getFixtureManager()->getModel($fixtureName, $modelName);
	}
}