Skip to content

Commit b00880f

Browse files
committed
feature #51562 [DoctrineBridge] Add message to #[MapEntity] for NotFoundHttpException (moesoha)
This PR was merged into the 7.1 branch. Discussion ---------- [DoctrineBridge] Add `message` to #[MapEntity] for NotFoundHttpException | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | N/A Allow the message in NotFoundHttpException to be overridden with the `message` argument in `#[MapEntity]`. Commits ------- 753ad20d6f [DoctrineBridge] Add `message` to #[MapEntity] for NotFoundHttpException
2 parents b55b949 + 8d5b07c commit b00880f

File tree

3 files changed

+5
-2
lines changed

3 files changed

+5
-2
lines changed

ArgumentResolver/EntityValueResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
7373
}
7474

7575
if (null === $object && !$argument->isNullable()) {
76-
throw new NotFoundHttpException(sprintf('"%s" object not found by "%s".', $options->class, self::class).$message);
76+
throw new NotFoundHttpException($options->message ?? (sprintf('"%s" object not found by "%s".', $options->class, self::class).$message));
7777
}
7878

7979
return [$object];

Attribute/MapEntity.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function __construct(
4444
public ?bool $evictCache = null,
4545
bool $disabled = false,
4646
string $resolver = EntityValueResolver::class,
47+
public ?string $message = null,
4748
) {
4849
parent::__construct($resolver, $disabled);
4950
}
@@ -59,6 +60,7 @@ public function withDefaults(self $defaults, ?string $class): static
5960
$clone->stripNull ??= $defaults->stripNull ?? false;
6061
$clone->id ??= $defaults->id;
6162
$clone->evictCache ??= $defaults->evictCache ?? false;
63+
$clone->message ??= $defaults->message;
6264

6365
return $clone;
6466
}

Tests/ArgumentResolver/EntityValueResolverTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function testResolveWithConversionFailedException()
153153
$request = new Request();
154154
$request->attributes->set('id', 'test');
155155

156-
$argument = $this->createArgument('stdClass', new MapEntity(id: 'id'));
156+
$argument = $this->createArgument('stdClass', new MapEntity(id: 'id', message: 'Test'));
157157

158158
$repository = $this->getMockBuilder(ObjectRepository::class)->getMock();
159159
$repository->expects($this->once())
@@ -167,6 +167,7 @@ public function testResolveWithConversionFailedException()
167167
->willReturn($repository);
168168

169169
$this->expectException(NotFoundHttpException::class);
170+
$this->expectExceptionMessage('Test');
170171

171172
$resolver->resolve($request, $argument);
172173
}

0 commit comments

Comments
 (0)