Skip to content

Commit 97e0650

Browse files
committed
Improve pre- and post-assertions for deletion tests
1 parent ba1e1d5 commit 97e0650

File tree

1 file changed

+78
-100
lines changed

1 file changed

+78
-100
lines changed

tests/wpunit/QueryBuilder/CRUDTest.php

Lines changed: 78 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,8 @@ public function testDeleteShouldWorkWithOrderByAndLimit()
229229
['post_title' => 'Delete Combined D', 'post_type' => 'delete_combined_test', 'post_content' => 'Content D'],
230230
];
231231

232-
$ids = [];
233-
foreach ($posts as $post) {
234-
DB::table('posts')->insert($post);
235-
$ids[] = DB::last_insert_id();
236-
}
232+
$ids = $this->insert_posts($posts);
233+
$this->assert_posts_exist($posts, $ids);
237234

238235
// Delete the 2 oldest posts (lowest IDs)
239236
DB::table('posts')
@@ -242,23 +239,13 @@ public function testDeleteShouldWorkWithOrderByAndLimit()
242239
->limit(2)
243240
->delete();
244241

245-
// Verify the first 2 posts were deleted
246-
$post1 = DB::table('posts')
247-
->where('ID', $ids[0])
248-
->get();
249-
$post2 = DB::table('posts')
250-
->where('ID', $ids[1])
251-
->get();
252-
253-
$this->assertNull($post1);
254-
$this->assertNull($post2);
242+
$foundPosts = DB::table('posts')
243+
->select('post_title', 'post_type', 'post_content')
244+
->whereIn('ID', $ids)
245+
->getAll(ARRAY_A);
246+
unset($posts[0], $posts[1]);
255247

256-
// Verify the other 2 posts still exist
257-
$count = DB::table('posts')
258-
->where('post_type', 'delete_combined_test')
259-
->count();
260-
261-
$this->assertEquals(2, $count);
248+
$this->assertEquals(array_values($posts), $foundPosts);
262249
}
263250

