Skip to content

Commit

Permalink
Correct the file writer in the version class too
Browse files Browse the repository at this point in the history
And refactoring the tests for it so that they take into account
the configurable column name

Conflicts:
	tests/Doctrine/DBAL/Migrations/Tests/VersionTest.php
  • Loading branch information
mikeSimonson committed Jan 7, 2016
1 parent bffd726 commit 91043f7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/Doctrine/DBAL/Migrations/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public function writeSqlFile($path, $direction = self::DIRECTION_UP)

$sqlQueries = [$this->version => $queries];
$sqlWriter = new SqlFileWriter(
$this->configuration->getMigrationsColumnName(),
$this->configuration->getMigrationsTableName(),
$path,
$this->outputWriter
Expand Down
16 changes: 16 additions & 0 deletions tests/Doctrine/DBAL/Migrations/Tests/MigrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,20 @@ protected function getOutputWriter()
}
return $this->outputWriter;
}

protected function createTempDirForMigrations($path)
{
if (!mkdir($path)) {
throw new \Exception('fail to create a temporary folder for the tests at ' . $path);
}
}

protected function getSqlFilesList($path)
{
if (is_dir($path)) {
return glob(realpath($path) . '/*.sql');
} elseif(is_file($path)) {
return [$path];
}
}
}
22 changes: 11 additions & 11 deletions tests/Doctrine/DBAL/Migrations/Tests/SqlFileWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

namespace Doctrine\DBAL\Migrations\Tests;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\DBAL\Migrations\SqlFileWriter;
use Doctrine\DBAL\Migrations\Version;
use \Mockery as m;
Expand Down Expand Up @@ -80,28 +82,26 @@ public function testConstructorEmptyDestPath()
*/
public function testWrite($path, $direction, array $queries, $withOw)
{
$columnName = 'columnName';
$tableName = 'tableName';
if ($withOw) {
$instance = new SqlFileWriter('version', 'test', $path, $this->ow);
$instance = new SqlFileWriter($columnName, $tableName, $path, $this->ow);
$this->ow->shouldReceive('write')->with(m::type('string'))->once();
} else {
$instance = new SqlFileWriter('version', 'test', $path);
$instance = new SqlFileWriter($columnName, $tableName, $path);
}
$instance->write($queries, $direction);

// file content tests & cleanup
$files = [];
if (is_dir($path)) {
$files = glob(realpath($path) . '/*.sql');
} elseif(is_file($path)) {
$files = [$path];
}
$files = $this->getSqlFilesList($path);

foreach ($files as $file) {
$contents = file_get_contents($file);
$this->assertNotEmpty($contents);
if ($direction == 'up') {
$this->assertContains('INSERT INTO test (version) VALUES (\'1\');', $contents);
if ($direction == Version::DIRECTION_UP) {
$this->assertContains("INSERT INTO $tableName ($columnName) VALUES ('1');", $contents);
} else {
$this->assertContains('DELETE FROM test WHERE version = \'1\';', $contents);
$this->assertContains("DELETE FROM $tableName WHERE $columnName = '1'", $contents);
}
unlink($file);
}
Expand Down
49 changes: 49 additions & 0 deletions tests/Doctrine/DBAL/Migrations/Tests/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

namespace Doctrine\DBAL\Migrations\Tests;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Migrations\MigrationException;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Migrations\Version;
Expand Down Expand Up @@ -263,4 +265,51 @@ public function testReturnTheSql()
$this->assertContains('Select 1', $version->execute('up'));
$this->assertContains('Select 1', $version->execute('down'));
}

/**
* @param $direction
* @param $columnName
* @param $tableName
*
* @dataProvider sqlWriteProvider
*/
public function testWriteSqlWriteToTheCorrectColumnName($direction, $columnName, $tableName)
{
$connection = $this->getSqliteConnection();
$configuration = new Configuration($connection, $this->outputWriter);
$configuration->setMigrationsColumnName($columnName);
$configuration->setMigrationsTableName($tableName);

$version = new Version(
$configuration,
$versionName = '005',
'Doctrine\DBAL\Migrations\Tests\Stub\VersionOutputSql'
);
$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'migrations';

$version->writeSqlFile($path, $direction);

$files = $this->getSqlFilesList($path);

foreach ($files as $file) {
$contents = file_get_contents($file);
$this->assertNotEmpty($contents);
if ($direction == Version::DIRECTION_UP) {
$this->assertContains("INSERT INTO $tableName ($columnName) VALUES ('$versionName');", $contents);
} else {
$this->assertContains("DELETE FROM $tableName WHERE $columnName = '$versionName'", $contents);
}
unlink($file);
}
}

public function sqlWriteProvider()
{
return [
[Version::DIRECTION_UP, 'balalala', 'fkqsdmfjl'],
[Version::DIRECTION_UP, 'fkqsdmfjl', 'balalala'],
[Version::DIRECTION_DOWN, 'balalala', 'fkqsdmfjl'],
[Version::DIRECTION_DOWN, 'fkqsdmfjl', 'balalala'],
];
}
}

0 comments on commit 91043f7

Please sign in to comment.