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
467254a3
Commit
467254a3
authored
Jan 03, 2014
by
Mark
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed CS, added exception check
parent
aa904971
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
14 deletions
+22
-14
FixtureController.php
extensions/yii/faker/FixtureController.php
+22
-14
No files found.
extensions/yii/faker/FixtureController.php
View file @
467254a3
...
@@ -16,7 +16,7 @@ use yii\helpers\Console;
...
@@ -16,7 +16,7 @@ use yii\helpers\Console;
* This command manage fixtures creations based on given template.
* This command manage fixtures creations based on given template.
*
*
* Fixtures are one of the important paths in unit testing. To speed up developers
* Fixtures are one of the important paths in unit testing. To speed up developers
* work th
is
fixtures can be generated automatically, based on prepared template.
* work th
ese
fixtures can be generated automatically, based on prepared template.
* This command is a simple wrapper for the fixtures library [Faker](https://github.com/fzaninotto/Faker).
* This command is a simple wrapper for the fixtures library [Faker](https://github.com/fzaninotto/Faker).
*
*
* You should configure this command as follows (you can use any alias, not only "faker:fixture"):
* You should configure this command as follows (you can use any alias, not only "faker:fixture"):
...
@@ -49,9 +49,9 @@ use yii\helpers\Console;
...
@@ -49,9 +49,9 @@ use yii\helpers\Console;
* ~~~
* ~~~
*
*
* If you use callback as a attribute value, then it will be called as shown with three parameters:
* If you use callback as a attribute value, then it will be called as shown with three parameters:
*
*
$fixture - current fixture array.
*
-
$fixture - current fixture array.
*
*
$faker - faker generator instance
*
-
$faker - faker generator instance
*
*
$index - current fixture index. For example if user need to generate 3 fixtures for tbl_user, it will be 0..2
*
-
$index - current fixture index. For example if user need to generate 3 fixtures for tbl_user, it will be 0..2
* After you set all needed fields in callback, you need to return $fixture array back from the callback.
* After you set all needed fields in callback, you need to return $fixture array back from the callback.
*
*
* After you prepared needed templates for tables you can simply generate your fixtures via command
* After you prepared needed templates for tables you can simply generate your fixtures via command
...
@@ -161,7 +161,7 @@ class FixtureController extends \yii\console\controllers\FixtureController
...
@@ -161,7 +161,7 @@ class FixtureController extends \yii\console\controllers\FixtureController
* More info in [Faker](https://github.com/fzaninotto/Faker.) library docs.
* More info in [Faker](https://github.com/fzaninotto/Faker.) library docs.
* @var array
* @var array
*/
*/
public
$providers
=
array
()
;
public
$providers
=
[]
;
/**
/**
* Faker generator instance
* Faker generator instance
...
@@ -202,13 +202,20 @@ class FixtureController extends \yii\console\controllers\FixtureController
...
@@ -202,13 +202,20 @@ class FixtureController extends \yii\console\controllers\FixtureController
$templatePath
=
Yii
::
getAlias
(
$this
->
templatePath
);
$templatePath
=
Yii
::
getAlias
(
$this
->
templatePath
);
$fixturesPath
=
Yii
::
getAlias
(
$this
->
fixturesPath
);
$fixturesPath
=
Yii
::
getAlias
(
$this
->
fixturesPath
);
if
(
$this
->
needToGenerateAll
(
$file
))
if
(
$this
->
needToGenerateAll
(
$file
))
{
$files
=
FileHelper
::
findFiles
(
$templatePath
,
[
'only'
=>
[
'.php'
]]);
$files
=
FileHelper
::
findFiles
(
$templatePath
,
[
'only'
=>
[
'.php'
]]);
else
}
else
{
$files
=
FileHelper
::
findFiles
(
$templatePath
,
[
'only'
=>
[
$file
.
'.php'
]]);
$files
=
FileHelper
::
findFiles
(
$templatePath
,
[
'only'
=>
[
$file
.
'.php'
]]);
}
foreach
(
$files
as
$templateFile
)
if
(
empty
(
$files
))
{
{
throw
new
Exception
(
"No files were found by name:
\"
{
$file
}
\"
.
\n
"
.
"Check that template with these name exists, under template path:
\n\"
{
$templatePath
}
\"
."
);
}
foreach
(
$files
as
$templateFile
)
{
$fixtureFileName
=
basename
(
$templateFile
);
$fixtureFileName
=
basename
(
$templateFile
);
$template
=
$this
->
getTemplate
(
$templateFile
);
$template
=
$this
->
getTemplate
(
$templateFile
);
$fixtures
=
[];
$fixtures
=
[];
...
@@ -229,9 +236,8 @@ class FixtureController extends \yii\console\controllers\FixtureController
...
@@ -229,9 +236,8 @@ class FixtureController extends \yii\console\controllers\FixtureController
*/
*/
public
function
getGenerator
()
public
function
getGenerator
()
{
{
if
(
is_null
(
$this
->
_generator
))
if
(
is_null
(
$this
->
_generator
))
{
{
//replacing - on _ because Faker support only en_US format and not intl
#replacing - on _ because Faker support only en_US format and not intl
$language
=
is_null
(
$this
->
language
)
?
str_replace
(
'-'
,
'_'
,
Yii
::
$app
->
language
)
:
$this
->
language
;
$language
=
is_null
(
$this
->
language
)
?
str_replace
(
'-'
,
'_'
,
Yii
::
$app
->
language
)
:
$this
->
language
;
$this
->
_generator
=
\Faker\Factory
::
create
(
$language
);
$this
->
_generator
=
\Faker\Factory
::
create
(
$language
);
...
@@ -247,18 +253,20 @@ class FixtureController extends \yii\console\controllers\FixtureController
...
@@ -247,18 +253,20 @@ class FixtureController extends \yii\console\controllers\FixtureController
{
{
$path
=
Yii
::
getAlias
(
$this
->
templatePath
);
$path
=
Yii
::
getAlias
(
$this
->
templatePath
);
if
(
!
is_dir
(
$path
))
if
(
!
is_dir
(
$path
))
{
throw
new
Exception
(
"The template path
\"
{
$this
->
templatePath
}
\"
not exist"
);
throw
new
Exception
(
"The template path
\"
{
$this
->
templatePath
}
\"
not exist"
);
}
}
}
/**
/**
* Adds users providers to the faker generator.
* Adds users providers to the faker generator.
*/
*/
public
function
addProviders
()
public
function
addProviders
()
{
{
foreach
(
$this
->
providers
as
$provider
)
foreach
(
$this
->
providers
as
$provider
)
{
$this
->
generator
->
addProvider
(
new
$provider
(
$this
->
generator
));
$this
->
generator
->
addProvider
(
new
$provider
(
$this
->
generator
));
}
}
}
/**
/**
* Checks if needed to generate all fixtures.
* Checks if needed to generate all fixtures.
...
...
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