264251
/**
@@ -276,30 +263,21 @@ public function testDeleteShouldWorkWithWhereIn()
276263
['post_title' => 'Delete WhereIn 4', 'post_type' => 'delete_wherein_test', 'post_content' => 'Content 4'],
277264
];
278265

279-
$ids = [];
280-
foreach ($posts as $post) {
281-
DB::table('posts')->insert($post);
282-
$ids[] = DB::last_insert_id();
283-
}
266+
$ids = $this->insert_posts($posts);
267+
$this->assert_posts_exist($posts, $ids);
284268

285269
// Delete posts with specific IDs using whereIn
286270
DB::table('posts')
287271
->whereIn('ID', [$ids[0], $ids[2]])
288272
->delete();
289273

290-
// Verify specific posts were deleted
291-
$post1 = DB::table('posts')->where('ID', $ids[0])->get();
292-
$post3 = DB::table('posts')->where('ID', $ids[2])->get();
293-
294-
$this->assertNull($post1);
295-
$this->assertNull($post3);
274+
$foundPosts = DB::table('posts')
275+
->select('post_title', 'post_type', 'post_content')
276+
->whereIn('ID', $ids)
277+
->getAll(ARRAY_A);
278+
unset($posts[0], $posts[2]);
296279

297-
// Verify other posts still exist
298-
$post2 = DB::table('posts')->where('ID', $ids[1])->get();
299-
$post4 = DB::table('posts')->where('ID', $ids[3])->get();
300-
301-
$this->assertNotNull($post2);
302-
$this->assertNotNull($post4);
280+
$this->assertEquals(array_values($posts), $foundPosts);
303281
}
304282

305283
/**
@@ -317,32 +295,22 @@ public function testDeleteShouldWorkWithWhereBetween()
317295
['post_title' => 'Delete Between 4', 'post_type' => 'delete_between_test', 'menu_order' => 40],
318296
];
319297

320-
foreach ($posts as $post) {
321-
DB::table('posts')->insert($post);
322-
}
298+
$ids = $this->insert_posts($posts);
299+
$this->assert_posts_exist($posts, $ids);
323300

324301
// Delete posts with menu_order between 15 and 35
325302
DB::table('posts')
326303
->where('post_type', 'delete_between_test')
327304
->whereBetween('menu_order', 15, 35)
328305
->delete();
329306

330-
// Should have deleted 2 posts (menu_order 20 and 30)
331-
$remaining = DB::table('posts')
332-
->where('post_type', 'delete_between_test')
333-
->count();
334-
335-
$this->assertEquals(2, $remaining);
336-
337-
// Verify the correct posts remain (menu_order 10 and 40)
338-
$posts = DB::table('posts')
339-
->select('menu_order')
340-
->where('post_type', 'delete_between_test')
341-
->orderBy('menu_order', 'ASC')
342-
->getAll();
307+
$foundPosts = DB::table('posts')
308+
->select('post_title', 'post_type', 'menu_order')
309+
->whereIn('ID', $ids)
310+
->getAll(ARRAY_A);
311+
unset($posts[1], $posts[2]);
343312

344-
$this->assertEquals(10, $posts[0]->menu_order);
345-
$this->assertEquals(40, $posts[1]->menu_order);
313+
$this->assertEquals(array_values($posts), $foundPosts);
346314
}
347315

348316
/**
@@ -360,9 +328,8 @@ public function testDeleteShouldWorkWithMultipleWhereConditions()
360328
['post_title' => 'Delete Multi 4', 'post_type' => 'type_b', 'post_status' => 'draft'],
361329
];
362330

363-
foreach ($posts as $post) {
364-
DB::table('posts')->insert($post);
365-
}
331+
$ids = $this->insert_posts($posts);
332+
$this->assert_posts_exist($posts, $ids);
366333

367334
// Delete only posts with type_a AND status publish
368335
DB::table('posts')
@@ -371,19 +338,13 @@ public function testDeleteShouldWorkWithMultipleWhereConditions()
371338
->where('post_title', 'Delete Multi 1')
372339
->delete();
373340

374-
// Verify only 1 post was deleted
375-
$deleted = DB::table('posts')
376-
->where('post_title', 'Delete Multi 1')
377-
->get();
378-
379-
$this->assertNull($deleted);
341+
$foundPosts = DB::table('posts')
342+
->select('post_title', 'post_type', 'post_status')
343+
->whereIn('ID', $ids)
344+
->getAll(ARRAY_A);
345+
unset($posts[0]);
380346

381-
// Verify other posts still exist
382-
$remaining = DB::table('posts')
383-
->whereIn('post_title', ['Delete Multi 2', 'Delete Multi 3', 'Delete Multi 4'])
384-
->count();
385-
386-
$this->assertEquals(3, $remaining);
347+
$this->assertEquals(array_values($posts), $foundPosts);
387348
}
388349

389350
/**
@@ -401,29 +362,21 @@ public function testDeleteShouldWorkWithWhereLike()
401362
['post_title' => 'Service: Widget ABC', 'post_type' => 'delete_like_test', 'post_content' => 'Content 4'],
402363
];
403364

404-
foreach ($posts as $post) {
405-
DB::table('posts')->insert($post);
406-
}
365+
$ids = $this->insert_posts($posts);
366+
$this->assert_posts_exist($posts, $ids);
407367

408368
// Delete all posts with titles containing "Widget"
409369
DB::table('posts')
410370
->where('post_type', 'delete_like_test')
411371
->whereLike('post_title', '%Widget%')
412372
->delete();
413373

414-
// Should have deleted 3 posts (all with "Widget" in title)
415-
$remaining = DB::table('posts')
416-
->where('post_type', 'delete_like_test')
417-
->count();
418-
419-
$this->assertEquals(1, $remaining);
420-
421-
// Verify the correct post remains (Gadget)
422-
$post = DB::table('posts')
423-
->where('post_type', 'delete_like_test')
424-
->get();
374+
$foundPosts = DB::table('posts')
375+
->select('post_title', 'post_type', 'post_content')
376+
->whereIn('ID', $ids)
377+
->getAll(ARRAY_A);
425378

426-
$this->assertStringContainsString('Gadget', $post->post_title);
379+
$this->assertEquals([$posts[2]], $foundPosts);
427380
}
428381

429382
/**
@@ -441,29 +394,54 @@ public function testDeleteShouldWorkWithWhereLikePrefix()
441394
['post_title' => 'Review: Meeting Notes', 'post_type' => 'delete_prefix_test', 'post_content' => 'Content 4'],
442395
];
443396

444-
foreach ($posts as $post) {
445-
DB::table('posts')->insert($post);
446-
}
397+
$ids = $this->insert_posts($posts);
398+
$this->assert_posts_exist($posts, $ids);
447399

448400
// Delete all posts starting with "Draft:"
449401
DB::table('posts')
450402
->where('post_type', 'delete_prefix_test')
451403
->whereLike('post_title', 'Draft:%')
452404
->delete();
453405

454-
// Should have deleted 2 posts (both starting with "Draft:")
455-
$remaining = DB::table('posts')
456-
->where('post_type', 'delete_prefix_test')
457-
->count();
406+
$foundPosts = DB::table('posts')
407+
->select('post_title', 'post_type', 'post_content')
408+
->whereIn('ID', $ids)
409+
->getAll(ARRAY_A);
458410

459-
$this->assertEquals(2, $remaining);
411+
$this->assertEquals([$posts[2], $posts[3]], $foundPosts);
412+
}
460413

461-
// Verify no "Draft:" posts remain
462-
$draftPosts = DB::table('posts')
463-
->where('post_type', 'delete_prefix_test')
464-
->whereLike('post_title', 'Draft:%')
465-
->count();
414+
/**
415+
* Inserts multiple posts into the database and returns their IDs.
416+
*
417+
* @param array $posts An array of associative arrays, where each associative array represents a post to insert.
418+
*
419+
* @return array An array of IDs corresponding to the inserted posts.
420+
*/
421+
private function insert_posts( array $posts ): array {
422+
$ids = [];
423+
foreach ($posts as $post) {
424+
DB::table('posts')->insert($post);
425+
$ids[] = DB::last_insert_id();
426+
}
427+
428+
return $ids;
429+
}
430+
431+
/**
432+
* Asserts that posts with the given IDs exist and match the specified data.
433+
*
434+
* @param array $posts An array of expected post data to validate against the database.
435+
* @param array $ids An array of post IDs to check for existence in the database.
436+
*
437+
* @return void
438+
*/
439+
private function assert_posts_exist( array $posts, array $ids ) {
440+
$foundPosts = DB::table('posts')
441+
->select(...array_keys($posts[0]))
442+
->whereIn('ID', $ids)
443+
->getAll(ARRAY_A);
466444

467-
$this->assertEquals(0, $draftPosts);
445+
$this->assertEquals($posts, $foundPosts);
468446
}
469447
}

0 commit comments

Comments
 (0)