mirror of
https://github.com/yiisoft/yii2.git
synced 2026-03-10 09:17:39 +01:00
Fixes #14546: Added dataDirectory property into BaseActiveFixture
This commit is contained in:
committed by
Alexander Makarov
parent
57b31fc92c
commit
810e9e5337
@@ -3,6 +3,8 @@ Yii Framework 2 Change Log
|
||||
|
||||
2.0.14 under development
|
||||
------------------------
|
||||
|
||||
- Enh #14546: Added `dataDirectory` property into `BaseActiveFixture` (leandrogehlen)
|
||||
- Bug #15522: Fixed `yii\db\ActiveRecord::refresh()` method does not use an alias in the condition (vladis84)
|
||||
- Enh #15476: Added `\yii\widgets\ActiveForm::$validationStateOn` to be able to specify where to add class for invalid fields (samdark)
|
||||
- Enh #13996: Added `yii\web\View::registerJsVar()` method that allows registering JavaScript variables (Eseperio, samdark)
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
namespace yii\test;
|
||||
|
||||
use Yii;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\db\TableSchema;
|
||||
|
||||
@@ -95,10 +94,15 @@ class ActiveFixture extends BaseActiveFixture
|
||||
protected function getData()
|
||||
{
|
||||
if ($this->dataFile === null) {
|
||||
$class = new \ReflectionClass($this);
|
||||
$dataFile = dirname($class->getFileName()) . '/data/' . $this->getTableSchema()->fullName . '.php';
|
||||
|
||||
return is_file($dataFile) ? require $dataFile : [];
|
||||
if ($this->dataDirectory !== null) {
|
||||
$dataFile = $this->getTableSchema()->fullName . '.php';
|
||||
} else {
|
||||
$class = new \ReflectionClass($this);
|
||||
$dataFile = dirname($class->getFileName()) . '/data/' . $this->getTableSchema()->fullName . '.php';
|
||||
}
|
||||
|
||||
return $this->loadData($dataFile);
|
||||
}
|
||||
|
||||
return parent::getData();
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
namespace yii\test;
|
||||
|
||||
use Yii;
|
||||
use yii\base\ArrayAccessTrait;
|
||||
use yii\base\InvalidConfigException;
|
||||
|
||||
@@ -22,16 +21,12 @@ use yii\base\InvalidConfigException;
|
||||
class ArrayFixture extends Fixture implements \IteratorAggregate, \ArrayAccess, \Countable
|
||||
{
|
||||
use ArrayAccessTrait;
|
||||
use FileFixtureTrait;
|
||||
|
||||
/**
|
||||
* @var array the data rows. Each array element represents one row of data (column name => column value).
|
||||
*/
|
||||
public $data = [];
|
||||
/**
|
||||
* @var string|bool the file path or [path alias](guide:concept-aliases) 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;
|
||||
|
||||
|
||||
/**
|
||||
@@ -56,15 +51,7 @@ class ArrayFixture extends Fixture implements \IteratorAggregate, \ArrayAccess,
|
||||
*/
|
||||
protected function getData()
|
||||
{
|
||||
if ($this->dataFile === false || $this->dataFile === null) {
|
||||
return [];
|
||||
}
|
||||
$dataFile = Yii::getAlias($this->dataFile);
|
||||
if (is_file($dataFile)) {
|
||||
return require $dataFile;
|
||||
}
|
||||
|
||||
throw new InvalidConfigException("Fixture data file does not exist: {$this->dataFile}");
|
||||
return $this->loadData($this->dataFile);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
namespace yii\test;
|
||||
|
||||
use Yii;
|
||||
use yii\base\ArrayAccessTrait;
|
||||
use yii\base\InvalidConfigException;
|
||||
|
||||
@@ -22,6 +21,7 @@ use yii\base\InvalidConfigException;
|
||||
abstract class BaseActiveFixture extends DbFixture implements \IteratorAggregate, \ArrayAccess, \Countable
|
||||
{
|
||||
use ArrayAccessTrait;
|
||||
use FileFixtureTrait;
|
||||
|
||||
/**
|
||||
* @var string the AR model class associated with this fixture.
|
||||
@@ -31,18 +31,11 @@ abstract class BaseActiveFixture extends DbFixture implements \IteratorAggregate
|
||||
* @var array the data rows. Each array element represents one row of data (column name => column value).
|
||||
*/
|
||||
public $data = [];
|
||||
/**
|
||||
* @var string|bool the file path or [path alias](guide:concept-aliases) 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;
|
||||
|
||||
/**
|
||||
* @var \yii\db\ActiveRecord[] the loaded AR models
|
||||
*/
|
||||
private $_models = [];
|
||||
|
||||
|
||||
/**
|
||||
* Returns the AR model by the specified model name.
|
||||
* A model name is the key of the corresponding data row in [[data]].
|
||||
@@ -87,23 +80,13 @@ abstract class BaseActiveFixture extends DbFixture implements \IteratorAggregate
|
||||
/**
|
||||
* 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.
|
||||
* @see [[loadDataFile]]
|
||||
*/
|
||||
protected function getData()
|
||||
{
|
||||
if ($this->dataFile === false || $this->dataFile === null) {
|
||||
return [];
|
||||
}
|
||||
$dataFile = Yii::getAlias($this->dataFile);
|
||||
if (is_file($dataFile)) {
|
||||
return require $dataFile;
|
||||
}
|
||||
|
||||
throw new InvalidConfigException("Fixture data file does not exist: {$this->dataFile}");
|
||||
return $this->loadData($this->dataFile);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
namespace yii\test;
|
||||
|
||||
use Yii;
|
||||
use yii\base\BaseObject;
|
||||
use yii\db\Connection;
|
||||
use yii\di\Instance;
|
||||
|
||||
59
framework/test/FileFixtureTrait.php
Normal file
59
framework/test/FileFixtureTrait.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?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\InvalidConfigException;
|
||||
|
||||
/**
|
||||
* FileFixtureTrait provides functionalities for loading data fixture from file.
|
||||
*
|
||||
* @author Leandro Guindani Gehlen <leandrogehlen@gmail.com>
|
||||
* @since 2.0.14
|
||||
*/
|
||||
trait FileFixtureTrait
|
||||
{
|
||||
/**
|
||||
* @var string the directory path or [path alias](guide:concept-aliases) that contains the fixture data
|
||||
* @since 2.0.14
|
||||
*/
|
||||
public $dataDirectory;
|
||||
/**
|
||||
* @var string|bool the file path or [path alias](guide:concept-aliases) 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;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param string $file the data file path
|
||||
* @return array the data to be put into the database
|
||||
* @throws InvalidConfigException if the specified data file does not exist.
|
||||
*/
|
||||
protected function loadData($file)
|
||||
{
|
||||
if ($file === false || $file === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (basename($file) == $file && $this->dataDirectory !== null) {
|
||||
$file = $this->dataDirectory . '/' . $file;
|
||||
}
|
||||
|
||||
$file = Yii::getAlias($file);
|
||||
if (is_file($file)) {
|
||||
return require $file;
|
||||
}
|
||||
|
||||
throw new InvalidConfigException("Fixture data file does not exist: {$file}");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,7 +27,15 @@ class CustomerFixture extends ActiveFixture
|
||||
];
|
||||
}
|
||||
|
||||
class MyDbTestCase
|
||||
class CustomDirectoryFixture extends ActiveFixture
|
||||
{
|
||||
public $modelClass = 'yiiunit\data\ar\Customer';
|
||||
|
||||
public $dataDirectory = '@app/framework/test/custom';
|
||||
}
|
||||
|
||||
|
||||
class BaseDbTestCase
|
||||
{
|
||||
use FixtureTrait;
|
||||
|
||||
@@ -39,7 +47,10 @@ class MyDbTestCase
|
||||
public function tearDown()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class CustomerDbTestCase extends BaseDbTestCase
|
||||
{
|
||||
public function fixtures()
|
||||
{
|
||||
return [
|
||||
@@ -48,6 +59,29 @@ class MyDbTestCase
|
||||
}
|
||||
}
|
||||
|
||||
class CustomDirectoryDbTestCase extends BaseDbTestCase
|
||||
{
|
||||
public function fixtures()
|
||||
{
|
||||
return [
|
||||
'customers' => CustomDirectoryFixture::className(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class DataPathDbTestCase extends BaseDbTestCase
|
||||
{
|
||||
public function fixtures()
|
||||
{
|
||||
return [
|
||||
'customers' => [
|
||||
'class' => CustomDirectoryFixture::className(),
|
||||
'dataFile' => '@app/framework/test/data/customer.php'
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @group fixture
|
||||
* @group db
|
||||
@@ -71,7 +105,7 @@ class ActiveFixtureTest extends DatabaseTestCase
|
||||
|
||||
public function testGetData()
|
||||
{
|
||||
$test = new MyDbTestCase();
|
||||
$test = new CustomerDbTestCase();
|
||||
$test->setUp();
|
||||
$fixture = $test->getFixture('customers');
|
||||
|
||||
@@ -90,7 +124,7 @@ class ActiveFixtureTest extends DatabaseTestCase
|
||||
|
||||
public function testGetModel()
|
||||
{
|
||||
$test = new MyDbTestCase();
|
||||
$test = new CustomerDbTestCase();
|
||||
$test->setUp();
|
||||
$fixture = $test->getFixture('customers');
|
||||
|
||||
@@ -105,4 +139,32 @@ class ActiveFixtureTest extends DatabaseTestCase
|
||||
|
||||
$test->tearDown();
|
||||
}
|
||||
|
||||
public function testDataDirectory()
|
||||
{
|
||||
$test = new CustomDirectoryDbTestCase();
|
||||
|
||||
$test->setUp();
|
||||
$fixture = $test->getFixture('customers');
|
||||
$directory = $fixture->getModel('directory');
|
||||
|
||||
$this->assertEquals(1, $directory->id);
|
||||
$this->assertEquals('directory@example.com', $directory['email']);
|
||||
$test->tearDown();
|
||||
|
||||
}
|
||||
|
||||
public function testDataPath()
|
||||
{
|
||||
$test = new DataPathDbTestCase();
|
||||
|
||||
$test->setUp();
|
||||
$fixture = $test->getFixture('customers');
|
||||
$customer = $fixture->getModel('customer1');
|
||||
|
||||
$this->assertEquals(1, $customer->id);
|
||||
$this->assertEquals('customer1@example.com', $customer['email']);
|
||||
$test->tearDown();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
15
tests/framework/test/custom/customer.php
Normal file
15
tests/framework/test/custom/customer.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
return [
|
||||
'directory' => [
|
||||
'email' => 'directory@example.com',
|
||||
'name' => 'directory name',
|
||||
'address' => 'directory-address1',
|
||||
'status' => 1
|
||||
]
|
||||
];
|
||||
Reference in New Issue
Block a user