Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the tests so they pass again following the release of phinx 0.4.6 #123

Merged
merged 3 commits into from
Sep 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"require": {
"php": ">=5.4",
"robmorgan/phinx": ">=0.4.2 <1.0",
"robmorgan/phinx": "dev-master",
"cakephp/cakephp": "~3.0"
},
"require-dev": {
Expand Down
69 changes: 68 additions & 1 deletion src/CakeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ public function __construct(AdapterInterface $adapter, Connection $connection)
$connection->driver()->connection($pdo);
}

/**
* Gets the database connection
*
* @return \PDO
*/
public function getConnection()
{
return $this->adapter->getConnection();
}

/**
* Get all migrated version numbers.
*
Expand Down Expand Up @@ -129,6 +139,60 @@ public function getOutput()
return $this->adapter->getOutput();
}

/**
* Sets the command start time
*
* @param int $time
* @return AdapterInterface
*/
public function setCommandStartTime($time)
{
return $this->adapter->setCommandStartTime($time);
}

/**
* Gets the command start time
*
* @return int
*/
public function getCommandStartTime()
{
return $this->adapter->getCommandStartTime();
}

/**
* Start timing a command.
*
* @return void
*/
public function startCommandTimer()
{
$this->adapter->startCommandTimer();
}

/**
* Stop timing the current command and write the elapsed time to the
* output.
*
* @return void
*/
public function endCommandTimer()
{
$this->adapter->endCommandTimer();
}

/**
* Write a Phinx command to the output.
*
* @param string $command Command Name
* @param array $args Command Args
* @return void
*/
public function writeCommand($command, $args = array())
{
$this->adapter->writeCommand($command, $args);
}

/**
* Records a migration being run.
*
Expand Down Expand Up @@ -506,6 +570,9 @@ public function addForeignKey(Table $table, ForeignKey $foreignKey)

/**
* Drops the specified foreign key from a database table.
* If the adapter property is an instance of the \Phinx\Db\Adapter\SQLiteAdapter,
* a specific method will be called. The original one from Phinx contains a bug
* that can drop a table in certain conditions.
*
* @param string $tableName
* @param string[] $columns Column(s)
Expand Down Expand Up @@ -557,7 +624,7 @@ public function getSqlType($type, $limit = null)
* @param array $options Options
* @return void
*/
public function createDatabase($name, $options = array())
public function createDatabase($name, $options = [])
{
return $this->adapter->createDatabase($name, $options);
}
Expand Down
26 changes: 26 additions & 0 deletions src/Migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
namespace Migrations;

use Cake\Datasource\ConnectionManager;
use Phinx\Config\Config;
use Phinx\Config\ConfigInterface;
use Symfony\Component\Console\Input\ArrayInput;
Expand Down Expand Up @@ -193,6 +194,7 @@ protected function run($method, $params, $input)
$this->setInput($input);
$newConfig = $this->getConfig(true);
$manager = $this->getManager($newConfig);

if (isset($migrationPath) && $newConfig->getMigrationPath() !== $migrationPath) {
$manager->resetMigrations();
}
Expand Down Expand Up @@ -220,9 +222,33 @@ public function getManager($config = null)
$this->manager->setConfig($config);
}

$this->setAdapter();
return $this->manager;
}

/**
* Sets the adapter the manager is going to need to operate on the DB
* This will make sure the adapter instance is a \Migrations\CakeAdapter instance
*
* @return void
*/
public function setAdapter()
{
if ($this->input !== null) {
$connectionName = 'default';
if ($this->input->getOption('connection')) {
$connectionName = $this->input->getOption('connection');
}
$connection = ConnectionManager::get($connectionName);

$env = $this->manager->getEnvironment('default');
$adapter = $env->getAdapter();
if (!$adapter instanceof CakeAdapter) {
$env->setAdapter(new CakeAdapter($adapter, $connection));
}
}
}

