From 04aede3893412f58dc7b7b157f6ba990ac33884a Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sun, 28 Mar 2021 20:05:23 -0700 Subject: [PATCH] Add tests for custom rules --- tests/Unit/Rules/ArrayNotEmptyTest.php | 25 +++++++ tests/Unit/Rules/InArrayTest.php | 25 +++++++ tests/Unit/Rules/RulesTestCase.php | 27 +++++++ .../Rules/StringIsDecimalOrFractionTest.php | 71 +++++++++++++++++++ tests/Unit/Rules/UsesIngredientTraitTest.php | 24 +++++++ 5 files changed, 172 insertions(+) create mode 100644 tests/Unit/Rules/ArrayNotEmptyTest.php create mode 100644 tests/Unit/Rules/InArrayTest.php create mode 100644 tests/Unit/Rules/RulesTestCase.php create mode 100644 tests/Unit/Rules/StringIsDecimalOrFractionTest.php create mode 100644 tests/Unit/Rules/UsesIngredientTraitTest.php diff --git a/tests/Unit/Rules/ArrayNotEmptyTest.php b/tests/Unit/Rules/ArrayNotEmptyTest.php new file mode 100644 index 0000000..cad3560 --- /dev/null +++ b/tests/Unit/Rules/ArrayNotEmptyTest.php @@ -0,0 +1,25 @@ +validator->setRules([new ArrayNotEmpty()]); + $this->validator->setData([['item']]); + $this->assertTrue($this->validator->passes()); + $this->validator->setData([[]]); + $this->assertFalse($this->validator->passes()); + } + +} diff --git a/tests/Unit/Rules/InArrayTest.php b/tests/Unit/Rules/InArrayTest.php new file mode 100644 index 0000000..6b907e4 --- /dev/null +++ b/tests/Unit/Rules/InArrayTest.php @@ -0,0 +1,25 @@ +validator->setRules([new InArray(['item'])]); + $this->validator->setData(['item']); + $this->assertTrue($this->validator->passes()); + $this->validator->setData(['not item']); + $this->assertFalse($this->validator->passes()); + } + +} diff --git a/tests/Unit/Rules/RulesTestCase.php b/tests/Unit/Rules/RulesTestCase.php new file mode 100644 index 0000000..c02e1c3 --- /dev/null +++ b/tests/Unit/Rules/RulesTestCase.php @@ -0,0 +1,27 @@ +validator = new Validator($translator, [], []); + } + +} diff --git a/tests/Unit/Rules/StringIsDecimalOrFractionTest.php b/tests/Unit/Rules/StringIsDecimalOrFractionTest.php new file mode 100644 index 0000000..09863ee --- /dev/null +++ b/tests/Unit/Rules/StringIsDecimalOrFractionTest.php @@ -0,0 +1,71 @@ +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] + ]; + } + +} diff --git a/tests/Unit/Rules/UsesIngredientTraitTest.php b/tests/Unit/Rules/UsesIngredientTraitTest.php new file mode 100644 index 0000000..db6abc6 --- /dev/null +++ b/tests/Unit/Rules/UsesIngredientTraitTest.php @@ -0,0 +1,24 @@ +validator->setRules([new UsesIngredientTrait()]); + $this->validator->setData([Food::class]); + $this->assertTrue($this->validator->passes()); + $this->validator->setData([User::class]); + $this->assertFalse($this->validator->passes()); + } +}