diff --git a/src/Http/Factory/Uri.php b/src/Http/Factory/Uri.php index 10435c7..15d01ff 100644 --- a/src/Http/Factory/Uri.php +++ b/src/Http/Factory/Uri.php @@ -29,7 +29,7 @@ public function __construct(mixed $value) $search = []; $replace = []; foreach ($parameters as $name => $parameter) { - if (!\is_string($name) || !\is_string($parameter)) { + if (!\is_string($name) || !\is_scalar($parameter)) { throw new InvalidArgumentException(\sprintf( 'Invalid parameter given {name: %s, parameter: %s}', var_export($name, true), @@ -37,7 +37,7 @@ public function __construct(mixed $value) )); } $search[] = $name; - $replace[] = $parameter; + $replace[] = (string) $parameter; } $this->uri = str_replace( diff --git a/tests/unit/Http/Factory/UriTest.php b/tests/unit/Http/Factory/UriTest.php index cc486aa..8b3cef5 100644 --- a/tests/unit/Http/Factory/UriTest.php +++ b/tests/unit/Http/Factory/UriTest.php @@ -6,6 +6,7 @@ use CHStudio\Raven\Http\Factory\Uri; use InvalidArgumentException; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Stringable; @@ -24,7 +25,7 @@ public function testItFailsToBuildFromArrayWithoutBase(): void new Uri(['parameters' => []]); } - #[\PHPUnit\Framework\Attributes\DataProvider('provideItCanBeBuiltFromArrayCases')] + #[DataProvider('provideItCanBeBuiltFromArrayCases')] public function testItCanBeBuiltFromArray($array, $expected): void { $uri = new Uri($array); @@ -51,6 +52,33 @@ public static function provideItCanBeBuiltFromArrayCases(): iterable ]], 'http://chstudio.fr?keyword=%s' ]; + yield [ + [ + 'base' => 'http://chstudio.fr?{name}={value}', + 'parameters' => [ + '{name}' => 'integer', + '{value}' => 13 + ]], 'http://chstudio.fr?integer=13' + ]; + + yield [ + [ + 'base' => 'http://chstudio.fr?{name}={value}', + 'parameters' => [ + '{name}' => 'boolean', + '{value}' => true + ]], 'http://chstudio.fr?boolean=1' + ]; + + yield [ + [ + 'base' => 'http://chstudio.fr?{name}={value}', + 'parameters' => [ + '{name}' => 'float', + '{value}' => 13.12 + ]], 'http://chstudio.fr?float=13.12' + ]; + yield [ [ 'base' => 'https://chstudio.fr' @@ -58,7 +86,7 @@ public static function provideItCanBeBuiltFromArrayCases(): iterable ]; } - #[\PHPUnit\Framework\Attributes\DataProvider('provideItCantBeBuiltFromOtherValuesCases')] + #[DataProvider('provideItCantBeBuiltFromOtherValuesCases')] public function testItCantBeBuiltFromOtherValues($value): void { $this->expectException(InvalidArgumentException::class); @@ -72,7 +100,6 @@ public static function provideItCantBeBuiltFromOtherValuesCases(): iterable yield "Nothing in the parameters" => [[]]; yield "No base but valid parameters" => [['parameters' => []]]; yield [['base' => 'http://example.com', 'parameters' => 'not-an-array']]; - yield [['base' => 'http://example.com', 'parameters' => ['int' => 0]]]; yield [null]; } }