-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'testing-with-faker' of https://github.com/vinterskogen/…
…framework into vinterskogen-testing-with-faker
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Illuminate\Foundation\Testing; | ||
|
||
use Faker\Factory; | ||
use Faker\Generator; | ||
|
||
trait WithFaker | ||
{ | ||
/** | ||
* Faker generator instance. | ||
* | ||
* @var \Faker\Generator | ||
*/ | ||
protected $faker; | ||
|
||
/** | ||
* Setup up faker generator instance. | ||
* | ||
* @return void | ||
*/ | ||
protected function setUpFaker() | ||
{ | ||
$this->faker = $this->makeFaker(); | ||
} | ||
|
||
/** | ||
* Get a default faker generator instance or get a new one for given locale. | ||
* | ||
* @param string $locale | ||
* @return \Faker\Generator | ||
*/ | ||
protected function faker(string $locale = null) | ||
{ | ||
if (is_null($locale)) { | ||
return $this->faker; | ||
} | ||
|
||
return $this->makeFaker($locale); | ||
} | ||
|
||
/** | ||
* Set a new faker generator instance for given locale. | ||
* | ||
* @param string $locale | ||
* @return void | ||
*/ | ||
protected function fakerSetLocale(string $locale) | ||
{ | ||
$this->faker = $this->makeFaker($locale); | ||
} | ||
|
||
/** | ||
* Make a faker generator instance for given or default locale. | ||
* | ||
* @param string $locale | ||
* @return \Faker\Generator | ||
*/ | ||
protected function makeFaker(string $locale = null) | ||
{ | ||
if (is_null($locale)) { | ||
$locale = Factory::DEFAULT_LOCALE; | ||
} | ||
|
||
return Factory::create($locale); | ||
} | ||
} |