47 lines
1.7 KiB
PHP
47 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Movies;
|
|
|
|
use App\Modules\Movies\Models\Movie;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Carbon;
|
|
use Tests\TestCase;
|
|
|
|
class DashboardLatestMoviesTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_api_movies_newest_with_limit_three_returns_latest_three(): void
|
|
{
|
|
// Create 4 movies with explicit created_at to ensure ordering
|
|
$t0 = now();
|
|
$m1 = Movie::factory()->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']);
|
|
}
|
|
}
|