From 9e9f1bf1d928077ff9c1f9e0456d07f33fa43db5 Mon Sep 17 00:00:00 2001 From: Rodolfo Berrios <20590102+rodber@users.noreply.github.com> Date: Fri, 19 Jul 2024 13:18:28 -0400 Subject: [PATCH] fix get outofbounds --- src/Parameters.php | 10 ++++---- tests/ParametersTest.php | 50 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/Parameters.php b/src/Parameters.php index 4017d82..9c6f25e 100644 --- a/src/Parameters.php +++ b/src/Parameters.php @@ -189,6 +189,7 @@ public function get(string $name): ParameterInterface public function required(string $name): ParameterCastInterface { + $parameter = $this->get($name); if ($this->optionalKeys()->contains($name)) { throw new InvalidArgumentException( (string) message( @@ -198,13 +199,12 @@ public function required(string $name): ParameterCastInterface ); } - return new ParameterCast( - $this->get($name) - ); + return new ParameterCast($parameter); } public function optional(string $name): ParameterCastInterface { + $parameter = $this->get($name); if (! $this->optionalKeys()->contains($name)) { throw new InvalidArgumentException( (string) message( @@ -214,9 +214,7 @@ public function optional(string $name): ParameterCastInterface ); } - return new ParameterCast( - $this->get($name) - ); + return new ParameterCast($parameter); } private function remove(string ...$name): void diff --git a/tests/ParametersTest.php b/tests/ParametersTest.php index 79d2b6a..e08250a 100644 --- a/tests/ParametersTest.php +++ b/tests/ParametersTest.php @@ -81,8 +81,24 @@ public function testConstructPositional(): void $bar = int(); $parameters = new Parameters($foo, $bar); $this->assertCount(2, $parameters); - $this->assertSame($foo, $parameters->get('0')); - $this->assertSame($bar, $parameters->get('1')); + $this->assertSame($foo, $parameters->get('1')); + $this->assertSame($bar, $parameters->get('2')); + } + + public function testRequiredMissing(): void + { + $parameters = new Parameters(); + $this->expectException(OutOfBoundsException::class); + $this->expectExceptionMessage('Key `foo` not found'); + $parameters->required('foo'); + } + + public function testOptionalMissing(): void + { + $parameters = new Parameters(); + $this->expectException(OutOfBoundsException::class); + $this->expectExceptionMessage('Key `foo` not found'); + $parameters->optional('foo'); } public function testRequiredCasting(): void @@ -95,6 +111,16 @@ public function testRequiredCasting(): void $parameters->optional('foo'); } + public function testRequiredCastingPositional(): void + { + $parameter = string(); + $parameters = new Parameters($parameter); + $this->assertSame($parameter, $parameters->required('1')->string()); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Parameter `1` is required'); + $parameters->optional('1'); + } + public function testOptionalCasting(): void { $parameter = string(); @@ -315,4 +341,24 @@ public function testWithMakeRequired(): void $this->expectException(InvalidArgumentException::class); $parametersWith->withMakeRequired('bar'); } + + public function testIsList(): void + { + $parameters = new Parameters(); + $this->assertTrue($parameters->isList()); + $parameters = new Parameters( + foo: string(), + bar: int() + ); + $this->assertFalse($parameters->isList()); + $parameters = new Parameters( + string(), + int() + ); + $this->assertTrue($parameters->isList()); + $parameters = (new Parameters()) + ->withRequired('a', string()) + ->withRequired('b', int()); + $this->assertTrue($parameters->isList()); + } }