Skip to content

Commit

Permalink
Use 'fake' helper in factories (#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicodevs authored Jan 15, 2025
1 parent ad398bf commit d0a7256
Show file tree
Hide file tree
Showing 31 changed files with 157 additions and 157 deletions.
10 changes: 5 additions & 5 deletions src/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected function buildDefinition(Model $model): string
} elseif (in_array($column->dataType(), ['enum', 'set']) && !empty($column->attributes())) {
$faker = FakerRegistry::fakerDataType($column->dataType()) ?? FakerRegistry::fakerData($column->name());
$definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => ";
$definition .= '$this->faker->' . $faker;
$definition .= 'fake()->' . $faker;
$definition .= ',' . PHP_EOL;
$definition = str_replace(
"/** {$column->dataType()}_attributes **/",
Expand All @@ -132,7 +132,7 @@ protected function buildDefinition(Model $model): string
} elseif (in_array($column->dataType(), ['decimal', 'double', 'float'])) {
$faker = FakerRegistry::fakerData($column->name()) ?? FakerRegistry::fakerDataType($column->dataType());
$definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => ";
$definition .= '$this->faker->' . $faker;
$definition .= 'fake()->' . $faker;
$definition .= ',' . PHP_EOL;

$precision = min([65, intval($column->attributes()[0] ?? 10)]);
Expand All @@ -150,8 +150,8 @@ protected function buildDefinition(Model $model): string
if ($column->isNullable()) {
continue;
}
$definition .= sprintf('%s%s => $this->faker->%s,%s', str_repeat(self::INDENT, 3), "'{$column->name()}_id'", FakerRegistry::fakerDataType('id'), PHP_EOL);
$definition .= sprintf('%s%s => $this->faker->%s,%s', str_repeat(self::INDENT, 3), "'{$column->name()}_type'", FakerRegistry::fakerDataType('string'), PHP_EOL);
$definition .= sprintf('%s%s => fake()->%s,%s', str_repeat(self::INDENT, 3), "'{$column->name()}_id'", FakerRegistry::fakerDataType('id'), PHP_EOL);
$definition .= sprintf('%s%s => fake()->%s,%s', str_repeat(self::INDENT, 3), "'{$column->name()}_type'", FakerRegistry::fakerDataType('string'), PHP_EOL);
} elseif ($column->dataType() === 'rememberToken') {
$definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => ";
$definition .= 'Str::random(10)';
Expand All @@ -178,7 +178,7 @@ protected function buildDefinition(Model $model): string
$faker = sprintf("regexify('[A-Za-z0-9]{%s}')", current($column->attributes()));
}

$definition .= '$this->faker->' . $faker;
$definition .= 'fake()->' . $faker;
$definition .= ',' . PHP_EOL;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Generators/PhpUnitTestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ protected function buildTestCases(Controller $controller): string
} else {
if ($local_column->isDate()) {
$this->addImport($controller, 'Illuminate\\Support\\Carbon');
$faker = sprintf('$%s = Carbon::parse($this->faker->%s);', $data, FakerRegistry::fakerData($local_column->name()) ?? FakerRegistry::fakerDataType($local_model->column($column)->dataType()));
$faker = sprintf('$%s = Carbon::parse(fake()->%s);', $data, FakerRegistry::fakerData($local_column->name()) ?? FakerRegistry::fakerDataType($local_model->column($column)->dataType()));
} else {
$faker = sprintf('$%s = $this->faker->%s;', $data, FakerRegistry::fakerData($local_column->name()) ?? FakerRegistry::fakerDataType($local_model->column($column)->dataType()));
$faker = sprintf('$%s = fake()->%s;', $data, FakerRegistry::fakerData($local_column->name()) ?? FakerRegistry::fakerDataType($local_model->column($column)->dataType()));
}
}

