Skip to content

Commit

Permalink
adds ColumnShouldBeIgnored class, adds readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Sakovich committed Mar 7, 2019
1 parent 920cec0 commit b47bf08
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 8 deletions.
64 changes: 63 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,63 @@
# Laravel Populated Factory
# Laravel Populated Factory

An easy way to generate populated factories for models according to types & names of their columns.

## Install

You can install this package via composer using this command:

```php
composer require coderello/laravel-populated-factory
```

The package will automatically register itself.

## Usage

```php
php artisan make:populated-factory User
```

> 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`.
Here is the populated factory generated for the `User` model according to its column types & names.

```php
<?php

use Faker\Generator as Faker;

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

$factory->define(\App\User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => $faker->dateTime,
'password' => '$2y$10$uTDnsRa0h7wLppc8/vB9C.YqsrAZwhjCgLWjcmpbndTmyo1k5tbRC',
'remember_token' => $faker->sha1,
'created_at' => $faker->dateTime,
'updated_at' => $faker->dateTime,
];
});
```

If you want a custom name for the factory, you need to pass it as the second argument like so:

```bash
php artisan make:populated-factory User AdminFactory
```

If you want to override the existent factory, you need to use `--force` flag like so:

```bash
php artisan make:populated-factory User --force
```

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
17 changes: 17 additions & 0 deletions src/ColumnShouldBeIgnored.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Coderello\PopulatedFactory;

use Doctrine\DBAL\Schema\Column;

class ColumnShouldBeIgnored
{
public function __invoke(Column $column): bool
{
if ($column->getAutoincrement()) {
return true;
}

return false;
}
}
24 changes: 17 additions & 7 deletions src/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ class FactoryGenerator

const NL = PHP_EOL;

protected $connection;

protected $guesser;

protected $connection;
protected $columnShouldBeIgnored;

protected $appendFactoryPhpDoc = true;

public function __construct(FakeValueExpressionGuesser $guesser, Connection $connection)
public function __construct(Connection $connection, FakeValueExpressionGuesser $guesser, ColumnShouldBeIgnored $columnShouldBeIgnored)
{
$this->connection = $connection;

$this->guesser = $guesser;

$this->connection = $connection;
$this->columnShouldBeIgnored = $columnShouldBeIgnored;
}

public function generate(Model $model): string
Expand All @@ -44,11 +48,17 @@ public function generate(Model $model): string
self::NL, self::TAB, 'return [', self::NL
])->pipe(function (Collection $collection) use ($columns) {
foreach ($columns as $column) {
if (! is_null($value = $this->guessValue($column))) {
$collection = $collection->merge([
self::TAB, self::TAB, '\'', $column->getName(), '\' => ', $value, ',', self::NL,
]);
if (($this->columnShouldBeIgnored)($column)) {
continue;
}

if (is_null($value = $this->guessValue($column))) {
continue;
}

$collection = $collection->merge([
self::TAB, self::TAB, '\'', $column->getName(), '\' => ', $value, ',', self::NL,
]);
}

return $collection;
Expand Down

0 comments on commit b47bf08

Please sign in to comment.