FvsJson's avatar

Pest test and homestead testing

Im still very newish to pest unit test and TTD as a whole and im not sure what I;m doing wrong.

I make use of hometead and I have about 1400 test and even if i run pest test in parralle its very very slow..

<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

use App\Traits\MorphMapper;
use App\Traits\Permission;
use Database\Seeders\PermissionSeeder;
use Database\Seeders\RoleSeeder;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Pest\Expectation;
use Tests\TestCase;

uses(
    Tests\TestCase::class,
    LazilyRefreshDatabase::class,
    MorphMapper::class,
    Permission::class
)->in('Feature', 'Unit');

uses()
    ->afterAll(function () {
        gc_collect_cycles();
        app()->flush();
    })
    ->beforeEach(function () {
        $this->seed(PermissionSeeder::class);
        $this->seed(RoleSeeder::class);
    })->in(
        'Feature/Articles',
        'Feature/Dashboard',
        'Feature/AdminUser',
        'Feature/Company',
        'Unit/Company',
        'Feature/UserProfile',
        'Feature/Logo',
        'Feature/Member',
        'Feature/Membership',
        'Feature/SettingsPage',
        'Feature/JobSeeker',
        'Feature/Group',
        'Feature/Partners',
        'Feature/StudyHelpInstitution',
        'Feature/LearningCourse',
        'Feature/IndustrialPsychologist',
        'Feature/Youth',
        'Feature/Comments',
        'Unit/DatePicker',
        'Unit/Actions',
        'Unit/CompanyAvatar',
        'Unit/UserProfilePublicComps',
    );

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeRedirectedFor', function (string $url, string $method = 'get') {
    if (! $this->value) {
        $response = test()->{$method}($url);
    } else {
        $response = actingAs($this->value)->{$method}($url);
    }

    return $response->assertStatus(302);
});

expect()->extend('isLessThanOrEqualTo', function (int $limit) {
    return $this->value->count() <= $limit;
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

/**
 * @param  Authenticatable  $user
 * @return TestCase
 */
function actingAs(Authenticatable $user): TestCase
{
    return test()->actingAs($user);
}

/**
 * @return Expectation
 */
function expectGuest(): Expectation
{
    return test()->expect(null);
}

Can one maybe have an idea whats going on..?

0 likes
2 replies
kevinbui's avatar

Are you having a lot of roles and permissions?

What about only creating necessary roles and permissions in each test case?

FvsJson's avatar

@kevinbui Sorry for the very very late reply, yes I have quite a few places that require roles and permissions.

Please or to participate in this conversation.