Expand All @@ -260,9 +260,9 @@ protected function buildTestCases(Controller $controller): string
} else {
if ($local_column->isDate()) {
$this->addImport($controller, 'Illuminate\\Support\\Carbon');
$faker = sprintf('$%s = Carbon::parse($this->faker->%s);', $local_column->name(), FakerRegistry::fakerData($local_column->name()) ?? FakerRegistry::fakerDataType($local_column->dataType()));
$faker = sprintf('$%s = Carbon::parse(fake()->%s);', $local_column->name(), FakerRegistry::fakerData($local_column->name()) ?? FakerRegistry::fakerDataType($local_column->dataType()));
} else {
$faker = sprintf('$%s = $this->faker->%s;', $local_column->name(), FakerRegistry::fakerData($local_column->name()) ?? FakerRegistry::fakerDataType($local_column->dataType()));
$faker = sprintf('$%s = fake()->%s;', $local_column->name(), FakerRegistry::fakerData($local_column->name()) ?? FakerRegistry::fakerDataType($local_column->dataType()));
}
$variable_name = $local_column->name();
}
Expand Down
94 changes: 47 additions & 47 deletions tests/fixtures/factories/all-column-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,57 +21,57 @@ class AllTypeFactory extends Factory
public function definition(): array
{
return [
'bigInteger' => $this->faker->numberBetween(-100000, 100000),
'binary' => $this->faker->sha256(),
'boolean' => $this->faker->boolean(),
'char' => $this->faker->randomLetter(),
'date' => $this->faker->date(),
'dateTime' => $this->faker->dateTime(),
'dateTimeTz' => $this->faker->dateTime(),
'decimal' => $this->faker->randomFloat(0, 0, 9999999999.),
'double' => $this->faker->randomFloat(0, 0, 9999999999.),
'enum' => $this->faker->randomElement(["1","2","3"]),
'float' => $this->faker->randomFloat(0, 0, 9999999999.),
'fullText' => $this->faker->text(),
'geometry' => $this->faker->word(),
'geometryCollection' => $this->faker->word(),
'integer' => $this->faker->numberBetween(-10000, 10000),
'ipAddress' => $this->faker->ipv4(),
'bigInteger' => fake()->numberBetween(-100000, 100000),
'binary' => fake()->sha256(),
'boolean' => fake()->boolean(),
'char' => fake()->randomLetter(),
'date' => fake()->date(),
'dateTime' => fake()->dateTime(),
'dateTimeTz' => fake()->dateTime(),
'decimal' => fake()->randomFloat(0, 0, 9999999999.),
'double' => fake()->randomFloat(0, 0, 9999999999.),
'enum' => fake()->randomElement(["1","2","3"]),
'float' => fake()->randomFloat(0, 0, 9999999999.),
'fullText' => fake()->text(),
'geometry' => fake()->word(),
'geometryCollection' => fake()->word(),
'integer' => fake()->numberBetween(-10000, 10000),
'ipAddress' => fake()->ipv4(),
'json' => '{}',
'jsonb' => '{}',
'lineString' => $this->faker->word(),
'longText' => $this->faker->text(),
'macAddress' => $this->faker->macAddress(),
'mediumInteger' => $this->faker->numberBetween(-10000, 10000),
'mediumText' => $this->faker->text(),
'morphs_id' => $this->faker->randomDigitNotNull(),
'morphs_type' => $this->faker->word(),
'ulidMorphs' => $this->faker->word(),
'uuidMorphs' => $this->faker->word(),
'multiLineString' => $this->faker->word(),
'multiPoint' => $this->faker->word(),
'multiPolygon' => $this->faker->word(),
'point' => $this->faker->word(),
'polygon' => $this->faker->word(),
'lineString' => fake()->word(),
'longText' => fake()->text(),
'macAddress' => fake()->macAddress(),
'mediumInteger' => fake()->numberBetween(-10000, 10000),
'mediumText' => fake()->text(),
'morphs_id' => fake()->randomDigitNotNull(),
'morphs_type' => fake()->word(),
'ulidMorphs' => fake()->word(),
'uuidMorphs' => fake()->word(),
'multiLineString' => fake()->word(),
'multiPoint' => fake()->word(),
'multiPolygon' => fake()->word(),
'point' => fake()->word(),
'polygon' => fake()->word(),
'rememberToken' => Str::random(10),
'set' => $this->faker->randomElement(["1","2","3"]),
'smallInteger' => $this->faker->numberBetween(-1000, 1000),
'string' => $this->faker->word(),
'text' => $this->faker->text(),
'time' => $this->faker->time(),
'timeTz' => $this->faker->time(),
'timestamp' => $this->faker->dateTime(),
'timestampTz' => $this->faker->dateTime(),
'tinyInteger' => $this->faker->numberBetween(-8, 8),
'unsignedBigInteger' => $this->faker->randomNumber(),
'unsignedDecimal' => $this->faker->randomNumber(),
'unsignedInteger' => $this->faker->randomNumber(),
'unsignedMediumInteger' => $this->faker->randomNumber(),
'unsignedSmallInteger' => $this->faker->randomNumber(),
'unsignedTinyInteger' => $this->faker->randomDigitNotNull(),
'set' => fake()->randomElement(["1","2","3"]),
'smallInteger' => fake()->numberBetween(-1000, 1000),
'string' => fake()->word(),
'text' => fake()->text(),
'time' => fake()->time(),
'timeTz' => fake()->time(),
'timestamp' => fake()->dateTime(),
'timestampTz' => fake()->dateTime(),
'tinyInteger' => fake()->numberBetween(-8, 8),
'unsignedBigInteger' => fake()->randomNumber(),
'unsignedDecimal' => fake()->randomNumber(),
'unsignedInteger' => fake()->randomNumber(),
'unsignedMediumInteger' => fake()->randomNumber(),
'unsignedSmallInteger' => fake()->randomNumber(),
'unsignedTinyInteger' => fake()->randomDigitNotNull(),
'ulid' => (string) Str::ulid(),
'uuid' => $this->faker->uuid(),
'year' => $this->faker->year(),
'uuid' => fake()->uuid(),
'year' => fake()->year(),
];
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/factories/factory-smallint-and-tinyint.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class ModelFactory extends Factory
public function definition(): array
{
return [
'market_type' => $this->faker->numberBetween(-8, 8),
'deposit' => $this->faker->randomNumber(),
'market_type' => fake()->numberBetween(-8, 8),
'deposit' => fake()->randomNumber(),
];
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/factories/fake-nullables.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class PostFactory extends Factory
public function definition(): array
{
return [
'title' => $this->faker->sentence(4),
'content' => $this->faker->paragraphs(3, true),
'title' => fake()->sentence(4),
'content' => fake()->paragraphs(3, true),
'author_id' => User::factory(),
];
}
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/factories/model-key-constraints.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public function definition(): array
{
return [
'user_id' => User::factory(),
'external_id' => $this->faker->word(),
'external_id' => fake()->word(),
'sub_id' => Subscription::factory(),
'expires_at' => $this->faker->dateTime(),
'expires_at' => fake()->dateTime(),
'meta' => '[]',
'customer_id' => Customer::factory(),
'tran_id' => Transaction::factory(),
Expand Down
16 changes: 8 additions & 8 deletions tests/fixtures/factories/model-modifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class ModifierFactory extends Factory
public function definition(): array
{
return [
'title' => $this->faker->sentence(4),
'name' => $this->faker->name(),
'content' => $this->faker->paragraphs(3, true),
'amount' => $this->faker->randomFloat(3, 0, 999999.999),
'total' => $this->faker->randomFloat(2, 0, 99999999.99),
'overflow' => $this->faker->randomFloat(30, 0, 99999999999999999999999999999999999.999999999999999999999999999999),
'ssn' => $this->faker->ssn(),
'role' => $this->faker->randomElement(["user","admin","owner"]),
'title' => fake()->sentence(4),
'name' => fake()->name(),
'content' => fake()->paragraphs(3, true),
'amount' => fake()->randomFloat(3, 0, 999999.999),
'total' => fake()->randomFloat(2, 0, 99999999.99),
'overflow' => fake()->randomFloat(30, 0, 99999999999999999999999999999999999.999999999999999999999999999999),
'ssn' => fake()->ssn(),
'role' => fake()->randomElement(["user","admin","owner"]),
];
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/factories/nested-components.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class UserFactory extends Factory
public function definition(): array
{
return [
'name' => $this->faker->name(),
'password' => $this->faker->password(),
'name' => fake()->name(),
'password' => fake()->password(),
];
}
}
14 changes: 7 additions & 7 deletions tests/fixtures/factories/phone.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class PhoneFactory extends Factory
public function definition(): array
{
return [
'label' => $this->faker->word(),
'label' => fake()->word(),
'user_id' => User::factory(),
'phone_number' => $this->faker->phoneNumber(),
'type' => $this->faker->randomElement(["home","cell"]),
'status' => $this->faker->randomElement(["archived","deleted"]),
'foo_id' => $this->faker->randomDigitNotNull(),
'foo_type' => $this->faker->word(),
'tag' => $this->faker->regexify('[A-Za-z0-9]{3}'),
'phone_number' => fake()->phoneNumber(),
'type' => fake()->randomElement(["home","cell"]),
'status' => fake()->randomElement(["archived","deleted"]),
'foo_id' => fake()->randomDigitNotNull(),
'foo_type' => fake()->word(),
'tag' => fake()->regexify('[A-Za-z0-9]{3}'),
];
}
}
12 changes: 6 additions & 6 deletions tests/fixtures/factories/post-configured.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class PostFactory extends Factory
public function definition(): array
{
return [
'title' => $this->faker->sentence(4),
'title' => fake()->sentence(4),
'author_id' => Author::factory(),
'author_bio' => $this->faker->text(),
'content' => $this->faker->paragraphs(3, true),
'published_at' => $this->faker->dateTime(),
'updated_at' => $this->faker->dateTime(),
'word_count' => $this->faker->randomNumber(),
'author_bio' => fake()->text(),
'content' => fake()->paragraphs(3, true),
'published_at' => fake()->dateTime(),
'updated_at' => fake()->dateTime(),
'word_count' => fake()->randomNumber(),
];
}
}
12 changes: 6 additions & 6 deletions tests/fixtures/factories/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class PostFactory extends Factory
public function definition(): array
{
return [
'title' => $this->faker->sentence(4),
'title' => fake()->sentence(4),
'author_id' => Author::factory(),
'author_bio' => $this->faker->text(),
'content' => $this->faker->paragraphs(3, true),
'published_at' => $this->faker->dateTime(),
'updated_at' => $this->faker->dateTime(),
'word_count' => $this->faker->randomNumber(),
'author_bio' => fake()->text(),
'content' => fake()->paragraphs(3, true),
'published_at' => fake()->dateTime(),
'updated_at' => fake()->dateTime(),
'word_count' => fake()->randomNumber(),
];
}
}
6 changes: 3 additions & 3 deletions tests/fixtures/factories/resource-statements.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class UserFactory extends Factory
public function definition(): array
{
return [
'name' => $this->faker->name(),
'email' => $this->faker->safeEmail(),
'password' => $this->faker->password(),
'name' => fake()->name(),
'email' => fake()->safeEmail(),
'password' => fake()->password(),
'remember_token' => Str::random(10),
];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/factories/team.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TeamFactory extends Factory
public function definition(): array
{
return [
'name' => $this->faker->name(),
'name' => fake()->name(),
'owner_id' => User::factory(),
];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/factories/unconventional-foreign-key.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class StateFactory extends Factory
public function definition(): array
{
return [
'name' => $this->faker->name(),
'name' => fake()->name(),
'countries_id' => Country::factory(),
'country_code' => Country::factory()->create()->code,
'ccid' => Country::factory()->create()->ccid,
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/factories/unconventional.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TeamFactory extends Factory
public function definition(): array
{
return [
'name' => $this->faker->name(),
'name' => fake()->name(),
'owner' => Owner::factory(),
'manager' => User::factory(),
'options' => '{}',
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/factories/with-enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PostFactory extends Factory
public function definition(): array
{
return [
'name' => $this->faker->randomElement(["one","two","three"]),
'name' => fake()->randomElement(["one","two","three"]),
];
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/tests/phpunit/api-resource-nested.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function store_uses_form_request_validation(): void
public function store_saves(): void
{
$post = Post::factory()->create();
$body = $this->faker->text();
$body = fake()->text();
$user = User::factory()->create();

$response = $this->post(route('comments.store', ['post' => $post]), [
Expand Down Expand Up @@ -95,7 +95,7 @@ public function update_behaves_as_expected(): void
{
$comment = Comment::factory()->create();
$post = Post::factory()->create();
$body = $this->faker->text();
$body = fake()->text();
$user = User::factory()->create();

$response = $this->put(route('comments.update', ['post' => $post, 'comment' => $comment]), [
Expand Down
16 changes: 8 additions & 8 deletions tests/fixtures/tests/phpunit/api-shorthand-validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public function store_uses_form_request_validation(): void
#[Test]
public function store_saves(): void
{
$name = $this->faker->name();
$name = fake()->name();
$certificate_type = CertificateType::factory()->create();
$reference = $this->faker->word();
$document = $this->faker->word();
$expiry_date = Carbon::parse($this->faker->date());
$reference = fake()->word();
$document = fake()->word();
$expiry_date = Carbon::parse(fake()->date());

$response = $this->post(route('certificates.store'), [
'name' => $name,
Expand Down Expand Up @@ -98,11 +98,11 @@ public function update_uses_form_request_validation(): void
public function update_behaves_as_expected(): void
{
$certificate = Certificate::factory()->create();
$name = $this->faker->name();
$name = fake()->name();
$certificate_type = CertificateType::factory()->create();
$reference = $this->faker->word();
$document = $this->faker->word();
$expiry_date = Carbon::parse($this->faker->date());
$reference = fake()->word();
$document = fake()->word();
$expiry_date = Carbon::parse(fake()->date());

$response = $this->put(route('certificates.update', $certificate), [
'name' => $name,
Expand Down
Loading

0 comments on commit d0a7256

Please sign in to comment.