Skip to content

Commit edaaf67

Browse files
committed
Add more tests
1 parent 0130383 commit edaaf67

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed

tests/UserTest.php

+170
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,174 @@ public function testFetchAllOrder(): void
204204
);
205205
}
206206

207+
public function testGetKey(): void
208+
{
209+
$this->cleanupUsers();
210+
$this->assertSame(0, User::count());
211+
$user1 = User::create(
212+
'5c8169b1-d6ef-4415-8c39-e1664df8b954',
213+
'Gwénola',
214+
'Etheve',
215+
null
216+
);
217+
$this->assertTrue($user1->save());
218+
$user2 = User::create(
219+
'874d1aa5-4db3-4953-88dd-2dd58a298d3e',
220+
'William',
221+
'Desportes',
222+
null
223+
);
224+
225+
$this->assertTrue($user2->save());
226+
$this->assertSame(2, User::count());
227+
$this->assertSame('5c8169b1-d6ef-4415-8c39-e1664df8b954', $user1->getKey());
228+
$this->assertSame('874d1aa5-4db3-4953-88dd-2dd58a298d3e', $user2->getKey());
229+
User::deleteAll();
230+
}
231+
232+
public function testDeleteWherePrimary(): void
233+
{
234+
$this->cleanupUsers();
235+
$this->assertSame(0, User::count());
236+
$user1 = User::create(
237+
'5c8169b1-d6ef-4415-8c39-e1664df8b954',
238+
'Gwénola',
239+
'Etheve',
240+
null
241+
);
242+
$this->assertTrue($user1->save());
243+
$user2 = User::create(
244+
'874d1aa5-4db3-4953-88dd-2dd58a298d3e',
245+
'William',
246+
'Desportes',
247+
null
248+
);
249+
250+
$this->assertTrue($user2->save());
251+
$this->assertSame(2, User::count());
252+
$this->assertNotNull(User::findById('5c8169b1-d6ef-4415-8c39-e1664df8b954'));
253+
User::deleteWherePrimary('5c8169b1-d6ef-4415-8c39-e1664df8b954');
254+
$this->assertNull(User::findById('5c8169b1-d6ef-4415-8c39-e1664df8b954'));
255+
$this->assertSame(1, User::count());
256+
User::deleteAll();
257+
$this->assertSame(0, User::count());
258+
}
259+
260+
public function testDeleteWhere(): void
261+
{
262+
$this->cleanupUsers();
263+
$this->assertSame(0, User::count());
264+
$user1 = User::create(
265+
'5c8169b1-d6ef-4415-8c39-e1664df8b954',
266+
'Gwénola',
267+
'Etheve',
268+
null
269+
);
270+
$this->assertTrue($user1->save());
271+
$user2 = User::create(
272+
'874d1aa5-4db3-4953-88dd-2dd58a298d3e',
273+
'William',
274+
'Desportes',
275+
null
276+
);
277+
278+
$this->assertTrue($user2->save());
279+
$this->assertSame(2, User::count());
280+
$this->assertNotNull(User::findById('5c8169b1-d6ef-4415-8c39-e1664df8b954'));
281+
$this->assertNotNull(User::findById('874d1aa5-4db3-4953-88dd-2dd58a298d3e'));
282+
User::deleteWhere(
283+
[
284+
'first_name' => 'William',
285+
]
286+
);
287+
$this->assertWasQuery(
288+
'DELETE FROM `users` WHERE `first_name` = ?;',
289+
[
290+
'William',
291+
]
292+
);
293+
$this->assertNull(User::findById('874d1aa5-4db3-4953-88dd-2dd58a298d3e'));
294+
$this->assertNotNull(User::findById('5c8169b1-d6ef-4415-8c39-e1664df8b954'));
295+
$this->assertSame(1, User::count());
296+
User::deleteAll();
297+
$this->assertSame(0, User::count());
298+
}
299+
300+
public function testRefreshDeleted(): void
301+
{
302+
$this->cleanupUsers();
303+
$this->assertSame(0, User::count());
304+
$user1 = User::create(
305+
'5c8169b1-d6ef-4415-8c39-e1664df8b954',
306+
'Gwénola',
307+
'Etheve',
308+
null
309+
);
310+
$this->assertTrue($user1->save());
311+
$this->assertTrue($user1->refresh());
312+
$this->assertTrue($user1->delete());
313+
$this->assertFalse($user1->refresh());
314+
$this->assertSame(0, User::count());
315+
}
316+
317+
public function testUpdateNoChanges(): void
318+
{
319+
$this->cleanupUsers();
320+
$this->assertSame(0, User::count());
321+
$user1 = User::create(
322+
'5c8169b1-d6ef-4415-8c39-e1664df8b954',
323+
'Gwénola',
324+
'Etheve',
325+
null
326+
);
327+
$this->assertTrue($user1->save());
328+
$this->assertFalse($user1->update());
329+
$this->assertTrue($user1->delete());
330+
}
331+
332+
public function testInsertBatch(): void
333+
{
334+
$this->cleanupUsers();
335+
$this->assertSame(0, User::count());
336+
$user1 = User::create(
337+
'5c8169b1-d6ef-4415-8c39-e1664df8b954',
338+
'Gwénola',
339+
'Etheve',
340+
null
341+
);
342+
$user2 = User::create(
343+
'874d1aa5-4db3-4953-88dd-2dd58a298d3e',
344+
'William',
345+
'Desportes',
346+
null
347+
);
348+
$this->assertFalse($user1->refresh(), 'Does not exist in DB');
349+
$this->assertFalse($user2->refresh(), 'Does not exist in DB');
350+
$this->assertTrue(User::saveBatch([]));
351+
$this->emptyQueries();
352+
$this->assertTrue(
353+
User::saveBatch(
354+
[
355+
$user1,
356+
$user2
357+
]
358+
)
359+
);
360+
$this->assertWasQuery(
361+
'INSERT INTO `users` (user_uuid, first_name, last_name, date_of_birth) VALUES (?, ?, ?, ?), (?, ?, ?, ?)',
362+
[
363+
0 => '5c8169b1-d6ef-4415-8c39-e1664df8b954',
364+
1 => 'Gwénola',
365+
2 => 'Etheve',
366+
3 => null,
367+
4 => '874d1aa5-4db3-4953-88dd-2dd58a298d3e',
368+
5 => 'William',
369+
6 => 'Desportes',
370+
7 => null,
371+
]
372+
);
373+
$this->assertTrue($user1->refresh(), 'Should exist in DB');
374+
$this->assertTrue($user2->refresh(), 'Should exist in DB');
375+
}
376+
207377
}

0 commit comments

Comments
 (0)