-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppTest.php
More file actions
186 lines (151 loc) · 5.91 KB
/
AppTest.php
File metadata and controls
186 lines (151 loc) · 5.91 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
use PHPUnit\Framework\TestCase;
class AppTest extends TestCase
{
private $pdo;
protected function setUp(): void
{
$host = 'localhost';
$dbname = 'gestion_notess';
$username = 'root';
$password = '';
$this->pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
public function testDatabaseConnection()
{
$this->assertInstanceOf(PDO::class, $this->pdo);
}
public function testInsertUser()
{
$sql = "INSERT INTO users (nom, email, sex, mdp, poste) VALUES (:nom, :email, :sex, :mdp, :poste)";
$stmt = $this->pdo->prepare($sql);
$nom = 'Test User';
$email = 'test@example.com';
$sex = 'M';
$mdp = password_hash('password123', PASSWORD_DEFAULT);
$poste = 'DAA';
$stmt->execute([
':nom' => $nom,
':email' => $email,
':sex' => $sex,
':mdp' => $mdp,
':poste' => $poste
]);
$this->assertTrue($stmt->rowCount() > 0);
// Clean up
$sql = "DELETE FROM users WHERE email = :email";
$stmt = $this->pdo->prepare($sql);
$stmt->execute([':email' => $email]);
}
public function testUserLogin()
{
// Insert test user
$sql = "INSERT INTO users (nom, email, sex, mdp, poste) VALUES (:nom, :email, :sex, :mdp, :poste)";
$stmt = $this->pdo->prepare($sql);
$nom = 'Login User';
$email = 'login@example.com';
$sex = 'F';
$mdp = password_hash('password456', PASSWORD_DEFAULT);
$poste = 'DSR';
$stmt->execute([
':nom' => $nom,
':email' => $email,
':sex' => $sex,
':mdp' => $mdp,
':poste' => $poste
]);
// Test login
$sql = "SELECT * FROM users WHERE nom = :nom";
$stmt = $this->pdo->prepare($sql);
$stmt->execute([':nom' => $nom]);
$user = $stmt->fetch();
$this->assertNotEmpty($user);
$this->assertTrue(password_verify('password456', $user['mdp']));
// Clean up
$sql = "DELETE FROM users WHERE email = :email";
$stmt = $this->pdo->prepare($sql);
$stmt->execute([':email' => $email]);
}
public function testInsertPendingGrade()
{
$sql = "INSERT INTO pending_grades (student_id, teacher_id, course, grade) VALUES (:student_id, :teacher_id, :course, :grade)";
$stmt = $this->pdo->prepare($sql);
$student_id = 1;
$teacher_id = 1;
$course = 'Math';
$grade = 'A';
$stmt->execute([
':student_id' => $student_id,
':teacher_id' => $teacher_id,
':course' => $course,
':grade' => $grade
]);
$this->assertTrue($stmt->rowCount() > 0);
// Clean up
$sql = "DELETE FROM pending_grades WHERE student_id = :student_id AND course = :course";
$stmt = $this->pdo->prepare($sql);
$stmt->execute([':student_id' => $student_id, ':course' => $course]);
}
public function testValidatePendingGrade()
{
// Insert test pending grade
$sql = "INSERT INTO pending_grades (student_id, teacher_id, course, grade) VALUES (:student_id, :teacher_id, :course, :grade)";
$stmt = $this->pdo->prepare($sql);
$student_id = 1;
$teacher_id = 1;
$course = 'Math';
$grade = 'A';
$stmt->execute([
':student_id' => $student_id,
':teacher_id' => $teacher_id,
':course' => $course,
':grade' => $grade
]);
// Retrieve the inserted pending grade
$sql = "SELECT * FROM pending_grades WHERE student_id = :student_id AND course = :course";
$stmt = $this->pdo->prepare($sql);
$stmt->execute([':student_id' => $student_id, ':course' => $course]);
$pending_grade = $stmt->fetch();
$this->assertNotEmpty($pending_grade);
// Insert the grade into grades table
$sql_insert = "INSERT INTO grades (student_id, teacher_id, course, grade) VALUES (:student_id, :teacher_id, :course, :grade)";
$stmt_insert = $this->pdo->prepare($sql_insert);
$stmt_insert->execute([
':student_id' => $pending_grade['student_id'],
':teacher_id' => $pending_grade['teacher_id'],
':course' => $pending_grade['course'],
':grade' => $pending_grade['grade']
]);
$this->assertTrue($stmt_insert->rowCount() > 0);
// Clean up
$sql_delete = "DELETE FROM grades WHERE student_id = :student_id AND course = :course";
$stmt_delete = $this->pdo->prepare($sql_delete);
$stmt_delete->execute([':student_id' => $student_id, ':course' => $course]);
$sql_delete = "DELETE FROM pending_grades WHERE student_id = :student_id AND course = :course";
$stmt_delete = $this->pdo->prepare($sql_delete);
$stmt_delete->execute([':student_id' => $student_id, ':course' => $course]);
}
public function testInsertTeacher()
{
$sql = "INSERT INTO teachers (nom_prenom, email, mdp, matiere) VALUES (:nom_prenom, :email, :mdp, :matiere)";
$stmt = $this->pdo->prepare($sql);
$nom_prenom = 'Test Teacher';
$email = 'teacher@example.com';
$mdp = password_hash('password789', PASSWORD_DEFAULT);
$matiere = 'Science';
$stmt->execute([
':nom_prenom' => $nom_prenom,
':email' => $email,
':mdp' => $mdp,
':matiere' => $matiere
]);
$this->assertTrue($stmt->rowCount() > 0);
// Clean up
$sql = "DELETE FROM teachers WHERE email = :email";
$stmt = $this->pdo->prepare($sql);
$stmt->execute([':email' => $email]);
}
}
?>