/ tests / Feature / Livewire / Users / RemoveTest.php
RemoveTest.php
 1  <?php
 2  
 3  namespace Tests\Feature\Livewire\Users;
 4  
 5  use App\Http\Livewire\Users\Remove;
 6  use App\Models\User;
 7  use Livewire\Livewire;
 8  use Tests\TestCase;
 9  
10  class RemoveTest extends TestCase
11  {
12      private User $admin;
13  
14      private User $worker;
15  
16      private User $user;
17  
18      public function setUp(): void
19      {
20          parent::setUp();
21          $this->admin = $this->getAdminUser();
22          $this->worker = $this->getWorkerUser();
23      }
24  
25      /** @test */
26      public function can_delete_a_user()
27      {
28          //GIVEN: A user with the role root and a user created
29          $this->actingAs($this->admin);
30          $this->user = User::factory()->create();
31  
32          //WHEN: The user remove another user 
33          $component = Livewire::test(Remove::class, [
34              'entityId' => $this->user->id,
35          ])
36              ->call('remove');
37  
38          //THEN: Check if the record has been deleted and a notification is displayed
39          $this->assertSoftDeleted($this->user);
40  
41          $component->assertDispatchedBrowserEvent('wireui:notification', [
42              'options'     => [
43                  'icon' => 'success',
44                  'title' => __('users.notifications.remove.success.title'),
45                  'description' => __('users.notifications.remove.success.message'),
46              ],
47              'componentId' => $component->id,
48          ]);
49      }
50  
51      /** @test */
52      public function user_without_permission_cannot_remove_a_user()
53      {
54          //GIVEN: A user without the necessary permission
55          $this->actingAs($this->worker);
56  
57          //WHEN: The user call the component
58          $component = Livewire::test(Remove::class);
59  
60          //THEN: The response return is 403
61          $component->assertForbidden();
62      }
63  }