aboutsummaryrefslogtreecommitdiffstats
path: root/source/tests/Feature/StudentApiTest.php
blob: 164c777c803379631739c05faa9dca64bbea768f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php

namespace Tests\Feature;

use App\Models\Admin;
use App\Models\Diploma;
use App\Models\Skill;
use App\Models\Student;
use Database\Seeders\DiplomaSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class StudentApiTest extends TestCase
{
    use RefreshDatabase, WithFaker;

    public function test_index_returns_students(): void
    {
        Student::factory()->count(3)->create();
        $admin = Admin::factory()->create();

        Sanctum::actingAs($admin, ['admin']);

        $response = $this->getJson('/api/students');
        $response->assertStatus(200)
            ->assertJsonStructure([
                'data' => [
                    '*' => [
                        'id',
                        'first_name',
                        'last_name',
                        'email',
                        'study_direction',
                        'graduation_track',
                        'job_preferences',
                        'cv',
                        'profile_complete',
                        'profile_photo',
                    ]
                ]
            ]);
    }

    public function test_store_creates_student(): void
    {
        $this->seed(DiplomaSeeder::class);
        $diploma = Diploma::first();


        $data = [
            'first_name' => 'John',
            'last_name' => 'Doe',
            'email' => $this->faker->unique()->safeEmail,
            'password' => 'password123',
            'study_direction' => 'Informatica',
            'graduation_track' => $diploma ? $diploma->id : 1,
            'job_preferences' => 'Software Development',
            'cv' => 'path/to/cv.pdf',
            'profile_complete' => true,
            'profile_photo' => 'path/to/photo.jpg',
        ];

        $student = Student::factory()->create();
        $response = $this->actingAs($student)->postJson('/api/students', $data);


        $response->assertStatus(201)
            ->assertJsonStructure([
                'data' => [
                    'id',
                    'first_name',
                    'last_name',
                    'email',
                    'study_direction',
                    'graduation_track',
                    'job_preferences',
                ],
                'message'
            ]);

        $this->assertDatabaseHas('students', [
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'study_direction' => $data['study_direction'],
            'graduation_track' => $data['graduation_track'],
            'job_preferences' => $data['job_preferences'],
        ]);

    }

    public function test_show_returns_student(): void
    {
        $student = Student::factory()->create();
        $skills = Skill::inRandomOrder()->take(2)->get();
        $student->skills()->attach($skills);

        Sanctum::actingAs($student, ['student']);
        $response = $this->getJson("/api/students/{$student->id}");

        $response->assertStatus(200)
            ->assertJson([
                'data' => [
                    'id' => $student->id,
                    'email' => $student->email,
                    'first_name' => $student->first_name,
                    'last_name' => $student->last_name,
                    'study_direction' => $student->study_direction,
                    'graduation_track' => $student->graduation_track,
                    'job_preferences' => $student->job_preferences,

                ]
            ]);
    }

    public function test_update_modifies_student(): void
    {
        $student = Student::factory()->create();
        $this->seed(DiplomaSeeder::class);
        $diploma = Diploma::first();

        Sanctum::actingAs($student, ['student']);

        $data = [
            'first_name' => 'Updated',
            'last_name' => 'Doe',
            'email' => $this->faker->unique()->safeEmail,
            'password' => 'password123',
            'study_direction' => 'Informatica',
            'graduation_track' => $diploma ? $diploma->id : 1,
            'job_preferences' => 'Software Development',
        ];

        $response = $this->putJson("/api/students/{$student->id}", $data);

        $response->assertStatus(200)
            ->assertJson([
                'message' => 'Student updated successfully',
            ]);

        $this->assertDatabaseHas('students', [
            'id' => $student->id,
            'first_name' => 'Updated',
            'last_name' => 'Doe',
            'email' => $data['email'],
            'study_direction' => $data['study_direction'],
            'graduation_track' => $data['graduation_track'],
            'job_preferences' => $data['job_preferences'],
        ]);
    }

    public function test_destroy_deletes_student(): void
    {
        $student = Student::factory()->create();

        Sanctum::actingAs($student, ['student']);
        $response = $this->deleteJson("/api/students/{$student->id}");

        $response->assertStatus(200)
            ->assertJson([
                'message' => 'Student deleted successfully',
            ]);

        $this->assertDatabaseMissing('students', [
            'id' => $student->id,
        ]);
    }
}