Skip to content

Commit 02bd81f

Browse files
committed
- Fixed factories for unique with optional
- Removed passthrough for strings
1 parent c430def commit 02bd81f

File tree

12 files changed

+119
-49
lines changed

12 files changed

+119
-49
lines changed

docs/basic-usage/using-generators.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,33 @@ class ProductFactory extends Factory
6060
public function definition(): array
6161
{
6262
return [
63-
'name' => $this->faker->passThrough(ucfirst($this->faker->text(255))),
64-
'address' => $this->faker->address(),
65-
'slug' => $this->faker->unique()->slug(),
66-
'description' => $this->faker->optional()->sentences(3, true),
67-
'price' => $this->faker->randomFloat(2, 0, 999999999999),
68-
'stock' => $this->faker->numberBetween(0, 4294967295),
69-
'on_sale' => $this->faker->boolean(),
70-
'features' => $this->faker->passThrough($this->faker->words()),
71-
'published_at' => $this->faker->dateTime()?->format('Y-m-d H:i'),
72-
'expire_at' => $this->faker->dateTime()?->format('Y-m-d H:i'),
73-
'released_on' => $this->faker->date(),
74-
'sale_time' => $this->faker->time(),
75-
'status' => $this->faker->randomElement(['draft', 'published']),
76-
'manufactured_year' => $this->faker->year(2100),
63+
'name' => fake()->text(fake()->numberBetween(5, 255)),
64+
'address' => fake()->address(),
65+
'slug' => fake()->unique()->slug(),
66+
'description' => fake()->optional()->sentences(3, true),
67+
'price' => fake()->randomFloat(2, 0, 999999999999),
68+
'stock' => fake()->numberBetween(0, 4294967295),
69+
'on_sale' => fake()->boolean(),
70+
'features' => fake()->words(),
71+
'published_at' => fake()->dateTime()?->format('Y-m-d H:i'),
72+
'expire_at' => fake()->dateTime()?->format('Y-m-d H:i'),
73+
'released_on' => fake()->date(),
74+
'sale_time' => fake()->time(),
75+
'status' => fake()->randomElement(['draft', 'published']),
76+
'manufactured_year' => fake()->year(2100),
7777
];
7878
}
7979

