#19 - year periods (#25)

* #19 - year periods

* #19 - fix
This commit is contained in:
Adrian Hopek 2022-01-19 11:00:17 +01:00 committed by GitHub
parent 9aa2d5ec0f
commit 652587dbf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 228 additions and 0 deletions

View File

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Toby\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Carbon;
use Toby\Models\YearPeriod;
class CheckYearPeriod implements ShouldQueue
{
use Dispatchable;
use Queueable;
public function handle(): void
{
$currentYearPeriod = YearPeriod::current();
if ($currentYearPeriod === null) {
$this->createCurrentYearPeriod();
}
if (YearPeriod::query()->max("year") === Carbon::now()->year) {
$this->createNextYearPeriod();
}
}
protected function createCurrentYearPeriod(): void
{
YearPeriod::query()->create([
"year" => Carbon::now()->year,
]);
}
protected function createNextYearPeriod(): void
{
YearPeriod::query()->create([
"year" => Carbon::now()->year + 1,
]);
}
}

30
app/Models/YearPeriod.php Normal file
View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Toby\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property int $year
*/
class YearPeriod extends Model
{
use HasFactory;
protected $fillable = [
"year",
];
public static function current(): ?static
{
/** @var YearPeriod $year */
$year = static::query()->where("year", Carbon::now()->year)->first();
return $year;
}
}

View File

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
class YearPeriodFactory extends Factory
{
public function definition(): array
{
return [
"year" => $this->faker->unique()->year,
];
}
}

View File

@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class() extends Migration {
public function up(): void
{
Schema::create("year_periods", function (Blueprint $table): void {
$table->id();
$table->integer("year")->unique()->index();
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists("year_periods");
}
};

View File

@ -5,7 +5,9 @@ declare(strict_types=1);
namespace Database\Seeders; namespace Database\Seeders;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
use Illuminate\Support\Carbon;
use Toby\Models\User; use Toby\Models\User;
use Toby\Models\YearPeriod;
class DatabaseSeeder extends Seeder class DatabaseSeeder extends Seeder
{ {
@ -15,5 +17,12 @@ class DatabaseSeeder extends Seeder
User::factory([ User::factory([
"email" => env("LOCAL_EMAIL_FOR_LOGIN_VIA_GOOGLE"), "email" => env("LOCAL_EMAIL_FOR_LOGIN_VIA_GOOGLE"),
])->create(); ])->create();
YearPeriod::factory([
"year" => Carbon::now()->year,
])->create();
YearPeriod::factory([
"year" => Carbon::now()->year + 1,
])->create();
} }
} }

View File

@ -0,0 +1,105 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Tests\TestCase;
use Toby\Jobs\CheckYearPeriod;
use Toby\Models\YearPeriod;
class CheckYearPeriodTest extends TestCase
{
use RefreshDatabase;
public function testYearPeriodsAreCreatedWhenDontExist(): void
{
$now = Carbon::now();
Carbon::setTestNow($now);
CheckYearPeriod::dispatchSync();
$this->assertDatabaseCount("year_periods", 2);
$this->assertDatabaseHas("year_periods", [
"year" => $now->year,
]);
$this->assertDatabaseHas("year_periods", [
"year" => $now->year + 1,
]);
}
public function testCurrentYearPeriodIsCreatedWhenDoesntExist(): void
{
$now = Carbon::now();
Carbon::setTestNow($now);
$this->assertDatabaseMissing("year_periods", [
"year" => $now->year,
]);
CheckYearPeriod::dispatchSync();
$this->assertDatabaseHas("year_periods", [
"year" => $now->year,
]);
}
public function testNextYearPeriodIsCreatedWhenDoesntExist(): void
{
$now = Carbon::now();
Carbon::setTestNow($now);
YearPeriod::factory([
"year" => $now->year,
]);
$this->assertDatabaseMissing("year_periods", [
"year" => $now->year + 1,
]);
CheckYearPeriod::dispatchSync();
$this->assertDatabaseHas("year_periods", [
"year" => $now->year + 1,
]);
}
public function testYearPeriodsAreNotCreatedWhenExists(): void
{
$now = Carbon::now();
Carbon::setTestNow($now);
YearPeriod::factory([
"year" => $now->year,
])->create();
YearPeriod::factory([
"year" => $now->year + 1,
])->create();
$this->assertDatabaseCount("year_periods", 2);
CheckYearPeriod::dispatchSync();
$this->assertDatabaseCount("year_periods", 2);
}
public function testYearPeriodsAreNotDuplicatedWhenJobCalledMultipleTimes(): void
{
$now = Carbon::now();
Carbon::setTestNow($now);
CheckYearPeriod::dispatchSync();
$this->assertDatabaseCount("year_periods", 2);
CheckYearPeriod::dispatchSync();
CheckYearPeriod::dispatchSync();
CheckYearPeriod::dispatchSync();
$this->assertDatabaseCount("year_periods", 2);
}
}