Code Monkey home page Code Monkey logo

factory-generator's Introduction

Laravel Model Factory Generator

This package generates model factories from existing models using the new class-based factories in Laravel 8.

Installation

You may install this package via composer by running:

composer require --dev laravel-shift/factory-generator

The package will automatically register itself.

Usage

This package adds an artisan command for generating model factories.

Without any arguments, this command will generate model factories for all existing models within your Laravel application:

php artisan generate:factory

Similar to Laravel, this will search for models within the app/Models folder, or if that folder does not exist, within the app folder.

To generate factories for models within a different folder, you may pass the --path option (or -p).

php artisan generate:factory --path=some/Other/Path

To generate a factory for a single model, you may pass the model name:

php artisan generate:factory User

By default nullable columns are not included in the factory definition. If you want to include nullable columns you may set the --include-nullable option (or -i).

php artisan generate:factory -i User

Attribution

This package was original forked from Naoray/laravel-factory-prefill by Krishan König.

It has diverged to support the latest version of Laravel and to power part of the automation by the Tests Generator.

Contributing

Contributions should be submitted to the master branch. Any submissions should be complete with tests and adhere to the PSR-2 code style. You may also contribute by opening an issue.

factory-generator's People

Contributors

chriischambers avatar daryledesilva avatar jasonmccreary avatar klaasnicolaas avatar mortenscheel avatar naoray avatar nexxai avatar nuernbergera avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

factory-generator's Issues

Support generation of nested factories

Right now this package only looks within the specific folder. It does not traverse into deeper directories. This issue aims to address that by searching within the subfolders and generating factories which mirror the structure.

For example, an app/Models/Blog/Comment.php model should be found and generate an database/factories/Blog/CommentFactory.php factory.

Factories in subfolders get the wrong namespace

Possibly related to #1

The directory structure of the factories does mirror the models, so if the model is in app/Feature/Models, the factory is created in database/factories/Features/Models. Which is great.

But the factory namespace is

namespace Database\Factories;

And when calling App\Feature\Models\Foo::factory(), laravel resolves the factory to Database\Factories\Feature\Models\FooFactory, which doesn't exist.

Only one of these can be installed: illuminate/support, laravel/framework replaces illuminate/support and thus cannot coexist with it.

I am following this Laravel 11.x Compatibility #29 issue/pr and attempting to run this I get the following error.

Only one of these can be installed: illuminate/support[v5.0.0, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev, v7.0.0, ..., 7.x-dev, 
  v8.0.0, ..., 8.x-dev, v9.0.0-beta.1, ..., 9.x-dev, v10.0.0, ..., 10.x-dev, v11.0.0, ..., 11.x-dev], 
laravel/framework[v11.0.0, ..., 11.x-dev].
laravel/framework replaces illuminate/support and thus cannot coexist with it.

Any ideas?

Incorrect mapping of faker data

Some of the commonly named columns did not map to the correct types. For example, email got mapped to word

In addition, it'd be nice if anything _id was mapped to an integer (or null) if it were not determined to be a model.

Example:

fix-types

BadMethodCallException when using laravel-promocodes package

I was running php artisan generate:factory and it returned this exception:

   BadMethodCallException 

  Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::getForeignKey()

  at vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:50
     46▕      * @throws \BadMethodCallException
     47▕      */
     48protected static function throwBadMethodCallException($method)
     49▕     {
  ➜  50throw new BadMethodCallException(sprintf(
     51'Call to undefined method %s::%s()', static::class, $method
     52▕         ));
     53▕     }
     54▕ }

  • Bad Method Call: Did you mean Illuminate\Database\Eloquent\Relations\BelongsToMany::getForeignPivotKeyName() ? 

      +23 vendor frames 
  24  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

After some debugging I found out that this happens in my user model. Further investigations revealed that the trait Rewardable was the problem. This trait comes from this package: https://github.com/anhofmann/laravel-promocodes I forked this package, but I think it will also happen in the original package: https://github.com/zgabievi/laravel-promocodes
I know that laravel-promocodes is pretty old, but it's working well in my laravel 8 app. Do you see a way to fix this? At this point I don't know if this is a bug in factory-generator or laravel-promocodes.

There is no column with name 'id' on table

When I try to run php artisan generate:factory Example I get a fatal error:

Doctrine\DBAL\Schema\SchemaException 
There is no column with name 'id' on table 'examples'.

I have run migrate:fresh and I'm reasonably confident the id column exists. This error happens for every model I try it on, meaning the factory generator is unusable for me until I can work out the solution.

Nothing turned up in existing issues or Google, do you have any idea why this could happening? Please let me know if there's any more information you need from me.

Fails on PostGIS enabled databases because geography isnt a known type of DBAL

Any chance you can add geography and geometry as valid types? There is a good discussion on this here https://laracasts.com/discuss/channels/general-discussion/doctrinedbal-how-to-add-data-types-when-using-xethronmigrations-generator

Doctrine\DBAL\Exception

Unknown database type geography requested, Doctrine\DBAL\Platforms\PostgreSQL100Platform may not support it.

at vendor/doctrine/dbal/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php:454
450▕
451▕ $dbType = strtolower($dbType);
452▕
453▕ if (! isset($this->doctrineTypeMapping[$dbType])) {
➜ 454▕ throw new Exception(
455▕ 'Unknown database type ' . $dbType . ' requested, ' . static::class . ' may not support it.'
456▕ );
457▕ }
458▕

  +28 vendor frames

29 artisan:35
Illuminate\Foundation\Console\Kernel::handle()

laravel on  Eight [?] is 📦 v1.9.4 via ⬢ v15.8.0 via 🐘 v8.0.3 on 🐳 v20.10.5 took 4s

[Bug] Hash Facade is not imported

When generating a factory for a Model with a password column it will generate the following column in the factory file:

'password' => Hash::make('password'),

We need to add the import statement at the top or let the developer know that he hast to add it.

use Illuminate\Support\Facades\Hash;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.