Skip to content

Commit 300cc3e

Browse files
author
Nathan Esayeas
authored
feat: add support for rememberToken() shorthand (#229)
1 parent cb93d7c commit 300cc3e

File tree

10 files changed

+125
-6
lines changed

10 files changed

+125
-6
lines changed

src/Generators/FactoryGenerator.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class FactoryGenerator implements Generator
1414
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
1515
private $files;
1616

17+
private $imports = [];
18+
1719
public function __construct($files)
1820
{
1921
$this->files = $files;
@@ -27,6 +29,9 @@ public function output(array $tree): array
2729

2830
/** @var \Blueprint\Models\Model $model */
2931
foreach ($tree['models'] as $model) {
32+
$this->addImport($model, 'Faker\Generator as Faker');
33+
$this->addImport($model, $model->fullyQualifiedClassName());
34+
3035
$path = $this->getPath($model);
3136

3237
if (!$this->files->exists(dirname($path))) {
@@ -53,9 +58,9 @@ protected function getPath(Model $model)
5358

5459
protected function populateStub(string $stub, Model $model)
5560
{
56-
$stub = str_replace('DummyModel', $model->fullyQualifiedClassName(), $stub);
5761
$stub = str_replace('DummyClass', $model->name(), $stub);
5862
$stub = str_replace('// definition...', $this->buildDefinition($model), $stub);
63+
$stub = str_replace('// imports...', $this->buildImports($model), $stub);
5964

6065
return $stub;
6166
}
@@ -144,6 +149,11 @@ protected function buildDefinition(Model $model)
144149
}
145150
$definition .= sprintf('%s%s => $faker->%s,%s', self::INDENT, "'{$column->name()}_id'", self::fakerDataType('id'), PHP_EOL);
146151
$definition .= sprintf('%s%s => $faker->%s,%s', self::INDENT, "'{$column->name()}_type'", self::fakerDataType('string'), PHP_EOL);
152+
} elseif ($column->dataType() === 'rememberToken') {
153+
$this->addImport($model, 'Illuminate\Support\Str');
154+
$definition .= self::INDENT . "'{$column->name()}' => ";
155+
$definition .= 'Str::random(10)';
156+
$definition .= ',' . PHP_EOL;
147157
} else {
148158
$definition .= self::INDENT . "'{$column->name()}' => ";
149159
$faker = self::fakerData($column->name()) ?? self::fakerDataType($column->dataType());
@@ -155,6 +165,21 @@ protected function buildDefinition(Model $model)
155165
return trim($definition);
156166
}
157167

168+
private function addImport(Model $model, $class)
169+
{
170+
$this->imports[$model->name()][] = $class;
171+
}
172+
173+
private function buildImports(Model $model)
174+
{
175+
$imports = array_unique($this->imports[$model->name()]);
176+
sort($imports);
177+
178+
return implode(PHP_EOL, array_map(function ($class) {
179+
return 'use ' . $class . ';';
180+
}, $imports));
181+
}
182+
158183
private function fillableColumns(array $columns): array
159184
{
160185
if (config('blueprint.fake_nullables')) {

src/Generators/MigrationGenerator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ protected function buildDefinition(Model $model)
111111
$column_definition = self::INDENT;
112112
if ($dataType === 'bigIncrements' && $this->isLaravel7orNewer()) {
113113
$column_definition .= '$table->id(';
114+
} elseif ($dataType === 'rememberToken') {
115+
$column_definition .= '$table->rememberToken(';
114116
} else {
115117
$column_definition .= '$table->' . $dataType . "('{$column->name()}'";
116118
}

src/Generators/ModelGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ private function fillableColumns(array $columns)
204204
'deleted_at',
205205
'created_at',
206206
'updated_at',
207+
'remember_token',
207208
]);
208209
}
209210

stubs/factory.stub

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
/** @var \Illuminate\Database\Eloquent\Factory $factory */
44

5-
use DummyModel;
6-
use Faker\Generator as Faker;
5+
// imports...
76

87
$factory->define(DummyClass::class, function (Faker $faker) {
98
return [

tests/Feature/Generator/FactoryGeneratorTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function output_writes_nothing_for_empty_tree()
4848
* @test
4949
* @dataProvider modelTreeDataProvider
5050
*/
51-
public function output_writes_migration_for_model_tree($definition, $path, $migration)
51+
public function output_writes_factory_for_model_tree($definition, $path, $factory)
5252
{
5353
$this->files->expects('stub')
5454
->with('factory.stub')
@@ -59,7 +59,7 @@ public function output_writes_migration_for_model_tree($definition, $path, $migr
5959
->andReturnTrue();
6060

6161
$this->files->expects('put')
62-
->with($path, $this->fixture($migration));
62+
->with($path, $this->fixture($factory));
6363

6464
$tokens = $this->blueprint->parse($this->fixture($definition));
6565
$tree = $this->blueprint->analyze($tokens);
@@ -151,6 +151,7 @@ public function modelTreeDataProvider()
151151
['definitions/model-key-constraints.bp', 'database/factories/OrderFactory.php', 'factories/model-key-constraints.php'],
152152
['definitions/unconventional-foreign-key.bp', 'database/factories/StateFactory.php', 'factories/unconventional-foreign-key.php'],
153153
['definitions/foreign-key-shorthand.bp', 'database/factories/CommentFactory.php', 'factories/foreign-key-shorthand.php'],
154+
['definitions/resource-statements.bp', 'database/factories/UserFactory.php', 'factories/resource-statements.php'],
154155
];
155156
}
156157
}

tests/Feature/Generator/MigrationGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ public function modelTreeDataProvider()
523523
['definitions/disable-auto-columns.bp', 'database/migrations/timestamp_create_states_table.php', 'migrations/disable-auto-columns.php'],
524524
['definitions/uuid-shorthand.bp', 'database/migrations/timestamp_create_people_table.php', 'migrations/uuid-shorthand.php'],
525525
['definitions/unconventional-foreign-key.bp', 'database/migrations/timestamp_create_states_table.php', 'migrations/unconventional-foreign-key.php'],
526+
['definitions/resource-statements.bp', 'database/migrations/timestamp_create_users_table.php', 'migrations/resource-statements.php'],
526527
];
527528
}
528529
}

