From e936f5f82afae5bacabc719e077b23b457ea820f Mon Sep 17 00:00:00 2001 From: Michele Benigni Date: Wed, 4 Jun 2025 17:39:41 +0200 Subject: [PATCH 1/6] commenti vari --- tests/unit/DecodersTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/unit/DecodersTest.php b/tests/unit/DecodersTest.php index c288c8a..f2bf2e6 100644 --- a/tests/unit/DecodersTest.php +++ b/tests/unit/DecodersTest.php @@ -23,20 +23,21 @@ public function testMap(): void /** @psalm-suppress UndefinedFunction */ $this ->forAll( - Generators::int() + Generators::int() //provare il decoder con molti interi casuali ) ->then(function (int $i) use ($decoder): void { $a = self::assertSuccessInstanceOf( DecodersTest\A::class, $decoder->decode($i) ); - self::assertSame($i, $a->getValue()); + self::assertSame($i, $a->getValue());//verifica che il valore dentro l'oggetto A sia uguale a $i }); } } namespace Tests\Facile\PhpCodec\DecodersTest; +//wrapper class usata per testare la trasformazione: prende un int e lo incapsula in un oggetto class A { private int $v; From c9748330ccabe32e38efef7703f2aa5003c32e33 Mon Sep 17 00:00:00 2001 From: Michele Benigni Date: Thu, 5 Jun 2025 09:44:38 +0200 Subject: [PATCH 2/6] new file Test --- tests/unit/StringDecoderTest.php | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/unit/StringDecoderTest.php diff --git a/tests/unit/StringDecoderTest.php b/tests/unit/StringDecoderTest.php new file mode 100644 index 0000000..b466d22 --- /dev/null +++ b/tests/unit/StringDecoderTest.php @@ -0,0 +1,35 @@ +validate('ciao', $context); + //è ciò che mi aspettavo? (Successo) + $this->assertInstanceOf(ValidationSuccess::class, $result); + } + + public function testInvalidValues(): void + { + $decoder = new StringDecoder(); + $context = new Context($decoder); + + foreach ([123, 3.14, true] as $input) { + $result = $decoder->validate($input, $context); + $this->assertNotInstanceOf(ValidationSuccess::class, $result); + } + } +} From 3fc6de81617c810d4f9ec0652e4ecef1fea730e2 Mon Sep 17 00:00:00 2001 From: Michele Benigni Date: Thu, 5 Jun 2025 12:23:27 +0200 Subject: [PATCH 3/6] fix String Decoder --- .../Internal/Primitives/StringDecoderTest.php | 46 +++++++++++++++++++ tests/unit/StringDecoderTest.php | 35 -------------- 2 files changed, 46 insertions(+), 35 deletions(-) create mode 100644 tests/unit/Internal/Primitives/StringDecoderTest.php delete mode 100644 tests/unit/StringDecoderTest.php diff --git a/tests/unit/Internal/Primitives/StringDecoderTest.php b/tests/unit/Internal/Primitives/StringDecoderTest.php new file mode 100644 index 0000000..856c527 --- /dev/null +++ b/tests/unit/Internal/Primitives/StringDecoderTest.php @@ -0,0 +1,46 @@ +decode('ciao'); + + $this->assertInstanceOf(ValidationSuccess::class, $result); + } + + /** + * @dataProvider invalidValuesProvider + */ + public function testInvalidValues($input): void + { + $decoder = new StringDecoder(); + $result = $decoder->decode($input); + $this->assertInstanceOf(ValidationFailures::class, $result); + } + + public static function invalidValuesProvider(): array + { + return [ + 'integer' => [123], + 'float' => [3.14], + 'boolean' => [true], + 'array' => [['not', 'a', 'string']], + 'object' => [new \stdClass()], + 'null' => [null], + ]; + } +} diff --git a/tests/unit/StringDecoderTest.php b/tests/unit/StringDecoderTest.php deleted file mode 100644 index b466d22..0000000 --- a/tests/unit/StringDecoderTest.php +++ /dev/null @@ -1,35 +0,0 @@ -validate('ciao', $context); - //è ciò che mi aspettavo? (Successo) - $this->assertInstanceOf(ValidationSuccess::class, $result); - } - - public function testInvalidValues(): void - { - $decoder = new StringDecoder(); - $context = new Context($decoder); - - foreach ([123, 3.14, true] as $input) { - $result = $decoder->validate($input, $context); - $this->assertNotInstanceOf(ValidationSuccess::class, $result); - } - } -} From 4cc5b40f9fd8cdef7e7fdb85d0636cdd1bdc6d8f Mon Sep 17 00:00:00 2001 From: Michele Benigni Date: Thu, 5 Jun 2025 17:13:43 +0200 Subject: [PATCH 4/6] Primitive Test aggiunti tutti --- .../Internal/Primitives/BoolDecoderTest.php | 55 +++++++++++++++++ .../Primitives/CallableDecoderTest.php | 60 +++++++++++++++++++ .../Internal/Primitives/FloatDecoderTest.php | 43 +++++++++++++ .../Internal/Primitives/IntDecoderTest.php | 44 ++++++++++++++ .../Internal/Primitives/MixedDecoderTest.php | 39 ++++++++++++ .../Internal/Primitives/NullDecoderTest.php | 46 ++++++++++++++ .../Primitives/UndefinedDecoderTest.php | 43 +++++++++++++ 7 files changed, 330 insertions(+) create mode 100644 tests/unit/Internal/Primitives/BoolDecoderTest.php create mode 100644 tests/unit/Internal/Primitives/CallableDecoderTest.php create mode 100644 tests/unit/Internal/Primitives/FloatDecoderTest.php create mode 100644 tests/unit/Internal/Primitives/IntDecoderTest.php create mode 100644 tests/unit/Internal/Primitives/MixedDecoderTest.php create mode 100644 tests/unit/Internal/Primitives/NullDecoderTest.php diff --git a/tests/unit/Internal/Primitives/BoolDecoderTest.php b/tests/unit/Internal/Primitives/BoolDecoderTest.php new file mode 100644 index 0000000..06d4437 --- /dev/null +++ b/tests/unit/Internal/Primitives/BoolDecoderTest.php @@ -0,0 +1,55 @@ +decode($input); + + $this->assertInstanceOf(ValidationSuccess::class, $result); + } + + /** + * @dataProvider invalidBoolProvider + */ + public function testInvalidBools(mixed $input): void + { + $decoder = new BoolDecoder(); + $result = $decoder->decode($input); + + $this->assertInstanceOf(ValidationFailures::class, $result); + } + + public static function validBoolProvider(): array + { + return [ + 'true' => [true], + 'false' => [false], + ]; + } + + public static function invalidBoolProvider(): array + { + return [ + 'int' => [1], + 'string true' => ['true'], + 'string false' => ['false'], + 'null' => [null], + 'float' => [1.0], + 'array' => [[true]], + ]; + } +} diff --git a/tests/unit/Internal/Primitives/CallableDecoderTest.php b/tests/unit/Internal/Primitives/CallableDecoderTest.php new file mode 100644 index 0000000..05a326a --- /dev/null +++ b/tests/unit/Internal/Primitives/CallableDecoderTest.php @@ -0,0 +1,60 @@ +decode($input); + + $this->assertInstanceOf(ValidationSuccess::class, $result); + } + + /** + * @dataProvider invalidCallableProvider + */ + public function testInvalidCallables(mixed $input): void + { + $decoder = new CallableDecoder(); + $result = $decoder->decode($input); + + $this->assertInstanceOf(ValidationFailures::class, $result); + } + + public static function validCallableProvider(): array + { + return [ + 'anonymous function' => [fn() => null], + 'named function' => ['strlen'], + 'static method as array' => [[self::class, 'helperStatic']], + ]; + } + + public static function invalidCallableProvider(): array + { + return [ + 'int' => [123], + 'string' => ['not_callable'], + 'array of strings' => [['a', 'b']], + 'null' => [null], + 'object' => [new \stdClass()], + ]; + } + + public static function helperStatic(): void + { + // Metodo statico valido per test + } +} diff --git a/tests/unit/Internal/Primitives/FloatDecoderTest.php b/tests/unit/Internal/Primitives/FloatDecoderTest.php new file mode 100644 index 0000000..6fb2d90 --- /dev/null +++ b/tests/unit/Internal/Primitives/FloatDecoderTest.php @@ -0,0 +1,43 @@ +decode(3.14); + + $this->assertInstanceOf(ValidationSuccess::class, $result); + } + + /** + * @dataProvider invalidFloatProvider + */ + public function testInvalidValues($invalidValue): void + { + $decoder = new FloatDecoder(); + $result = $decoder->decode($invalidValue); + + $this->assertInstanceOf(ValidationFailures::class, $result); + } + + public static function invalidFloatProvider(): array + { + return [ + 'int' => [42], + 'string' => ['3.14'], + 'bool' => [true], + 'array' => [[3.14]], + 'null' => [null], + ]; + } +} diff --git a/tests/unit/Internal/Primitives/IntDecoderTest.php b/tests/unit/Internal/Primitives/IntDecoderTest.php new file mode 100644 index 0000000..a98924a --- /dev/null +++ b/tests/unit/Internal/Primitives/IntDecoderTest.php @@ -0,0 +1,44 @@ +decode(42); + + $this->assertInstanceOf(ValidationSuccess::class, $result); + } + + /** + * @dataProvider invalidIntProvider + */ + public function testInvalidValues(mixed $input): void + { + $decoder = new IntDecoder(); + $result = $decoder->decode($input); + + $this->assertInstanceOf(ValidationFailures::class, $result); + } + + public static function invalidIntProvider(): array + { + return [ + 'float' => [3.14], + 'string' => ['42'], + 'bool' => [true], + 'null' => [null], + 'array' => [[1]], + 'object' => [new \stdClass()], + ]; + } +} diff --git a/tests/unit/Internal/Primitives/MixedDecoderTest.php b/tests/unit/Internal/Primitives/MixedDecoderTest.php new file mode 100644 index 0000000..6d76633 --- /dev/null +++ b/tests/unit/Internal/Primitives/MixedDecoderTest.php @@ -0,0 +1,39 @@ +decode($value); + + $this->assertInstanceOf(ValidationSuccess::class, $result); + $this->assertSame($value, $result->getValue()); + } + + public static function provideValues(): array + { + return [ + 'string' => ['hello'], + 'int' => [42], + 'float' => [3.14], + 'bool true' => [true], + 'bool false' => [false], + 'null' => [null], + 'array' => [[1, 2, 3]], + 'object' => [new \stdClass()], + 'callable' => [fn() => 'test'], + ]; + } +} diff --git a/tests/unit/Internal/Primitives/NullDecoderTest.php b/tests/unit/Internal/Primitives/NullDecoderTest.php new file mode 100644 index 0000000..43fc9d6 --- /dev/null +++ b/tests/unit/Internal/Primitives/NullDecoderTest.php @@ -0,0 +1,46 @@ +decode(null); + + $this->assertInstanceOf(ValidationSuccess::class, $result); + $this->assertSame(null, $result->getValue()); + } + + /** + * @dataProvider provideInvalidValues + */ + public function testInvalidValues(mixed $value): void + { + $decoder = new NullDecoder(); + $result = $decoder->decode($value); + + $this->assertInstanceOf(ValidationFailures::class, $result); + } + + public static function provideInvalidValues(): array + { + return [ + 'int' => [42], + 'string' => ['null'], + 'bool' => [true], + 'float' => [3.14], + 'array' => [[null]], + 'object' => [new \stdClass()], + 'callable' => [fn() => null], + ]; + } +} diff --git a/tests/unit/Internal/Primitives/UndefinedDecoderTest.php b/tests/unit/Internal/Primitives/UndefinedDecoderTest.php index e541180..0f6ad82 100644 --- a/tests/unit/Internal/Primitives/UndefinedDecoderTest.php +++ b/tests/unit/Internal/Primitives/UndefinedDecoderTest.php @@ -6,7 +6,10 @@ use Eris\TestTrait; use Facile\PhpCodec\Decoders; +use Facile\PhpCodec\Internal\Primitives\UndefinedDecoder; use Facile\PhpCodec\Internal\Undefined; +use Facile\PhpCodec\Validation\ValidationFailures; +use Facile\PhpCodec\Validation\ValidationSuccess; use Tests\Facile\PhpCodec\BaseTestCase; use Tests\Facile\PhpCodec\GeneratorUtils; @@ -29,8 +32,48 @@ function ($default): void { $x = self::assertValidationSuccess( Decoders::undefined($default)->decode(new Undefined()) ); + self::assertSame($default, $x); } ); } + + //Aggiunti Michele + public function testValidUndefined(): void + { + $default = 'default-value'; + $decoder = new UndefinedDecoder($default); + $result = $decoder->decode(new Undefined()); + + //fwrite(STDOUT, "\n[testValidUndefined] result: " . var_export($result, true) . "\n"); + + $this->assertInstanceOf(ValidationSuccess::class, $result); + $this->assertSame($default, $result->getValue()); + } + + /** + * @dataProvider provideInvalidValues + */ + public function testInvalidValues(mixed $input): void + { + $decoder = new UndefinedDecoder('default'); + $result = $decoder->decode($input); + + //fwrite(STDOUT, "\n[testInvalidValues] input: " . var_export($input, true) . "\n"); + //fwrite(STDOUT, "[testInvalidValues] result: " . var_export($result, true) . "\n"); + + $this->assertInstanceOf(ValidationFailures::class, $result); + } + + public static function provideInvalidValues(): array + { + return [ + 'null' => [null], + 'string' => ['undefined'], + 'int' => [0], + 'bool' => [true], + 'array' => [[]], + 'object' => [new \stdClass()], + ]; + } } From b69e530ddaa62b1a30765ca6c1db210ec4fbb2f5 Mon Sep 17 00:00:00 2001 From: Michele Benigni Date: Fri, 6 Jun 2025 12:55:32 +0200 Subject: [PATCH 5/6] Update PHP 7.4 --- Makefile | 2 +- psalm-baseline.xml | 30 ++++++++++++++----- src/Internal/Primitives/FloatDecoder.php | 2 -- src/Internal/Primitives/IntDecoder.php | 2 -- src/Internal/Primitives/MixedDecoder.php | 2 -- src/Internal/Primitives/NullDecoder.php | 2 -- src/Internal/Primitives/StringDecoder.php | 2 -- src/Internal/Primitives/UndefinedDecoder.php | 2 -- tests/unit/DecodersTest.php | 6 ++-- .../Internal/Primitives/BoolDecoderTest.php | 10 +++++-- .../Primitives/CallableDecoderTest.php | 10 +++++-- .../Internal/Primitives/FloatDecoderTest.php | 4 ++- .../Internal/Primitives/IntDecoderTest.php | 6 ++-- .../Internal/Primitives/MixedDecoderTest.php | 6 ++-- .../Internal/Primitives/NullDecoderTest.php | 6 ++-- .../Internal/Primitives/StringDecoderTest.php | 6 ++-- .../Primitives/UndefinedDecoderTest.php | 13 ++++---- 17 files changed, 67 insertions(+), 44 deletions(-) diff --git a/Makefile b/Makefile index 6476e3d..f8745a6 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ help: .PHONY: run run-php7.4 run-php8.0 run-php8.1 run-php8.2 run-php7.4: @# Help: It creates and runs a docker image with PHP 7.4 - docker-compose run --rm php74 bash -c "rm composer.lock || true; composer install --no-interaction; bash" + docker compose run --rm php74 bash -c "rm composer.lock || true; composer install --no-interaction; bash" run-php8.0: @# Help: It creates and runs a docker image with PHP 8.0 docker-compose run --rm php80 bash -c "rm composer.lock || true; composer install --no-interaction; bash" diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 5605487..7e00714 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,12 +1,28 @@ - - + + + new BoolDecoder() + new BoolDecoder() + + + decode + decode + - - - Generators::oneOf(Generators::int(), Generators::float(), Generators::bool()) - Generators::oneOf(Generators::string(), Generators::float(), Generators::bool()) - + + + new CallableDecoder() + new CallableDecoder() + + + decode + decode + + + + + assertSame + diff --git a/src/Internal/Primitives/FloatDecoder.php b/src/Internal/Primitives/FloatDecoder.php index 090aa3a..a90f233 100644 --- a/src/Internal/Primitives/FloatDecoder.php +++ b/src/Internal/Primitives/FloatDecoder.php @@ -13,8 +13,6 @@ * @template I of mixed * * @template-implements Decoder - * - * @psalm-internal Facile\PhpCodec */ final class FloatDecoder implements Decoder { diff --git a/src/Internal/Primitives/IntDecoder.php b/src/Internal/Primitives/IntDecoder.php index 41f4b7c..493e1af 100644 --- a/src/Internal/Primitives/IntDecoder.php +++ b/src/Internal/Primitives/IntDecoder.php @@ -13,8 +13,6 @@ * @template I of mixed * * @template-implements Decoder - * - * @psalm-internal Facile\PhpCodec */ final class IntDecoder implements Decoder { diff --git a/src/Internal/Primitives/MixedDecoder.php b/src/Internal/Primitives/MixedDecoder.php index b143307..09b4914 100644 --- a/src/Internal/Primitives/MixedDecoder.php +++ b/src/Internal/Primitives/MixedDecoder.php @@ -13,8 +13,6 @@ * @psalm-template U of mixed * * @template-implements Decoder - * - * @psalm-internal Facile\PhpCodec */ final class MixedDecoder implements Decoder { diff --git a/src/Internal/Primitives/NullDecoder.php b/src/Internal/Primitives/NullDecoder.php index 3718c9f..429d901 100644 --- a/src/Internal/Primitives/NullDecoder.php +++ b/src/Internal/Primitives/NullDecoder.php @@ -13,8 +13,6 @@ * @template I of mixed * * @template-implements Decoder - * - * @psalm-internal Facile\PhpCodec */ final class NullDecoder implements Decoder { diff --git a/src/Internal/Primitives/StringDecoder.php b/src/Internal/Primitives/StringDecoder.php index 912aede..b2e1542 100644 --- a/src/Internal/Primitives/StringDecoder.php +++ b/src/Internal/Primitives/StringDecoder.php @@ -13,8 +13,6 @@ * @template I of mixed * * @template-implements Decoder - * - * @psalm-internal Facile\PhpCodec */ final class StringDecoder implements Decoder { diff --git a/src/Internal/Primitives/UndefinedDecoder.php b/src/Internal/Primitives/UndefinedDecoder.php index 3c9dd6b..a28d5aa 100644 --- a/src/Internal/Primitives/UndefinedDecoder.php +++ b/src/Internal/Primitives/UndefinedDecoder.php @@ -14,8 +14,6 @@ * @psalm-template U * * @template-implements Decoder - * - * @psalm-internal Facile\PhpCodec */ final class UndefinedDecoder implements Decoder { diff --git a/tests/unit/DecodersTest.php b/tests/unit/DecodersTest.php index f2bf2e6..cc14455 100644 --- a/tests/unit/DecodersTest.php +++ b/tests/unit/DecodersTest.php @@ -23,21 +23,21 @@ public function testMap(): void /** @psalm-suppress UndefinedFunction */ $this ->forAll( - Generators::int() //provare il decoder con molti interi casuali + Generators::int() // provare il decoder con molti interi casuali ) ->then(function (int $i) use ($decoder): void { $a = self::assertSuccessInstanceOf( DecodersTest\A::class, $decoder->decode($i) ); - self::assertSame($i, $a->getValue());//verifica che il valore dentro l'oggetto A sia uguale a $i + self::assertSame($i, $a->getValue()); // verifica che il valore dentro l'oggetto A sia uguale a $i }); } } namespace Tests\Facile\PhpCodec\DecodersTest; -//wrapper class usata per testare la trasformazione: prende un int e lo incapsula in un oggetto +// wrapper class usata per testare la trasformazione: prende un int e lo incapsula in un oggetto class A { private int $v; diff --git a/tests/unit/Internal/Primitives/BoolDecoderTest.php b/tests/unit/Internal/Primitives/BoolDecoderTest.php index 06d4437..310e15a 100644 --- a/tests/unit/Internal/Primitives/BoolDecoderTest.php +++ b/tests/unit/Internal/Primitives/BoolDecoderTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Unit\Internal\Primitives; +namespace Tests\Facile\PhpCodec\Internal\Primitives; use Facile\PhpCodec\Internal\Primitives\BoolDecoder; use Facile\PhpCodec\Validation\ValidationFailures; @@ -13,8 +13,10 @@ final class BoolDecoderTest extends TestCase { /** * @dataProvider validBoolProvider + * + * @param mixed $input */ - public function testValidBools(mixed $input): void + public function testValidBools($input): void { $decoder = new BoolDecoder(); $result = $decoder->decode($input); @@ -24,8 +26,10 @@ public function testValidBools(mixed $input): void /** * @dataProvider invalidBoolProvider + * + * @param mixed $input */ - public function testInvalidBools(mixed $input): void + public function testInvalidBools($input): void { $decoder = new BoolDecoder(); $result = $decoder->decode($input); diff --git a/tests/unit/Internal/Primitives/CallableDecoderTest.php b/tests/unit/Internal/Primitives/CallableDecoderTest.php index 05a326a..addba04 100644 --- a/tests/unit/Internal/Primitives/CallableDecoderTest.php +++ b/tests/unit/Internal/Primitives/CallableDecoderTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Unit\Internal\Primitives; +namespace Tests\Facile\PhpCodec\Internal\Primitives; use Facile\PhpCodec\Internal\Primitives\CallableDecoder; use Facile\PhpCodec\Validation\ValidationFailures; @@ -13,8 +13,10 @@ final class CallableDecoderTest extends TestCase { /** * @dataProvider validCallableProvider + * + * @param mixed $input */ - public function testValidCallables(mixed $input): void + public function testValidCallables($input): void { $decoder = new CallableDecoder(); $result = $decoder->decode($input); @@ -24,8 +26,10 @@ public function testValidCallables(mixed $input): void /** * @dataProvider invalidCallableProvider + * + * @param mixed $input */ - public function testInvalidCallables(mixed $input): void + public function testInvalidCallables($input): void { $decoder = new CallableDecoder(); $result = $decoder->decode($input); diff --git a/tests/unit/Internal/Primitives/FloatDecoderTest.php b/tests/unit/Internal/Primitives/FloatDecoderTest.php index 6fb2d90..876c730 100644 --- a/tests/unit/Internal/Primitives/FloatDecoderTest.php +++ b/tests/unit/Internal/Primitives/FloatDecoderTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Unit\Internal\Primitives; +namespace Tests\Facile\PhpCodec\Internal\Primitives; use Facile\PhpCodec\Internal\Primitives\FloatDecoder; use Facile\PhpCodec\Validation\ValidationFailures; @@ -21,6 +21,8 @@ public function testValidFloat(): void /** * @dataProvider invalidFloatProvider + * + * @param mixed $invalidValue */ public function testInvalidValues($invalidValue): void { diff --git a/tests/unit/Internal/Primitives/IntDecoderTest.php b/tests/unit/Internal/Primitives/IntDecoderTest.php index a98924a..1bdaa05 100644 --- a/tests/unit/Internal/Primitives/IntDecoderTest.php +++ b/tests/unit/Internal/Primitives/IntDecoderTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Unit\Internal\Primitives; +namespace Tests\Facile\PhpCodec\Internal\Primitives; use Facile\PhpCodec\Internal\Primitives\IntDecoder; use Facile\PhpCodec\Validation\ValidationSuccess; @@ -21,8 +21,10 @@ public function testValidInt(): void /** * @dataProvider invalidIntProvider + * + * @param mixed $input */ - public function testInvalidValues(mixed $input): void + public function testInvalidValues($input): void { $decoder = new IntDecoder(); $result = $decoder->decode($input); diff --git a/tests/unit/Internal/Primitives/MixedDecoderTest.php b/tests/unit/Internal/Primitives/MixedDecoderTest.php index 6d76633..1bca640 100644 --- a/tests/unit/Internal/Primitives/MixedDecoderTest.php +++ b/tests/unit/Internal/Primitives/MixedDecoderTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Unit\Internal\Primitives; +namespace Tests\Facile\PhpCodec\Internal\Primitives; use Facile\PhpCodec\Internal\Primitives\MixedDecoder; use Facile\PhpCodec\Validation\ValidationSuccess; @@ -12,8 +12,10 @@ final class MixedDecoderTest extends TestCase { /** * @dataProvider provideValues + * + * @param mixed $value */ - public function testAcceptsAnyValue(mixed $value): void + public function testAcceptsAnyValue($value): void { $decoder = new MixedDecoder(); $result = $decoder->decode($value); diff --git a/tests/unit/Internal/Primitives/NullDecoderTest.php b/tests/unit/Internal/Primitives/NullDecoderTest.php index 43fc9d6..0711d6e 100644 --- a/tests/unit/Internal/Primitives/NullDecoderTest.php +++ b/tests/unit/Internal/Primitives/NullDecoderTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Unit\Internal\Primitives; +namespace Tests\Facile\PhpCodec\Internal\Primitives; use Facile\PhpCodec\Internal\Primitives\NullDecoder; use Facile\PhpCodec\Validation\ValidationFailures; @@ -22,8 +22,10 @@ public function testValidNull(): void /** * @dataProvider provideInvalidValues + * + * @param mixed $value */ - public function testInvalidValues(mixed $value): void + public function testInvalidValues($value): void { $decoder = new NullDecoder(); $result = $decoder->decode($value); diff --git a/tests/unit/Internal/Primitives/StringDecoderTest.php b/tests/unit/Internal/Primitives/StringDecoderTest.php index 856c527..a0604f4 100644 --- a/tests/unit/Internal/Primitives/StringDecoderTest.php +++ b/tests/unit/Internal/Primitives/StringDecoderTest.php @@ -2,17 +2,15 @@ declare(strict_types=1); -namespace Tests\unit\Internal\Primitives; +namespace Tests\Facile\PhpCodec\Internal\Primitives; use Facile\PhpCodec\Internal\Primitives\StringDecoder; -use Facile\PhpCodec\Validation\Context; use Facile\PhpCodec\Validation\ValidationFailures; use Facile\PhpCodec\Validation\ValidationSuccess; use PHPUnit\Framework\TestCase; final class StringDecoderTest extends TestCase { - public function testValidString(): void { $decoder = new StringDecoder(); @@ -24,6 +22,8 @@ public function testValidString(): void /** * @dataProvider invalidValuesProvider + * + * @param mixed $input */ public function testInvalidValues($input): void { diff --git a/tests/unit/Internal/Primitives/UndefinedDecoderTest.php b/tests/unit/Internal/Primitives/UndefinedDecoderTest.php index 0f6ad82..ce17022 100644 --- a/tests/unit/Internal/Primitives/UndefinedDecoderTest.php +++ b/tests/unit/Internal/Primitives/UndefinedDecoderTest.php @@ -38,29 +38,32 @@ function ($default): void { ); } - //Aggiunti Michele + // Aggiunti Michele public function testValidUndefined(): void { $default = 'default-value'; $decoder = new UndefinedDecoder($default); $result = $decoder->decode(new Undefined()); - //fwrite(STDOUT, "\n[testValidUndefined] result: " . var_export($result, true) . "\n"); + // fwrite(STDOUT, "\n[testValidUndefined] result: " . var_export($result, true) . "\n"); $this->assertInstanceOf(ValidationSuccess::class, $result); + $this->assertSame($default, $result->getValue()); } /** * @dataProvider provideInvalidValues + * + * @param mixed $input */ - public function testInvalidValues(mixed $input): void + public function testInvalidValues($input): void { $decoder = new UndefinedDecoder('default'); $result = $decoder->decode($input); - //fwrite(STDOUT, "\n[testInvalidValues] input: " . var_export($input, true) . "\n"); - //fwrite(STDOUT, "[testInvalidValues] result: " . var_export($result, true) . "\n"); + // fwrite(STDOUT, "\n[testInvalidValues] input: " . var_export($input, true) . "\n"); + // fwrite(STDOUT, "[testInvalidValues] result: " . var_export($result, true) . "\n"); $this->assertInstanceOf(ValidationFailures::class, $result); } From 9c1024f3d2930aa8bcf9b1ffa82ab180eec26e67 Mon Sep 17 00:00:00 2001 From: Michele Benigni Date: Fri, 6 Jun 2025 15:56:12 +0200 Subject: [PATCH 6/6] BoundedIntDecoder e Test Aggiunti --- psalm-baseline.xml | 14 +++++ src/Internal/Useful/BoundedIntDecoder.php | 61 +++++++++++++++++++ .../Internal/Useful/BoundedIntDecoderTest.php | 61 +++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 src/Internal/Useful/BoundedIntDecoder.php create mode 100644 tests/unit/Internal/Useful/BoundedIntDecoderTest.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 7e00714..246ad85 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -25,4 +25,18 @@ assertSame + + + new BoundedIntDecoder(10, 20) + new BoundedIntDecoder(10, 20) + new BoundedIntDecoder(20, 10) + + + decode + decode + new BoundedIntDecoder(10, 20) + new BoundedIntDecoder(10, 20) + new BoundedIntDecoder(20, 10) + + diff --git a/src/Internal/Useful/BoundedIntDecoder.php b/src/Internal/Useful/BoundedIntDecoder.php new file mode 100644 index 0000000..25172de --- /dev/null +++ b/src/Internal/Useful/BoundedIntDecoder.php @@ -0,0 +1,61 @@ + + * + * @psalm-internal Facile\PhpCodec + */ +final class BoundedIntDecoder implements Decoder +{ + /** + * @psalm-readonly + */ + private int $min; + + /** + * @psalm-readonly + */ + private int $max; + + public function __construct(int $min, int $max) + { + if ($min > $max) { + throw new \InvalidArgumentException('Lower bound cannot be greater than upper bound.'); + } + + $this->min = $min; + $this->max = $max; + } + + public function validate($i, Context $context): Validation + { + if (! \is_int($i)) { + return Validation::failure($i, $context); + } + + if ($i < $this->min || $i > $this->max) { + return Validation::failure($i, $context); + } + + return Validation::success($i); + } + + public function decode($i): Validation + { + return FunctionUtils::standardDecode($this, $i); + } + + public function getName(): string + { + return sprintf('BoundedInt(%d, %d)', $this->min, $this->max); + } +} diff --git a/tests/unit/Internal/Useful/BoundedIntDecoderTest.php b/tests/unit/Internal/Useful/BoundedIntDecoderTest.php new file mode 100644 index 0000000..55a837f --- /dev/null +++ b/tests/unit/Internal/Useful/BoundedIntDecoderTest.php @@ -0,0 +1,61 @@ +decode($input); + + $this->assertInstanceOf(ValidationSuccess::class, $result); + } + + /** + * @dataProvider invalidIntProvider + * + * @param mixed $input + */ + public function testInvalidInts($input): void + { + $decoder = new BoundedIntDecoder(10, 20); + $result = $decoder->decode($input); + + $this->assertInstanceOf(ValidationFailures::class, $result); + } + + public static function validIntProvider(): array + { + return [ + 'lower bound' => [10], + 'middle' => [15], + 'upper bound' => [20], + ]; + } + + public static function invalidIntProvider(): array + { + return [ + 'below range' => [9], + 'above range' => [21], + 'string' => ['15'], + 'float' => [15.0], + 'null' => [null], + 'bool' => [true], + 'array' => [[15]], + 'object' => [new \stdClass()], + ]; +}