Files
animethemes-server/tests/Unit/Rules/Api/IsValidBooleanTest.php
T
paranarimasuandGitHub 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

127 lines
3.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Rules\Api;
use App\Rules\Api\IsValidBoolean;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Validator;
use Tests\TestCase;
/**
* IsValidBooleanTest.
*/
class IsValidBooleanTest extends TestCase
{
use WithFaker;
/**
* The Is Valid Boolean Rule shall return true if a boolean is provided.
*
* @return void
*/
public function testPassesIfBoolean(): void
{
$attribute = $this->faker->word();
$validator = Validator::make(
[$attribute => $this->faker->boolean()],
[$attribute => new IsValidBoolean()]
);
static::assertTrue($validator->passes());
}
/**
* The Is Valid Boolean Rule shall return true if "true" or "false" is provided.
*
* @return void
*/
public function testPassesIfBooleanString(): void
{
$booleanString = $this->faker->boolean() ? 'true' : 'false';
$attribute = $this->faker->word();
$validator = Validator::make(
[$attribute => $booleanString],
[$attribute => new IsValidBoolean()]
);
static::assertTrue($validator->passes());
}
/**
* The Is Valid Boolean Rule shall return true if 1 or 0 is provided.
*
* @return void
*/
public function testPassesIfBooleanInteger(): void
{
$booleanInteger = $this->faker->boolean() ? 1 : 0;
$attribute = $this->faker->word();
$validator = Validator::make(
[$attribute => $booleanInteger],
[$attribute => new IsValidBoolean()]
);
static::assertTrue($validator->passes());
}
/**
* The Is Valid Boolean Rule shall return true if "on" or "off" is provided.
*
* @return void
*/
public function testPassesIfBooleanCheckbox(): void
{
$booleanCheckbox = $this->faker->boolean() ? 'on' : 'off';
$attribute = $this->faker->word();
$validator = Validator::make(
[$attribute => $booleanCheckbox],
[$attribute => new IsValidBoolean()]
);
static::assertTrue($validator->passes());
}
/**
* The Is Valid Boolean Rule shall return false a string is provided.
*
* @return void
*/
public function testFailsIfString(): void
{
$attribute = $this->faker->word();
$validator = Validator::make(
[$attribute => $this->faker->word()],
[$attribute => new IsValidBoolean()]
);
static::assertFalse($validator->passes());
}
/**
* The Is Valid Boolean Rule shall return false a number is provided.
*
* @return void
*/
public function testFailsIfNumber(): void
{
$attribute = $this->faker->word();
$validator = Validator::make(
[$attribute => $this->faker->numberBetween(2, 9)],
[$attribute => new IsValidBoolean()]
);
static::assertFalse($validator->passes());
}
}