#4 - tests
This commit is contained in:
parent
4ef131021c
commit
fe1d98a218
@ -5,19 +5,148 @@ declare(strict_types=1);
|
|||||||
namespace Tests\Feature;
|
namespace Tests\Feature;
|
||||||
|
|
||||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Inertia\Testing\AssertableInertia as Assert;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
use Toby\Enums\FormOfEmployment;
|
||||||
use Toby\Models\User;
|
use Toby\Models\User;
|
||||||
|
|
||||||
class UserTest extends TestCase
|
class UserTest extends TestCase
|
||||||
{
|
{
|
||||||
use DatabaseMigrations;
|
use DatabaseMigrations;
|
||||||
|
|
||||||
public function testUserCanSeeUsersList(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
Storage::fake();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAdminCanSeeUsersList(): void
|
||||||
|
{
|
||||||
|
User::factory()->count(10)->create();
|
||||||
|
$admin = User::factory()->create();
|
||||||
|
|
||||||
|
$this->assertDatabaseCount("users", 11);
|
||||||
|
|
||||||
|
$this->actingAs($admin)
|
||||||
|
->get("/users")
|
||||||
|
->assertInertia(fn(Assert $page) => $page
|
||||||
|
->component("Users/Index")
|
||||||
|
->has("users.data", 11)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAdminCanSearchUsersList(): void
|
||||||
|
{
|
||||||
|
User::factory(["name" => "Test User1"])->create();
|
||||||
|
User::factory(["name" => "Test User2"])->create();
|
||||||
|
User::factory(["name" => "Test User3"])->create();
|
||||||
|
$admin = User::factory(["name" => "John Doe"])->create();
|
||||||
|
|
||||||
|
$this->assertDatabaseCount("users", 4);
|
||||||
|
|
||||||
|
$this->actingAs($admin)
|
||||||
|
->get("/users?search=test")
|
||||||
|
->assertInertia(fn(Assert $page) => $page
|
||||||
|
->component("Users/Index")
|
||||||
|
->has("users.data", 3)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUserListIsPaginated(): void
|
||||||
|
{
|
||||||
|
User::factory()->count(15)->create();
|
||||||
|
$admin = User::factory()->create();
|
||||||
|
|
||||||
|
$this->assertDatabaseCount("users", 16);
|
||||||
|
|
||||||
|
$this->actingAs($admin)
|
||||||
|
->get("/users?page=2")
|
||||||
|
->assertInertia(fn(Assert $page) => $page
|
||||||
|
->component("Users/Index")
|
||||||
|
->has("users.data", 1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAdminCanCreateUser(): void
|
||||||
|
{
|
||||||
|
$admin = User::factory()->create();
|
||||||
|
Carbon::setTestNow(Carbon::now());
|
||||||
|
|
||||||
|
$this->actingAs($admin)
|
||||||
|
->post("/users", [
|
||||||
|
"name" => "John Doe",
|
||||||
|
"email" => "john.doe@example.com",
|
||||||
|
"employmentForm" => FormOfEmployment::B2B_CONTRACT->value,
|
||||||
|
"employmentDate" => Carbon::now()->toDateTimeString(),
|
||||||
|
])
|
||||||
|
->assertSessionHasNoErrors();
|
||||||
|
|
||||||
|
$this->assertDatabaseHas("users", [
|
||||||
|
"name" => "John Doe",
|
||||||
|
"email" => "john.doe@example.com",
|
||||||
|
"employment_form" => FormOfEmployment::B2B_CONTRACT->value,
|
||||||
|
"employment_date" => Carbon::now()->toDateTimeString(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAdminCanEditUser(): void
|
||||||
|
{
|
||||||
|
$admin = User::factory()->create();
|
||||||
$user = User::factory()->create();
|
$user = User::factory()->create();
|
||||||
|
|
||||||
$this->actingAs($user)
|
Carbon::setTestNow();
|
||||||
->get("/users")
|
|
||||||
->assertOk();
|
$this->assertDatabaseHas("users", [
|
||||||
|
"name" => $user->name,
|
||||||
|
"email" => $user->email,
|
||||||
|
"employment_form" => $user->employment_form->value,
|
||||||
|
"employment_date" => $user->employment_date->toDateTimeString(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->actingAs($admin)
|
||||||
|
->put("/users/{$user->id}", [
|
||||||
|
"name" => "John Doe",
|
||||||
|
"email" => "john.doe@example.com",
|
||||||
|
"employmentForm" => FormOfEmployment::B2B_CONTRACT->value,
|
||||||
|
"employmentDate" => Carbon::now()->toDateTimeString(),
|
||||||
|
])
|
||||||
|
->assertSessionHasNoErrors();
|
||||||
|
|
||||||
|
$this->assertDatabaseHas("users", [
|
||||||
|
"name" => "John Doe",
|
||||||
|
"email" => "john.doe@example.com",
|
||||||
|
"employment_form" => FormOfEmployment::B2B_CONTRACT->value,
|
||||||
|
"employment_date" => Carbon::now()->toDateTimeString(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAdminCanDeleteUser(): void
|
||||||
|
{
|
||||||
|
$admin = User::factory()->create();
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
$this->actingAs($admin)
|
||||||
|
->delete("/users/{$user->id}")
|
||||||
|
->assertSessionHasNoErrors();
|
||||||
|
|
||||||
|
$this->assertSoftDeleted($user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAdminCanRestoreUser(): void
|
||||||
|
{
|
||||||
|
$admin = User::factory()->create();
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$user->delete();
|
||||||
|
|
||||||
|
$this->assertSoftDeleted($user);
|
||||||
|
|
||||||
|
$this->actingAs($admin)
|
||||||
|
->post("/users/{$user->id}/restore")
|
||||||
|
->assertSessionHasNoErrors();
|
||||||
|
|
||||||
|
$this->assertNotSoftDeleted($user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
66
tests/Unit/AvatarTest.php
Normal file
66
tests/Unit/AvatarTest.php
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Unit;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Tests\TestCase;
|
||||||
|
use Toby\Models\User;
|
||||||
|
|
||||||
|
class AvatarTest extends TestCase
|
||||||
|
{
|
||||||
|
use DatabaseMigrations;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
Storage::fake();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAvatarIsGeneratedWhenUserIsCreated(): void
|
||||||
|
{
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
Storage::assertExists($user->avatar);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAvatarIsDeletedWhenUserIsForceDeleted(): void
|
||||||
|
{
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
Storage::assertExists($user->avatar);
|
||||||
|
|
||||||
|
$user->forceDelete();
|
||||||
|
|
||||||
|
Storage::assertMissing($user->avatar);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAvatarIsReplacedWhenUserChangedTheirName(): void
|
||||||
|
{
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$oldAvatar = $user->avatar;
|
||||||
|
|
||||||
|
Storage::assertExists($oldAvatar);
|
||||||
|
|
||||||
|
$user->update(["name" => "John Doe"]);
|
||||||
|
|
||||||
|
Storage::assertMissing($oldAvatar);
|
||||||
|
Storage::assertExists($user->avatar);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAvatarIsNotReplacedWhenUserChangedOtherData(): void
|
||||||
|
{
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$avatar = $user->avatar;
|
||||||
|
|
||||||
|
Storage::assertExists($avatar);
|
||||||
|
|
||||||
|
$user->update(["email" => "john.doe@example.com"]);
|
||||||
|
|
||||||
|
Storage::assertExists($avatar);
|
||||||
|
}
|
||||||
|
}
|
@ -1,15 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Tests\Unit;
|
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
|
||||||
|
|
||||||
class ExampleTest extends TestCase
|
|
||||||
{
|
|
||||||
public function testExample(): void
|
|
||||||
{
|
|
||||||
$this->assertTrue(true);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user