|
2 | 2 |
|
3 | 3 | declare(strict_types=1);
|
4 | 4 |
|
5 |
| -test('example test', function (): void { |
| 5 | +use App\Models\Role; |
| 6 | +use Illuminate\Http\Response; |
| 7 | +use Illuminate\Testing\Fluent\AssertableJson; |
6 | 8 |
|
| 9 | +beforeEach(function (): void { |
| 10 | + [$this->user, $this->company, $this->token] = frontendApiLoginWithUser('Super Admin'); |
| 11 | +}); |
| 12 | + |
| 13 | +test('it can fetch roles', function (): void { |
| 14 | + $response = $this->withToken($this->token)->getJson(route('api.roles.fetch')); |
| 15 | + |
| 16 | + $response->assertOk() |
| 17 | + ->assertJson( |
| 18 | + fn (AssertableJson $json): AssertableJson => $json |
| 19 | + ->has( |
| 20 | + 'data', |
| 21 | + fn (AssertableJson $json): AssertableJson => $json |
| 22 | + ->has( |
| 23 | + '0', |
| 24 | + fn (AssertableJson $json): AssertableJson => $json |
| 25 | + ->where('id', ($role = Role::latest()->first())->id) |
| 26 | + ->where('name', $role->name) |
| 27 | + ->etc() |
| 28 | + ) |
| 29 | + ->etc() |
| 30 | + ) |
| 31 | + ); |
| 32 | +}); |
| 33 | + |
| 34 | +test('it can create role', function (): void { |
| 35 | + $response = $this->withToken($this->token)->postJson(route('api.roles.create'), [ |
| 36 | + 'name' => $name = 'Access Manager', |
| 37 | + ]); |
| 38 | + |
| 39 | + $response->assertOk()->assertJsonStructure(['success', 'role_id']); |
| 40 | + |
| 41 | + $this->assertDatabaseHas(Role::class, [ |
| 42 | + 'name' => $name, |
| 43 | + ]); |
| 44 | +}); |
| 45 | + |
| 46 | +test('it can delete the role', function (): void { |
| 47 | + $role = Role::factory()->named('Access Manager')->create(); |
| 48 | + |
| 49 | + $response = $this->withToken($this->token)->deleteJson(route('api.roles.delete', [ |
| 50 | + 'id' => $role->id, |
| 51 | + ])); |
| 52 | + |
| 53 | + $response->assertOk()->assertJsonStructure(['success']); |
| 54 | + |
| 55 | + $this->assertModelMissing($role); |
| 56 | +}); |
| 57 | + |
| 58 | +test('it cannot delete the role if it assign to the user', function (): void { |
| 59 | + $response = $this->withToken($this->token)->deleteJson(route('api.roles.delete', [ |
| 60 | + 'id' => Role::min('id'), |
| 61 | + ])); |
| 62 | + |
| 63 | + $response->assertStatus(Response::HTTP_NOT_ACCEPTABLE); |
| 64 | +}); |
| 65 | + |
| 66 | +test('it can update the role', function (): void { |
| 67 | + $response = $this->withToken($this->token)->postJson(route('api.roles.update', [ |
| 68 | + 'id' => Role::min('id'), |
| 69 | + ]), [ |
| 70 | + 'name' => $name = 'Access Manager', |
| 71 | + ]); |
| 72 | + |
| 73 | + $response->assertOk()->assertJsonStructure(['success']); |
| 74 | + |
| 75 | + $this->assertDatabaseHas(Role::class, [ |
| 76 | + 'name' => $name, |
| 77 | + ]); |
7 | 78 | });
|
0 commit comments