Files
yii2/framework/test/Fixture.php
2014-01-16 22:36:38 -05:00

129 lines
3.0 KiB
PHP

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\test;
use yii\base\Component;
/**
* Fixture represents a fixed state of a test environment.
*
* Each fixture instance represents a particular aspect of a test environment. For example,
* you may use `UserFixture` to initialize the user database table with a set of known data. You may
* load the fixture when running every test method so that the user table always contains the fixed data
* and thus allows your test predictable and repeatable.
*
* A fixture may depend on other fixtures, specified via the [[depends]] property. When a fixture is being loaded,
* its dependent fixtures will be automatically loaded BEFORE the fixture; and when the fixture is being unloaded,
* its dependent fixtures will be unloaded AFTER the fixture.
*
* You should normally override [[load()]] to specify how to set up a fixture; and override [[unload()]]
* for clearing up a fixture.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Fixture extends Component
{
/**
* @var array the fixtures that this fixture depends on. This must be a list of the dependent
* fixture class names.
*/
public $depends = [];
/**
* Loads the fixture.
* This method is called before performing every test method.
* You should override this method with concrete implementation about how to set up the fixture.
*/
public function load()
{
}
/**
* This method is called BEFORE any fixture data is loaded for the current test.
*/
public function beforeLoad()
{
}
/**
* This method is called AFTER all fixture data have been loaded for the current test.
*/
public function afterLoad()
{
}
/**
* Unloads the fixture.
* This method is called after every test method finishes.
* You may override this method to perform necessary cleanup work for the fixture.
*/
public function unload()
{
}
}
class TestCase extends \Codeception\TestCase\Test
{
public $fixtures;
protected function setUp()
{
parent::setUp();
$this->mockApplication();
$this->loadFixtures();
}
protected function tearDown()
{
$this->unloadFixtures();
$this->destroyApplication();
parent::tearDown();
}
protected function getFixture($name)
{
// return the named fixture object
}
public function fixtures()
{
return [
// anonymous fixture
PostFixture::className(),
// "users" fixture
'users' => UserFixture::className(),
// "cache" fixture with configuration
'cache' => [
'class' => CacheFixture::className(),
'host' => 'xxx',
],
];
}
public function testSave()
{
$user = $this->getFixture('users')->getModel('user1');
$rows = $this->getFixture('users')->getRows();
// or support magic property like the following:
$user = $this->users->getModel('user1');
}
}
class UserFixture extends DbFixture
{
public $modelClass = 'app\models\User';
protected function loadData($table)
{
return require(__DIR__ . '/' . $table->name . '.php');
}
}