exSnake's avatar

With Refreshdatabase test fails

This is my test:

    use RefreshDatabase;
    /** @test */
    public function an_operator_can_view_changes(){
        $this->withoutExceptionHandling();

        $user = factory('App\User')->create();
        $role = Role::firstOrCreate(['name' => 'Operator']);
        $permission = Permission::firstOrCreate(['name' => 'view_changes']);
        $role->givePermissionTo($permission);
        $user->assignRole($role);
        $this->actingAs($user);
        $this->assertTrue($user->can('view_changes'));
    }

if i launch it with Refreshdatabase it will fail from the second execution till i remove use Refreshdatabase

What's happening? Can't debug because if i add Refreshdatabase DB will be erased, as you see i already tried to get a fresh version of the user with no result. I'm using firstOrCreate because default records will be populated with migration so view_changes and operator already exists in DB.

Test with Refreshdatabase:

root@918e53969b70:/var/www# phpunit --filter an_operator_can_view_cha
PHPUnit 7.5.15 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 5.28 seconds, Memory: 26.00 MB

There was 1 failure:

1) Tests\Feature\ChangesTest::an_operator_can_view_changes
Failed asserting that false is true.

/var/www/tests/Feature/ChangesTest.php:26

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

without first fail, second success:

root@918e53969b70:/var/www# phpunit --filter an_operator_can_view_cha
PHPUnit 7.5.15 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 707 ms, Memory: 20.00 MB

There was 1 failure:

1) Tests\Feature\ChangesTest::an_operator_can_view_changes
Failed asserting that false is true.

/var/www/tests/Feature/ChangesTest.php:23

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
root@918e53969b70:/var/www# phpunit --filter an_operator_can_view_cha
PHPUnit 7.5.15 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 660 ms, Memory: 20.00 MB

OK (1 test, 1 assertion)

0 likes
5 replies
bugsysha's avatar

You do not need $this->actingAs($user);. How do the implementations behind givePermissionTo and assignRole look like? Are you sure it is persisted?

When you are using RefreshDatabase you can always debug things like the following:

$this->assertDatabaseHas('permissions', ['name' => 'view_changes']);
$this->assertDatabaseHas('roles', ['name' => 'Operator']);
$this->assertDatabaseHas('role_user', ['role_id' => $role->id, 'user_id' => $user->id]);

If those assertions pass, then your can is probably not hooked up as it is supposed to be. If not you will see where is the issue.

Do not forget to remove $this->withoutExceptionHandling();.

exSnake's avatar

Seem to fail on the can, how can't be hooked up? Every other test passes, this is my AuthServiceProvider module

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot(GateContract $gate)
    {
        parent::registerPolicies($gate);

        if (Schema::hasTable('permissions')) {
            // Dynamically register permissions with Laravel's Gate.
            foreach ($this->getPermissions() as $permission) {
                $gate->define($permission->name, function ($user) use ($permission) {
                    return $user->hasPermission($permission) || $user->isAdmin();
                });
            }
        }
    }

    /**
     * Fetch the collection of site permissions.
     *
     * @return \Illuminate\Database\Eloquent\Collection
     */
    protected function getPermissions()
    {
        return Permission::with('roles')->get();
    }
}

i register it after AppServiceProviders

        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\TelescopeServiceProvider::class,
        App\Providers\RouteServiceProvider::class,

    ],
bugsysha's avatar

Are you using any packages for roles and permissions or this is your custom solution?

exSnake's avatar
exSnake
OP
Best Answer
Level 8

Switched to spatie/permissions

Please or to participate in this conversation.