jay_gorio
3 years ago

Why laravel 5.3 phpunit so slow when using faker

Posted 3 years ago by jay_gorio

I am using sqlite for my test and using phpunit. I configure created another .env.testing and defined connection as sqlite. I also configure phpunit.xml and using DB_CONNECTION as sqlite. Also I used faker for my test as shown below. However it took so long (more than 3 minutes) for the test to be completed. I created also a new laravel 5.3 project but with the same testing result. When using laravel 5.2, my tests are extremely fast. However in 5.3 it seems there is something wrong, especially using faker. Did anyone experience this in laravel 5.3? Or am I missing something?

https://s17.postimg.org/cwjnz6567/result_of_test.png

use Faker\Factory as Faker;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class SeriesTest extends TestCase
{
    use DatabaseTransactions;

    protected $fake;

    public function setUp()
    {
        parent::setUp();

        $this->fake = Faker::create();
    }

        /** @test */
     public function it_display_all_series()
     {
             $series = factory(App\Series::class, 2)->create();

            $this->get('/api/v1/series');

            foreach($series as $series){
            $this->seeJson([        
                'title' => $series->title,
                'description' => $series->description,              
                'image_cover' => $series->image_cover,
                'slug' => $series->slug
            ]);
          }

        $this->assertResponseOk();
    }

Here is my model factory setup

$factory->define(App\User::class, function (Faker\Generator $faker) {
        static $password;

        return [
            'name' => $faker->name,
            'email' => $faker->unique()->safeEmail,
            'password' => $password ?: $password = bcrypt('secret'),
            'remember_token' => str_random(10),
        ];
    });

    $factory->define(App\Series::class, function (Faker\Generator $faker) {
        
        $title = $faker->sentence;

        return [
            'user_id' => factory(App\User::class)->create()->id,
            'title' => $title,
            'description' => $faker->paragraph,
            'image_cover' => $faker->image($dir = '/tmp', $width = 640, $height = 480),
            'slug' => str_slug($title)
        ];
    }); 

Please sign in or create an account to participate in this conversation.