8080
public function withCategory(): ProductFactory
8181
{
8282
return $this->state(function (array $attributes) {
8383
return [
84-
'category_id' => $this->faker->passThrough(random_id_or_generate(\App\Models\Category::class, 'id')),
84+
'category_id' => fake()->passThrough(random_id_or_generate(\App\Models\Category::class, 'id', generate: true)),
8585
];
8686
});
8787
}
8888
}
89+
8990
```
9091

9192
# Generating for specific columns
@@ -120,8 +121,8 @@ class ProductFactory extends Factory
120121
public function definition(): array
121122
{
122123
return [
123-
'name' => $this->faker->passThrough(ucfirst($this->faker->text(255))),
124-
'address' => $this->faker->address(),
124+
'name' => fake()->text(fake()->numberBetween(5, 255)),
125+
'address' => fake()->address(),
125126
];
126127
}
127128
}

docs/intro.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,28 @@ class ProductFactory extends Factory
5353
public function definition(): array
5454
{
5555
return [
56-
'name' => $this->faker->passThrough(ucfirst($this->faker->text(255))),
57-
'address' => $this->faker->address(),
58-
'slug' => $this->faker->unique()->slug(),
59-
'description' => $this->faker->optional()->sentences(3, true),
60-
'price' => $this->faker->randomFloat(2, 0, 999999999999),
61-
'stock' => $this->faker->numberBetween(0, 4294967295),
62-
'on_sale' => $this->faker->boolean(),
63-
'features' => $this->faker->passThrough($this->faker->words()),
64-
'published_at' => $this->faker->dateTime()?->format('Y-m-d H:i'),
65-
'expire_at' => $this->faker->dateTime()?->format('Y-m-d H:i'),
66-
'released_on' => $this->faker->date(),
67-
'sale_time' => $this->faker->time(),
68-
'status' => $this->faker->randomElement(['draft', 'published']),
69-
'manufactured_year' => $this->faker->year(2100),
56+
'name' => fake()->text(fake()->numberBetween(5, 255)),
57+
'address' => fake()->address(),
58+
'slug' => fake()->unique()->slug(),
59+
'description' => fake()->optional()->sentences(3, true),
60+
'price' => fake()->randomFloat(2, 0, 999999999999),
61+
'stock' => fake()->numberBetween(0, 4294967295),
62+
'on_sale' => fake()->boolean(),
63+
'features' => fake()->words(),
64+
'published_at' => fake()->dateTime()?->format('Y-m-d H:i'),
65+
'expire_at' => fake()->dateTime()?->format('Y-m-d H:i'),
66+
'released_on' => fake()->date(),
67+
'sale_time' => fake()->time(),
68+
'status' => fake()->randomElement(['draft', 'published']),
69+
'manufactured_year' => fake()->year(2100),
7070
];
7171
}
7272

7373
public function withCategory(): ProductFactory
7474
{
7575
return $this->state(function (array $attributes) {
7676
return [
77-
'category_id' => $this->faker->passThrough(random_id_or_generate(\App\Models\Category::class, 'id')),
77+
'category_id' => fake()->passThrough(random_id_or_generate(\App\Models\Category::class, 'id', generate: true)),
7878
];
7979
});
8080
}

src/FieldTypes/JsonField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function getFormInputName(): string
1212

1313
public function generateFactoryStatement(): string
1414
{
15-
return 'passThrough(fake()->words())';
15+
return 'words()';
1616
}
1717

1818
public function formatFactoryDbValue(string $value): string

src/FieldTypes/StringField.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ public function generateFactoryStatement(): string
99
$max = $this->getMax();
1010

1111
if ($max < 5) {
12-
return "passThrough(fake()->regexify('[a-z]{{$max}}'))";
12+
return "regexify('[a-z]{{$max}}')";
1313
}
1414

15-
return "passThrough(ucfirst(Str::limit(fake()->text($max), fake()->numberBetween(5, $max), '')))";
15+
return "text(fake()->numberBetween(5, $max))";
1616
}
1717

1818
public function generateValidationRules(): array

src/Generators/Concerns/GeneratesFactory.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,16 @@ public function getFakerStatement(string $column, bool $use_faker_method = true,
5454

5555
$statement = 'fake()';
5656

57+
if ($field->isNullable() && (! $required)) {
58+
$statement .= '->optional()';
59+
}
60+
5761
if ($field->isUnique()) {
5862
$statement .= '->unique()';
59-
}
6063

61-
if ($field->isNullable() && (! $required)) {
62-
$statement .= '->optional()';
64+
if ($field->isNullable() && (! $required)) {
65+
$statement .= '?';
66+
}
6367
}
6468

6569
$faker_method = null;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('countries', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('name');
19+
$table->string('code', 4)->unique()->nullable();
20+
$table->timestamps();
21+
});
22+
}
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('countries');
31+
}
32+
};

tests/Unit/Generators/FactoryGeneratorTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ public function it_can_determine_the_faker_statement_for_strings_shorter_than_5_
7373
{
7474
$factory_generator = new FactoryGenerator('orders');
7575

76-
$this->assertEquals('fake()->passThrough(fake()->regexify(\'[a-z]{4}\'))', $factory_generator->getFakerStatement('order_no'));
76+
$this->assertEquals('fake()->regexify(\'[a-z]{4}\')', $factory_generator->getFakerStatement('order_no'));
7777
}
7878

7979
/** @test */
8080
public function it_can_determine_the_faker_statement_for_strings(): void
8181
{
8282
$factory_generator = new FactoryGenerator('products');
8383

84-
$this->assertEquals('fake()->passThrough(ucfirst(Str::limit(fake()->text(255), fake()->numberBetween(5, 255), \'\')))', $factory_generator->getFakerStatement('name'));
84+
$this->assertEquals('fake()->text(fake()->numberBetween(5, 255))', $factory_generator->getFakerStatement('name'));
8585
}
8686

8787
/** @test */
@@ -154,7 +154,7 @@ public function it_can_determine_the_faker_statement_for_json_fields(): void
154154
{
155155
$factory_generator = new FactoryGenerator('products');
156156

157-
$this->assertEquals('fake()->passThrough(fake()->words())', $factory_generator->getFakerStatement('features'));
157+
$this->assertEquals('fake()->words()', $factory_generator->getFakerStatement('features'));
158158
}
159159

160160
/** @test */
@@ -216,4 +216,15 @@ public function it_can_generate_a_factory_with_unique_foreign_keys(): void
216216

217217
$this->assertEquals($expected_content, $actual_content);
218218
}
219+
220+
/** @test */
221+
public function it_can_generate_a_factory_with_optional_unique_values(): void
222+
{
223+
$factory_generator = new FactoryGenerator('countries');
224+
225+
$expected_content = $this->getTestStubContents('factories/CountryFactory.php');
226+
$actual_content = $factory_generator->render();
227+
228+
$this->assertEquals($expected_content, $actual_content);
229+
}
219230
}

tests/stubs/factories/CategoryFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Database\Factories;
44

55
use Illuminate\Database\Eloquent\Factories\Factory;
6-
use Illuminate\Support\Str;
76

87
/**
98
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Category>
@@ -18,7 +17,7 @@ class CategoryFactory extends Factory
1817
public function definition(): array
1918
{
2019
return [
21-
'name' => fake()->unique()->passThrough(ucfirst(Str::limit(fake()->text(255), fake()->numberBetween(5, 255), ''))),
20+
'name' => fake()->unique()->text(fake()->numberBetween(5, 255)),
2221
'slug' => fake()->unique()->slug(),
2322
];
2423
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
7+
/**
8+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Country>
9+
*/
10+
class CountryFactory extends Factory
11+
{
12+
/**
13+
* Define the model's default state.
14+
*
15+
* @return array<string, mixed>
16+
*/
17+
public function definition(): array
18+
{
19+
return [
20+
'name' => fake()->text(fake()->numberBetween(5, 255)),
21+
'code' => fake()->optional()->unique()?->regexify('[a-z]{4}'),
22+
];
23+
}
24+
25+
}

tests/stubs/factories/CustomerFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Database\Factories;
44

55
use Illuminate\Database\Eloquent\Factories\Factory;
6-
use Illuminate\Support\Str;
76

87
/**
98
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Customer>
@@ -22,7 +21,7 @@ public function definition(): array
2221
$definitions['password'] = 'Customer@123456';
2322

2423
$definitions += [
25-
'designation' => fake()->unique()->passThrough(ucfirst(Str::limit(fake()->text(255), fake()->numberBetween(5, 255), ''))),
24+
'designation' => fake()->unique()->text(fake()->numberBetween(5, 255)),
2625
'address' => fake()->address(),
2726
'on_sale' => fake()->boolean(),
2827
'expire_at' => fake()->dateTime()?->format('Y-m-d H:i'),

0 commit comments

Comments
 (0)