This commit is contained in:
Adrian Hopek
2022-01-21 15:17:59 +01:00
parent a62a428781
commit 5a51f24342
23 changed files with 2492 additions and 183 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

@@ -8,11 +8,11 @@ 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;

21
tests/FeatureTestCase.php Normal file
View File

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

View File

@@ -0,0 +1,38 @@
<?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([]);
}
}

View File

@@ -7,12 +7,14 @@ 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 +56,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 +74,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,83 @@
<?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;
public function setUp(): void
{
parent::setUp();
$this->current = Carbon::now();
Carbon::setTestNow($this->current);
$this->yearPeriodRetriever = new YearPeriodRetriever();
$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 testRetrievesCurrentYearPeriodWhenNoSelected(): 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());
}
}