From bb9dfbef42832a4ec0b7368a2e5a0a17009eecce Mon Sep 17 00:00:00 2001 From: Toni Uebernickel Date: Sat, 18 Feb 2012 02:34:34 +0100 Subject: [PATCH] add WipeDataLoader --- DataFixtures/Loader/AbstractDataLoader.php | 17 +---- DataFixtures/Loader/WipeDataLoader.php | 60 +++++++++++++++ .../Loader/WipeDataLoaderTest.php | 76 +++++++++++++++++++ 3 files changed, 138 insertions(+), 15 deletions(-) create mode 100644 DataFixtures/Loader/WipeDataLoader.php create mode 100644 Tests/DataFixtures/Loader/WipeDataLoaderTest.php diff --git a/DataFixtures/Loader/AbstractDataLoader.php b/DataFixtures/Loader/AbstractDataLoader.php index c4bb9ea..ddd8aed 100644 --- a/DataFixtures/Loader/AbstractDataLoader.php +++ b/DataFixtures/Loader/AbstractDataLoader.php @@ -31,24 +31,11 @@ abstract class AbstractDataLoader extends AbstractDataHandler implements DataLoa /** * @var array */ - private $deletedClasses; + private $deletedClasses = array(); /** * @var array */ - private $object_references; - - /** - * Default constructor - * - * @param string $rootDir The root directory. - */ - public function __construct($rootDir) - { - parent::__construct($rootDir); - - $this->deletedClasses = array(); - $this->object_references = array(); - } + private $object_references = array(); /** * Transforms a file containing data in an array. diff --git a/DataFixtures/Loader/WipeDataLoader.php b/DataFixtures/Loader/WipeDataLoader.php new file mode 100644 index 0000000..7d3e4cd --- /dev/null +++ b/DataFixtures/Loader/WipeDataLoader.php @@ -0,0 +1,60 @@ + + */ +class WipeDataLoader extends AbstractDataLoader +{ + /** + * Clears the database completely. + * + * @param array $files A set of files containing datas to load. + * @param string $connectionName The Propel connection name + */ + public function load($files = array(), $connectionName) + { + $this->loadMapBuilders($connectionName); + + $this->con = \Propel::getConnection($connectionName); + + try { + $this->con->beginTransaction(); + + $tables = array(); + foreach ($this->dbMap->getTables() as $eachTable) { + /* @var $eachTable \TableMap */ + $tables[$eachTable->getClassname()] = array(); + } + + $this->deleteCurrentData($tables); + + $this->con->commit(); + } catch (\Exception $e) { + $this->con->rollBack(); + + throw $e; + } + } + + /** + * Not used by this data loader. + * + * @param string $file A filename. + * + * @return array + */ + protected function transformDataToArray($file) + { + return array(); + } +} \ No newline at end of file diff --git a/Tests/DataFixtures/Loader/WipeDataLoaderTest.php b/Tests/DataFixtures/Loader/WipeDataLoaderTest.php new file mode 100644 index 0000000..b0f34dc --- /dev/null +++ b/Tests/DataFixtures/Loader/WipeDataLoaderTest.php @@ -0,0 +1,76 @@ + + */ +class WipeDataLoaderTest extends TestCase +{ + protected $con = null; + + public function setUp() + { + $this->loadPropelQuickBuilder(); + + $schema = << + + + + +
+ +SCHEMA; + + $builder = new \PropelQuickBuilder(); + $builder->setSchema($schema); + $this->con = $builder->build(); + } + + public function testWipesExistingData() + { + $book = new \WipeTestBook(); + $book + ->setName('Armageddon is near') + ->setSlug('armageddon-is-near') + ->save($this->con) + ; + + $savedBook = \WipeTestBookPeer::doSelectOne(new \Criteria(), $this->con); + $this->assertInstanceOf('WipeTestBook', $savedBook, 'The fixture has been saved correctly.'); + + $builder = $this->getMockBuilder('Propel\PropelBundle\DataFixtures\Loader\WipeDataLoader'); + $wipeout = $builder + ->setMethods(array('loadMapBuilders')) + ->disableOriginalConstructor() + ->getMock() + ; + + $dbMap = new \DatabaseMap('book'); + $dbMap->addTableFromMapClass('WipeTestBookTableMap'); + $reflection = new \ReflectionObject($wipeout); + $property = $reflection->getProperty('dbMap'); + $property->setAccessible(true); + $property->setValue($wipeout, $dbMap); + + $wipeout + ->expects($this->once()) + ->method('loadMapBuilders') + ; + + $wipeout->load(array(), 'book'); + + $this->assertCount(0, \WipeTestBookPeer::doSelect(new \Criteria(), $this->con)); + } +} \ No newline at end of file