82 lines
1.8 KiB
PHP
82 lines
1.8 KiB
PHP
<?php
|
|
|
|
use App\Events\Permission\Created;
|
|
use App\Events\Permission\Deleted;
|
|
use App\Events\Permission\Updated;
|
|
use App\Models\Permission;
|
|
use App\Repositories\Permission\EloquentPermission;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Str;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
beforeEach(function () {
|
|
$this->repo = app(EloquentPermission::class);
|
|
});
|
|
|
|
test('all', function () {
|
|
$permissions = Permission::factory()->times(4)->create();
|
|
|
|
$this->assertEquals($permissions->toArray(), $this->repo->all()->toArray());
|
|
});
|
|
|
|
test('create permission', function () {
|
|
Event::fake([
|
|
Created::class,
|
|
]);
|
|
|
|
$data = getPermissionStubData();
|
|
|
|
$perm = $this->repo->create($data);
|
|
|
|
$this->assertDatabaseHas('permissions', $data + ['id' => $perm->id]);
|
|
|
|
Event::assertDispatched(Created::class);
|
|
});
|
|
|
|
test('update permission', function () {
|
|
Event::fake([
|
|
Updated::class,
|
|
]);
|
|
|
|
Cache::put('foo', 'bar');
|
|
|
|
$data = getPermissionStubData();
|
|
|
|
$perm = Permission::factory()->create();
|
|
|
|
$this->repo->update($perm->id, $data);
|
|
|
|
$this->assertDatabaseHas('permissions', $data + ['id' => $perm->id])
|
|
->assertNull(Cache::get('foo'));
|
|
|
|
Event::assertDispatched(Updated::class);
|
|
});
|
|
|
|
test('delete permission', function () {
|
|
Event::fake([
|
|
Deleted::class,
|
|
]);
|
|
|
|
Cache::put('foo', 'bar');
|
|
|
|
$perm = Permission::factory()->create();
|
|
|
|
$this->repo->delete($perm->id);
|
|
|
|
$this->assertDatabaseMissing('permissions', ['id' => $perm->id])
|
|
->assertNull(Cache::get('foo'));
|
|
|
|
Event::assertDispatched(Deleted::class);
|
|
});
|
|
|
|
function getPermissionStubData(): array
|
|
{
|
|
return [
|
|
'name' => Str::random(5),
|
|
'display_name' => Str::random(5),
|
|
'description' => 'foo',
|
|
'removable' => true,
|
|
];
|
|
}
|