Skip to content

Commit 173a293

Browse files
committed
testing team controller
1 parent bc83087 commit 173a293

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

tests/Feature/TeamTest.php.php

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
3+
use Tests\TestCase;
4+
use App\Models\Team;
5+
use App\Models\User;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
8+
class TeamControllerTest extends TestCase
9+
{
10+
use RefreshDatabase;
11+
12+
public function testIndex()
13+
{
14+
15+
$team1 = Team::factory()->create(['created_at' => now()->subDays(2)]);
16+
$team2 = Team::factory()->create(['created_at' => now()->subDays(1)]);
17+
$team3 = Team::factory()->create(['created_at' => now()]);
18+
19+
$user1 = User::factory()->create();
20+
$user2 = User::factory()->create();
21+
$user3 = User::factory()->create();
22+
23+
$team1->members()->attach($user1);
24+
$team1->members()->attach($user2);
25+
$team2->members()->attach($user2);
26+
$team3->members()->attach($user1);
27+
$team3->members()->attach($user3);
28+
29+
30+
$response = $this->get('/api/teams');
31+
32+
33+
$response->assertStatus(200);
34+
$response->assertJson([
35+
'result' => true,
36+
'message' => '',
37+
'data' => [
38+
'teams' => [
39+
[
40+
'id' => $team3->id,
41+
'name' => $team3->name,
42+
43+
],
44+
[
45+
'id' => $team2->id,
46+
'name' => $team2->name,
47+
48+
],
49+
[
50+
'id' => $team1->id,
51+
'name' => $team1->name,
52+
53+
],
54+
],
55+
],
56+
]);
57+
}
58+
59+
60+
public function testStore()
61+
{
62+
63+
$user = User::factory()->create();
64+
65+
$teamData = [
66+
'name' => 'New Team',
67+
68+
];
69+
70+
71+
$response = $this->actingAs($user)
72+
->post('/api/teams', $teamData); // افتراضيًا يتم استدعاء الوظيفة store()
73+
74+
75+
$response->assertStatus(200);
76+
$response->assertJson([
77+
'result' => true,
78+
'message' => 'Team created successfully.',
79+
'data' => [
80+
'team' => [
81+
'id' => 1,
82+
'name' => $teamData['name'],
83+
84+
],
85+
],
86+
]);
87+
88+
89+
$this->assertDatabaseHas('teams', [
90+
'name' => $teamData['name'],
91+
92+
]);
93+
94+
95+
$this->assertDatabaseHas('team_user', [
96+
'team_id' => 1,
97+
'user_id' => $user->id,
98+
]);
99+
}
100+
101+
102+
public function testUpdate()
103+
{
104+
105+
$user = User::factory()->create();
106+
$team = Team::factory()->create();
107+
108+
$updatedTeamData = [
109+
'name' => 'Updated Team',
110+
111+
];
112+
113+
114+
$response = $this->actingAs($user)
115+
->put('/api/teams/'.$team->id, $updatedTeamData); // افتراضيًا يتم استدعاء الوظيفة update()
116+
117+
118+
$response->assertStatus(200);
119+
$response->assertJson([
120+
'result' => true,
121+
'message' => 'Team updated successfully.',
122+
'data' => [
123+
'team' => [
124+
'id' => $team->id,
125+
'name' => $updatedTeamData['name'],
126+
127+
],
128+
],
129+
]);
130+
131+
132+
$this->assertDatabaseHas('teams', [
133+
'id' => $team->id,
134+
'name' => $updatedTeamData['name'],
135+
136+
]);
137+
}
138+
139+
140+
141+
142+
public function testDelete()
143+
{
144+
145+
$user = User::factory()->create();
146+
$team = Team::factory()->create();
147+
148+
149+
$response = $this->actingAs($user)
150+
->delete('/api/teams/'.$team->id); // افتراضيًا يتم استدعاء الوظيفة delete()
151+
152+
153+
$response->assertStatus(200);
154+
$response->assertJson([
155+
'result' => true,
156+
'message' => 'Team deleted successfully.',
157+
'data' => null,
158+
]);
159+
160+
161+
$this->assertDatabaseMissing('teams', [
162+
'id' => $team->id,
163+
]);
164+
}
165+
}

0 commit comments

Comments
 (0)