Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined variable: factory #870

Open
di1606 opened this issue Nov 5, 2019 · 12 comments

Comments

@di1606
Copy link

@di1606 di1606 commented Nov 5, 2019

When I run my tests with this command ./vendor/bin/phpunit --coverage-text --colors=never I have this error:
Undefined variable: factory.

I have checked that the registerFactories() method is called in my provider (MyModuleServiceProvider), it is well called.

I saw on the documentation that we need to add this piece of code in MyModuleServiceProvider

$this->app->singleton(Factory::class, function () {
            $faker = $this->app->make(\Faker\Generator::class);
            return Factory::construct($faker, __DIR__ . '/../Database/factories');
});

it doesn't work ( same after I ran the command composer dumpautoload )

Do you know how to fix the error ?

@nWidart

This comment has been minimized.

Copy link
Owner

@nWidart nWidart commented Nov 6, 2019

Where is this variable factory undefined?

@di1606

This comment has been minimized.

Copy link
Author

@di1606 di1606 commented Nov 6, 2019

It's in my file test

class InstitutionControllerTest extends TestCase
{
    /**
     * @testdox Get the data of institutions
     * @covers \Modules\Institution\Http\Controllers\InstitutionController::index
     * */
    public function testIndex():void
    {
        $institutionCollection = new Collection(factory(Institution::class)->make()); // it's here factory 
        $mock = Mockery::mock(InstitutionRepository::class);
        $mock->shouldReceive('all')->andReturn($institutionCollection);
        $this->app->instance(InstitutionRepository::class, $mock);
        $response = $this->json('GET', 'api/institutions');
        $this->assertEquals(Response::HTTP_OK, $response->status());
        $this->assertEquals($institutionCollection, $response->getContent());
    }
}

the error appears when I execute the command ./vendor/bin/phpunit --coverage-text --colors=never and not when i execute the command ./vendor/bin/phpunit
Capture d’écran 2019-11-06 à 09 05 10

@nWidart

This comment has been minimized.

Copy link
Owner

@nWidart nWidart commented Nov 6, 2019

I just tried to reproduce this, creating a factory:

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */
use Faker\Generator as Faker;
use Modules\Blog\Entities\Post;

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

And in a migration creating it:

factory(Post::class, 3)->create();

And it ran fine.

@di1606

This comment has been minimized.

Copy link
Author

@di1606 di1606 commented Nov 6, 2019

I use the make() function, but it's not the problem but the factory function which is not recognized when I run the tests with coverage.

@nWidart

This comment has been minimized.

Copy link
Owner

@nWidart nWidart commented Nov 6, 2019

Hm not sure why it's not loaded for you.
Did you check in your service provider if the factory is properly loaded for tests?

@di1606

This comment has been minimized.

Copy link
Author

@di1606 di1606 commented Nov 6, 2019

Here is my provide InstitutionServiceProvider

class InstitutionServiceProvider extends ServiceProvider
{

    /**
     * Boot the application events.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerTranslations();
        $this->registerConfig();
        $this->registerViews();
        $this->registerFactories();
        $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->register(RouteServiceProvider::class);
        /*$this->app->singleton(Factory::class, function () {
            $faker = $this->app->make(\Faker\Generator::class);
            return Factory::construct($faker, __DIR__ . '/../Database/factories');
        });*/ // I put that in commentary. If I remove the comments it doesn't work.
    }
...
}
@nWidart

This comment has been minimized.

Copy link
Owner

@nWidart nWidart commented Nov 6, 2019

What you commented is what loads the factories so you need that.

You could have something like this:

    public function registerFactories()
    {
        if (! app()->environment('production') && $this->app->runningInConsole()) {
            app(Factory::class)->load(__DIR__ . '/../Database/factories');
        }
    }
@di1606

This comment has been minimized.

Copy link
Author

@di1606 di1606 commented Nov 6, 2019

the registerFactories() method is already called in the boot() method. I removed the comments but it doesn't work.

class InstitutionServiceProvider extends ServiceProvider
{
    public function boot()
    {
       ...
        $this->registerFactories();
       ...
    }
    public function register()
    {
        $this->app->register(RouteServiceProvider::class);
        $this->app->singleton(Factory::class, function () {
            $faker = $this->app->make(\Faker\Generator::class);
            return Factory::construct($faker, __DIR__ . '/../Database/factories');
        });
    }
...
    public function registerFactories()
    {
        if (! app()->environment('production') && $this->app->runningInConsole()) {
            app(Factory::class)->load(__DIR__ . '/../Database/factories');
        }
    }
....
}

Now, I just updated the packages. When I execute the commande phpunit. This error appear

There was 1 error:

1) Modules\Institution\Tests\Feature\Controllers\InstitutionControllerTest::testIndex
InvalidArgumentException: Unable to locate factory with name [default] [Modules\Institution\Entities\Institution].
@di1606

This comment has been minimized.

Copy link
Author

@di1606 di1606 commented Nov 9, 2019

I fix the error with phpunit, the solution is enable the module. But, I don't find the solution for the error with the command ./vendor/bin/phpunit --coverage-text --colors=never

message error:
Undefined variable: factory

@di1606

This comment has been minimized.

Copy link
Author

@di1606 di1606 commented Nov 10, 2019

I found where the error comes from, it comes when I create a factory class. But I don't know solve this

factory class

<?php

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

use Faker\Generator as Faker;
use Modules\Institution\Entities\Institution;

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

@nWidart

This comment has been minimized.

Copy link
Owner

@nWidart nWidart commented Nov 12, 2019

Is your module loaded in a test environment?

@di1606

This comment has been minimized.

Copy link
Author

@di1606 di1606 commented Nov 12, 2019

that is to say ? setting up the .env.testing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.