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
b892ffc8
Commit
b892ffc8
authored
Feb 02, 2014
by
Mark
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixtures controller improved
parent
a8007c8c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
106 additions
and
57 deletions
+106
-57
FixtureController.php
framework/console/controllers/FixtureController.php
+106
-57
No files found.
framework/console/controllers/FixtureController.php
View file @
b892ffc8
...
...
@@ -12,9 +12,11 @@ use yii\console\Controller;
use
yii\console\Exception
;
use
yii\helpers\FileHelper
;
use
yii\helpers\Console
;
use
yii\test\FixtureTrait
;
use
yii\helpers\Inflector
;
/**
* This command manages fixtures load
to the database tables
.
* This command manages fixtures load.
* You can specify different options of this command to point fixture manager
* to the specific tables of the different database connections.
*
...
...
@@ -28,23 +30,20 @@ use yii\helpers\Console;
* 'password' => '',
* 'charset' => 'utf8',
* ],
* 'fixture' => [
* 'class' => 'yii\test\DbFixtureManager',
* ],
* ~~~
*
* ~~~
* #load fixtures under $fixturePath
to the "users" table
* yii fixture/apply
u
sers
* #load fixtures under $fixturePath
from UsersFixture class with default namespace "tests\unit\fixtures"
* yii fixture/apply
U
sers
*
* #also a short version of this command (generate action is default)
* yii fixture
u
sers
* yii fixture
U
sers
*
* #load fixtures under $fixturePath
to the "users" table to the different
connection
* yii fixture/apply
u
sers --db=someOtherDbConneciton
* #load fixtures under $fixturePath
with the different database
connection
* yii fixture/apply
U
sers --db=someOtherDbConneciton
*
* #load fixtures under different $fixturePath
to the "users" table
.
* yii fixture/apply
u
sers --fixturePath=@app/some/other/path/to/fixtures
* #load fixtures under different $fixturePath.
* yii fixture/apply
U
sers --fixturePath=@app/some/other/path/to/fixtures
* ~~~
*
* @author Mark Jebri <mark.github@yandex.ru>
...
...
@@ -52,8 +51,9 @@ use yii\helpers\Console;
*/
class
FixtureController
extends
Controller
{
use
DbTestTrait
;
use
FixtureTrait
;
/**
* type of fixture apply to database
*/
...
...
@@ -64,16 +64,18 @@ class FixtureController extends Controller
*/
public
$defaultAction
=
'apply'
;
/**
* Alias to the path, where all fixtures are stored.
* @var string
* @var string alias to the path, where all fixtures are stored.
*/
public
$fixturePath
=
'@tests/unit/fixtures'
;
/**
* Id of the database connection component of the application.
* @var string
* @var string id of the database connection component of the application.
*/
public
$db
=
'db'
;
/**
* @var string default namespace to search fixtures in
*/
public
$namespace
=
'tests\unit\fixtures'
;
/**
* Returns the names of the global options for this command.
...
...
@@ -82,7 +84,7 @@ class FixtureController extends Controller
public
function
globalOptions
()
{
return
array_merge
(
parent
::
globalOptions
(),
[
'db'
,
'fixturePath'
'db'
,
'fixturePath'
,
'namespace'
]);
}
...
...
@@ -111,10 +113,6 @@ class FixtureController extends Controller
*/
public
function
actionApply
(
array
$fixtures
,
array
$except
=
[])
{
if
(
$this
->
getFixtureManager
()
===
null
)
{
throw
new
Exception
(
'Fixture manager is not configured properly. Please refer to official documentation for this purposes.'
);
}
$foundFixtures
=
$this
->
findFixtures
(
$fixtures
);
if
(
!
$this
->
needToApplyAll
(
$fixtures
[
0
]))
{
...
...
@@ -128,24 +126,26 @@ class FixtureController extends Controller
if
(
!
$foundFixtures
)
{
throw
new
Exception
(
"No files were found by name:
\"
"
.
implode
(
', '
,
$fixtures
)
.
"
\"
.
\n
"
.
"Check that fixtures with these name exists, under fixtures path:
\n\"
"
.
Yii
::
getAlias
(
$this
->
fixturePath
)
.
"
\"
."
);
);
}
if
(
!
$this
->
confirmApply
(
$foundFixtures
,
$except
))
{
return
;
}
}
$fixtures
=
array_diff
(
$foundFixtures
,
$except
);
$fixtures
=
$this
->
getFixturesConfigs
(
array_diff
(
$foundFixtures
,
$except
)
);
$this
->
getFixtureManager
()
->
basePath
=
$this
->
fixturePath
;
$this
->
getFixtureManager
()
->
db
=
$this
->
db
;
if
(
!
$fixtures
)
{
throw
new
Exception
(
"No fixtures were found in namespace:
\"
"
.
$this
->
namespace
.
"
\"
."
);
}
$transaction
=
Yii
::
$app
->
db
->
beginTransaction
();
try
{
$this
->
loadFixtures
(
$foundFixtures
);
$this
->
getDbConnection
()
->
createCommand
()
->
checkIntegrity
(
false
)
->
execute
();
$this
->
loadFixtures
(
$fixtures
);
$this
->
getDbConnection
()
->
createCommand
()
->
checkIntegrity
(
true
)
->
execute
();
$transaction
->
commit
();
}
catch
(
\Exception
$e
)
{
$transaction
->
rollback
();
$this
->
stdout
(
"Exception occured, transaction rollback. Tables will be in same state.
\n
"
,
Console
::
BG_RED
);
...
...
@@ -155,32 +155,49 @@ class FixtureController extends Controller
}
/**
*
Truncate given table and clear all fixtures from it. You can clear several tables
specifying
*
Unloads given fixtures. You can clear environment and unload multiple fixtures by
specifying
* their names separated with commas, like: tbl_user,tbl_profile. Be sure there is no
* whitespace between tables names.
* @param array|string $tables
* @param array|string $fixtures
* @param array|string $except
*/
public
function
actionClear
(
array
$tables
,
array
$except
=
[
'tbl_migration'
])
{
if
(
$this
->
needToApplyAll
(
$tables
[
0
]))
{
$tables
=
$this
->
getDbConnection
()
->
schema
->
getTableNames
();
public
function
actionClear
(
array
$fixtures
,
array
$except
=
[])
{
$foundFixtures
=
$this
->
findFixtures
(
$fixtures
);
if
(
!
$this
->
needToApplyAll
(
$fixtures
[
0
]))
{
$notFoundFixtures
=
array_diff
(
$fixtures
,
$foundFixtures
);
if
(
$notFoundFixtures
)
{
$this
->
notifyNotFound
(
$notFoundFixtures
);
}
}
if
(
!
$this
->
confirmClear
(
$tables
,
$except
))
{
if
(
!
$foundFixtures
)
{
throw
new
Exception
(
"No files were found by name:
\"
"
.
implode
(
', '
,
$fixtures
)
.
"
\"
.
\n
"
.
"Check that fixtures with these name exists, under fixtures path:
\n\"
"
.
Yii
::
getAlias
(
$this
->
fixturePath
)
.
"
\"
."
);
}
if
(
!
$this
->
confirmClear
(
$foundFixtures
,
$except
))
{
return
;
}
$tables
=
array_diff
(
$tables
,
$except
);
$fixtures
=
$this
->
getFixturesConfigs
(
array_diff
(
$foundFixtures
,
$except
));
if
(
!
$fixtures
)
{
throw
new
Exception
(
"No fixtures were found in namespace:
\"
"
.
$this
->
namespace
.
"
\"
."
);
}
$transaction
=
Yii
::
$app
->
db
->
beginTransaction
();
try
{
$this
->
getDbConnection
()
->
createCommand
()
->
checkIntegrity
(
false
)
->
execute
();
foreach
(
$
tables
as
$table
)
{
$
this
->
getDbConnection
()
->
createCommand
()
->
delete
(
$table
)
->
execute
(
);
$
this
->
getDbConnection
()
->
createCommand
()
->
resetSequence
(
$table
)
->
execute
();
$this
->
stdout
(
"
Table
\"
{
$table
}
\"
was successfully clear
ed.
\n
"
,
Console
::
FG_GREEN
);
foreach
(
$
fixtures
as
$fixtureConfig
)
{
$
fixture
=
Yii
::
createObject
(
$fixtureConfig
);
$
fixture
->
unload
();
$this
->
stdout
(
"
Fixture
\"
{
$fixture
::
className
()
}
\"
was successfully unload
ed.
\n
"
,
Console
::
FG_GREEN
);
}
$this
->
getDbConnection
()
->
createCommand
()
->
checkIntegrity
(
true
)
->
execute
();
...
...
@@ -195,7 +212,7 @@ class FixtureController extends Controller
/**
* Checks if the database and fixtures path are available.
* @throws Exception
* @throws
yii\console\
Exception
*/
public
function
checkRequirements
()
{
...
...
@@ -210,7 +227,7 @@ class FixtureController extends Controller
/**
* Returns database connection component
* @return \yii\db\Connection
* @throws Exception if [[db]] is invalid.
* @throws
yii\console\
Exception if [[db]] is invalid.
*/
public
function
getDbConnection
()
{
...
...
@@ -230,7 +247,7 @@ class FixtureController extends Controller
private
function
notifySuccess
(
$fixtures
)
{
$this
->
stdout
(
"Fixtures were successfully loaded from path:
\n
"
,
Console
::
FG_YELLOW
);
$this
->
stdout
(
Yii
::
getAlias
(
$this
->
fixturePath
)
.
"
\n\n
"
,
Console
::
FG_GREEN
);
$this
->
stdout
(
"
\t
"
.
Yii
::
getAlias
(
$this
->
fixturePath
)
.
"
\n\n
"
,
Console
::
FG_GREEN
);
$this
->
outputList
(
$fixtures
);
}
...
...
@@ -241,7 +258,7 @@ class FixtureController extends Controller
private
function
notifyNotFound
(
$fixtures
)
{
$this
->
stdout
(
"Some fixtures were not found under path:
\n
"
,
Console
::
BG_RED
);
$this
->
stdout
(
Yii
::
getAlias
(
$this
->
fixturePath
)
.
"
\n\n
"
,
Console
::
FG_GREEN
);
$this
->
stdout
(
"
\t
"
.
Yii
::
getAlias
(
$this
->
fixturePath
)
.
"
\n\n
"
,
Console
::
FG_GREEN
);
$this
->
outputList
(
$fixtures
);
$this
->
stdout
(
"
\n
"
);
}
...
...
@@ -255,7 +272,11 @@ class FixtureController extends Controller
private
function
confirmApply
(
$fixtures
,
$except
)
{
$this
->
stdout
(
"Fixtures will be loaded from path:
\n
"
,
Console
::
FG_YELLOW
);
$this
->
stdout
(
Yii
::
getAlias
(
$this
->
fixturePath
)
.
"
\n\n
"
,
Console
::
FG_GREEN
);
$this
->
stdout
(
"
\t
"
.
Yii
::
getAlias
(
$this
->
fixturePath
)
.
"
\n\n
"
,
Console
::
FG_GREEN
);
$this
->
stdout
(
"Fixtures namespace is:
\n
"
,
Console
::
FG_YELLOW
);
$this
->
stdout
(
"
\t
"
.
$this
->
namespace
.
"
\n\n
"
,
Console
::
FG_GREEN
);
$this
->
stdout
(
"Fixtures below will be loaded:
\n\n
"
,
Console
::
FG_YELLOW
);
$this
->
outputList
(
$fixtures
);
if
(
count
(
$except
))
{
...
...
@@ -263,26 +284,31 @@ class FixtureController extends Controller
$this
->
outputList
(
$except
);
}
return
$this
->
confirm
(
"
\n
Load to
database
above fixtures?"
);
return
$this
->
confirm
(
"
\n
Load to above fixtures?"
);
}
/**
* Prompts user with confirmation for
tables that should be clear
ed.
* @param array $
tabl
es
* Prompts user with confirmation for
fixtures that should be unload
ed.
* @param array $
fixtur
es
* @param array $except
* @return boolean
*/
private
function
confirmClear
(
$
tabl
es
,
$except
)
private
function
confirmClear
(
$
fixtur
es
,
$except
)
{
$this
->
stdout
(
"Tables below will be cleared:
\n\n
"
,
Console
::
FG_YELLOW
);
$this
->
outputList
(
$tables
);
$this
->
stdout
(
"Fixtures will be loaded from path:
\n
"
,
Console
::
FG_YELLOW
);
$this
->
stdout
(
"
\t
"
.
Yii
::
getAlias
(
$this
->
fixturePath
)
.
"
\n\n
"
,
Console
::
FG_GREEN
);
$this
->
stdout
(
"Fixtures namespace is:
\n
"
,
Console
::
FG_YELLOW
);
$this
->
stdout
(
"
\t
"
.
$this
->
namespace
.
"
\n\n
"
,
Console
::
FG_GREEN
);
$this
->
stdout
(
"Fixtures below will be unloaded:
\n\n
"
,
Console
::
FG_YELLOW
);
$this
->
outputList
(
$fixtures
);
if
(
count
(
$except
))
{
$this
->
stdout
(
"
\n
Tables that will NOT be clear
ed:
\n\n
"
,
Console
::
FG_YELLOW
);
$this
->
stdout
(
"
\n
Fixtures that will NOT be unload
ed:
\n\n
"
,
Console
::
FG_YELLOW
);
$this
->
outputList
(
$except
);
}
return
$this
->
confirm
(
"
\n
Clear tabl
es?"
);
return
$this
->
confirm
(
"
\n
Unload fixtur
es?"
);
}
/**
...
...
@@ -292,7 +318,7 @@ class FixtureController extends Controller
private
function
outputList
(
$data
)
{
foreach
(
$data
as
$index
=>
$item
)
{
$this
->
stdout
(
"
"
.
(
$index
+
1
)
.
".
{
$item
}
\n
"
,
Console
::
FG_GREEN
);
$this
->
stdout
(
"
\t
"
.
(
$index
+
1
)
.
".
{
$item
}
\n
"
,
Console
::
FG_GREEN
);
}
}
...
...
@@ -314,11 +340,11 @@ class FixtureController extends Controller
{
$fixturesPath
=
Yii
::
getAlias
(
$this
->
fixturePath
);
$filesToSearch
=
[
'*.php'
];
$filesToSearch
=
[
'*
Fixture
.php'
];
if
(
!
$this
->
needToApplyAll
(
$fixtures
[
0
]))
{
$filesToSearch
=
[];
foreach
(
$fixtures
as
$fileName
)
{
$filesToSearch
[]
=
$fileName
.
'.php'
;
$filesToSearch
[]
=
$fileName
.
'
Fixture
.php'
;
}
}
...
...
@@ -326,10 +352,33 @@ class FixtureController extends Controller
$foundFixtures
=
[];
foreach
(
$files
as
$fixture
)
{
$foundFixtures
[]
=
basename
(
$fixture
,
'.php'
);
$foundFixtures
[]
=
basename
(
$fixture
,
'
Fixture
.php'
);
}
return
$foundFixtures
;
}
/**
* Returns valid fixtures config that can be used to load them.
* @param array $fixtures fixtures to configure
* @return array
*/
private
function
getFixturesConfigs
(
$fixtures
)
{
$config
=
[];
foreach
(
$fixtures
as
$fixture
)
{
$fullClassName
=
$this
->
namespace
.
'\\'
.
$fixture
.
'Fixture'
;
if
(
class_exists
(
$fullClassName
))
{
$config
[
Inflector
::
camel2id
(
$fixture
,
'_'
)]
=
[
'class'
=>
$fullClassName
,
];
}
}
return
$config
;
}
}
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