Commit a5245ad0 by Ragazzo Committed by Qiang Xue

added array fixture class

parent 92f7b3fb
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\test;
use Yii;
use yii\base\ArrayAccessTrait;
use yii\base\InvalidParamException;
/**
* Base array fixture class, that supports loading data from data files.
*
* @author Mark Jebri <mark.github@yandex.ru>
* @since 2.0
*/
class ArrayFixture extends Fixture implements \IteratorAggregate, \ArrayAccess, \Countable
{
use ArrayAccessTrait;
/**
* @var array the data rows. Each array element represents one row of data (column name => column value).
*/
public $data = [];
/**
* @var string|boolean the file path or path alias of the data file that contains the fixture data
* to be returned by [[getData()]]. You can set this property to be false to prevent loading any data.
*/
public $dataFile;
/**
* Loads the fixture.
*
* The default implementation simply stores the data returned by [[getData()]] in [[data]].
* You should usually override this method by putting the data into the underlying database.
*/
public function load()
{
$this->data = $this->getData();
}
/**
* Returns the fixture data.
*
* The default implementation will try to return the fixture data by including the external file specified by [[dataFile]].
* The file should return the data array that will be stored in [[data]] after inserting into the database.
*
* @return array the data to be put into the database
* @throws InvalidConfigException if the specified data file does not exist.
*/
protected function getData()
{
if ($this->dataFile === false || $this->dataFile === null) {
return [];
}
$dataFile = Yii::getAlias($this->dataFile);
if (is_file($dataFile)) {
return require($dataFile);
} else {
throw new InvalidConfigException("Fixture data file does not exist: {$this->dataFile}");
}
}
/**
* @inheritdoc
*/
public function unload()
{
parent::unload();
$this->data = [];
}
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit\framework\test;
use yiiunit\TestCase;
use yii\test\ArrayFixture;
class ArrayFixtureTest extends TestCase
{
/**
* @var \yii\test\ArrayFixture
*/
private $_fixture;
protected function setUp()
{
parent::setUp();
$this->_fixture = new ArrayFixture();
}
public function testLoadUnloadParticularFile()
{
$this->_fixture->dataFile = '@yiiunit/framework/test/data/array_fixture.php';
$this->assertEmpty($this->_fixture->data, 'fixture data should be empty');
$this->_fixture->load();
$this->assertCount(2, $this->_fixture->data, 'fixture data should match needed total count');
$this->assertEquals('customer1', $this->_fixture['customer1']['name'], 'first fixture data should match');
$this->assertEquals('customer2@example.com', $this->_fixture['customer2']['email'], 'second fixture data should match');
}
public function testNothingToLoad()
{
$this->_fixture->dataFile = false;
$this->assertEmpty($this->_fixture->data, 'fixture data should be empty');
$this->_fixture->load();
$this->assertEmpty($this->_fixture->data, 'fixture data should not be loaded');
}
/**
* @expectedException yii\base\InvalidParamException
*/
public function testWrongDataFileException()
{
$this->_fixture->dataFile = '@wrong/fixtures/data/path/alias';
$this->_fixture->load();
}
}
<?php
return [
'customer1' => [
'email' => 'customer1@example.com',
'name' => 'customer1',
'address' => 'address1',
'status' => 1,
],
'customer2' => [
'email' => 'customer2@example.com',
'name' => 'customer2',
'address' => 'address2',
'status' => 2,
],
];
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment