-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unit tests for CustomUser and Category
- Loading branch information
1 parent
aed56b5
commit aa6aaa5
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |