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;
...
@@ -12,9 +12,11 @@ use yii\console\Controller;
use
yii\console\Exception
;
use
yii\console\Exception
;
use
yii\helpers\FileHelper
;
use
yii\helpers\FileHelper
;
use
yii\helpers\Console
;
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
* You can specify different options of this command to point fixture manager
* to the specific tables of the different database connections.
* to the specific tables of the different database connections.
*
*
...
@@ -28,23 +30,20 @@ use yii\helpers\Console;
...
@@ -28,23 +30,20 @@ use yii\helpers\Console;
* 'password' => '',
* 'password' => '',
* 'charset' => 'utf8',
* 'charset' => 'utf8',
* ],
* ],
* 'fixture' => [
* 'class' => 'yii\test\DbFixtureManager',
* ],
* ~~~
* ~~~
*
*
* ~~~
* ~~~
* #load fixtures under $fixturePath
to the "users" table
* #load fixtures under $fixturePath
from UsersFixture class with default namespace "tests\unit\fixtures"
* yii fixture/apply
u
sers
* yii fixture/apply
U
sers
*
*
* #also a short version of this command (generate action is default)
* #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
* #load fixtures under $fixturePath
with the different database
connection
* yii fixture/apply
u
sers --db=someOtherDbConneciton
* yii fixture/apply
U
sers --db=someOtherDbConneciton
*
*
* #load fixtures under different $fixturePath
to the "users" table
.
* #load fixtures under different $fixturePath.
* yii fixture/apply
u
sers --fixturePath=@app/some/other/path/to/fixtures
* yii fixture/apply
U
sers --fixturePath=@app/some/other/path/to/fixtures
* ~~~
* ~~~
*
*
* @author Mark Jebri <mark.github@yandex.ru>
* @author Mark Jebri <mark.github@yandex.ru>
...
@@ -52,8 +51,9 @@ use yii\helpers\Console;
...
@@ -52,8 +51,9 @@ use yii\helpers\Console;
*/
*/
class
FixtureController
extends
Controller
class
FixtureController
extends
Controller
{
{
use
DbTestTrait
;
use
FixtureTrait
;
/**
/**
* type of fixture apply to database
* type of fixture apply to database
*/
*/
...
@@ -64,16 +64,18 @@ class FixtureController extends Controller
...
@@ -64,16 +64,18 @@ class FixtureController extends Controller
*/
*/
public
$defaultAction
=
'apply'
;
public
$defaultAction
=
'apply'
;
/**
/**
* Alias to the path, where all fixtures are stored.
* @var string alias to the path, where all fixtures are stored.
* @var string
*/
*/
public
$fixturePath
=
'@tests/unit/fixtures'
;
public
$fixturePath
=
'@tests/unit/fixtures'
;
/**
/**
* Id of the database connection component of the application.
* @var string id of the database connection component of the application.
* @var string
*/
*/
public
$db
=
'db'
;
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.
* Returns the names of the global options for this command.
...
@@ -82,7 +84,7 @@ class FixtureController extends Controller
...
@@ -82,7 +84,7 @@ class FixtureController extends Controller
public
function
globalOptions
()
public
function
globalOptions
()
{
{
return
array_merge
(
parent
::
globalOptions
(),
[
return
array_merge
(
parent
::
globalOptions
(),
[
'db'
,
'fixturePath'
'db'
,
'fixturePath'
,
'namespace'
]);
]);
}
}
...
@@ -111,10 +113,6 @@ class FixtureController extends Controller
...
@@ -111,10 +113,6 @@ class FixtureController extends Controller
*/
*/
public
function
actionApply
(
array
$fixtures
,
array
$except
=
[])
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
);
$foundFixtures
=
$this
->
findFixtures
(
$fixtures
);
if
(
!
$this
->
needToApplyAll
(
$fixtures
[
0
]))
{
if
(
!
$this
->
needToApplyAll
(
$fixtures
[
0
]))
{
...
@@ -128,24 +126,26 @@ class FixtureController extends Controller
...
@@ -128,24 +126,26 @@ class FixtureController extends Controller
if
(
!
$foundFixtures
)
{
if
(
!
$foundFixtures
)
{
throw
new
Exception
(
"No files were found by name:
\"
"
.
implode
(
', '
,
$fixtures
)
.
"
\"
.
\n
"
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
)
.
"
\"
."
.
"Check that fixtures with these name exists, under fixtures path:
\n\"
"
.
Yii
::
getAlias
(
$this
->
fixturePath
)
.
"
\"
."
);
);
}
}
if
(
!
$this
->
confirmApply
(
$foundFixtures
,
$except
))
{
if
(
!
$this
->
confirmApply
(
$foundFixtures
,
$except
))
{
return
;
return
;
}
}
$fixtures
=
array_diff
(
$foundFixtures
,
$except
);
$fixtures
=
$this
->
getFixturesConfigs
(
array_diff
(
$foundFixtures
,
$except
)
);
$this
->
getFixtureManager
()
->
basePath
=
$this
->
fixturePath
;
if
(
!
$fixtures
)
{
$this
->
getFixtureManager
()
->
db
=
$this
->
db
;
throw
new
Exception
(
"No fixtures were found in namespace:
\"
"
.
$this
->
namespace
.
"
\"
."
);
}
$transaction
=
Yii
::
$app
->
db
->
beginTransaction
();
$transaction
=
Yii
::
$app
->
db
->
beginTransaction
();
try
{
try
{
$this
->
loadFixtures
(
$foundFixtures
);
$this
->
getDbConnection
()
->
createCommand
()
->
checkIntegrity
(
false
)
->
execute
();
$this
->
loadFixtures
(
$fixtures
);
$this
->
getDbConnection
()
->
createCommand
()
->
checkIntegrity
(
true
)
->
execute
();
$transaction
->
commit
();
$transaction
->
commit
();
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
$transaction
->
rollback
();
$transaction
->
rollback
();
$this
->
stdout
(
"Exception occured, transaction rollback. Tables will be in same state.
\n
"
,
Console
::
BG_RED
);
$this
->
stdout
(
"Exception occured, transaction rollback. Tables will be in same state.
\n
"
,
Console
::
BG_RED
);
...
@@ -155,32 +155,49 @@ class FixtureController extends Controller
...
@@ -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
* their names separated with commas, like: tbl_user,tbl_profile. Be sure there is no
* whitespace between tables names.
* 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'
])
public
function
actionClear
(
array
$fixtures
,
array
$except
=
[])
{
{
if
(
$this
->
needToApplyAll
(
$tables
[
0
]))
{
$foundFixtures
=
$this
->
findFixtures
(
$fixtures
);
$tables
=
$this
->
getDbConnection
()
->
schema
->
getTableNames
();
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
;
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
();
$transaction
=
Yii
::
$app
->
db
->
beginTransaction
();
try
{
try
{
$this
->
getDbConnection
()
->
createCommand
()
->
checkIntegrity
(
false
)
->
execute
();
$this
->
getDbConnection
()
->
createCommand
()
->
checkIntegrity
(
false
)
->
execute
();
foreach
(
$
tables
as
$table
)
{
foreach
(
$
fixtures
as
$fixtureConfig
)
{
$
this
->
getDbConnection
()
->
createCommand
()
->
delete
(
$table
)
->
execute
(
);
$
fixture
=
Yii
::
createObject
(
$fixtureConfig
);
$
this
->
getDbConnection
()
->
createCommand
()
->
resetSequence
(
$table
)
->
execute
();
$
fixture
->
unload
();
$this
->
stdout
(
"
Table
\"
{
$table
}
\"
was successfully clear
ed.
\n
"
,
Console
::
FG_GREEN
);
$this
->
stdout
(
"
Fixture
\"
{
$fixture
::
className
()
}
\"
was successfully unload
ed.
\n
"
,
Console
::
FG_GREEN
);
}
}
$this
->
getDbConnection
()
->
createCommand
()
->
checkIntegrity
(
true
)
->
execute
();
$this
->
getDbConnection
()
->
createCommand
()
->
checkIntegrity
(
true
)
->
execute
();
...
@@ -195,7 +212,7 @@ class FixtureController extends Controller
...
@@ -195,7 +212,7 @@ class FixtureController extends Controller
/**
/**
* Checks if the database and fixtures path are available.
* Checks if the database and fixtures path are available.
* @throws Exception
* @throws
yii\console\
Exception
*/
*/
public
function
checkRequirements
()
public
function
checkRequirements
()
{
{
...
@@ -210,7 +227,7 @@ class FixtureController extends Controller
...
@@ -210,7 +227,7 @@ class FixtureController extends Controller
/**
/**
* Returns database connection component
* Returns database connection component
* @return \yii\db\Connection
* @return \yii\db\Connection
* @throws Exception if [[db]] is invalid.
* @throws
yii\console\
Exception if [[db]] is invalid.
*/
*/
public
function
getDbConnection
()
public
function
getDbConnection
()
{
{
...
@@ -230,7 +247,7 @@ class FixtureController extends Controller
...
@@ -230,7 +247,7 @@ class FixtureController extends Controller
private
function
notifySuccess
(
$fixtures
)
private
function
notifySuccess
(
$fixtures
)
{
{
$this
->
stdout
(
"Fixtures were successfully loaded from path:
\n
"
,
Console
::
FG_YELLOW
);
$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
);
$this
->
outputList
(
$fixtures
);
}
}
...
@@ -241,7 +258,7 @@ class FixtureController extends Controller
...
@@ -241,7 +258,7 @@ class FixtureController extends Controller
private
function
notifyNotFound
(
$fixtures
)
private
function
notifyNotFound
(
$fixtures
)
{
{
$this
->
stdout
(
"Some fixtures were not found under path:
\n
"
,
Console
::
BG_RED
);
$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
->
outputList
(
$fixtures
);
$this
->
stdout
(
"
\n
"
);
$this
->
stdout
(
"
\n
"
);
}
}
...
@@ -255,7 +272,11 @@ class FixtureController extends Controller
...
@@ -255,7 +272,11 @@ class FixtureController extends Controller
private
function
confirmApply
(
$fixtures
,
$except
)
private
function
confirmApply
(
$fixtures
,
$except
)
{
{
$this
->
stdout
(
"Fixtures will be loaded from path:
\n
"
,
Console
::
FG_YELLOW
);
$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
);
$this
->
outputList
(
$fixtures
);
if
(
count
(
$except
))
{
if
(
count
(
$except
))
{
...
@@ -263,26 +284,31 @@ class FixtureController extends Controller
...
@@ -263,26 +284,31 @@ class FixtureController extends Controller
$this
->
outputList
(
$except
);
$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.
* Prompts user with confirmation for
fixtures that should be unload
ed.
* @param array $
tabl
es
* @param array $
fixtur
es
* @param array $except
* @param array $except
* @return boolean
* @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
->
stdout
(
"Fixtures will be loaded from path:
\n
"
,
Console
::
FG_YELLOW
);
$this
->
outputList
(
$tables
);
$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
))
{
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
);
$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
...
@@ -292,7 +318,7 @@ class FixtureController extends Controller
private
function
outputList
(
$data
)
private
function
outputList
(
$data
)
{
{
foreach
(
$data
as
$index
=>
$item
)
{
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
...
@@ -314,11 +340,11 @@ class FixtureController extends Controller
{
{
$fixturesPath
=
Yii
::
getAlias
(
$this
->
fixturePath
);
$fixturesPath
=
Yii
::
getAlias
(
$this
->
fixturePath
);
$filesToSearch
=
[
'*.php'
];
$filesToSearch
=
[
'*
Fixture
.php'
];
if
(
!
$this
->
needToApplyAll
(
$fixtures
[
0
]))
{
if
(
!
$this
->
needToApplyAll
(
$fixtures
[
0
]))
{
$filesToSearch
=
[];
$filesToSearch
=
[];
foreach
(
$fixtures
as
$fileName
)
{
foreach
(
$fixtures
as
$fileName
)
{
$filesToSearch
[]
=
$fileName
.
'.php'
;
$filesToSearch
[]
=
$fileName
.
'
Fixture
.php'
;
}
}
}
}
...
@@ -326,10 +352,33 @@ class FixtureController extends Controller
...
@@ -326,10 +352,33 @@ class FixtureController extends Controller
$foundFixtures
=
[];
$foundFixtures
=
[];
foreach
(
$files
as
$fixture
)
{
foreach
(
$files
as
$fixture
)
{
$foundFixtures
[]
=
basename
(
$fixture
,
'.php'
);
$foundFixtures
[]
=
basename
(
$fixture
,
'
Fixture
.php'
);
}
}
return
$foundFixtures
;
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