Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed May 1, 2024
1 parent f37be4b commit e2c9998
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Config/Server/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
final class Frontend
{
/** @var int<1, max> */
/** @var int<1, 65535> */
#[Env('TRAP_FRONTEND_PORT')]
#[CliOption('ui')]
#[XPath('/trap/frontend/@port')]
Expand Down
8 changes: 6 additions & 2 deletions src/Service/Config/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ final class ConfigLoader

/**
* @param null|callable(): non-empty-string $xmlProvider
* @psalm-suppress RiskyTruthyFalsyComparison
*/
public function __construct(
private Logger $logger,
Expand Down Expand Up @@ -57,14 +58,15 @@ public function hidrate(object $config): void

/**
* @param \ReflectionProperty $property
* @param array<\ReflectionAttribute> $attributes
* @param list<\ReflectionAttribute<ConfigAttribute>> $attributes
*/
private function injectValue(object $config, \ReflectionProperty $property, array $attributes): void
{
foreach ($attributes as $attribute) {
try {
$attribute = $attribute->newInstance();

/** @var mixed $value */
$value = match (true) {
$attribute instanceof XPath => $this->xml?->xpath($attribute->path)[$attribute->key],
$attribute instanceof Env => \getenv($attribute->name) === false ? null : \getenv($attribute->name),
Expand All @@ -78,8 +80,10 @@ private function injectValue(object $config, \ReflectionProperty $property, arra

// Cast value to the property type
$type = $property->getType();

/** @var mixed $result */
$result = match (true) {
$type === null => $value,
!$type instanceof \ReflectionNamedType => $value,
$type->allowsNull() && $value === '' => null,
$type->isBuiltin() => match ($type->getName()) {
'int' => (int) $value,
Expand Down
2 changes: 1 addition & 1 deletion src/Service/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct()
* @param array $arguments Will be used if the object is created for the first time.
* @return T
*
* @psalm-suppress MoreSpecificImplementedParamType
* @psalm-suppress MoreSpecificImplementedParamType, InvalidReturnType
*/
public function get(string $id, array $arguments = []): object
{
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Service/Config/ConfigLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Buggregator\Trap\Tests\Unit\Service\Config;

use Buggregator\Trap\Logger;
use Buggregator\Trap\Service\Config\ConfigLoader;
use Buggregator\Trap\Service\Config\XPath;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -33,7 +34,7 @@ public function testSimpleHydration(): void
</trap>
XML;

$loader = new ConfigLoader(fn() => $xml);
$loader = new ConfigLoader(new Logger(), null, fn() => $xml);
$loader->hidrate($dto);

self::assertTrue($dto->myBool);
Expand Down

0 comments on commit e2c9998

Please sign in to comment.