trip-planner/backend/tests/Unit/UserTest.php

224 lines
5.8 KiB
PHP
Raw Normal View History

2025-09-27 11:52:59 +02:00
<?php
namespace Tests\Unit;
use App\Models\User;
use App\Models\Trip;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Laravel\Sanctum\PersonalAccessToken;
use Tests\TestCase;
class UserTest extends TestCase
{
use RefreshDatabase;
/**
* Test user creation with factory.
*/
public function test_user_can_be_created()
{
$user = User::factory()->create([
'name' => 'John Doe',
'email' => 'john@example.com',
]);
$this->assertInstanceOf(User::class, $user);
$this->assertEquals('John Doe', $user->name);
$this->assertEquals('john@example.com', $user->email);
$this->assertNotNull($user->id);
$this->assertNotNull($user->created_at);
$this->assertNotNull($user->updated_at);
}
/**
* Test user fillable attributes.
*/
public function test_user_fillable_attributes()
{
$userData = [
'name' => 'Jane Doe',
'email' => 'jane@example.com',
'password' => 'password123',
];
$user = User::create($userData);
$this->assertEquals('Jane Doe', $user->name);
$this->assertEquals('jane@example.com', $user->email);
$this->assertTrue(Hash::check('password123', $user->password));
}
/**
* Test user hidden attributes.
*/
public function test_user_hidden_attributes()
{
$user = User::factory()->create([
'password' => Hash::make('secret123'),
]);
$userArray = $user->toArray();
$this->assertArrayNotHasKey('password', $userArray);
$this->assertArrayNotHasKey('remember_token', $userArray);
}
/**
* Test user casts.
*/
public function test_user_casts()
{
$user = User::factory()->create([
'email_verified_at' => now(),
]);
$this->assertInstanceOf(\Illuminate\Support\Carbon::class, $user->email_verified_at);
}
/**
* Test password is automatically hashed.
*/
public function test_password_is_automatically_hashed()
{
$user = User::factory()->create([
'password' => 'plaintext-password',
]);
$this->assertNotEquals('plaintext-password', $user->password);
$this->assertTrue(Hash::check('plaintext-password', $user->password));
}
/**
* Test user has API tokens trait.
*/
public function test_user_can_create_api_tokens()
{
$user = User::factory()->create();
$token = $user->createToken('test-token');
$this->assertInstanceOf(PersonalAccessToken::class, $token->accessToken);
$this->assertIsString($token->plainTextToken);
$this->assertEquals('test-token', $token->accessToken->name);
}
/**
* Test user can have multiple tokens.
*/
public function test_user_can_have_multiple_tokens()
{
$user = User::factory()->create();
$token1 = $user->createToken('token-1');
$token2 = $user->createToken('token-2');
$this->assertCount(2, $user->tokens);
$this->assertNotEquals($token1->plainTextToken, $token2->plainTextToken);
}
/**
* Test user can delete tokens.
*/
public function test_user_can_delete_tokens()
{
$user = User::factory()->create();
$token = $user->createToken('test-token');
$this->assertCount(1, $user->tokens);
$token->accessToken->delete();
$user->refresh();
$this->assertCount(0, $user->tokens);
}
/**
* Test user has trips relationship.
*/
public function test_user_has_trips_relationship()
{
$user = User::factory()->create();
// Create some trips for this user
Trip::factory()->count(3)->create([
'created_by_user_id' => $user->id,
]);
// Create a trip for another user
$otherUser = User::factory()->create();
Trip::factory()->create([
'created_by_user_id' => $otherUser->id,
]);
$this->assertCount(3, $user->trips);
$this->assertInstanceOf(Trip::class, $user->trips->first());
}
/**
* Test user factory creates valid users.
*/
public function test_user_factory_creates_valid_users()
{
$user = User::factory()->create();
$this->assertNotEmpty($user->name);
$this->assertNotEmpty($user->email);
$this->assertNotEmpty($user->password);
$this->assertNotNull($user->email_verified_at);
$this->assertTrue(filter_var($user->email, FILTER_VALIDATE_EMAIL) !== false);
}
/**
* Test user factory can create unverified users.
*/
public function test_user_factory_can_create_unverified_users()
{
$user = User::factory()->unverified()->create();
$this->assertNull($user->email_verified_at);
}
/**
* Test user email must be unique.
*/
public function test_user_email_must_be_unique()
{
User::factory()->create(['email' => 'test@example.com']);
$this->expectException(\Illuminate\Database\QueryException::class);
User::factory()->create(['email' => 'test@example.com']);
}
/**
* Test user model uses correct table.
*/
public function test_user_uses_correct_table()
{
$user = new User();
$this->assertEquals('users', $user->getTable());
}
/**
* Test user model has correct primary key.
*/
public function test_user_has_correct_primary_key()
{
$user = new User();
$this->assertEquals('id', $user->getKeyName());
$this->assertTrue($user->getIncrementing());
}
/**
* Test user model uses timestamps.
*/
public function test_user_uses_timestamps()
{
$user = new User();
$this->assertTrue($user->usesTimestamps());
}
}