Skip to content

Commit

Permalink
Fix warning when value not found by XPath
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed May 1, 2024
1 parent 2060195 commit 9165cf3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Service/Config/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private function injectValue(object $config, \ReflectionProperty $property, arra

/** @var mixed $value */
$value = match (true) {
$attribute instanceof XPath => $this->xml?->xpath($attribute->path)[$attribute->key],
$attribute instanceof XPath => @$this->xml?->xpath($attribute->path)[$attribute->key],
$attribute instanceof Env => $this->env[$attribute->name] ?? null,
$attribute instanceof InputOption => $this->inputOptions[$attribute->name] ?? null,
$attribute instanceof InputArgument => $this->inputArguments[$attribute->name] ?? null,
Expand Down
28 changes: 25 additions & 3 deletions tests/Unit/Service/Config/ConfigLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public function testSimpleHydration(): void
public string $myString;
#[XPath('/trap/container/MyFloat/@value')]
public float $myFloat;
#[XPath('/trap/container/Nothing/@value')]
public float $none = 3.14;
};
$xml = <<<'XML'
<?xml version="1.0"?>
Expand All @@ -44,7 +42,31 @@ public function testSimpleHydration(): void
self::assertSame(200, $dto->myInt);
self::assertSame('foo-bar', $dto->myString);
self::assertSame(42.0, $dto->myFloat);
self::assertSame(3.14, $dto->none);
}

public function testNonExistingOptions(): void
{
$dto = new class() {
#[XPath('/trap/container/Nothing/@value')]
public float $none1 = 3.14;
#[Env('f--o--o')]
public float $none2 = 3.14;
#[InputOption('f--o--o')]
public float $none3 = 3.14;
#[InputArgument('f--o--o')]
public float $none4 = 3.14;
};
$xml = <<<'XML'
<?xml version="1.0"?>
<trap my-string="foo-bar"> </trap>
XML;

$this->createConfigLoader(xml: $xml)->hidrate($dto);

self::assertSame(3.14, $dto->none1);
self::assertSame(3.14, $dto->none2);
self::assertSame(3.14, $dto->none3);
self::assertSame(3.14, $dto->none4);
}

public function testAttributesOrder(): void
Expand Down

0 comments on commit 9165cf3

Please sign in to comment.