Skip to content

Commit

Permalink
added the ability for modifiers - and added a unique modifier by maki…
Browse files Browse the repository at this point in the history
…ng faker a singleton
  • Loading branch information
Owen Melbourne committed May 30, 2018
1 parent fb6412d commit 1d04f79
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
44 changes: 36 additions & 8 deletions app/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace App;

use Faker\Factory as Faker;

/**
* Class Column
* @package App
Expand All @@ -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.
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

}
6 changes: 5 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 1d04f79

Please sign in to comment.