From 1d04f79a494d8096b7bd6d74e62a3db23b24ec21 Mon Sep 17 00:00:00 2001 From: Owen Melbourne Date: Wed, 30 May 2018 13:38:14 +0100 Subject: [PATCH] added the ability for modifiers - and added a unique modifier by making faker a singleton --- app/Column.php | 44 +++++++++++++++++++++++----- app/Providers/AppServiceProvider.php | 6 +++- tests/Unit/ColumnTest.php | 8 +++++ 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/app/Column.php b/app/Column.php index b9d0cae..46f607e 100644 --- a/app/Column.php +++ b/app/Column.php @@ -2,8 +2,6 @@ namespace App; -use Faker\Factory as Faker; - /** * Class Column * @package App @@ -27,11 +25,12 @@ class Column protected $replacementMethod; /** - * Just an instance of Faker to replace the information. + * This flags if the field is required to be unique, + * which solves unique constraints such as emails. * - * @var \Faker\Generator + * @var string */ - private $faker; + protected $unique; /** * Column constructor. @@ -42,8 +41,8 @@ class Column public function __construct(string $name, string $replacementMethod) { $this->name = $name; - $this->replacementMethod = $replacementMethod; - $this->faker = Faker::create(); + $this->unique = $this->checkIfUnique($replacementMethod); + $this->replacementMethod = $this->getReplacementMethod($replacementMethod); } /** @@ -53,7 +52,11 @@ public function __construct(string $name, string $replacementMethod) */ public function generate(): string { - return $this->faker->format($this->replacementMethod); + if ($this->unique) { + return app('faker')->unique()->format($this->replacementMethod); + } + + return app('faker')->format($this->replacementMethod); } /** @@ -65,4 +68,29 @@ public function getName(): string { return $this->name; } + + /** + * Returns true/false depending if the unique modifier has been used. + * + * @return bool + */ + private function checkIfUnique(string $string): bool + { + $parts = explode(':', $string); + + return current($parts) === 'unique'; + } + + /** + * Returns the name of the faker method to be used. + * + * @return string + */ + private function getReplacementMethod(string $string): string + { + $parts = explode(':', $string); + + return end($parts); + } + } \ No newline at end of file diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index e8eec71..319fb4f 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -16,7 +16,11 @@ class AppServiceProvider extends ServiceProvider */ public function boot(): void { - // + // We only want one instance of faker, this allows + // us to use unique() to prevent data clashes. + $this->app->singleton('faker', function ($app) { + return \Faker\Factory::create(); + }); } /** diff --git a/tests/Unit/ColumnTest.php b/tests/Unit/ColumnTest.php index 221f034..6c36268 100755 --- a/tests/Unit/ColumnTest.php +++ b/tests/Unit/ColumnTest.php @@ -24,4 +24,12 @@ public function test_i_can_get_the_column_name() $this->assertEquals('my_column', $name); } + + public function test_i_can_get_a_unique_column() + { + $column = new Column('my_column', 'unique:email'); + $name = $column->getName(); + + $this->assertEquals('my_column', $name); + } }