mirror of https://github.com/kcal-app/kcal.git
Add tests for custom rules
This commit is contained in:
parent
09c60f96f0
commit
04aede3893
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Tests\Unit\Rules;
|
||||
|
||||
use App\Rules\ArrayNotEmpty;
|
||||
|
||||
class ArrayNotEmptyTest extends RulesTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* Test array not empty rule.
|
||||
*
|
||||
* @see \App\Rules\ArrayNotEmpty
|
||||
*/
|
||||
public function testArrayNotEmptyRule(): void
|
||||
{
|
||||
$this->validator->setRules([new ArrayNotEmpty()]);
|
||||
$this->validator->setData([['item']]);
|
||||
$this->assertTrue($this->validator->passes());
|
||||
$this->validator->setData([[]]);
|
||||
$this->assertFalse($this->validator->passes());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Tests\Unit\Rules;
|
||||
|
||||
use App\Rules\InArray;
|
||||
|
||||
class InArrayTest extends RulesTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* Test in array rule.
|
||||
*
|
||||
* @see \App\Rules\InArray
|
||||
*/
|
||||
public function testInArrayRule(): void
|
||||
{
|
||||
$this->validator->setRules([new InArray(['item'])]);
|
||||
$this->validator->setData(['item']);
|
||||
$this->assertTrue($this->validator->passes());
|
||||
$this->validator->setData(['not item']);
|
||||
$this->assertFalse($this->validator->passes());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Rules;
|
||||
|
||||
use App\Support\ArrayFormat;
|
||||
use Illuminate\Translation\ArrayLoader;
|
||||
use Illuminate\Translation\Translator;
|
||||
use Illuminate\Validation\Validator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
abstract class RulesTestCase extends TestCase
|
||||
{
|
||||
|
||||
protected Validator $validator;
|
||||
|
||||
/**
|
||||
* @{inheritdoc}
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$loader = new ArrayLoader();
|
||||
$translator = new Translator($loader, 'en');
|
||||
$this->validator = new Validator($translator, [], []);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Tests\Unit\Rules;
|
||||
|
||||
use App\Rules\StringIsDecimalOrFraction;
|
||||
|
||||
class StringIsDecimalOrFractionTest extends RulesTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @{inheritdoc}
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->validator->setRules([new StringIsDecimalOrFraction()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test string is decimal or faction rule with failing values.
|
||||
*
|
||||
* @dataProvider invalidDecimalsAndFractions
|
||||
*
|
||||
* @see \App\Rules\InArray
|
||||
*/
|
||||
public function testStringIsDecimalOrFractionRuleFails(mixed $value): void
|
||||
{
|
||||
$this->validator->setData([$value]);
|
||||
$this->assertFalse($this->validator->passes());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test string is decimal or faction rule with passing values.
|
||||
*
|
||||
* @dataProvider validDecimalsAndFractions
|
||||
*
|
||||
* @see \App\Rules\InArray
|
||||
*/
|
||||
public function testStringIsDecimalOrFractionRulePasses(mixed $value): void
|
||||
{
|
||||
$this->validator->setData([$value]);
|
||||
$this->assertTrue($this->validator->passes());
|
||||
}
|
||||
|
||||
/**
|
||||
* Data providers.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Provide valid decimals or fractions
|
||||
*
|
||||
* @see \Tests\Unit\Rules\StringIsDecimalOrFractionTest::testStringIsDecimalOrFractionRule()
|
||||
*/
|
||||
public function invalidDecimalsAndFractions(): array {
|
||||
return [['0'], [0], ['string']];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide valid decimals or fractions
|
||||
*
|
||||
* @see \Tests\Unit\Rules\StringIsDecimalOrFractionTest::testStringIsDecimalOrFractionRule()
|
||||
*/
|
||||
public function validDecimalsAndFractions(): array {
|
||||
return [
|
||||
['0.5'], ['1'], ['1.25'], ['1 1/2'], ['2 2/3'],
|
||||
[1/3], [1], [2.5]
|
||||
];
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Rules;
|
||||
|
||||
use App\Models\Food;
|
||||
use App\Models\User;
|
||||
use App\Rules\UsesIngredientTrait;
|
||||
|
||||
class UsesIngredientTraitTest extends RulesTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* Test uses Ingredient trait rule.
|
||||
*
|
||||
* @see \App\Rules\UsesIngredientTrait
|
||||
*/
|
||||
public function testUsesIngredientTraitRule(): void {
|
||||
$this->validator->setRules([new UsesIngredientTrait()]);
|
||||
$this->validator->setData([Food::class]);
|
||||
$this->assertTrue($this->validator->passes());
|
||||
$this->validator->setData([User::class]);
|
||||
$this->assertFalse($this->validator->passes());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue