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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
0e3fae36
Commit
0e3fae36
authored
Feb 04, 2014
by
Mark
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added globalFixtures option
parent
ce2f58f0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
30 deletions
+38
-30
FixtureController.php
framework/console/controllers/FixtureController.php
+38
-30
No files found.
framework/console/controllers/FixtureController.php
View file @
0e3fae36
...
...
@@ -12,8 +12,6 @@ use yii\console\Controller;
use
yii\console\Exception
;
use
yii\helpers\Console
;
use
yii\helpers\FileHelper
;
use
yii\helpers\ArrayHelper
;
use
yii\helpers\Inflector
;
use
yii\test\FixtureTrait
;
/**
...
...
@@ -68,11 +66,17 @@ class FixtureController extends Controller
* @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'
;
/**
* @var array global fixtures that should be applied when loading and unloading. By default it is set to `InitDbFixture`
* that disables and enables integrity check, so your data can be safely loaded.
*/
public
$globalFixtures
=
[
'yii\test\InitDbFixture'
,
];
/**
* Returns the names of the global options for this command.
...
...
@@ -81,18 +85,19 @@ class FixtureController extends Controller
public
function
globalOptions
()
{
return
array_merge
(
parent
::
globalOptions
(),
[
'db'
,
'namespace'
'db'
,
'namespace'
,
'globalFixtures'
]);
}
/**
* Apply given fixture to the table. You can load several fixtures specifying
* their names separated with commas, like: tbl_user,tbl_profile. Be sure there is no
* whitespace between tables names.
* Loads given fixture. You can load several fixtures specifying
* their names separated with commas, like: User,UserProfile,MyCustom. Be sure there is no
* whitespace between names. Note that if you are loading fixtures to storage, for example: database or nosql,
* storage will not be cleared, data will be appended to already existed.
* @param array $fixtures
* @throws \yii\console\Exception
*/
public
function
action
Apply
(
array
$fixtures
,
array
$except
=
[])
public
function
action
Load
(
array
$fixtures
,
array
$except
=
[])
{
$foundFixtures
=
$this
->
findFixtures
(
$fixtures
);
...
...
@@ -110,39 +115,38 @@ class FixtureController extends Controller
);
}
if
(
!
$this
->
confirm
Apply
(
$foundFixtures
,
$except
))
{
if
(
!
$this
->
confirm
Load
(
$foundFixtures
,
$except
))
{
return
;
}
$fixtures
=
$this
->
getFixturesConfig
(
array_diff
(
$foundFixtures
,
$except
));
$filtered
=
array_diff
(
$foundFixtures
,
$except
);
$fixtures
=
$this
->
getFixturesConfig
(
array_merge
(
$this
->
globalFixtures
,
$filtered
));
if
(
!
$fixtures
)
{
throw
new
Exception
(
'No fixtures were found in namespace: "'
.
$this
->
namespace
.
'"'
.
''
);
}
$transaction
=
Yii
::
$app
->
db
->
beginTransaction
();
$transaction
=
$this
->
getDbConnection
()
->
beginTransaction
();
try
{
$this
->
getDbConnection
()
->
createCommand
()
->
checkIntegrity
(
false
)
->
execute
();
$this
->
loadFixtures
(
$this
->
createFixtures
(
$fixtures
));
$this
->
getDbConnection
()
->
createCommand
()
->
checkIntegrity
(
true
)
->
execute
();
$transaction
->
commit
();
}
catch
(
\Exception
$e
)
{
$transaction
->
rollback
();
$this
->
stdout
(
"Exception occurred, transaction rollback. Tables will be in same state.
\n
"
,
Console
::
BG_RED
);
throw
$e
;
}
$this
->
notifyLoaded
(
ArrayHelper
::
getColumn
(
$fixtures
,
'class'
,
false
)
);
$this
->
notifyLoaded
(
$fixtures
);
}
/**
* 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:
User,UserProfile,MyCustom
. Be sure there is no
* whitespace between tables names.
* @param array|string $fixtures
* @param array|string $except
*/
public
function
action
Clear
(
array
$fixtures
,
array
$except
=
[])
public
function
action
Unload
(
array
$fixtures
,
array
$except
=
[])
{
$foundFixtures
=
$this
->
findFixtures
(
$fixtures
);
...
...
@@ -160,22 +164,21 @@ class FixtureController extends Controller
);
}
if
(
!
$this
->
confirm
Clear
(
$foundFixtures
,
$except
))
{
if
(
!
$this
->
confirm
Unload
(
$foundFixtures
,
$except
))
{
return
;
}
$fixtures
=
$this
->
getFixturesConfig
(
array_diff
(
$foundFixtures
,
$except
));
$filtered
=
array_diff
(
$foundFixtures
,
$except
);
$fixtures
=
$this
->
getFixturesConfig
(
array_merge
(
$this
->
globalFixtures
,
$filtered
));
if
(
!
$fixtures
)
{
throw
new
Exception
(
'No fixtures were found in namespace: '
.
$this
->
namespace
.
'".'
);
}
$transaction
=
Yii
::
$app
->
db
->
beginTransaction
();
$transaction
=
$this
->
getDbConnection
()
->
beginTransaction
();
try
{
$this
->
getDbConnection
()
->
createCommand
()
->
checkIntegrity
(
false
)
->
execute
();
$this
->
unloadFixtures
(
$this
->
createFixtures
(
$fixtures
));
$this
->
getDbConnection
()
->
createCommand
()
->
checkIntegrity
(
true
)
->
execute
();
$transaction
->
commit
();
}
catch
(
\Exception
$e
)
{
...
...
@@ -183,7 +186,7 @@ class FixtureController extends Controller
$this
->
stdout
(
"Exception occurred, transaction rollback. Tables will be in same state.
\n
"
,
Console
::
BG_RED
);
throw
$e
;
}
$this
->
notifyUnloaded
(
ArrayHelper
::
getColumn
(
$fixtures
,
'class'
,
false
)
);
$this
->
notifyUnloaded
(
$fixtures
);
}
/**
...
...
@@ -243,12 +246,15 @@ class FixtureController extends Controller
* @param array $except
* @return boolean
*/
private
function
confirm
Apply
(
$fixtures
,
$except
)
private
function
confirm
Load
(
$fixtures
,
$except
)
{
$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
->
stdout
(
"Global fixtures will be loaded:
\n\n
"
,
Console
::
FG_YELLOW
);
$this
->
outputList
(
$this
->
globalFixtures
);
$this
->
stdout
(
"
\n
Fixtures below will be loaded:
\n\n
"
,
Console
::
FG_YELLOW
);
$this
->
outputList
(
$fixtures
);
if
(
count
(
$except
))
{
...
...
@@ -265,12 +271,15 @@ class FixtureController extends Controller
* @param array $except
* @return boolean
*/
private
function
confirm
Clear
(
$fixtures
,
$except
)
private
function
confirm
Unload
(
$fixtures
,
$except
)
{
$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
->
stdout
(
"Global fixtures will be unloaded:
\n\n
"
,
Console
::
FG_YELLOW
);
$this
->
outputList
(
$this
->
globalFixtures
);
$this
->
stdout
(
"
\n
Fixtures below will be unloaded:
\n\n
"
,
Console
::
FG_YELLOW
);
$this
->
outputList
(
$fixtures
);
if
(
count
(
$except
))
{
...
...
@@ -339,12 +348,11 @@ class FixtureController extends Controller
foreach
(
$fixtures
as
$fixture
)
{
$fullClassName
=
$this
->
namespace
.
'\\'
.
$fixture
.
'Fixture'
;
$isNamespaced
=
(
strpos
(
$fixture
,
'\\'
)
!==
false
);
$fullClassName
=
$isNamespaced
?
$fixture
:
$this
->
namespace
.
'\\'
.
$fixture
.
'Fixture'
;
if
(
class_exists
(
$fullClassName
))
{
$config
[
Inflector
::
camel2id
(
$fixture
,
'_'
)]
=
[
'class'
=>
$fullClassName
,
];
$config
[]
=
$fullClassName
;
}
}
...
...
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