#23 - collective editing vacation days (#26)

* #23 - wip

* #23 - wip

* #23 - wip

* #23 - wip

* #23 - fix

* #23 - ecs fix

* #23 - fix

* #23 - fix

* #23 - cr fix
This commit is contained in:
Adrian Hopek
2022-01-24 11:28:00 +01:00
committed by GitHub
parent 652587dbf1
commit e147d24365
35 changed files with 1000 additions and 41 deletions

View File

@@ -5,10 +5,10 @@ declare(strict_types=1);
namespace Tests\Feature;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
use Tests\FeatureTestCase;
use Toby\Models\User;
class AuthenticationTest extends TestCase
class AuthenticationTest extends FeatureTestCase
{
use DatabaseMigrations;

View File

@@ -6,10 +6,10 @@ namespace Tests\Feature;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\TestCase;
use Tests\FeatureTestCase;
use Toby\Models\User;
class InertiaTest extends TestCase
class InertiaTest extends FeatureTestCase
{
use DatabaseMigrations;

View File

@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Carbon;
use Tests\FeatureTestCase;
use Toby\Helpers\YearPeriodRetriever;
use Toby\Models\User;
class SelectYearPeriodTest extends FeatureTestCase
{
use DatabaseMigrations;
protected YearPeriodRetriever $yearPeriodRetriever;
protected function setUp(): void
{
parent::setUp();
$this->yearPeriodRetriever = $this->app->make(YearPeriodRetriever::class);
}
public function testUserCanSelectNextYearPeriod(): void
{
$nextYearPeriod = $this->createYearPeriod(Carbon::now()->year + 1);
$user = User::factory()->create();
$this->actingAs($user)
->post("/year-periods/{$nextYearPeriod->id}/select")
->assertRedirect();
$this->assertSame($nextYearPeriod->id, $this->yearPeriodRetriever->selected()->id);
}
public function testUserCannotSelectNextYearPeriodIfDoesntExist(): void
{
$user = User::factory()->create();
$this->actingAs($user)
->post("/year-periods/25/select")
->assertNotFound();
}
public function testIfUserDoesntSelectAnyYearPeriodCurrentActsAsSelected(): void
{
$currentYearPeriod = $this->yearPeriodRetriever->current();
$this->assertSame($currentYearPeriod->id, $this->yearPeriodRetriever->selected()->id);
}
}

View File

@@ -6,23 +6,15 @@ namespace Tests\Feature;
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\FeatureTestCase;
use Toby\Enums\EmploymentForm;
use Toby\Models\User;
class UserTest extends TestCase
class UserTest extends FeatureTestCase
{
use DatabaseMigrations;
protected function setUp(): void
{
parent::setUp();
Storage::fake();
}
public function testAdminCanSeeUsersList(): void
{
User::factory()->count(10)->create();

View File

@@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\FeatureTestCase;
use Toby\Models\User;
use Toby\Models\VacationLimit;
class VacationLimitTest extends FeatureTestCase
{
use DatabaseMigrations;
public function testAdminCanSeeVacationLimits(): void
{
$admin = User::factory()->createQuietly();
User::factory(10)->create();
$this->actingAs($admin)
->get("/vacation-limits")
->assertOk()
->assertInertia(
fn(Assert $page) => $page
->component("VacationLimits")
->has("limits.data", 10),
);
}
public function testAdminCanUpdateVacationLimits(): void
{
$admin = User::factory()->createQuietly();
User::factory(3)->create();
[$limit1, $limit2, $limit3] = VacationLimit::all();
$data = [
[
"id" => $limit1->id,
"days" => 25,
],
[
"id" => $limit2->id,
"days" => null,
],
[
"id" => $limit3->id,
"days" => 20,
],
];
$this->actingAs($admin)
->put("/vacation-limits", [
"items" => $data,
])
->assertRedirect();
$this->assertDatabaseHas("vacation_limits", [
"id" => $limit1->id,
"days" => 25,
]);
$this->assertDatabaseHas("vacation_limits", [
"id" => $limit2->id,
"days" => null,
]);
$this->assertDatabaseHas("vacation_limits", [
"id" => $limit3->id,
"days" => 20,
]);
}
}

23
tests/FeatureTestCase.php Normal file
View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Carbon;
use Tests\Traits\InteractsWithYearPeriods;
abstract class FeatureTestCase extends BaseTestCase
{
use CreatesApplication;
use InteractsWithYearPeriods;
protected function setUp(): void
{
parent::setUp();
Carbon::setTestNow(Carbon::now());
$this->createCurrentYearPeriod();
}
}

View File

@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace Tests\Traits;
use Illuminate\Foundation\Testing\Concerns\InteractsWithSession;
use Illuminate\Support\Carbon;
use Toby\Helpers\YearPeriodRetriever;
use Toby\Models\YearPeriod;
trait InteractsWithYearPeriods
{
use InteractsWithSession;
public function createYearPeriod(int $year): YearPeriod
{
/** @var YearPeriod $yearPeriod */
$yearPeriod = YearPeriod::factory()->create([
"year" => $year,
]);
return $yearPeriod;
}
public function createCurrentYearPeriod(): YearPeriod
{
return $this->createYearPeriod(Carbon::now()->year);
}
public function markYearPeriodAsSelected(YearPeriod $yearPeriod): void
{
$this->session([
YearPeriodRetriever::SESSION_KEY => $yearPeriod->id,
]);
}
public function clearSelectedYearPeriod(): void
{
$this->session([]);
}
public function cleanYearPeriods(): void
{
$this->clearSelectedYearPeriod();
YearPeriod::query()->delete();
}
}

View File

@@ -7,16 +7,19 @@ namespace Tests\Unit;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
use Tests\Traits\InteractsWithYearPeriods;
use Toby\Models\User;
class AvatarTest extends TestCase
{
use DatabaseMigrations;
use InteractsWithYearPeriods;
protected function setUp(): void
{
parent::setUp();
$this->createCurrentYearPeriod();
Storage::fake();
}

View File

@@ -7,12 +7,13 @@ namespace Tests\Unit;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Tests\TestCase;
use Tests\Traits\InteractsWithYearPeriods;
use Toby\Jobs\CheckYearPeriod;
use Toby\Models\YearPeriod;
class CheckYearPeriodTest extends TestCase
{
use RefreshDatabase;
use InteractsWithYearPeriods;
public function testYearPeriodsAreCreatedWhenDontExist(): void
{
@@ -54,9 +55,7 @@ class CheckYearPeriodTest extends TestCase
$now = Carbon::now();
Carbon::setTestNow($now);
YearPeriod::factory([
"year" => $now->year,
]);
$this->createCurrentYearPeriod();
$this->assertDatabaseMissing("year_periods", [
"year" => $now->year + 1,
@@ -74,12 +73,8 @@ class CheckYearPeriodTest extends TestCase
$now = Carbon::now();
Carbon::setTestNow($now);
YearPeriod::factory([
"year" => $now->year,
])->create();
YearPeriod::factory([
"year" => $now->year + 1,
])->create();
$this->createCurrentYearPeriod();
$this->createYearPeriod($now->year + 1);
$this->assertDatabaseCount("year_periods", 2);

View File

@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
use Tests\Traits\InteractsWithYearPeriods;
use Toby\Models\User;
use Toby\Models\YearPeriod;
class VacationLimitTest extends TestCase
{
use DatabaseMigrations;
use InteractsWithYearPeriods;
protected function setUp(): void
{
parent::setUp();
$this->createCurrentYearPeriod();
}
public function testWhenUserIsCreatedThenVacationLimitIsCreatedForCurrentYearPeriod(): void
{
$this->assertDatabaseCount("vacation_limits", 0);
$currentYearPeriod = YearPeriod::current();
$user = User::factory()->create();
$this->assertDatabaseCount("vacation_limits", 1);
$this->assertDatabaseHas("vacation_limits", [
"user_id" => $user->id,
"year_period_id" => $currentYearPeriod->id,
]);
}
public function testWhenYearPeriodIsCreatedThenVacationLimitsForThisYearPeriodAreCreated(): void
{
$this->assertDatabaseCount("vacation_limits", 0);
User::factory(10)->createQuietly();
YearPeriod::factory()->create();
$this->assertDatabaseCount("vacation_limits", 10);
}
}

View File

@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use Illuminate\Foundation\Testing\Concerns\InteractsWithSession;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Tests\TestCase;
use Tests\Traits\InteractsWithYearPeriods;
use Toby\Helpers\YearPeriodRetriever;
use Toby\Models\YearPeriod;
class YearPeriodRetrieverTest extends TestCase
{
use RefreshDatabase;
use InteractsWithSession;
use InteractsWithYearPeriods;
public Carbon $current;
public YearPeriod $previousYearPeriod;
public YearPeriod $currentYearPeriod;
public YearPeriod $nextYearPeriod;
public YearPeriodRetriever $yearPeriodRetriever;
protected function setUp(): void
{
parent::setUp();
$this->current = Carbon::now();
Carbon::setTestNow($this->current);
$this->yearPeriodRetriever = $this->app->make(YearPeriodRetriever::class);
$this->previousYearPeriod = $this->createYearPeriod($this->current->year - 1);
$this->currentYearPeriod = $this->createCurrentYearPeriod();
$this->nextYearPeriod = $this->createYearPeriod($this->current->year + 1);
}
public function testRetrievesCorrectCurrentYearPeriod(): void
{
$this->assertSame($this->currentYearPeriod->id, $this->yearPeriodRetriever->current()->id);
}
public function testRetrievesCurrentYearPeriodWhenNoneIsSelected(): void
{
$this->clearSelectedYearPeriod();
$this->assertSame($this->currentYearPeriod->id, $this->yearPeriodRetriever->selected()->id);
}
public function testRetrievesCorrectYearPeriodWhenSelected(): void
{
$this->markYearPeriodAsSelected($this->nextYearPeriod);
$this->assertSame($this->nextYearPeriod->id, $this->yearPeriodRetriever->selected()->id);
}
public function testLinks(): void
{
$expected = [
"current" => $this->current->year,
"navigation" => [
[
"year" => $this->previousYearPeriod->year,
"link" => route("year-periods.select", $this->previousYearPeriod),
],
[
"year" => $this->currentYearPeriod->year,
"link" => route("year-periods.select", $this->currentYearPeriod),
],
[
"year" => $this->nextYearPeriod->year,
"link" => route("year-periods.select", $this->nextYearPeriod),
],
],
];
$this->assertSame($expected, $this->yearPeriodRetriever->links());
}
}