Skip to content

Commit

Permalink
fix get outofbounds
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jul 19, 2024
1 parent 5e36fb2 commit 9e9f1bf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
10 changes: 4 additions & 6 deletions src/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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
Expand Down
50 changes: 48 additions & 2 deletions tests/ParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Check failure on line 84 in tests/ParametersTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-latest

Failed asserting that two variables reference the same object.

Check failure on line 84 in tests/ParametersTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 test on ubuntu-latest

Failed asserting that two variables reference the same object.

Check failure on line 84 in tests/ParametersTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-latest

Failed asserting that two variables reference the same object.

Check failure on line 84 in tests/ParametersTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 test on ubuntu-latest

Failed asserting that two variables reference the same object.
$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
Expand All @@ -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();
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 9e9f1bf

Please sign in to comment.