From 719a721ebe07413ae885b2326354d32088496b89 Mon Sep 17 00:00:00 2001 From: kate-kate Date: Mon, 20 Jan 2014 18:50:38 +0200 Subject: [PATCH 1/5] active fixture for mongodb extension --- extensions/mongodb/ActiveFixture.php | 111 +++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 extensions/mongodb/ActiveFixture.php diff --git a/extensions/mongodb/ActiveFixture.php b/extensions/mongodb/ActiveFixture.php new file mode 100644 index 0000000000..c3d4fa1e4b --- /dev/null +++ b/extensions/mongodb/ActiveFixture.php @@ -0,0 +1,111 @@ +modelClass) && !isset($this->collectionName)) { + throw new InvalidConfigException('Either "modelClass" or "collectionName" must be set.'); + } + } + + + /** + * Loads the fixture data. + * The default implementation will first reset the DB table and then populate it with the data + * returned by [[getData()]]. + */ + protected function loadData() + { + if ($this->resetCollection) { + $this->resetCollection(); + } + foreach ($this->getData() as $alias => $row) { + $this->getCollection()->insert($row); + $this->data[$alias] = $row; + } + } + + protected function getCollection() + { + return $this->db->getCollection($this->getCollectionName()); + } + + protected function getCollectionName() + { + if ($this->collectionName) { + return $this->collectionName; + } else { + $modelClass = $this->modelClass; + return $modelClass::collectionName(); + } + } + + /** + * Returns the fixture data. + * + * This method is called by [[loadData()]] to get the needed fixture data. + * + * The default implementation will try to return the fixture data by including the external file specified by [[dataFile]]. + * The file should return an array of data rows (column name => column value), each corresponding to a row in the table. + * + * If the data file does not exist, an empty array will be returned. + * + * @return array the data rows to be inserted into the collection. + */ + protected function getData() + { + if ($this->dataFile === false) { + return []; + } + if ($this->dataFile !== null) { + $dataFile = Yii::getAlias($this->dataFile); + } else { + $class = new \ReflectionClass($this); + $dataFile = dirname($class->getFileName()) . '/data/' . $this->getCollectionName() . '.php'; + } + return is_file($dataFile) ? require($dataFile) : []; + } + + /** + * Removes all existing data from the specified collection and resets sequence number if any. + * This method is called before populating fixture data into the collection associated with this fixture. + */ + protected function resetCollection() + { + $this->getCollection()->remove(); + } +} \ No newline at end of file From a4b6eeca7e2f7c38582ff4cff84b44aeaf40c1c5 Mon Sep 17 00:00:00 2001 From: kate-kate Date: Mon, 20 Jan 2014 19:03:44 +0200 Subject: [PATCH 2/5] refactoring according to the new version --- extensions/mongodb/ActiveFixture.php | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/extensions/mongodb/ActiveFixture.php b/extensions/mongodb/ActiveFixture.php index c3d4fa1e4b..8241dd8c5a 100644 --- a/extensions/mongodb/ActiveFixture.php +++ b/extensions/mongodb/ActiveFixture.php @@ -16,7 +16,6 @@ class ActiveFixture extends \yii\test\BaseActiveFixture */ public $collectionName; - public $loadSchema = false; /** * @var string the file path or path alias of the data file that contains the fixture data * and will be loaded by [[loadData()]]. If this is not set, it will default to `FixturePath/data/TableName.php`, @@ -24,12 +23,6 @@ class ActiveFixture extends \yii\test\BaseActiveFixture * name of the table associated with this fixture. */ public $dataFile; - /** - * @var boolean whether to reset the collection associated with this fixture. - * By setting this property to be true, when [[loadData()]] is called, all existing data in the table - * will be removed and the sequence number (if any) will be reset. - */ - public $resetCollection = true; /** * @inheritdoc @@ -48,11 +41,10 @@ class ActiveFixture extends \yii\test\BaseActiveFixture * The default implementation will first reset the DB table and then populate it with the data * returned by [[getData()]]. */ - protected function loadData() + public function load() { - if ($this->resetCollection) { - $this->resetCollection(); - } + $this->resetCollection(); + foreach ($this->getData() as $alias => $row) { $this->getCollection()->insert($row); $this->data[$alias] = $row; From 0d35c269fe74ffee6efb95183cea96fd57a2852b Mon Sep 17 00:00:00 2001 From: kate-kate Date: Mon, 20 Jan 2014 20:02:51 +0200 Subject: [PATCH 3/5] using batchInsert --- extensions/mongodb/ActiveFixture.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/extensions/mongodb/ActiveFixture.php b/extensions/mongodb/ActiveFixture.php index 8241dd8c5a..fbc1d2c109 100644 --- a/extensions/mongodb/ActiveFixture.php +++ b/extensions/mongodb/ActiveFixture.php @@ -44,9 +44,8 @@ class ActiveFixture extends \yii\test\BaseActiveFixture public function load() { $this->resetCollection(); - + $this->getCollection()->batchInsert($this->getData()); foreach ($this->getData() as $alias => $row) { - $this->getCollection()->insert($row); $this->data[$alias] = $row; } } From d5d810dd869e72a3b25e9202c71973e88342f4e8 Mon Sep 17 00:00:00 2001 From: kate-kate Date: Tue, 21 Jan 2014 00:54:55 +0200 Subject: [PATCH 4/5] local data variable and some code style fixation --- extensions/mongodb/ActiveFixture.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/extensions/mongodb/ActiveFixture.php b/extensions/mongodb/ActiveFixture.php index fbc1d2c109..83099f6ed1 100644 --- a/extensions/mongodb/ActiveFixture.php +++ b/extensions/mongodb/ActiveFixture.php @@ -7,15 +7,16 @@ use yii\base\InvalidConfigException; class ActiveFixture extends \yii\test\BaseActiveFixture { - + /** + * @var Connection|string the DB connection object or the application component ID of the DB connection. + */ public $db = 'mongodb'; /** - * @var string the name of the collection that this fixture is about. If this property is not set, + * @var string|array the collection name that this fixture is about. If this property is not set, * the table name will be determined via [[modelClass]]. - * @see modelClass + * @see [[yii\mongodb\Connection::getCollection()]] */ public $collectionName; - /** * @var string the file path or path alias of the data file that contains the fixture data * and will be loaded by [[loadData()]]. If this is not set, it will default to `FixturePath/data/TableName.php`, @@ -24,6 +25,7 @@ class ActiveFixture extends \yii\test\BaseActiveFixture */ public $dataFile; + /** * @inheritdoc */ @@ -35,7 +37,6 @@ class ActiveFixture extends \yii\test\BaseActiveFixture } } - /** * Loads the fixture data. * The default implementation will first reset the DB table and then populate it with the data @@ -44,8 +45,9 @@ class ActiveFixture extends \yii\test\BaseActiveFixture public function load() { $this->resetCollection(); - $this->getCollection()->batchInsert($this->getData()); - foreach ($this->getData() as $alias => $row) { + $data = $this->getData(); + $this->getCollection()->batchInsert($data); + foreach ($data as $alias => $row) { $this->data[$alias] = $row; } } From 3986e1158566dbb3e1669560539f8989678faf14 Mon Sep 17 00:00:00 2001 From: kate-kate Date: Tue, 21 Jan 2014 11:35:22 +0200 Subject: [PATCH 5/5] data file removed --- extensions/mongodb/ActiveFixture.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/extensions/mongodb/ActiveFixture.php b/extensions/mongodb/ActiveFixture.php index 83099f6ed1..45a073a702 100644 --- a/extensions/mongodb/ActiveFixture.php +++ b/extensions/mongodb/ActiveFixture.php @@ -17,13 +17,6 @@ class ActiveFixture extends \yii\test\BaseActiveFixture * @see [[yii\mongodb\Connection::getCollection()]] */ public $collectionName; - /** - * @var string the file path or path alias of the data file that contains the fixture data - * and will be loaded by [[loadData()]]. If this is not set, it will default to `FixturePath/data/TableName.php`, - * where `FixturePath` stands for the directory containing this fixture class, and `TableName` stands for the - * name of the table associated with this fixture. - */ - public $dataFile; /**