create(['title' => 'Oldest', 'created_at' => $t0->copy()->subMinutes(30)]); $m2 = Movie::factory()->create(['title' => 'Older', 'created_at' => $t0->copy()->subMinutes(20)]); $m3 = Movie::factory()->create(['title' => 'Newer', 'created_at' => $t0->copy()->subMinutes(10)]); $m4 = Movie::factory()->create(['title' => 'Newest', 'created_at' => $t0->copy()->subMinutes(1)]); $res = $this->getJson('/api/movies?sort=newest&per_page=3'); $res->assertOk(); $json = $res->json(); $this->assertSame(4, $json['total']); $this->assertCount(3, $json['data']); // Expect newest first $this->assertSame('Newest', $json['data'][0]['title']); $this->assertSame('Newer', $json['data'][1]['title']); $this->assertSame('Older', $json['data'][2]['title']); } public function test_api_movies_newest_with_limit_three_when_fewer_exist(): void { Movie::factory()->create(['title' => 'Only One']); Movie::factory()->create(['title' => 'Only Two']); $res = $this->getJson('/api/movies?sort=newest&per_page=3'); $res->assertOk(); $json = $res->json(); $this->assertSame(2, $json['total']); $this->assertCount(2, $json['data']); } }