Created
August 17, 2012 18:35
-
-
Save jippi/3381416 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| App::uses('CakeFixtureManager', 'TestSuite/Fixture'); | |
| App::uses('CakeTestFixture', 'TestSuite/Fixture'); | |
| App::uses('ConnectionManager', 'Model'); | |
| class NodesFixtureManager extends CakeFixtureManager { | |
| public function loadAllFixtures($source, $fixtures) { | |
| $this->_initDb($source); | |
| $this->_loadFixtures($fixtures); | |
| $nested = $this->_db->useNestedTransactions; | |
| $this->_db->useNestedTransactions = false; | |
| $this->_db->begin(); | |
| CakeLog::debug('Begin fixture import'); | |
| CakeLog::debug(''); | |
| foreach ($fixtures as $f) { | |
| CakeLog::debug(sprintf('Working on %s', $f)); | |
| if (empty($this->_loaded[$f])) { | |
| CakeLog::notice('-> Can not find it in the loaded array.. weird'); | |
| continue; | |
| } | |
| $fixture = $this->_loaded[$f]; | |
| CakeLog::debug(sprintf('-> Found fixture: %s', get_class($fixture))); | |
| $this->_setupTable($fixture, $this->_db, true); | |
| CakeLog::debug('-> Created table "OK"'); | |
| if ($fixture->insert($this->_db)) { | |
| CakeLog::debug('-> Inserted fixture data "OK"'); | |
| } else { | |
| CakeLog::error('-> Inserted fixture data "ERROR"'); | |
| } | |
| CakeLog::debug(''); | |
| } | |
| $this->_db->commit(); | |
| $this->_useNestedTransactions = $nested; | |
| CakeLog::debug('Done!'); | |
| } | |
| protected function _initDb($source = 'default') { | |
| if ($this->_initialized) { | |
| return; | |
| } | |
| $db = ConnectionManager::getDataSource($source); | |
| $db->cacheSources = false; | |
| $this->_db = $db; | |
| $this->_initialized = true; | |
| } | |
| } | |
| class FixtureShell extends AppShell { | |
| public function main() { | |
| $CakeFixtureManager = new NodesFixtureManager(); | |
| $CakeFixtureManager->loadAllFixtures($this->params['datasource'], explode(',', $this->args[0])); | |
| } | |
| /** | |
| * get the option parser. | |
| * | |
| * @return void | |
| */ | |
| public function getOptionParser() { | |
| $parser = parent::getOptionParser(); | |
| return $parser | |
| ->description('Contains a set of utilities to store records from database into elastic search') | |
| ->addArgument('fixtures', array( | |
| 'help' => 'A comma separated list of fixtures to use (Format is same as $fixtures property in CakeTest classes', | |
| 'required' => true | |
| )) | |
| ->addOption('datasource', array( | |
| 'help' => 'Datasource to use', | |
| 'default' => 'default' | |
| )); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2012-08-17 20:34:52 Debug: Begin fixture import
2012-08-17 20:34:52 Debug:
2012-08-17 20:34:52 Debug: Working on app.event
2012-08-17 20:34:52 Debug: -> Found fixture: EventFixture
2012-08-17 20:34:52 Debug: -> Created table "OK"
2012-08-17 20:34:52 Debug: -> Inserted fixture data "OK"
2012-08-17 20:34:52 Debug:
2012-08-17 20:34:52 Debug: Working on app.category
2012-08-17 20:34:52 Debug: -> Found fixture: CategoryFixture
2012-08-17 20:34:52 Debug: -> Created table "OK"
2012-08-17 20:34:52 Debug: -> Inserted fixture data "OK"
2012-08-17 20:34:52 Debug:
2012-08-17 20:34:52 Debug: Done!