Skip to content
Merged
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
7 changes: 6 additions & 1 deletion src/Tempest/Validation/src/Rules/IsFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ public function isValid(mixed $value): bool
return true;
}

return is_float($value);
if ($value === null || $value === false || $value === '' || $value === []) {
return false;
}

// @mago-expect strictness/require-identity-comparison
return is_float($value) || floatval($value) == $value;
}

public function message(): string
Expand Down
7 changes: 6 additions & 1 deletion src/Tempest/Validation/src/Rules/IsInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ public function isValid(mixed $value): bool
return true;
}

return is_int($value);
if ($value === null || $value === false || $value === '' || $value === []) {
return false;
}

// @mago-expect strictness/require-identity-comparison
return is_int($value) || intval($value) == $value;
}

public function message(): string
Expand Down
1 change: 0 additions & 1 deletion src/Tempest/Validation/src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Tempest\Validation\Rules\IsInteger;
use Tempest\Validation\Rules\IsString;
use Tempest\Validation\Rules\NotNull;
use TypeError;

use function Tempest\Support\arr;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @internal
*/
final class BooleanTest extends TestCase
final class IsBooleanTest extends TestCase
{
public function test_boolean(): void
{
Expand Down
28 changes: 28 additions & 0 deletions src/Tempest/Validation/tests/Rules/IsFloatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Tempest\Validation\Tests\Rules;

use PHPUnit\Framework\TestCase;
use Tempest\Validation\Rules\IsFloat;

/**
* @internal
*/
final class IsFloatTest extends TestCase
{
public function test_float(): void
{
$rule = new IsFloat();

$this->assertTrue($rule->isValid(1));
$this->assertTrue($rule->isValid(0.1));
$this->assertTrue($rule->isValid('0.1'));
$this->assertFalse($rule->isValid('a'));
$this->assertFalse($rule->isValid(''));
$this->assertFalse($rule->isValid(null));
$this->assertFalse($rule->isValid(false));
$this->assertFalse($rule->isValid([]));
}
}
27 changes: 27 additions & 0 deletions src/Tempest/Validation/tests/Rules/IsIntegerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Tempest\Validation\Tests\Rules;

use PHPUnit\Framework\TestCase;
use Tempest\Validation\Rules\IsInteger;

/**
* @internal
*/
final class IsIntegerTest extends TestCase
{
public function test_integer(): void
{
$rule = new IsInteger();

$this->assertTrue($rule->isValid(1));
$this->assertTrue($rule->isValid('1'));
$this->assertFalse($rule->isValid('a'));
$this->assertFalse($rule->isValid(''));
$this->assertFalse($rule->isValid(null));
$this->assertFalse($rule->isValid(false));
$this->assertFalse($rule->isValid([]));
}
}
19 changes: 19 additions & 0 deletions tests/Integration/Route/Fixtures/RequestWithTypedQueryParam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Tests\Tempest\Integration\Route\Fixtures;

use Tempest\Router\IsRequest;
use Tempest\Router\Request;

final class RequestWithTypedQueryParam implements Request
{
use IsRequest;

public string $stringParam;

public float $floatParam;

public bool $boolParam;

public int $intParam;
}
17 changes: 17 additions & 0 deletions tests/Integration/Route/RequestToObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Tests\Tempest\Fixtures\Modules\Books\Requests\CreateBookRequest;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
use Tests\Tempest\Integration\Route\Fixtures\RequestObjectA;
use Tests\Tempest\Integration\Route\Fixtures\RequestWithTypedQueryParam;

use function Tempest\map;
use function Tempest\Support\arr;
Expand Down Expand Up @@ -64,4 +65,20 @@ public function test_query_parameters_are_mapped_to_properties(): void

$this->assertSame('hello', $request->queryParam);
}

public function test_query_params_with_types(): void
{
$request = map(new GenericRequest(
method: Method::GET,
uri: '/books?stringParam=a&intParam=1&floatParam=0.1&boolParam=1',
body: ['title' => 'Timeline Taxi'],
))->with(
RequestToObjectMapper::class,
)->to(RequestWithTypedQueryParam::class);

$this->assertSame(1, $request->intParam);
$this->assertSame('a', $request->stringParam);
$this->assertSame(true, $request->boolParam);
$this->assertSame(0.1, $request->floatParam);
}
}
Loading