Skip to content

Commit 9def16a

Browse files
committed
Containerize Uuid optional dependencies for testability
This change makes the ramsey/uuid optional dependency testable by using the ContainerRegistry to acquire an instance to its factory instead of relying on `class_exists` directly.
1 parent b352f17 commit 9def16a

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

src/Validators/Uuid.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020

2121
use Attribute;
2222
use Ramsey\Uuid\Rfc4122\FieldsInterface;
23-
use Ramsey\Uuid\Uuid as RamseyUuid;
23+
use Ramsey\Uuid\UuidFactory;
2424
use Ramsey\Uuid\UuidInterface;
25+
use Respect\Validation\ContainerRegistry;
2526
use Respect\Validation\Exceptions\InvalidValidatorException;
2627
use Respect\Validation\Exceptions\MissingComposerDependencyException;
2728
use Respect\Validation\Message\Template;
2829
use Respect\Validation\Result;
2930
use Respect\Validation\Validator;
3031
use Throwable;
3132

32-
use function class_exists;
3333
use function is_string;
3434

3535
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
@@ -50,8 +50,11 @@ final class Uuid implements Validator
5050
public function __construct(
5151
private readonly int|null $version = null,
5252
) {
53-
if (!class_exists(RamseyUuid::class)) {
54-
throw new MissingComposerDependencyException('Uuid rule requires ramsey/uuid', '=');
53+
if (!ContainerRegistry::getContainer()->has(UuidFactory::class)) {
54+
throw new MissingComposerDependencyException(
55+
'Uuid rule requires ramsey/uuid package',
56+
'ramsey/uuid',
57+
);
5558
}
5659

5760
if ($version !== null && !$this->isSupportedVersion($version)) {
@@ -72,7 +75,9 @@ public function evaluate(mixed $input): Result
7275
}
7376

7477
try {
75-
$uuid = is_string($input) ? RamseyUuid::fromString($input) : $input;
78+
$uuid = is_string($input) ? ContainerRegistry::getContainer()
79+
->get(UuidFactory::class)
80+
->fromString($input) : $input;
7681
} catch (Throwable) {
7782
return Result::failed($input, $this, $parameters, $template);
7883
}

tests/unit/Validators/UuidTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414

1515
namespace Respect\Validation\Validators;
1616

17+
use DI;
1718
use PHPUnit\Framework\Attributes\CoversClass;
1819
use PHPUnit\Framework\Attributes\Group;
1920
use PHPUnit\Framework\Attributes\Test;
2021
use Ramsey\Uuid\Uuid as RamseyUuid;
22+
use Respect\Validation\ContainerRegistry;
2123
use Respect\Validation\Exceptions\InvalidValidatorException;
24+
use Respect\Validation\Exceptions\MissingComposerDependencyException;
2225
use Respect\Validation\Test\RuleTestCase;
2326
use stdClass;
2427

@@ -67,6 +70,24 @@ public function itShouldThrowExceptionWhenVersionIsLessThanOne(): void
6770
new Uuid($version);
6871
}
6972

73+
#[Test]
74+
public function shouldThrowWhenMissingComponent(): void
75+
{
76+
$mainContainer = ContainerRegistry::getContainer();
77+
ContainerRegistry::setContainer((new DI\ContainerBuilder())->useAutowiring(false)->build());
78+
try {
79+
new Uuid();
80+
$this->fail('Expected MissingComposerDependencyException was not thrown.');
81+
} catch (MissingComposerDependencyException $e) {
82+
$this->assertStringContainsString(
83+
'Uuid rule requires ramsey/uuid package',
84+
$e->getMessage(),
85+
);
86+
} finally {
87+
ContainerRegistry::setContainer($mainContainer);
88+
}
89+
}
90+
7091
/** @return iterable<array{Uuid, mixed}> */
7192
public static function providerForValidInput(): iterable
7293
{

0 commit comments

Comments
 (0)