From 810e9e53370e31fa45dbda1ed4ff7ebf4e2ac754 Mon Sep 17 00:00:00 2001 From: Leandro Gehlen Date: Sat, 3 Feb 2018 10:11:11 -0200 Subject: [PATCH] Fixes #14546: Added `dataDirectory` property into `BaseActiveFixture` --- framework/CHANGELOG.md | 2 + framework/test/ActiveFixture.php | 12 ++++-- framework/test/ArrayFixture.php | 17 +------- framework/test/BaseActiveFixture.php | 23 ++-------- framework/test/DbFixture.php | 1 - framework/test/FileFixtureTrait.php | 59 ++++++++++++++++++++++++++ tests/framework/test/ActiveFixtureTest.php | 68 ++++++++++++++++++++++++++++-- tests/framework/test/custom/customer.php | 15 +++++++ 8 files changed, 154 insertions(+), 43 deletions(-) create mode 100644 framework/test/FileFixtureTrait.php create mode 100644 tests/framework/test/custom/customer.php diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 8100918..8fea4a4 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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) diff --git a/framework/test/ActiveFixture.php b/framework/test/ActiveFixture.php index 87eff0f..b89e732 100644 --- a/framework/test/ActiveFixture.php +++ b/framework/test/ActiveFixture.php @@ -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(); diff --git a/framework/test/ArrayFixture.php b/framework/test/ArrayFixture.php index 3ce967d..ea0c000 100644 --- a/framework/test/ArrayFixture.php +++ b/framework/test/ArrayFixture.php @@ -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); } /** diff --git a/framework/test/BaseActiveFixture.php b/framework/test/BaseActiveFixture.php index 1e7a778..ccdc7e1 100644 --- a/framework/test/BaseActiveFixture.php +++ b/framework/test/BaseActiveFixture.php @@ -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. @@ -32,17 +32,10 @@ abstract class BaseActiveFixture extends DbFixture implements \IteratorAggregate */ 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); } /** diff --git a/framework/test/DbFixture.php b/framework/test/DbFixture.php index bc10e69..098219e 100644 --- a/framework/test/DbFixture.php +++ b/framework/test/DbFixture.php @@ -7,7 +7,6 @@ namespace yii\test; -use Yii; use yii\base\BaseObject; use yii\db\Connection; use yii\di\Instance; diff --git a/framework/test/FileFixtureTrait.php b/framework/test/FileFixtureTrait.php new file mode 100644 index 0000000..2313acc --- /dev/null +++ b/framework/test/FileFixtureTrait.php @@ -0,0 +1,59 @@ + + * @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}"); + } + +} diff --git a/tests/framework/test/ActiveFixtureTest.php b/tests/framework/test/ActiveFixtureTest.php index fe46136..edcfeb2 100644 --- a/tests/framework/test/ActiveFixtureTest.php +++ b/tests/framework/test/ActiveFixtureTest.php @@ -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(); + + } } diff --git a/tests/framework/test/custom/customer.php b/tests/framework/test/custom/customer.php new file mode 100644 index 0000000..065bc3c --- /dev/null +++ b/tests/framework/test/custom/customer.php @@ -0,0 +1,15 @@ + [ + 'email' => 'directory@example.com', + 'name' => 'directory name', + 'address' => 'directory-address1', + 'status' => 1 + ] +];