Skip to content

Commit

Permalink
unit tests for CustomUser and Category
Browse files Browse the repository at this point in the history
  • Loading branch information
QuitoTactico committed Nov 19, 2024
1 parent aed56b5 commit aa6aaa5
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/Unit/CategoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Tests\Unit;

use App\Models\Category;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;

class CategoryTest extends TestCase
{
use RefreshDatabase;

#[Test]
public function it_can_create_a_category(): void
{
$category = Category::create([
'name' => 'Test Category',
'description' => 'This is a test category.',
]);

$this->assertDatabaseHas('categories', [
'name' => 'Test Category',
'description' => 'This is a test category.',
]);
}

#[Test]
public function it_requires_a_name_to_create_a_category(): void
{
$this->expectException(QueryException::class);

Category::create([
'description' => 'This is a test category.',
]);
}

#[Test]
public function it_requires_a_description_to_create_a_category(): void
{
$this->expectException(QueryException::class);

Category::create([
'name' => 'Test Category',
]);
}
}
48 changes: 48 additions & 0 deletions tests/Unit/CustomUserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Tests\Unit;

use App\Models\CustomUser;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;

class CustomUserTest extends TestCase
{
use RefreshDatabase;

#[Test]
public function it_can_create_a_user(): void
{
$user = CustomUser::factory()->create([
'name' => 'John Doe',
'email' => '[email protected]',
'password' => bcrypt('password'),
'is_admin' => false,
]);

$this->assertDatabaseHas('custom_users', [
'email' => '[email protected]',
]);
}

#[Test]
public function it_can_check_if_user_is_admin(): void
{
$user = CustomUser::factory()->create([
'is_admin' => true,
]);

$this->assertTrue($user->getIsAdmin());
}

#[Test]
public function it_can_check_if_user_is_not_admin(): void
{
$user = CustomUser::factory()->create([
'is_admin' => false,
]);

$this->assertFalse($user->getIsAdmin());
}
}

0 comments on commit aa6aaa5

Please sign in to comment.