parent
9aa2d5ec0f
commit
652587dbf1
44
app/Jobs/CheckYearPeriod.php
Normal file
44
app/Jobs/CheckYearPeriod.php
Normal 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
30
app/Models/YearPeriod.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
17
database/factories/YearPeriodFactory.php
Normal file
17
database/factories/YearPeriodFactory.php
Normal 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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -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");
|
||||||
|
}
|
||||||
|
};
|
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
105
tests/Unit/CheckYearPeriodTest.php
Normal file
105
tests/Unit/CheckYearPeriodTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user