/**
* Get the input needed for each commands to be run
*
Expand Down
2 changes: 1 addition & 1 deletion src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function create()
*/
protected function filterPrimaryKey()
{
if (!($this->getAdapter() instanceof SQLiteAdapter) || empty($this->options['primary_key'])) {
if ($this->getAdapter()->getAdapterType() !== 'sqlite' || empty($this->options['primary_key'])) {
return;
}

Expand Down
9 changes: 5 additions & 4 deletions src/Template/Bake/config/snapshot.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,15 @@ class <%= $name %> extends AbstractMigration

public function down()
{
<%- foreach ($dropForeignKeys as $table => $columnsList): %>
<%- foreach ($dropForeignKeys as $table => $columnsList):
$maxKey = count($columnsList) - 1;
%>
$this->table('<%= $table %>')
<%- foreach ($columnsList as $columns): %>
<%- foreach ($columnsList as $key => $columns): %>
->dropForeignKey(
<%= $columns %>
)
)<%= ($key === $maxKey) ? ';' : '' %>
<%- endforeach; %>
->update();

<%- endforeach; %>
<%- foreach ($tables as $table): %>
Expand Down
42 changes: 29 additions & 13 deletions tests/TestCase/MigrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ public function testStatus()
]
];
$this->assertEquals($expected, $result);

$adapter = $this->migrations
->getManager()
->getEnvironment('default')
->getAdapter();

$this->assertInstanceOf('\Migrations\CakeAdapter', $adapter);
}

