Skip to content

Commit abeca86

Browse files
Create RoleManagementTest.php file
1 parent 2e0a251 commit abeca86

File tree

3 files changed

+159
-6
lines changed

3 files changed

+159
-6
lines changed

database/factories/RoleFactory.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
7+
/**
8+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Role>
9+
*/
10+
class RoleFactory extends Factory
11+
{
12+
/**
13+
* Define the model's default state.
14+
*
15+
* @return array<string, mixed>
16+
*/
17+
public function definition(): array
18+
{
19+
return [
20+
'name' => $this->faker->name(),
21+
];
22+
}
23+
}

tests/Feature/RoleManagementTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use App\Models\Role;
6+
use App\Models\User;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Tests\TestCase;
9+
10+
class RoleManagementTest extends TestCase
11+
{
12+
use RefreshDatabase;
13+
14+
public function test_new_role_model_validation()
15+
{
16+
$this->actingAs(User::factory()->create());
17+
18+
$response = $this->post('/roles', []);
19+
20+
$response->assertSessionHasErrors(['name']);
21+
}
22+
23+
public function test_role_model_update_feature()
24+
{
25+
$this->actingAs(User::factory()->create());
26+
27+
$role = Role::factory()->create();
28+
29+
$response = $this->put('/roles/' . $role->id, [
30+
'name' => 'Updated role name']);
31+
32+
$this->assertDatabaseHas('roles', [
33+
'id' => $role->id,
34+
'name' => 'Updated role name',
35+
]);
36+
37+
$response->assertStatus(302);
38+
}
39+
40+
public function test_role_model_delete_feature()
41+
{
42+
$this->actingAs(User::factory()->create());
43+
44+
$role = Role::factory()->create();
45+
46+
$response = $this->delete('/roles/' . $role->id);
47+
48+
$response->assertRedirect('/')->assertStatus(302);
49+
$this->assertDatabaseMissing('roles', ['id' => $role->id]);
50+
}
51+
52+
public function test_all_roles_are_shown_on_index_page()
53+
{
54+
$this->actingAs(User::factory()->create());
55+
56+
$role = Role::factory()->create();
57+
58+
$response = $this->get('/roles');
59+
60+
$response->assertSee($role->name);
61+
}
62+
63+
public function test_role_user_count_is_shown()
64+
{
65+
$this->actingAs(User::factory()->create());
66+
67+
$role = Role::factory()->create();
68+
69+
$response = $this->get('/roles');
70+
71+
$response->assertSee(9);
72+
}
73+
74+
public function test_role_user_count_is_shown_with_one_user()
75+
{
76+
$role = Role::create([
77+
'name' => 'Admin',
78+
]);
79+
80+
$user = User::create([
81+
'name' => 'John Doe',
82+
'email' => 'johndoe@mail.com',
83+
'role_id' => $role->id,
84+
'password' => bcrypt('password'),
85+
]);
86+
87+
$this->actingAs($user);
88+
89+
$response = $this->get('/roles');
90+
91+
$response->assertSee(1);
92+
}
93+
94+
}

tests/Feature/UserManagementTest.php

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class UserManagementTest extends TestCase
1010
{
1111
use RefreshDatabase;
1212

13-
public function test_user_validation()
13+
public function test_new_user_model_validation()
1414
{
1515
$this->actingAs(User::factory()->create());
1616

@@ -19,13 +19,13 @@ public function test_user_validation()
1919
$response->assertSessionHasErrors(['name', 'email', 'password', 'avatar']);
2020
}
2121

22-
public function test_user_update_feature()
22+
public function test_user_model_update_feature()
2323
{
2424
$this->actingAs(User::factory()->create());
2525

2626
$user = User::factory()->create();
2727

28-
$this->put('/users/' . $user->id, [
28+
$response = $this->put('/users/' . $user->id, [
2929
'name' => 'John Doe',
3030
'email' => 'updated@mail.com']);
3131

@@ -34,21 +34,23 @@ public function test_user_update_feature()
3434
'name' => 'John Doe',
3535
'email' => 'updated@mail.com']
3636
);
37+
38+
$response->assertStatus(302);
3739
}
3840

39-
public function test_user_delete_feature()
41+
public function test_user_model_delete_feature()
4042
{
4143
$this->actingAs(User::factory()->create());
4244

4345
$user = User::factory()->create();
4446

4547
$response = $this->delete('/users/' . $user->id);
4648

47-
$response->assertRedirect('/');
49+
$response->assertRedirect('/')->assertStatus(302);
4850
$this->assertDatabaseMissing('users', ['id' => $user->id]);
4951
}
5052

51-
public function test_user_delete_feature_with_invalid_id()
53+
public function test_user_model_delete_feature_with_invalid_id()
5254
{
5355
$this->actingAs(User::factory()->create());
5456

@@ -79,4 +81,38 @@ public function test_user_creation_date_is_in_valid_format()
7981

8082
$response->assertSee($user->created_at->format('d M Y'));
8183
}
84+
85+
public function test_user_search_feature_on_index_page()
86+
{
87+
$this->actingAs(User::factory()->create());
88+
89+
$response = $this->get('/users?term=Admin');
90+
$response2 = $this->get('/users?term=something');
91+
92+
$response->assertSee('Admin');
93+
$response2->assertDontSee('Admin');
94+
}
95+
96+
public function test_user_create_page_contains_necessary_input_fields()
97+
{
98+
$this->actingAs(User::factory()->create());
99+
100+
$response = $this->get('/users/create');
101+
102+
$response->assertSee('name');
103+
$response->assertSee('email');
104+
$response->assertSee('password');
105+
$response->assertSee('avatar');
106+
}
107+
108+
public function test_user_logout_feature()
109+
{
110+
$user = User::factory()->create();
111+
112+
$this->actingAs($user);
113+
114+
$response = $this->post('/logout');
115+
116+
$response->assertRedirect('/');
117+
}
82118
}

0 commit comments

Comments
 (0)