Skip to content

Commit

Permalink
Merge branch 'testing-with-faker' of https://github.com/vinterskogen/…
Browse files Browse the repository at this point in the history
…framework into vinterskogen-testing-with-faker
  • Loading branch information
taylorotwell committed Dec 4, 2017
2 parents 8004b10 + 8eb6348 commit 56363e4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Foundation/Testing/TestCase.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ protected function setUpTraits()
$this->disableEventsForAllTests();
}

if (isset($uses[WithFaker::class])) {
$this->setUpFaker();
}

return $uses;
}

Expand Down
67 changes: 67 additions & 0 deletions src/Illuminate/Foundation/Testing/WithFaker.php
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);
}
}

0 comments on commit 56363e4

Please sign in to comment.