Skip to content

Commit 327f1ea

Browse files
committed
cover missing lines
Signed-off-by: alexmerlin <alex.merlin.1985@gmail.com>
1 parent be4162f commit 327f1ea

3 files changed

Lines changed: 50 additions & 4 deletions

File tree

src/Exception/InvalidArgumentException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66

77
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
88
{
9+
public const MESSAGE_MISSING_KEY =
10+
'The key "%s" provided in the dotted notation could not be found in the array service.';
911
}

src/Factory/AttributedServiceFactory.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
class AttributedServiceFactory
2626
{
27+
protected string $originalKey;
28+
2729
/**
2830
* @throws ContainerExceptionInterface
2931
* @throws NotFoundExceptionInterface
@@ -95,6 +97,8 @@ protected function getServicesToInject(ContainerInterface $container, array $par
9597
*/
9698
protected function getServiceToInject(ContainerInterface $container, string $serviceKey): mixed
9799
{
100+
$this->originalKey = $serviceKey;
101+
98102
/**
99103
* Even when dots are found, try to find a service with the full name.
100104
* If it is not found, then assume dots are used to get part of an array service
@@ -121,10 +125,9 @@ protected function readKeysFromArray(array $keys, mixed $array): mixed
121125
{
122126
$key = array_shift($keys);
123127
if (! isset($array[$key])) {
124-
throw new InvalidArgumentException(sprintf(
125-
'The key "%s" provided in the dotted notation could not be found in the array service.',
126-
$key
127-
));
128+
throw new InvalidArgumentException(
129+
sprintf(InvalidArgumentException::MESSAGE_MISSING_KEY, $this->originalKey)
130+
);
128131
}
129132

130133
$value = $array[$key];

test/Factory/AttributedServiceFactoryTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace DotTest\AnnotatedServices\Factory;
66

77
use Dot\AnnotatedServices\Attribute\Inject;
8+
use Dot\AnnotatedServices\Exception\InvalidArgumentException;
89
use Dot\AnnotatedServices\Exception\RuntimeException;
910
use Dot\AnnotatedServices\Factory\AttributedServiceFactory;
1011
use DotTest\AnnotatedServices\TestData\RecursionService;
@@ -87,6 +88,46 @@ public function testWillThrowExceptionOnRecursiveInjection(): void
8788
(new AttributedServiceFactory())($container, $subject::class);
8889
}
8990

91+
/**
92+
* @throws ContainerExceptionInterface
93+
* @throws Exception
94+
* @throws NotFoundExceptionInterface
95+
*/
96+
public function testWillThrowExceptionIfDottedServiceNotFound(): void
97+
{
98+
$mapping = [
99+
'config' => [],
100+
'uration' => [],
101+
];
102+
103+
$container = $this->createMock(ContainerInterface::class);
104+
$container->expects($this->any())->method('has')->willReturnCallback(
105+
function (string $key) use ($mapping): bool {
106+
return array_key_exists($key, $mapping);
107+
},
108+
);
109+
$container->expects($this->any())->method('get')->willReturnCallback(
110+
function (string $key) use ($mapping): array {
111+
return $mapping[$key] ?? [];
112+
},
113+
);
114+
115+
$subject = new class
116+
{
117+
#[Inject('config.key')]
118+
public function __construct(array $config = [])
119+
{
120+
}
121+
};
122+
123+
$this->expectException(InvalidArgumentException::class);
124+
$this->expectExceptionMessage(
125+
sprintf(InvalidArgumentException::MESSAGE_MISSING_KEY, 'config.key')
126+
);
127+
128+
(new AttributedServiceFactory())($container, $subject::class);
129+
}
130+
90131
/**
91132
* @throws ContainerExceptionInterface
92133
* @throws Exception

0 commit comments

Comments
 (0)