Skip to content

Commit b47bf08

Browse files
author
Ilya Sakovich
committed
adds ColumnShouldBeIgnored class, adds readme
1 parent 920cec0 commit b47bf08

File tree

3 files changed

+97
-8
lines changed

3 files changed

+97
-8
lines changed

README.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,63 @@
1-
# Laravel Populated Factory
1+
# Laravel Populated Factory
2+
3+
An easy way to generate populated factories for models according to types & names of their columns.
4+
5+
## Install
6+
7+
You can install this package via composer using this command:
8+
9+
```php
10+
composer require coderello/laravel-populated-factory
11+
```
12+
13+
The package will automatically register itself.
14+
15+
## Usage
16+
17+
```php
18+
php artisan make:populated-factory User
19+
```
20+
21+
> This command assumes that the `User` model is in the `App` namespace. If your models are situated in another namespace (e.g. `App\Models`) you should specify them either as `Models\\User` or `\\App\\Models\\User`.
22+
23+
Here is the populated factory generated for the `User` model according to its column types & names.
24+
25+
```php
26+
<?php
27+
28+
use Faker\Generator as Faker;
29+
30+
/** @var $factory \Illuminate\Database\Eloquent\Factory */
31+
32+
$factory->define(\App\User::class, function (Faker $faker) {
33+
return [
34+
'name' => $faker->name,
35+
'email' => $faker->unique()->safeEmail,
36+
'email_verified_at' => $faker->dateTime,
37+
'password' => '$2y$10$uTDnsRa0h7wLppc8/vB9C.YqsrAZwhjCgLWjcmpbndTmyo1k5tbRC',
38+
'remember_token' => $faker->sha1,
39+
'created_at' => $faker->dateTime,
40+
'updated_at' => $faker->dateTime,
41+
];
42+
});
43+
```
44+
45+
If you want a custom name for the factory, you need to pass it as the second argument like so:
46+
47+
```bash
48+
php artisan make:populated-factory User AdminFactory
49+
```
50+
51+
If you want to override the existent factory, you need to use `--force` flag like so:
52+
53+
```bash
54+
php artisan make:populated-factory User --force
55+
```
56+
57+
## Contributing
58+
59+
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
60+
61+
## License
62+
63+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

src/ColumnShouldBeIgnored.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Coderello\PopulatedFactory;
4+
5+
use Doctrine\DBAL\Schema\Column;
6+
7+
class ColumnShouldBeIgnored
8+
{
9+
public function __invoke(Column $column): bool
10+
{
11+
if ($column->getAutoincrement()) {
12+
return true;
13+
}
14+
15+
return false;
16+
}
17+
}

src/FactoryGenerator.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@ class FactoryGenerator
1414

1515
const NL = PHP_EOL;
1616

17+
protected $connection;
18+
1719
protected $guesser;
1820

19-
protected $connection;
21+
protected $columnShouldBeIgnored;
2022

2123
protected $appendFactoryPhpDoc = true;
2224

23-
public function __construct(FakeValueExpressionGuesser $guesser, Connection $connection)
25+
public function __construct(Connection $connection, FakeValueExpressionGuesser $guesser, ColumnShouldBeIgnored $columnShouldBeIgnored)
2426
{
27+
$this->connection = $connection;
28+
2529
$this->guesser = $guesser;
2630

27-
$this->connection = $connection;
31+
$this->columnShouldBeIgnored = $columnShouldBeIgnored;
2832
}
2933

3034
public function generate(Model $model): string
@@ -44,11 +48,17 @@ public function generate(Model $model): string
4448
self::NL, self::TAB, 'return [', self::NL
4549
])->pipe(function (Collection $collection) use ($columns) {
4650
foreach ($columns as $column) {
47-
if (! is_null($value = $this->guessValue($column))) {
48-
$collection = $collection->merge([
49-
self::TAB, self::TAB, '\'', $column->getName(), '\' => ', $value, ',', self::NL,
50-
]);
51+
if (($this->columnShouldBeIgnored)($column)) {
52+
continue;
53+
}
54+
55+
if (is_null($value = $this->guessValue($column))) {
56+
continue;
5157
}
58+
59+
$collection = $collection->merge([
60+
self::TAB, self::TAB, '\'', $column->getName(), '\' => ', $value, ',', self::NL,
61+
]);
5262
}
5363

5464
return $collection;

0 commit comments

Comments
 (0)