248 lines
7.1 KiB
PHP
248 lines
7.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Unit;
|
||
|
|
|
||
|
|
use App\Models\Trip;
|
||
|
|
use App\Models\User;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Tests\TestCase;
|
||
|
|
|
||
|
|
class TripTest extends TestCase
|
||
|
|
{
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test trip creation with factory.
|
||
|
|
*/
|
||
|
|
public function test_trip_can_be_created()
|
||
|
|
{
|
||
|
|
$user = User::factory()->create();
|
||
|
|
$trip = Trip::factory()->create([
|
||
|
|
'name' => 'Paris Adventure',
|
||
|
|
'description' => 'A wonderful trip to Paris',
|
||
|
|
'created_by_user_id' => $user->id,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertInstanceOf(Trip::class, $trip);
|
||
|
|
$this->assertEquals('Paris Adventure', $trip->name);
|
||
|
|
$this->assertEquals('A wonderful trip to Paris', $trip->description);
|
||
|
|
$this->assertEquals($user->id, $trip->created_by_user_id);
|
||
|
|
$this->assertNotNull($trip->id);
|
||
|
|
$this->assertNotNull($trip->created_at);
|
||
|
|
$this->assertNotNull($trip->updated_at);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test trip fillable attributes.
|
||
|
|
*/
|
||
|
|
public function test_trip_fillable_attributes()
|
||
|
|
{
|
||
|
|
$user = User::factory()->create();
|
||
|
|
$tripData = [
|
||
|
|
'name' => 'Tokyo Trip',
|
||
|
|
'description' => 'Exploring Japan',
|
||
|
|
'start_date' => '2025-06-01',
|
||
|
|
'end_date' => '2025-06-15',
|
||
|
|
'created_by_user_id' => $user->id,
|
||
|
|
];
|
||
|
|
|
||
|
|
$trip = Trip::create($tripData);
|
||
|
|
|
||
|
|
$this->assertEquals('Tokyo Trip', $trip->name);
|
||
|
|
$this->assertEquals('Exploring Japan', $trip->description);
|
||
|
|
$this->assertEquals('2025-06-01', $trip->start_date->format('Y-m-d'));
|
||
|
|
$this->assertEquals('2025-06-15', $trip->end_date->format('Y-m-d'));
|
||
|
|
$this->assertEquals($user->id, $trip->created_by_user_id);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test trip date casting.
|
||
|
|
*/
|
||
|
|
public function test_trip_date_casting()
|
||
|
|
{
|
||
|
|
$trip = Trip::factory()->create([
|
||
|
|
'start_date' => '2025-07-01',
|
||
|
|
'end_date' => '2025-07-10',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertInstanceOf(\Illuminate\Support\Carbon::class, $trip->start_date);
|
||
|
|
$this->assertInstanceOf(\Illuminate\Support\Carbon::class, $trip->end_date);
|
||
|
|
$this->assertEquals('2025-07-01', $trip->start_date->format('Y-m-d'));
|
||
|
|
$this->assertEquals('2025-07-10', $trip->end_date->format('Y-m-d'));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test trip can have null dates.
|
||
|
|
*/
|
||
|
|
public function test_trip_can_have_null_dates()
|
||
|
|
{
|
||
|
|
$trip = Trip::factory()->withoutDates()->create([
|
||
|
|
'name' => 'Flexible Trip',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertNull($trip->start_date);
|
||
|
|
$this->assertNull($trip->end_date);
|
||
|
|
$this->assertEquals('Flexible Trip', $trip->name);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test trip belongs to user relationship.
|
||
|
|
*/
|
||
|
|
public function test_trip_belongs_to_user()
|
||
|
|
{
|
||
|
|
$user = User::factory()->create(['name' => 'John Doe']);
|
||
|
|
$trip = Trip::factory()->create([
|
||
|
|
'created_by_user_id' => $user->id,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertInstanceOf(User::class, $trip->user);
|
||
|
|
$this->assertEquals($user->id, $trip->user->id);
|
||
|
|
$this->assertEquals('John Doe', $trip->user->name);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test trip factory creates valid trips.
|
||
|
|
*/
|
||
|
|
public function test_trip_factory_creates_valid_trips()
|
||
|
|
{
|
||
|
|
$trip = Trip::factory()->create();
|
||
|
|
|
||
|
|
$this->assertNotEmpty($trip->name);
|
||
|
|
$this->assertNotNull($trip->created_by_user_id);
|
||
|
|
$this->assertInstanceOf(User::class, $trip->user);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test trip factory upcoming state.
|
||
|
|
*/
|
||
|
|
public function test_trip_factory_upcoming_state()
|
||
|
|
{
|
||
|
|
$trip = Trip::factory()->upcoming()->create();
|
||
|
|
|
||
|
|
$this->assertNotNull($trip->start_date);
|
||
|
|
$this->assertNotNull($trip->end_date);
|
||
|
|
$this->assertTrue($trip->start_date->isFuture());
|
||
|
|
$this->assertTrue($trip->end_date->isAfter($trip->start_date));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test trip factory past state.
|
||
|
|
*/
|
||
|
|
public function test_trip_factory_past_state()
|
||
|
|
{
|
||
|
|
$trip = Trip::factory()->past()->create();
|
||
|
|
|
||
|
|
$this->assertNotNull($trip->start_date);
|
||
|
|
$this->assertNotNull($trip->end_date);
|
||
|
|
$this->assertTrue($trip->start_date->isPast());
|
||
|
|
$this->assertTrue($trip->end_date->isPast());
|
||
|
|
$this->assertTrue($trip->end_date->isAfter($trip->start_date));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test trip model uses correct table.
|
||
|
|
*/
|
||
|
|
public function test_trip_uses_correct_table()
|
||
|
|
{
|
||
|
|
$trip = new Trip();
|
||
|
|
|
||
|
|
$this->assertEquals('trips', $trip->getTable());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test trip model has correct primary key.
|
||
|
|
*/
|
||
|
|
public function test_trip_has_correct_primary_key()
|
||
|
|
{
|
||
|
|
$trip = new Trip();
|
||
|
|
|
||
|
|
$this->assertEquals('id', $trip->getKeyName());
|
||
|
|
$this->assertTrue($trip->getIncrementing());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test trip model uses timestamps.
|
||
|
|
*/
|
||
|
|
public function test_trip_uses_timestamps()
|
||
|
|
{
|
||
|
|
$trip = new Trip();
|
||
|
|
|
||
|
|
$this->assertTrue($trip->usesTimestamps());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test trip can be created with minimal data.
|
||
|
|
*/
|
||
|
|
public function test_trip_can_be_created_with_minimal_data()
|
||
|
|
{
|
||
|
|
$user = User::factory()->create();
|
||
|
|
$trip = Trip::create([
|
||
|
|
'name' => 'Minimal Trip',
|
||
|
|
'created_by_user_id' => $user->id,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertEquals('Minimal Trip', $trip->name);
|
||
|
|
$this->assertNull($trip->description);
|
||
|
|
$this->assertNull($trip->start_date);
|
||
|
|
$this->assertNull($trip->end_date);
|
||
|
|
$this->assertEquals($user->id, $trip->created_by_user_id);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test trip name is required.
|
||
|
|
*/
|
||
|
|
public function test_trip_name_is_required()
|
||
|
|
{
|
||
|
|
$user = User::factory()->create();
|
||
|
|
|
||
|
|
$this->expectException(\Illuminate\Database\QueryException::class);
|
||
|
|
|
||
|
|
Trip::create([
|
||
|
|
'description' => 'A trip without a name',
|
||
|
|
'created_by_user_id' => $user->id,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test trip requires a user.
|
||
|
|
*/
|
||
|
|
public function test_trip_requires_user()
|
||
|
|
{
|
||
|
|
$this->expectException(\Illuminate\Database\QueryException::class);
|
||
|
|
|
||
|
|
Trip::create([
|
||
|
|
'name' => 'Orphaned Trip',
|
||
|
|
'description' => 'A trip without a user',
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test trip dates can be formatted.
|
||
|
|
*/
|
||
|
|
public function test_trip_dates_can_be_formatted()
|
||
|
|
{
|
||
|
|
$trip = Trip::factory()->create([
|
||
|
|
'start_date' => '2025-12-25',
|
||
|
|
'end_date' => '2025-12-31',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertEquals('2025-12-25', $trip->start_date->format('Y-m-d'));
|
||
|
|
$this->assertEquals('2025-12-31', $trip->end_date->format('Y-m-d'));
|
||
|
|
$this->assertEquals('December 25, 2025', $trip->start_date->format('F j, Y'));
|
||
|
|
$this->assertEquals('December 31, 2025', $trip->end_date->format('F j, Y'));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Test trip can calculate duration.
|
||
|
|
*/
|
||
|
|
public function test_trip_can_calculate_duration()
|
||
|
|
{
|
||
|
|
$trip = Trip::factory()->create([
|
||
|
|
'start_date' => '2025-06-01',
|
||
|
|
'end_date' => '2025-06-07',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$duration = $trip->start_date->diffInDays($trip->end_date) + 1; // Include both start and end day
|
||
|
|
$this->assertEquals(7, $duration);
|
||
|
|
}
|
||
|
|
}
|