Code Monkey home page Code Monkey logo

laravel-factory-enhanced's People

Contributors

andresayej avatar deligoez avatar rasmuscnielsen avatar samuelnitsche 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  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  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  avatar  avatar

laravel-factory-enhanced's Issues

Will this package also work with non-persistent data (i.e. make() vs. create())?

Simple question: Can this package also create non-persistent models with their relations?

With the normal faker, it currently is a bit of a pain to create such structures in a non-persistent way, because you cannot work with relations properly:

$users = factory(App\User::class, 3)
    ->create()
    ->each(function ($user) {
        $user->setRelation('posts', factory(App\Post::class, 3)->make());
        $user->posts->each(function ($post) use ($user) {
            $post->setRelation('user', $user);
        });
    });

Note: $user->setRelation('posts', $posts) because setting it directly via $user->posts = $posts will set it as attribute and not as relation.

The reason I ask is that for a lot of my code I don't need persisted models (which is a good thing in regards to testing the code, because it is much faster without a database). But above isn't quite practical in regards of creating test data. Especially when the scenario is not as simple as this one-to-many relationship.

Second parameter ($attributes) missing

Hi.
In FactoryBuilder:376 -> mergeAndExpandAttributes in call_user_func there should be a second parameter with the $attributes. I had my defineAs accepting the attributes as the second parameter in the callback and after i installed this package it blew up.
I will come back with more details.

Laravel version: 5.7
Package version: v1.1

Creating additional record on one-to-one relations using `with()` and `factory closure` together

Case:

Lets say we have Post and Category one to one relation. Factories will look like

$factory->define(Post::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'category_id' => function(){
        	return factory(Category::class);
        },
        'text' => $faker->paragraph 
    ];
});

$factory->define(Category::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
    ];
});

In case if I generate Post like this

$post = factory(Post::class)
  ->with('category', ['name' => 'programming'])
  ->create()

I will get correct Post with related programming category. But as side effect I also get another dummy category generated by post factory. So I end up with two categories instead of one (it can be unexpected in tests). The only way to prevent this is to manually set 'category_id' to null but this is not elegant and requires to remember about all relations and sub-relations

$post = factory(Post::class)
  ->with('category', ['name' => 'programming'])
  ->create(['category_id' => null])

I really like the power that with provides, but I still want to have default relation generation using factory closures.

Can you help with the way to use with() alongside with factory closures without side effects?

Use with fakerphp/faker

Is it also somehow possible to use this library with fakerphp/faker? Currently, fakerphp/faker conflicts with fzaninotto/faker and thus I cannot install this library.

P.S.: even with a fork of this repository it seems it doesn't run well with Laravel 8 ๐Ÿ˜ž

Generate random number of relationships

Hi! Thanks for this great factory package. I have a simple question.

How can we generate random number of related models_?

For example:

factory(Show::class)
    ->with(random_int(2, 10) ,'trackers')
    ->times(random_int(10, 20))
    ->create();

This code will create exactly same number of Tracker's for every Show.
How can I generate random number of Tracker's for every Show

Using relationships in state definitions

This is a suggestion.

How about to apply same philosophy into factory definitions?

Sometimes a factory definition requires a lot of nested function to define a default initial state.

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.