Skip to content

Commit

Permalink
Rename a variable and add tests for the feature
Browse files Browse the repository at this point in the history
  • Loading branch information
HavokInspiration committed Feb 7, 2016
1 parent d049525 commit 085a892
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 53 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"autoload-dev": {
"psr-4": {
"Cake\\Test\\": "./vendor/cakephp/cakephp/tests",
"Migrations\\Test\\": "tests"
"Migrations\\Test\\": "tests",
"TestBlog\\": "tests/test_app/Plugin/TestBlog/src"
}
},
"suggest": {
Expand Down
11 changes: 6 additions & 5 deletions src/Shell/Task/MigrationSnapshotTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,16 @@ public function fetchTableName($className, $pluginName = null)
$tables[] = $table->associations()->get($key)->_junctionTableName();
}
}
$t = $table->table();
$splitted = array_reverse(explode('.', $t));
$tableName = $table->table();
$splitted = array_reverse(explode('.', $tableName, 2));
if (isset($splitted[1])) {
$config = ConnectionManager::config($this->connection);
if ($config['database'] === $splitted[1]) {
$t = $splitted[0];
$key = isset($config['schema']) ? 'schema' : 'database';
if ($config[$key] === $splitted[1]) {
$tableName = $splitted[0];
}
}
$tables[] = $t;
$tables[] = $tableName;

return $tables;
}
Expand Down
42 changes: 38 additions & 4 deletions tests/TestCase/Shell/Task/MigrationSnapshotTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Bake\Shell\Task\BakeTemplateTask;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\StringCompareTrait;
use Cake\TestSuite\TestCase;
use Cake\Utility\Inflector;
Expand Down Expand Up @@ -82,13 +83,13 @@ public function testGetTableNames()
{
$this->Task->expects($this->any())
->method('findTables')
->with('Blog')
->with('TestBlog')
->will($this->returnValue(['ArticlesTable.php', 'TagsTable.php']));

$this->Task->method('fetchTableName')
->will($this->onConsecutiveCalls(['articles_tags', 'articles'], ['articles_tags', 'tags']));

$results = $this->Task->getTableNames('Blog');
$results = $this->Task->getTableNames('TestBlog');
$expected = ['articles_tags', 'articles', 'tags'];
$this->assertEquals(array_values($expected), array_values($results));
}
Expand Down Expand Up @@ -149,15 +150,48 @@ public function testPluginBlog()
$task = $this->getTaskMock(['in', 'err', 'dispatchShell', '_stop']);
$task->params['require-table'] = false;
$task->params['connection'] = 'test';
$task->params['plugin'] = 'Blog';
$task->plugin = 'Blog';
$task->params['plugin'] = 'TestBlog';
$task->plugin = 'TestBlog';

$bakeName = $this->getBakeName('TestPluginBlog');
$result = $task->bake($bakeName);

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

/**
* Test that using MigrationSnapshotTask::fetchTableName in a Table object class
* where the table name is composed with the database name (e.g. mydb.mytable)
* will return :
* - only the table name if the current connection `database` parameter is the first part
* of the table name
* - the full string (e.g. mydb.mytable) if the current connection `database` parameter
* is not the first part of the table name
*
* @return void
*/
public function testFetchTableNames()
{
$task = $this->getTaskMock(['in', 'err']);
$expected = ['alternative.special_tags'];
$this->assertEquals($expected, $task->fetchTableName('SpecialTagsTable.php', 'TestBlog'));

ConnectionManager::config('alternative', [
'database' => 'alternative'
]);
$task->connection = 'alternative';
$expected = ['special_tags'];
$this->assertEquals($expected, $task->fetchTableName('SpecialTagsTable.php', 'TestBlog'));

ConnectionManager::drop('alternative');
ConnectionManager::config('alternative', [
'schema' => 'alternative'
]);
$task->connection = 'alternative';
$expected = ['special_tags'];
$this->assertEquals($expected, $task->fetchTableName('SpecialTagsTable.php', 'TestBlog'));
}

/**
* Get the baked filename based on the current db environment
*
Expand Down
4 changes: 3 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,6 @@
Plugin::load('Migrations', [
'path' => dirname(dirname(__FILE__)) . DS,
]);
Plugin::load('Blog');
Plugin::load('TestBlog', [
'path' => ROOT . 'Plugin' . DS . 'TestBlog' . DS,
]);
36 changes: 36 additions & 0 deletions tests/comparisons/Migration/pgsql/test_plugin_blog_pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,41 @@ public function up()
)
->create();

$table = $this->table('categories');
$table
->addColumn('parent_id', 'integer', [
'default' => null,
'limit' => 10,
'null' => true,
])
->addColumn('title', 'string', [
'default' => 'NULL::character varying',
'limit' => 255,
'null' => true,
])
->addColumn('slug', 'string', [
'default' => 'NULL::character varying',
'limit' => 255,
'null' => true,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->addColumn('modified', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->addIndex(
[
'slug',
],
['unique' => true]
)
->create();

$this->table('articles')
->addForeignKey(
'category_id',
Expand Down Expand Up @@ -84,5 +119,6 @@ public function down()
);

$this->dropTable('articles');
$this->dropTable('categories');
}
}
36 changes: 36 additions & 0 deletions tests/comparisons/Migration/sqlite/test_plugin_blog_sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,41 @@ public function up()
)
->create();

$table = $this->table('categories');
$table
->addColumn('parent_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
])
->addColumn('title', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('slug', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->addColumn('modified', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->addIndex(
[
'slug',
],
['unique' => true]
)
->create();

$this->table('articles')
->addForeignKey(
'category_id',
Expand Down Expand Up @@ -83,5 +118,6 @@ public function down()
);

$this->dropTable('articles');
$this->dropTable('categories');
}
}
36 changes: 36 additions & 0 deletions tests/comparisons/Migration/test_plugin_blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,41 @@ public function up()
)
->create();

$table = $this->table('categories');
$table
->addColumn('parent_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
])
->addColumn('title', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('slug', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->addColumn('modified', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->addIndex(
[
'slug',
],
['unique' => true]
)
->create();

$this->table('articles')
->addForeignKey(
'category_id',
Expand Down Expand Up @@ -84,5 +119,6 @@ public function down()
);

$this->dropTable('articles');
$this->dropTable('categories');
}
}
29 changes: 0 additions & 29 deletions tests/test_app/Plugin/Blog/src/Model/Entity/Article.php

This file was deleted.

11 changes: 0 additions & 11 deletions tests/test_app/Plugin/Blog/src/Model/Entity/Dog.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Blog\Model\Table;
namespace TestBlog\Model\Table;

use Cake\ORM\Table;

Expand Down
27 changes: 27 additions & 0 deletions tests/test_app/Plugin/TestBlog/src/Model/Table/CategoriesTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace TestBlog\Model\Table;

use Cake\ORM\Table;

/**
* Articles Model
*
*/
class CategoriesTable extends Table
{
public function initialize(array $config)
{
$db = getenv('DB');
switch($db) {
case 'sqlite':
$dbName = ':memory:.';
break;
case 'mysql':
case 'pgsql':
$dbName = 'cakephp_test.';
break;
}
$dbName .= 'categories';
$this->table($dbName);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Blog\Model\Table;
namespace TestBlog\Model\Table;

use Cake\ORM\Table;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace TestBlog\Model\Table;

use Cake\ORM\Table;

/**
* Articles Model
*
*/
class SpecialTagsTable extends Table
{
public function initialize(array $config)
{
$this->table('alternative.special_tags');
}
}

0 comments on commit 085a892

Please sign in to comment.