Skip to content
This repository has been archived by the owner on Jan 7, 2021. It is now read-only.

Commit

Permalink
Merge pull request #17 from Naoray/dev
Browse files Browse the repository at this point in the history
add check if faker instance has native resolver for given property name
  • Loading branch information
Naoray authored Sep 3, 2019
2 parents 916e5c0 + c1f5d1b commit 81fb5b2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 79 deletions.
63 changes: 33 additions & 30 deletions src/TypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Str;
use Doctrine\DBAL\Types\Type;
use Faker\Generator as Faker;
use InvalidArgumentException;

class TypeGuesser
{
Expand All @@ -13,6 +14,11 @@ class TypeGuesser
*/
protected $generator;

/**
* @var string
*/
protected static $default = 'word';

/**
* Create a new TypeGuesser instance.
*
Expand All @@ -32,9 +38,13 @@ public function __construct(Faker $generator)
*/
public function guess($name, Type $type, $size = null)
{
$name = Str::lower($name);
$name = str_replace('_', '', Str::lower($name));

if (! $size && $this->hasNativeResolverFor($name)) {
return $name;
}

if ('word' !== $typeNameGuess = $this->guessBasedOnName($name, $size)) {
if (self::$default !== $typeNameGuess = $this->guessBasedOnName($name, $size)) {
return $typeNameGuess;
}

Expand All @@ -51,49 +61,27 @@ public function guess($name, Type $type, $size = null)
*/
private function guessBasedOnName($name, $size = null)
{
switch (str_replace('_', '', $name)) {
case 'name':
return 'name';
case 'firstname':
return 'firstName';
case 'lastname':
return 'lastName';
case 'username':
switch ($name) {
case 'login':
return 'userName';
case 'email':
case 'emailaddress':
return 'email';
case 'phonenumber':
case 'phone':
case 'telephone':
case 'telnumber':
return 'phoneNumber';
case 'address':
return 'address';
case 'city':
case 'town':
return 'city';
case 'streetaddress':
return 'streetAddress';
case 'postcode':
case 'zipcode':
return 'postcode';
case 'state':
return 'state';
case 'county':
return $this->predictCountyType();
case 'country':
return $this->predictCountryType($size);
case 'locale':
return 'locale';
case 'currency':
case 'currencycode':
return 'currencyCode';
case 'url':
case 'website':
return 'url';
case 'company':
case 'companyname':
case 'employer':
return 'company';
Expand All @@ -102,10 +90,28 @@ private function guessBasedOnName($name, $size = null)
case 'password':
return "bcrypt(\$faker->word($size))";
default:
return 'word';
return self::$default;
}
}

/**
* Check if faker instance has a native resolver for the given property.
*
* @param string $property
*
* @return bool
*/
protected function hasNativeResolverFor($property)
{
try {
$this->generator->getFormatter($property);
} catch (InvalidArgumentException $e) {
return false;
}

return true;
}

/**
* Try to guess the right faker method for the given type.
*
Expand All @@ -132,15 +138,12 @@ protected function guessBasedOnType(Type $type, $size)
case Type::DECIMAL:
case Type::FLOAT:
return 'randomFloat' . ($size ? "($size)" : '');
case Type::GUID:
case Type::STRING:
return 'word';
case Type::TEXT:
return 'text';
case Type::TIME:
return 'time';
default:
return 'word';
return self::$default;
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/PrefillFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,27 @@ protected function setUp(): void
{
parent::setUp();

$this->setUpDatabase($this->app);

$this->beforeApplicationDestroyed(function () {
File::cleanDirectory(app_path());
File::cleanDirectory(database_path('factories'));
});
}

/**
* Set up the database.
*
* @param \Illuminate\Foundation\Application $app
*/
protected function setUpDatabase($app)
{
$this->loadMigrationsFrom([
'--database' => 'mysql',
'--realpath' => realpath(__DIR__ . '/migrations'),
]);
}

/** @test */
public function it_asks_if_a_model_shall_be_created_if_it_does_not_yet_exist()
{
Expand Down
31 changes: 3 additions & 28 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,10 @@

class TestCase extends Orchestra
{
/**
* Setup the test environment.
*/
protected function setUp(): void
{
parent::setUp();

$this->setUpDatabase($this->app);
}

/**
* Get package providers.
*
* @param \Illuminate\Foundation\Application $app
* @param \Illuminate\Foundation\Application $app
*
* @return array
*/
Expand All @@ -34,13 +24,11 @@ protected function getPackageProviders($app)
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
*
* @return void
* @param \Illuminate\Foundation\Application $app
*/
protected function getEnvironmentSetUp($app)
{
if (!file_exists(__DIR__ . '/../.env')) {
if (! file_exists(__DIR__ . '/../.env')) {
return;
}

Expand All @@ -60,17 +48,4 @@ protected function getEnvironmentSetUp($app)
'strict' => false,
]);
}

/**
* Set up the database.
*
* @param \Illuminate\Foundation\Application $app
*/
protected function setUpDatabase($app)
{
$this->loadMigrationsFrom([
'--database' => 'mysql',
'--realpath' => realpath(__DIR__ . '/migrations')
]);
}
}
42 changes: 21 additions & 21 deletions tests/TypeGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ public function it_can_guess_date_time_values_by_type()
$this->assertEquals('time', $this->typeGuesser->guess('closing_at', $this->getType(Type::TIME)));
}

/** @test */
public function it_can_guess_guid_values_by_type()
{
$this->assertEquals('word', $this->typeGuesser->guess('uuid', $this->getType(Type::GUID)));
}

/** @test */
public function it_can_guess_text_values_by_type()
{
Expand All @@ -93,37 +87,37 @@ public function it_can_guess_name_values()
/** @test */
public function it_can_guess_first_name_values()
{
$this->assertEquals('firstName', $this->typeGuesser->guess('first_name', $this->getType()));
$this->assertEquals('firstName', $this->typeGuesser->guess('firstname', $this->getType()));
$this->assertEquals('firstname', $this->typeGuesser->guess('first_name', $this->getType()));
$this->assertEquals('firstname', $this->typeGuesser->guess('firstname', $this->getType()));
}

/** @test */
public function it_can_guess_last_name_values()
{
$this->assertEquals('lastName', $this->typeGuesser->guess('last_name', $this->getType()));
$this->assertEquals('lastName', $this->typeGuesser->guess('lastname', $this->getType()));
$this->assertEquals('lastname', $this->typeGuesser->guess('last_name', $this->getType()));
$this->assertEquals('lastname', $this->typeGuesser->guess('lastname', $this->getType()));
}

/** @test */
public function it_can_guess_user_name_values()
{
$this->assertEquals('userName', $this->typeGuesser->guess('username', $this->getType(), $this->getType(), $this->getType()));
$this->assertEquals('userName', $this->typeGuesser->guess('user_name', $this->getType(), $this->getType(), $this->getType()));
$this->assertEquals('userName', $this->typeGuesser->guess('login', $this->getType(), $this->getType(), $this->getType()));
$this->assertEquals('username', $this->typeGuesser->guess('username', $this->getType()));
$this->assertEquals('username', $this->typeGuesser->guess('user_name', $this->getType()));
$this->assertEquals('userName', $this->typeGuesser->guess('login', $this->getType()));
}

/** @test */
public function it_can_guess_email_values()
{
$this->assertEquals('email', $this->typeGuesser->guess('email', $this->getType(), $this->getType()));
$this->assertEquals('email', $this->typeGuesser->guess('emailaddress', $this->getType(), $this->getType()));
$this->assertEquals('email', $this->typeGuesser->guess('email_address', $this->getType(), $this->getType()));
$this->assertEquals('email', $this->typeGuesser->guess('email', $this->getType()));
$this->assertEquals('email', $this->typeGuesser->guess('emailaddress', $this->getType()));
$this->assertEquals('email', $this->typeGuesser->guess('email_address', $this->getType()));
}

/** @test */
public function it_can_guess_phone_number_values()
{
$this->assertEquals('phoneNumber', $this->typeGuesser->guess('phonenumber', $this->getType()));
$this->assertEquals('phonenumber', $this->typeGuesser->guess('phonenumber', $this->getType()));
$this->assertEquals('phoneNumber', $this->typeGuesser->guess('phone', $this->getType()));
$this->assertEquals('phoneNumber', $this->typeGuesser->guess('telephone', $this->getType()));
$this->assertEquals('phoneNumber', $this->typeGuesser->guess('telnumber', $this->getType()));
Expand All @@ -145,8 +139,8 @@ public function it_can_guess_city_values()
/** @test */
public function it_can_guess_street_address_values()
{
$this->assertEquals('streetAddress', $this->typeGuesser->guess('street_address', $this->getType()));
$this->assertEquals('streetAddress', $this->typeGuesser->guess('streetAddress', $this->getType()));
$this->assertEquals('streetaddress', $this->typeGuesser->guess('street_address', $this->getType()));
$this->assertEquals('streetaddress', $this->typeGuesser->guess('streetAddress', $this->getType()));
}

/** @test */
Expand Down Expand Up @@ -183,8 +177,8 @@ public function it_can_guess_locale_values()
public function it_can_guess_currency_code_values()
{
$this->assertEquals('currencyCode', $this->typeGuesser->guess('currency', $this->getType()));
$this->assertEquals('currencyCode', $this->typeGuesser->guess('currencycode', $this->getType()));
$this->assertEquals('currencyCode', $this->typeGuesser->guess('currency_code', $this->getType()));
$this->assertEquals('currencycode', $this->typeGuesser->guess('currencycode', $this->getType()));
$this->assertEquals('currencycode', $this->typeGuesser->guess('currency_code', $this->getType()));
}

/** @test */
Expand Down Expand Up @@ -227,4 +221,10 @@ public function it_returns_word_as_default_value()
{
$this->assertEquals('word', $this->typeGuesser->guess('not_guessable', $this->getType()));
}

/** @test */
public function it_can_guess_properties_based_on_their_names()
{
$this->assertEquals('latitude', $this->typeGuesser->guess('latitude', $this->getType()));
}
}

0 comments on commit 81fb5b2

Please sign in to comment.