tests/Feature/Generator/ModelGeneratorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function output_generates_models($definition, $path, $model)
5555
->with('model/fillable.stub')
5656
->andReturn(file_get_contents('stubs/model/fillable.stub'));
5757

58-
if ($definition === 'definitions/nested-components.bp') {
58+
if (in_array($definition, ['definitions/nested-components.bp','definitions/resource-statements.bp'])) {
5959
$this->files->expects('stub')
6060
->with('model/hidden.stub')
6161
->andReturn(file_get_contents('stubs/model/hidden.stub'));
@@ -415,6 +415,7 @@ public function modelTreeDataProvider()
415415
['definitions/relationships.bp', 'app/Comment.php', 'models/relationships.php'],
416416
['definitions/unconventional.bp', 'app/Team.php', 'models/unconventional.php'],
417417
['definitions/nested-components.bp', 'app/Admin/User.php', 'models/nested-components.php'],
418+
['definitions/resource-statements.bp', 'app/User.php', 'models/resource-statements.php'],
418419
];
419420
}
420421

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/** @var \Illuminate\Database\Eloquent\Factory $factory */
4+
5+
use App\User;
6+
use Faker\Generator as Faker;
7+
use Illuminate\Support\Str;
8+
9+
$factory->define(User::class, function (Faker $faker) {
10+
return [
11+
'name' => $faker->name,
12+
'email' => $faker->safeEmail,
13+
'password' => $faker->password,
14+
'remember_token' => Str::random(10),
15+
];
16+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateUsersTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('users', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('name');
19+
$table->string('email');
20+
$table->string('password');
21+
$table->rememberToken();
22+
$table->timestamps();
23+
});
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*
29+
* @return void
30+
*/
31+
public function down()
32+
{
33+
Schema::dropIfExists('users');
34+
}
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class User extends Model
8+
{
9+
/**
10+
* The attributes that are mass assignable.
11+
*
12+
* @var array
13+
*/
14+
protected $fillable = [
15+
'name',
16+
'email',
17+
'password',
18+
];
19+
20+
/**
21+
* The attributes that should be hidden for serialization.
22+
*
23+
* @var array
24+
*/
25+
protected $hidden = [
26+
'password',
27+
'remember_token',
28+
];
29+
30+
/**
31+
* The attributes that should be cast to native types.
32+
*
33+
* @var array
34+
*/
35+
protected $casts = [
36+
'id' => 'integer',
37+
];
38+
}

0 commit comments

Comments
 (0)