Files
animethemes-server/tests/Unit/Rules/Wiki/Resource/AnimeResourceLinkFormatTest.php
T
paranarimasu fd2cebe3cf chore: address deprecations to validation rules in Laravel 10 (#584)
* chore: address deprecations to validation rules in Laravel 10

* style: fix StyleCI findings
2023-06-05 01:01:49 -05:00

189 lines
5.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Rules\Wiki\Resource;
use App\Enums\Models\Wiki\ResourceSite;
use App\Rules\Wiki\Resource\AnimeResourceLinkFormatRule;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use Tests\TestCase;
/**
* Class AnimeResourceLinkFormatRule.
*/
class AnimeResourceLinkFormatTest extends TestCase
{
use WithFaker;
/**
* The Anime Resource Link Format Rule shall pass for sites with no expected pattern.
*
* @return void
*/
public function testPassesForNoPattern(): void
{
$attribute = $this->faker->word();
$validator = Validator::make(
[$attribute => $this->faker->url()],
[$attribute => new AnimeResourceLinkFormatRule(ResourceSite::OFFICIAL_SITE())],
);
static::assertTrue($validator->passes());
}
/**
* The Anime Resource Link Format Rule shall pass for URLs that match the expected pattern.
*
* @return void
*/
public function testPassesForPattern(): void
{
/** @var ResourceSite $site */
$site = Arr::random([
ResourceSite::TWITTER(),
ResourceSite::ANIDB(),
ResourceSite::ANILIST(),
ResourceSite::ANIME_PLANET(),
ResourceSite::ANN(),
ResourceSite::KITSU(),
ResourceSite::MAL(),
]);
$url = $site->formatAnimeResourceLink($this->faker->randomDigitNotNull(), $this->faker->word());
$attribute = $this->faker->word();
$validator = Validator::make(
[$attribute => $url],
[$attribute => new AnimeResourceLinkFormatRule($site)],
);
static::assertTrue($validator->passes());
}
/**
* The Anime Resource Link Format Rule shall fail for trailing slashes in URLs with defined patterns.
*
* @return void
*/
public function testFailsForTrailingSlash(): void
{
/** @var ResourceSite $site */
$site = Arr::random([
ResourceSite::TWITTER(),
ResourceSite::ANIDB(),
ResourceSite::ANILIST(),
ResourceSite::ANIME_PLANET(),
ResourceSite::ANN(),
ResourceSite::KITSU(),
ResourceSite::MAL(),
]);
$url = $site->formatAnimeResourceLink($this->faker->randomDigitNotNull(), $this->faker->word());
$url = Str::of($url)
->append('/')
->__toString();
$attribute = $this->faker->word();
$validator = Validator::make(
[$attribute => $url],
[$attribute => new AnimeResourceLinkFormatRule($site)],
);
static::assertFalse($validator->passes());
}
/**
* The Anime Resource Link Format Rule shall fail for trailing slugs in URLs with defined patterns.
*
* @return void
*/
public function testFailsForTrailingSlug(): void
{
/** @var ResourceSite $site */
$site = Arr::random([
ResourceSite::ANILIST(),
ResourceSite::MAL(),
]);
$url = $site->formatAnimeResourceLink($this->faker->randomDigitNotNull(), $this->faker->word());
$url = Str::of($url)
->append('/')
->append($this->faker->word())
->__toString();
$attribute = $this->faker->word();
$validator = Validator::make(
[$attribute => $url],
[$attribute => new AnimeResourceLinkFormatRule($site)],
);
static::assertFalse($validator->passes());
}
/**
* The Anime Resource Link Format Rule shall fail for artist resources.
*
* @return void
*/
public function testFailsForArtistResource(): void
{
/** @var ResourceSite $site */
$site = Arr::random([
ResourceSite::ANIDB(),
ResourceSite::ANILIST(),
ResourceSite::ANIME_PLANET(),
ResourceSite::ANN(),
ResourceSite::MAL(),
]);
$url = $site->formatArtistResourceLink($this->faker->randomDigitNotNull(), $this->faker->word());
$attribute = $this->faker->word();
$validator = Validator::make(
[$attribute => $url],
[$attribute => new AnimeResourceLinkFormatRule($site)],
);
static::assertFalse($validator->passes());
}
/**
* The Anime Resource Link Format Rule shall fail for studio resources.
*
* @return void
*/
public function testFailsForStudioResource(): void
{
/** @var ResourceSite $site */
$site = Arr::random([
ResourceSite::ANIDB(),
ResourceSite::ANILIST(),
ResourceSite::ANIME_PLANET(),
ResourceSite::ANN(),
ResourceSite::MAL(),
]);
$url = $site->formatStudioResourceLink($this->faker->randomDigitNotNull(), $this->faker->word());
$attribute = $this->faker->word();
$validator = Validator::make(
[$attribute => $url],
[$attribute => new AnimeResourceLinkFormatRule($site)],
);
static::assertFalse($validator->passes());
}
}