From 8eb63482d9f5f0fdf8153183e8eee30758b8e3e9 Mon Sep 17 00:00:00 2001 From: Vinter Skogen Date: Sat, 2 Dec 2017 04:15:47 +0300 Subject: [PATCH] Added 'WithFaker' testing trait --- .../Foundation/Testing/TestCase.php | 4 ++ .../Foundation/Testing/WithFaker.php | 67 +++++++++++++++++++ 2 files changed, 71 insertions(+) mode change 100755 => 100644 src/Illuminate/Foundation/Testing/TestCase.php create mode 100644 src/Illuminate/Foundation/Testing/WithFaker.php diff --git a/src/Illuminate/Foundation/Testing/TestCase.php b/src/Illuminate/Foundation/Testing/TestCase.php old mode 100755 new mode 100644 index 2a0124ff1b76..1e9d33e2077c --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -120,6 +120,10 @@ protected function setUpTraits() $this->disableEventsForAllTests(); } + if (isset($uses[WithFaker::class])) { + $this->setUpFaker(); + } + return $uses; } diff --git a/src/Illuminate/Foundation/Testing/WithFaker.php b/src/Illuminate/Foundation/Testing/WithFaker.php new file mode 100644 index 000000000000..894a3fc067e3 --- /dev/null +++ b/src/Illuminate/Foundation/Testing/WithFaker.php @@ -0,0 +1,67 @@ +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); + } +}