diff --git a/src/TypeGuesser.php b/src/TypeGuesser.php index 7ff19bc..4ff5bde 100644 --- a/src/TypeGuesser.php +++ b/src/TypeGuesser.php @@ -5,6 +5,7 @@ use Illuminate\Support\Str; use Doctrine\DBAL\Types\Type; use Faker\Generator as Faker; +use InvalidArgumentException; class TypeGuesser { @@ -13,6 +14,11 @@ class TypeGuesser */ protected $generator; + /** + * @var string + */ + protected static $default = 'word'; + /** * Create a new TypeGuesser instance. * @@ -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; } @@ -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'; @@ -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. * @@ -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; } } diff --git a/tests/PrefillFactoryTest.php b/tests/PrefillFactoryTest.php index a42227f..6067a72 100644 --- a/tests/PrefillFactoryTest.php +++ b/tests/PrefillFactoryTest.php @@ -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() { diff --git a/tests/TestCase.php b/tests/TestCase.php index 6d4d4d0..854b630 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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 */ @@ -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; } @@ -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') - ]); - } } diff --git a/tests/TypeGuesserTest.php b/tests/TypeGuesserTest.php index e901e10..7be9b3a 100644 --- a/tests/TypeGuesserTest.php +++ b/tests/TypeGuesserTest.php @@ -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() { @@ -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())); @@ -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 */ @@ -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 */ @@ -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())); + } }