#23 - wip
This commit is contained in:
@@ -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;
|
||||
|
||||
|
@@ -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;
|
||||
|
||||
|
@@ -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
21
tests/FeatureTestCase.php
Normal 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();
|
||||
}
|
||||
}
|
38
tests/Traits/InteractsWithYearPeriods.php
Normal file
38
tests/Traits/InteractsWithYearPeriods.php
Normal 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([]);
|
||||
}
|
||||
}
|
@@ -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);
|
||||
|
||||
|
83
tests/Unit/YearPeriodRetrieverTest.php
Normal file
83
tests/Unit/YearPeriodRetrieverTest.php
Normal 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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user