Skip to content

add decodertest #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
46 changes: 38 additions & 8 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.30.0@d0bc6e25d89f649e4f36a534f330f8bb4643dd69">
<file src="tests/unit/GeneratorUtils.php">
<TooManyArguments occurrences="1"/>
</file>
<file src="tests/unit/Internal/Combinators/IntersectionDecoderTest.php">
<TooManyArguments occurrences="4">
<code>Generators::oneOf(Generators::int(), Generators::float(), Generators::bool())</code>
<code>Generators::oneOf(Generators::string(), Generators::float(), Generators::bool())</code>
</TooManyArguments>
<file src="tests/unit/Internal/Primitives/BoolDecoderTest.php">
<InternalClass occurrences="2">
<code>new BoolDecoder()</code>
<code>new BoolDecoder()</code>
</InternalClass>
<InternalMethod occurrences="2">
<code>decode</code>
<code>decode</code>
</InternalMethod>
</file>
<file src="tests/unit/Internal/Primitives/CallableDecoderTest.php">
<InternalClass occurrences="2">
<code>new CallableDecoder()</code>
<code>new CallableDecoder()</code>
</InternalClass>
<InternalMethod occurrences="2">
<code>decode</code>
<code>decode</code>
</InternalMethod>
</file>
<file src="tests/unit/Internal/Primitives/UndefinedDecoderTest.php">
<RedundantConditionGivenDocblockType occurrences="1">
<code>assertSame</code>
</RedundantConditionGivenDocblockType>
</file>
<file src="tests/unit/Internal/Useful/BoundedIntDecoderTest.php">
<InternalClass occurrences="3">
<code>new BoundedIntDecoder(10, 20)</code>
<code>new BoundedIntDecoder(10, 20)</code>
<code>new BoundedIntDecoder(20, 10)</code>
</InternalClass>
<InternalMethod occurrences="5">
<code>decode</code>
<code>decode</code>
<code>new BoundedIntDecoder(10, 20)</code>
<code>new BoundedIntDecoder(10, 20)</code>
<code>new BoundedIntDecoder(20, 10)</code>
</InternalMethod>
</file>
</files>
2 changes: 0 additions & 2 deletions src/Internal/Primitives/FloatDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
* @template I of mixed
*
* @template-implements Decoder<I, float>
*
* @psalm-internal Facile\PhpCodec
*/
final class FloatDecoder implements Decoder
{
Expand Down
2 changes: 0 additions & 2 deletions src/Internal/Primitives/IntDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
* @template I of mixed
*
* @template-implements Decoder<I, int>
*
* @psalm-internal Facile\PhpCodec
*/
final class IntDecoder implements Decoder
{
Expand Down
2 changes: 0 additions & 2 deletions src/Internal/Primitives/MixedDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
* @psalm-template U of mixed
*
* @template-implements Decoder<U, U>
*
* @psalm-internal Facile\PhpCodec
*/
final class MixedDecoder implements Decoder
{
Expand Down
2 changes: 0 additions & 2 deletions src/Internal/Primitives/NullDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
* @template I of mixed
*
* @template-implements Decoder<I, null>
*
* @psalm-internal Facile\PhpCodec
*/
final class NullDecoder implements Decoder
{
Expand Down
2 changes: 0 additions & 2 deletions src/Internal/Primitives/StringDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
* @template I of mixed
*
* @template-implements Decoder<I, string>
*
* @psalm-internal Facile\PhpCodec
*/
final class StringDecoder implements Decoder
{
Expand Down
2 changes: 0 additions & 2 deletions src/Internal/Primitives/UndefinedDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* @psalm-template U
*
* @template-implements Decoder<mixed, U>
*
* @psalm-internal Facile\PhpCodec
*/
final class UndefinedDecoder implements Decoder
{
Expand Down
61 changes: 61 additions & 0 deletions src/Internal/Useful/BoundedIntDecoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Facile\PhpCodec\Internal\Useful;

use Facile\PhpCodec\Decoder;
use Facile\PhpCodec\Internal\FunctionUtils;
use Facile\PhpCodec\Validation\Context;
use Facile\PhpCodec\Validation\Validation;

/**
* @template-implements Decoder<mixed, int>
*
* @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);
}
}
5 changes: 3 additions & 2 deletions tests/unit/DecodersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 59 additions & 0 deletions tests/unit/Internal/Primitives/BoolDecoderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Tests\Facile\PhpCodec\Internal\Primitives;

use Facile\PhpCodec\Internal\Primitives\BoolDecoder;
use Facile\PhpCodec\Validation\ValidationFailures;
use Facile\PhpCodec\Validation\ValidationSuccess;
use PHPUnit\Framework\TestCase;

final class BoolDecoderTest extends TestCase
{
/**
* @dataProvider validBoolProvider
*
* @param mixed $input
*/
public function testValidBools($input): void
{
$decoder = new BoolDecoder();
$result = $decoder->decode($input);

$this->assertInstanceOf(ValidationSuccess::class, $result);
}

/**
* @dataProvider invalidBoolProvider
*
* @param mixed $input
*/
public function testInvalidBools($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]],
];
}
}
64 changes: 64 additions & 0 deletions tests/unit/Internal/Primitives/CallableDecoderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Tests\Facile\PhpCodec\Internal\Primitives;

use Facile\PhpCodec\Internal\Primitives\CallableDecoder;
use Facile\PhpCodec\Validation\ValidationFailures;
use Facile\PhpCodec\Validation\ValidationSuccess;
use PHPUnit\Framework\TestCase;

final class CallableDecoderTest extends TestCase
{
/**
* @dataProvider validCallableProvider
*
* @param mixed $input
*/
public function testValidCallables($input): void
{
$decoder = new CallableDecoder();
$result = $decoder->decode($input);

$this->assertInstanceOf(ValidationSuccess::class, $result);
}

/**
* @dataProvider invalidCallableProvider
*
* @param mixed $input
*/
public function testInvalidCallables($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
}
}
45 changes: 45 additions & 0 deletions tests/unit/Internal/Primitives/FloatDecoderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Tests\Facile\PhpCodec\Internal\Primitives;

use Facile\PhpCodec\Internal\Primitives\FloatDecoder;
use Facile\PhpCodec\Validation\ValidationFailures;
use Facile\PhpCodec\Validation\ValidationSuccess;
use PHPUnit\Framework\TestCase;

final class FloatDecoderTest extends TestCase
{
public function testValidFloat(): void
{
$decoder = new FloatDecoder();
$result = $decoder->decode(3.14);

$this->assertInstanceOf(ValidationSuccess::class, $result);
}

/**
* @dataProvider invalidFloatProvider
*
* @param mixed $invalidValue
*/
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],
];
}
}
Loading
Loading