Skip to content

Commit

Permalink
#335 - refactor deprecated rule contract (#365)
Browse files Browse the repository at this point in the history
* #335 - wip

* #335 - wip
  • Loading branch information
EwelinaSkrzypacz authored Oct 20, 2023
1 parent 4cc2f52 commit 8e3e751
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 18 deletions.
16 changes: 7 additions & 9 deletions app/Infrastructure/Http/Rules/YearPeriodExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@

namespace Toby\Infrastructure\Http\Rules;

use Illuminate\Contracts\Validation\Rule;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Carbon;
use Toby\Eloquent\Models\YearPeriod;

class YearPeriodExists implements Rule
class YearPeriodExists implements ValidationRule
{
public function passes($attribute, $value): bool
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$yearPeriod = YearPeriod::findByYear(Carbon::create($value)->year);

return $yearPeriod !== null;
}

public function message(): string
{
return __("The year period for given year does not exist.");
if ($yearPeriod === null) {
$fail(__("The year period for given year does not exist."));
}
}
}
16 changes: 7 additions & 9 deletions app/Infrastructure/Slack/Rules/SlackUserExistsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@

namespace Toby\Infrastructure\Slack\Rules;

use Illuminate\Contracts\Validation\Rule;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Str;
use Toby\Eloquent\Models\Profile;

class SlackUserExistsRule implements Rule
class SlackUserExistsRule implements ValidationRule
{
public function passes($attribute, $value): bool
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$slackId = Str::between($value, "<@", "|");

return Profile::query()->where("slack_id", $slackId)->exists();
}

public function message(): string
{
return __("User :input does not exist in toby");
if (!Profile::query()->where("slack_id", $slackId)->exists()) {
$fail(__("User :input does not exist in toby"));
}
}
}
56 changes: 56 additions & 0 deletions tests/Unit/SlackUserExistsRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Tests\Unit;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use Toby\Eloquent\Models\User;
use Toby\Infrastructure\Slack\Rules\SlackUserExistsRule;

class SlackUserExistsRuleTest extends TestCase
{
use RefreshDatabase;

protected SlackUserExistsRule $rule;

protected function setUp(): void
{
parent::setUp();

$this->rule = $this->app->make(SlackUserExistsRule::class);
}

public function testItPassesIfSlackUserExists(): void
{
$slackId = "U03AZBA3RKL";
$user = User::factory()->hasProfile(["slack_id" => $slackId])->create();

$fail = false;

$this->rule->validate("user", $slackId, function () use (&$fail): void {
$fail = true;
});

$this->assertFalse($fail, "User $user->name does not exist in toby");
}

public function testItFailsIfSlackUserDoesNotExist(): void
{
$user = User::factory()->hasProfile()->create();

$invalidValues = [
"U03AZBA3RKL",
null,
];

foreach ($invalidValues as $value) {
$fail = false;
$this->rule->validate("user", null, function () use (&$fail): void {
$fail = true;
});
$this->assertTrue($fail, "User $user->name does not exist in toby");
}
}
}
61 changes: 61 additions & 0 deletions tests/Unit/YearPeriodExistsRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Tests\Unit;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Tests\TestCase;
use Tests\Traits\InteractsWithYearPeriods;
use Toby\Infrastructure\Http\Rules\YearPeriodExists;

class YearPeriodExistsRuleTest extends TestCase
{
use RefreshDatabase;
use InteractsWithYearPeriods;

protected YearPeriodExists $rule;

protected function setUp(): void
{
parent::setUp();

$this->rule = $this->app->make(YearPeriodExists::class);
}

public function testItPassesIfYearPeriodExists(): void
{
$now = Carbon::now();
Carbon::setTestNow($now);

$this->createCurrentYearPeriod();

$fail = false;
$value = $now->year;

$this->rule->validate("year", $value, function () use (&$fail): void {
$fail = true;
});
$this->assertFalse($fail, "The year period for given year does not exist.");
}

public function testItFailsIfYearPeriodDoesNotExist(): void
{
$now = Carbon::now();
Carbon::setTestNow($now);

$invalidValues = [
$now->year,
null,
];

foreach ($invalidValues as $value) {
$fail = false;
$this->rule->validate("year", $value, static function () use (&$fail): void {
$fail = true;
});
$this->assertTrue($fail, "The year period for given year does not exist.");
}
}
}

0 comments on commit 8e3e751

Please sign in to comment.