Merge pull request #106 from havvg/feature/WipeDataLoader
add WipeDataLoader
This commit is contained in:
commit
5552ebd96f
|
@ -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.
|
||||
|
|
66
DataFixtures/Loader/DataWiper.php
Normal file
66
DataFixtures/Loader/DataWiper.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the PropelBundle package.
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
namespace Propel\PropelBundle\DataFixtures\Loader;
|
||||
|
||||
/**
|
||||
* @author Toni Uebernickel <tuebernickel@gmail.com>
|
||||
*/
|
||||
class DataWiper 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();
|
||||
if ('mysql' === $this->con->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
|
||||
$this->con->exec('SET FOREIGN_KEY_CHECKS = 0;');
|
||||
}
|
||||
|
||||
$tables = array();
|
||||
foreach ($this->dbMap->getTables() as $eachTable) {
|
||||
/* @var $eachTable \TableMap */
|
||||
$tables[$eachTable->getClassname()] = array();
|
||||
}
|
||||
|
||||
$this->deleteCurrentData($tables);
|
||||
|
||||
if ('mysql' === $this->con->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
|
||||
$this->con->exec('SET FOREIGN_KEY_CHECKS = 1;');
|
||||
}
|
||||
$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();
|
||||
}
|
||||
}
|
76
Tests/DataFixtures/Loader/DataWiperTest.php
Normal file
76
Tests/DataFixtures/Loader/DataWiperTest.php
Normal file
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the PropelBundle package.
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
namespace Propel\PropelBundle\Tests\DataFixtures\Loader;
|
||||
|
||||
use Propel\PropelBundle\Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @author Toni Uebernickel <tuebernickel@gmail.com>
|
||||
*/
|
||||
class DataWiperTest extends TestCase
|
||||
{
|
||||
protected $con = null;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->loadPropelQuickBuilder();
|
||||
|
||||
$schema = <<<SCHEMA
|
||||
<database name="book" defaultIdMethod="native">
|
||||
<table name="book" phpName="WipeTestBook">
|
||||
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
|
||||
<column name="name" type="varchar" size="255" primaryString="true" />
|
||||
<column name="slug" type="varchar" size="255" />
|
||||
</table>
|
||||
</database>
|
||||
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\DataWiper');
|
||||
$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));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue