Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
f6ef4aac
Commit
f6ef4aac
authored
Jul 10, 2014
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Unit test for `MigrateController` added
parent
ce609218
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
313 additions
and
0 deletions
+313
-0
MigrateControllerTest.php
...t/framework/console/controllers/MigrateControllerTest.php
+54
-0
MigrateControllerTestTrait.php
...mework/console/controllers/MigrateControllerTestTrait.php
+259
-0
No files found.
tests/unit/framework/console/controllers/MigrateControllerTest.php
0 → 100644
View file @
f6ef4aac
<?php
namespace
yiiunit\framework\console\controllers
;
use
Yii
;
use
yii\console\controllers\MigrateController
;
use
yii\db\Migration
;
use
yii\db\Query
;
use
yiiunit\TestCase
;
/**
* Unit test for [[\yii\console\controllers\MigrateController]].
* @see MigrateController
*
* @group console
*/
class
MigrateControllerTest
extends
TestCase
{
use
MigrateControllerTestTrait
;
public
function
setUp
()
{
$this
->
migrateControllerClass
=
MigrateController
::
className
();
$this
->
migrationBaseClass
=
Migration
::
className
();
$this
->
mockApplication
([
'components'
=>
[
'db'
=>
[
'class'
=>
'yii\db\Connection'
,
'dsn'
=>
'sqlite::memory:'
,
],
],
]);
$this
->
setUpMigrationPath
();
parent
::
setUp
();
}
public
function
tearDown
()
{
$this
->
tearDownMigrationPath
();
parent
::
tearDown
();
}
/**
* @return array applied migration entries
*/
protected
function
getMigrationHistory
()
{
$query
=
new
Query
();
return
$query
->
from
(
'migration'
)
->
all
();
}
}
\ No newline at end of file
tests/unit/framework/console/controllers/MigrateControllerTestTrait.php
0 → 100644
View file @
f6ef4aac
<?php
namespace
yiiunit\framework\console\controllers
;
use
Yii
;
use
yii\console\controllers\BaseMigrateController
;
use
yii\helpers\FileHelper
;
use
yiiunit\TestCase
;
/**
* This trait provides unit tests shared by the different migration controllers implementations
* @see BaseMigrateController
*/
trait
MigrateControllerTestTrait
{
/* @var $this TestCase */
/**
* @var string name of the migration controller class, which is under test.
*/
protected
$migrateControllerClass
;
/**
* @var string name of the migration base class.
*/
protected
$migrationBaseClass
;
/**
* @var string test migration path.
*/
protected
$migrationPath
;
public
function
setUpMigrationPath
()
{
$this
->
migrationPath
=
Yii
::
getAlias
(
'@yiiunit/runtime/test_migrations'
);
FileHelper
::
createDirectory
(
$this
->
migrationPath
);
if
(
!
file_exists
(
$this
->
migrationPath
))
{
$this
->
markTestIncomplete
(
'Unit tests runtime directory should have writable permissions!'
);
}
}
public
function
tearDownMigrationPath
()
{
FileHelper
::
removeDirectory
(
$this
->
migrationPath
);
}
/**
* @return array applied migration entries
*/
abstract
protected
function
getMigrationHistory
();
/**
* Creates test migrate controller instance.
* @return BaseMigrateController migrate command instance.
*/
protected
function
createMigrateController
()
{
$module
=
$this
->
getMock
(
'yii\\base\\Module'
,
[
'fake'
],
[
'console'
]);
$class
=
$this
->
migrateControllerClass
;
$migrateController
=
new
$class
(
'migrate'
,
$module
);
$migrateController
->
interactive
=
false
;
$migrateController
->
migrationPath
=
$this
->
migrationPath
;
return
$migrateController
;
}
/**
* Emulates running of the migrate controller action.
* @param string $actionId id of action to be run.
* @param array $args action arguments.
* @return string command output.
*/
protected
function
runMigrateControllerAction
(
$actionId
,
array
$args
=
[])
{
$controller
=
$this
->
createMigrateController
();
ob_start
();
ob_implicit_flush
(
false
);
$controller
->
run
(
$actionId
,
$args
);
return
ob_get_clean
();
}
/**
* @param string $name
* @param string|null $date
* @return string generated class name
*/
protected
function
createMigration
(
$name
,
$date
=
null
)
{
if
(
$date
===
null
)
{
$date
=
gmdate
(
'ymd_His'
);
}
$class
=
'm'
.
$date
.
'_'
.
$name
;
$baseClass
=
$this
->
migrationBaseClass
;
$code
=
<<<CODE
<?php
class {$class} extends {$baseClass}
{
public function up()
{
}
public function down()
{
}
}
CODE;
file_put_contents
(
$this
->
migrationPath
.
DIRECTORY_SEPARATOR
.
$class
.
'.php'
,
$code
);
return
$class
;
}
/**
* Checks if applied migration history matches expected one.
* @param array $expectedMigrations migration names in expected order
* @param string $message failure message
*/
protected
function
assertMigrationHistory
(
array
$expectedMigrations
,
$message
=
''
)
{
$success
=
true
;
$migrationHistory
=
$this
->
getMigrationHistory
();
$appliedMigrations
=
$migrationHistory
;
foreach
(
$expectedMigrations
as
$expectedMigrationName
)
{
$appliedMigration
=
array_shift
(
$appliedMigrations
);
if
(
strpos
(
$appliedMigration
[
'version'
],
$expectedMigrationName
)
===
false
)
{
$success
=
false
;
break
;
}
}
if
(
!
$success
)
{
$message
.=
"
\n
"
;
$message
.=
"Expected: "
.
var_export
(
$expectedMigrations
,
true
)
.
"
\n
"
;
$actualMigrations
=
[];
foreach
(
$migrationHistory
as
$row
)
{
$actualMigrations
[]
=
$row
[
'version'
];
}
$message
.=
"Actual: "
.
var_export
(
$actualMigrations
,
true
)
.
"
\n
"
;
}
$this
->
assertTrue
(
$success
,
$message
);
}
// Tests :
public
function
testCreate
()
{
$migrationName
=
'test_migration'
;
$this
->
runMigrateControllerAction
(
'create'
,
[
$migrationName
]);
$files
=
FileHelper
::
findFiles
(
$this
->
migrationPath
);
$this
->
assertCount
(
1
,
$files
,
'Unable to create new migration!'
);
$this
->
assertContains
(
$migrationName
,
basename
(
$files
[
0
]),
'Wrong migration name!'
);
}
public
function
testUp
()
{
$this
->
createMigration
(
'test1'
);
$this
->
createMigration
(
'test2'
);
$this
->
runMigrateControllerAction
(
'up'
);
$this
->
assertMigrationHistory
([
'base'
,
'test1'
,
'test2'
]);
}
/**
* @depends testUp
*/
public
function
testUpCount
()
{
$this
->
createMigration
(
'test1'
);
$this
->
createMigration
(
'test2'
);
$this
->
runMigrateControllerAction
(
'up'
,
[
1
]);
$this
->
assertMigrationHistory
([
'base'
,
'test1'
]);
}
/**
* @depends testUp
*/
public
function
testDownCount
()
{
$this
->
createMigration
(
'test1'
);
$this
->
createMigration
(
'test2'
);
$this
->
runMigrateControllerAction
(
'up'
);
$this
->
runMigrateControllerAction
(
'down'
,
[
1
]);
$this
->
assertMigrationHistory
([
'base'
,
'test1'
]);
}
/**
* @depends testDownCount
*/
public
function
testDownAll
()
{
$this
->
createMigration
(
'test1'
);
$this
->
createMigration
(
'test2'
);
$this
->
runMigrateControllerAction
(
'up'
);
$this
->
runMigrateControllerAction
(
'down'
,
[
'all'
]);
$this
->
assertMigrationHistory
([
'base'
]);
}
/**
* @depends testUp
*/
public
function
testHistory
()
{
$output
=
$this
->
runMigrateControllerAction
(
'history'
);
$this
->
assertContains
(
'No migration'
,
$output
);
$this
->
createMigration
(
'test1'
);
$this
->
createMigration
(
'test2'
);
$this
->
runMigrateControllerAction
(
'up'
);
$output
=
$this
->
runMigrateControllerAction
(
'history'
);
$this
->
assertContains
(
'_test1'
,
$output
);
$this
->
assertContains
(
'_test2'
,
$output
);
}
/**
* @depends testUp
*/
public
function
testNew
()
{
$this
->
createMigration
(
'test1'
);
$output
=
$this
->
runMigrateControllerAction
(
'new'
);
$this
->
assertContains
(
'_test1'
,
$output
);
$this
->
runMigrateControllerAction
(
'up'
);
$output
=
$this
->
runMigrateControllerAction
(
'new'
);
$this
->
assertNotContains
(
'_test1'
,
$output
);
}
public
function
testMark
()
{
$version
=
'010101_000001'
;
$this
->
createMigration
(
'test1'
,
$version
);
$this
->
runMigrateControllerAction
(
'mark'
,
[
$version
]);
$this
->
assertMigrationHistory
([
'base'
,
'test1'
]);
}
/**
* @depends testUp
*/
public
function
testRedo
()
{
$this
->
createMigration
(
'test1'
);
$this
->
runMigrateControllerAction
(
'up'
);
$this
->
runMigrateControllerAction
(
'redo'
);
$this
->
assertMigrationHistory
([
'base'
,
'test1'
]);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment