Skip to content

Commit

Permalink
[HttpKernel] Adjust CS on MapSessionParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
jtattevin committed Apr 5, 2024
1 parent dee8f83 commit 93fd9a9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\SessionParameterValueResolver;

/**
* Controller parameter tag to map the session values to an object.
* Can be used to pass a session parameter to a controller argument.
*/
#[\Attribute(\Attribute::TARGET_PARAMETER)]
class MapSessionParameter extends ValueResolver
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CHANGELOG
* Add `HttpException::fromStatusCode()`
* Add `$validationFailedStatusCode` argument to `#[MapQueryParameter]` that allows setting a custom HTTP status code when validation fails
* Add `NearMissValueResolverException` to let value resolvers report when an argument could be under their watch but failed to be resolved
* Add `MapSessionParameter` to map an object to the session
* Add `MapSessionParameter` to pass a session parameter to a controller argument

7.0
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,27 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
return [];
}

if ((!$type = $argument->getType()) || (!class_exists($type) && !interface_exists($type))) {
if ((!$type = $argument->getType()) || (!class_exists($type) && !interface_exists($type, false))) {
throw new \LogicException(sprintf('#[MapSessionParameter] cannot be used on controller argument "$%s": "%s" is not a class or interface name.', $argument->getName(), $type));
}

if (interface_exists($type) && !$argument->hasDefaultValue() && !$argument->isNullable()) {
if (interface_exists($type, false) && !$argument->hasDefaultValue() && !$argument->isNullable()) {
throw new \LogicException(sprintf('#[MapSessionParameter] cannot be used on controller argument "$%s": "%s" is an interface, you need to make the parameter nullable or provide a default value.', $argument->getName(), $type));
}

if ($request->getSession()->has($argument->getName())) {
if (!$request->getSession()->get($argument->getName()) instanceof $type) {
throw new \LogicException(sprintf('#[MapSessionParameter] Can\'t provide a value for controller argument "$%s": Session contain an instance of "%s" which is not an instance of "%s".', $argument->getName(), \get_class($request->getSession()->get($argument->getName())), $type));
$value = $request->getSession()->get($argument->getName());
if (!$value instanceof $type) {
throw new \LogicException(sprintf('#[MapSessionParameter] cannot be used to map controller argument "$%s": the session contains a value of type "%s" which is not an instance of "%s".', $argument->getName(), get_debug_type($value), $type));
}

return [
$request->getSession()->get($argument->getName()),
];
return [$value];
}

if (\is_object($object = $argument->hasDefaultValue() ? $argument->getDefaultValue() : new $type())) {
$request->getSession()->set($argument->getName(), $object);
if (\is_object($value = $argument->hasDefaultValue() ? $argument->getDefaultValue() : new $type())) {
$request->getSession()->set($argument->getName(), $value);
}

return [$object];
return [$value];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,57 +60,6 @@ public function testResolvingWithInvalidArgumentType(ArgumentMetadata $metadata,
$this->resolver->resolve($this->request, $metadata);
}

/**
* @dataProvider validDataProvider
*/
public function testResolvingSuccessfully(ArgumentMetadata $metadata, ?string $expectedType)
{
$result = $this->resolver->resolve($this->request, $metadata);
$this->assertCount(1, $result);

if (null === $expectedType) {
$this->assertNull($result[0]);
} else {
$this->assertInstanceOf($expectedType, $result[0]);
}
}

public function testResolvingCorrectTypeSuccessfully()
{
$this->request->getSession()->set('MySessionObject', new ExtendingEmptySessionParameter());
$result = $this->resolver->resolve($this->request, new ArgumentMetadata('MySessionObject', EmptySessionParameter::class, false, false, false, attributes: [new MapSessionParameter()]));

$this->assertCount(1, $result);
$this->assertInstanceOf(EmptySessionParameter::class, $result[0]);
}

public function testResolvingCorrectInterfaceSuccessfully()
{
$this->request->getSession()->set('MySessionObject', new BasicSessionParameter());
$result = $this->resolver->resolve($this->request, new ArgumentMetadata('MySessionObject', SessionParameterInterface::class, false, false, false, isNullable: true, attributes: [new MapSessionParameter()]));

$this->assertCount(1, $result);
$this->assertInstanceOf(SessionParameterInterface::class, $result[0]);
}

public function testResolvingIncorrectTypeFailure()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('#[MapSessionParameter] Can\'t provide a value for controller argument "$MySessionObject": Session contain an instance of "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\BasicSessionParameter" which is not an instance of "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\EmptySessionParameter".');

$this->request->getSession()->set('MySessionObject', new BasicSessionParameter());
$this->resolver->resolve($this->request, new ArgumentMetadata('MySessionObject', EmptySessionParameter::class, false, false, false, attributes: [new MapSessionParameter()]));
}

public function testResolvingIncorrectInterfaceFailure()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('#[MapSessionParameter] Can\'t provide a value for controller argument "$MySessionObject": Session contain an instance of "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\EmptySessionParameter" which is not an instance of "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\SessionParameterInterface".');

$this->request->getSession()->set('MySessionObject', new EmptySessionParameter());
$this->resolver->resolve($this->request, new ArgumentMetadata('MySessionObject', SessionParameterInterface::class, false, false, false, isNullable: true, attributes: [new MapSessionParameter()]));
}

/**
* @return iterable<string, array{ArgumentMetadata,string}>
*/
Expand All @@ -137,6 +86,21 @@ public static function invalidArgumentTypeProvider(): iterable
];
}

/**
* @dataProvider validDataProvider
*/
public function testResolvingSuccessfully(ArgumentMetadata $metadata, ?string $expectedType)
{
$result = $this->resolver->resolve($this->request, $metadata);
$this->assertCount(1, $result);

if (null === $expectedType) {
$this->assertNull($result[0]);
} else {
$this->assertInstanceOf($expectedType, $result[0]);
}
}

/**
* @return iterable<string, array{ArgumentMetadata,string|null}>
*/
Expand Down Expand Up @@ -175,6 +139,42 @@ public static function validDataProvider(): iterable
BasicSessionParameter::class,
];
}

public function testResolvingCorrectTypeSuccessfully()
{
$this->request->getSession()->set('MySessionObject', new ExtendingEmptySessionParameter());
$result = $this->resolver->resolve($this->request, new ArgumentMetadata('MySessionObject', EmptySessionParameter::class, false, false, false, attributes: [new MapSessionParameter()]));

$this->assertCount(1, $result);
$this->assertInstanceOf(EmptySessionParameter::class, $result[0]);
}

public function testResolvingCorrectInterfaceSuccessfully()
{
$this->request->getSession()->set('MySessionObject', new BasicSessionParameter());
$result = $this->resolver->resolve($this->request, new ArgumentMetadata('MySessionObject', SessionParameterInterface::class, false, false, false, isNullable: true, attributes: [new MapSessionParameter()]));

$this->assertCount(1, $result);
$this->assertInstanceOf(SessionParameterInterface::class, $result[0]);
}

public function testResolvingIncorrectTypeFailure()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('#[MapSessionParameter] cannot be used to map controller argument "$MySessionObject": the session contains a value of type "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\BasicSessionParameter" which is not an instance of "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\EmptySessionParameter".');

$this->request->getSession()->set('MySessionObject', new BasicSessionParameter());
$this->resolver->resolve($this->request, new ArgumentMetadata('MySessionObject', EmptySessionParameter::class, false, false, false, attributes: [new MapSessionParameter()]));
}

public function testResolvingIncorrectInterfaceFailure()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('#[MapSessionParameter] cannot be used to map controller argument "$MySessionObject": the session contains a value of type "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\EmptySessionParameter" which is not an instance of "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\SessionParameterInterface".');

$this->request->getSession()->set('MySessionObject', new EmptySessionParameter());
$this->resolver->resolve($this->request, new ArgumentMetadata('MySessionObject', SessionParameterInterface::class, false, false, false, isNullable: true, attributes: [new MapSessionParameter()]));
}
}

class BasicSessionParameter implements SessionParameterInterface
Expand Down

0 comments on commit 93fd9a9

Please sign in to comment.