/**
Expand Down Expand Up @@ -430,16 +437,16 @@ public function testMigrateDateOption()
public function testMigrateSnapshots($basePath, $files)
{
$destination = ROOT . 'config' . DS . 'SnapshotTests' . DS;
$timestamp = Util::getCurrentTimestamp();

if (!file_exists($destination)) {
mkdir($destination);
}

foreach ($files as $file) {
$copiedFileName = $timestamp . '_' . $file . '.php';
list($filename, $timestamp) = $file;
$copiedFileName = $timestamp . '_' . $filename . '.php';
copy(
$basePath . $file . '.php',
$basePath . $filename . '.php',
$destination . $copiedFileName
);

Expand All @@ -462,21 +469,30 @@ public function migrationsProvider()
[
Plugin::path('Migrations') . 'tests' . DS . 'comparisons' . DS . 'Migration' . DS,
[
'testCompositeConstraintsSnapshot',
'testNotEmptySnapshot',
'testAutoIdDisabledSnapshot',
'testCreatePrimaryKey',
'testCreatePrimaryKeyUuid'
['test_composite_constraints_snapshot', 20150912015600],
['test_not_empty_snapshot', 20150912015601],
['test_auto_id_disabled_snapshot', 20150912015602],
['testCreatePrimaryKey', 20150912015603],
['testCreatePrimaryKeyUuid', 20150912015604]
]
],
[
Plugin::path('Migrations') . 'tests' . DS . 'comparisons' . DS . 'Migration' . DS . 'sqlite' . DS,
['testCompositeConstraintsSnapshot', 'testNotEmptySnapshot', 'testAutoIdDisabledSnapshot']
],
[
Plugin::path('Migrations') . 'tests' . DS . 'comparisons' . DS . 'Migration' . DS . 'pgsql' . DS,
['testCompositeConstraintsSnapshot', 'testNotEmptySnapshot', 'testAutoIdDisabledSnapshot']
[
['test_composite_constraints_snapshot_pgsql', 20150912015605],
['test_not_empty_snapshot_pgsql', 20150912015606],
['test_auto_id_disabled_snapshot_pgsql', 20150912015607]
]
]
,
[
Plugin::path('Migrations') . 'tests' . DS . 'comparisons' . DS . 'Migration' . DS . 'sqlite' . DS,
[
['test_composite_constraints_snapshot_sqlite', 20150912015608],
['test_not_empty_snapshot_sqlite', 20150912015609],
['test_auto_id_disabled_snapshot_sqlite', 20150912015610]
]
],
];
}
}
36 changes: 26 additions & 10 deletions tests/TestCase/Shell/Task/MigrationSnapshotTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Cake\Core\Plugin;
use Cake\TestSuite\StringCompareTrait;
use Cake\TestSuite\TestCase;
use Cake\Utility\Inflector;

/**
* MigrationSnapshotTaskTest class
Expand Down Expand Up @@ -85,9 +86,10 @@ public function testNotEmptySnapshot()
)
);

$result = $this->Task->bake('NotEmptySnapshot');
$bakeName = $this->getBakeName('TestNotEmptySnapshot');
$result = $this->Task->bake($bakeName);

$this->assertCorrectSnapshot(__FUNCTION__, $result);
$this->assertCorrectSnapshot($bakeName, $result);
}

public function testAutoIdDisabledSnapshot()
Expand All @@ -97,9 +99,10 @@ public function testAutoIdDisabledSnapshot()
$this->Task->params['connection'] = 'test';
$this->Task->params['plugin'] = 'BogusPlugin';

$result = $this->Task->bake('AutoIdDisabledSnapshot');
$bakeName = $this->getBakeName('TestAutoIdDisabledSnapshot');
$result = $this->Task->bake($bakeName);

$this->assertCorrectSnapshot(__FUNCTION__, $result);
$this->assertCorrectSnapshot($bakeName, $result);
}

public function testCompositeConstraintsSnapshot()
Expand All @@ -116,18 +119,31 @@ public function testCompositeConstraintsSnapshot()

$this->Task->params['require-table'] = false;
$this->Task->params['connection'] = 'test';
$result = $this->Task->bake('CompositeConstraintsSnapshot');

$this->assertCorrectSnapshot(__FUNCTION__, $result);
$bakeName = $this->getBakeName('TestCompositeConstraintsSnapshot');
$result = $this->Task->bake($bakeName);

$this->assertCorrectSnapshot($bakeName, $result);
}

public function getBakeName($name)
{
$dbenv = getenv("DB");
if ($dbenv !== 'mysql') {
$name .= ucfirst($dbenv);
}

return $name;
}

public function assertCorrectSnapshot($function, $result)
public function assertCorrectSnapshot($bakeName, $result)
{
$dbenv = getenv("DB");
if (file_exists($this->_compareBasePath . $dbenv . DS . $function . '.php')) {
$this->assertSameAsFile($dbenv . DS . $function . '.php', $result);
$bakeName = Inflector::underscore($bakeName);
if (file_exists($this->_compareBasePath . $dbenv . DS . $bakeName . '.php')) {
$this->assertSameAsFile($dbenv . DS . $bakeName . '.php', $result);
} else {
$this->assertSameAsFile($function . '.php', $result);
$this->assertSameAsFile($bakeName . '.php', $result);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
use Migrations\AbstractMigration;

class AutoIdDisabledSnapshot extends AbstractMigration
class TestAutoIdDisabledSnapshotPgsql extends AbstractMigration
{

public $autoId = false;
Expand Down Expand Up @@ -297,14 +297,12 @@ public function down()
)
->dropForeignKey(
'product_id'
)
->update();
);

$this->table('products')
->dropForeignKey(
'category_id'
)
->update();
);

$this->dropTable('articles');
$this->dropTable('categories');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
use Migrations\AbstractMigration;

class CompositeConstraintsSnapshot extends AbstractMigration
class TestCompositeConstraintsSnapshotPgsql extends AbstractMigration
{
public function up()
{
Expand Down Expand Up @@ -295,23 +295,20 @@ public function down()
)
->dropForeignKey(
'product_id'
)
->update();
);

$this->table('orders')
->dropForeignKey(
[
'product_category',
'product_id',
]
)
->update();
);

$this->table('products')
->dropForeignKey(
'category_id'
)
->update();
);

$this->dropTable('articles');
$this->dropTable('categories');
Expand Down
Loading