|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | +use Symfony\Component\HttpKernel\Controller\ArgumentResolver\UidValueResolver; |
| 17 | +use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; |
| 18 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 19 | +use Symfony\Component\Uid\AbstractUid; |
| 20 | +use Symfony\Component\Uid\Factory\UlidFactory; |
| 21 | +use Symfony\Component\Uid\Ulid; |
| 22 | +use Symfony\Component\Uid\UuidV1; |
| 23 | +use Symfony\Component\Uid\UuidV4; |
| 24 | + |
| 25 | +class UidValueResolverTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @dataProvider provideSupports |
| 29 | + */ |
| 30 | + public function testSupports(bool $expected, Request $request, ArgumentMetadata $argument) |
| 31 | + { |
| 32 | + $this->assertSame($expected, (new UidValueResolver())->supports($request, $argument)); |
| 33 | + } |
| 34 | + |
| 35 | + public function provideSupports() |
| 36 | + { |
| 37 | + return [ |
| 38 | + 'Variadic argument' => [false, new Request([], [], ['foo' => (string) $uuidV4 = new UuidV4()]), new ArgumentMetadata('foo', UuidV4::class, true, false, null)], |
| 39 | + 'No attribute for argument' => [false, new Request([], [], []), new ArgumentMetadata('foo', UuidV4::class, false, false, null)], |
| 40 | + 'Attribute is not a string' => [false, new Request([], [], ['foo' => ['bar']]), new ArgumentMetadata('foo', UuidV4::class, false, false, null)], |
| 41 | + 'Argument has no type' => [false, new Request([], [], ['foo' => (string) $uuidV4]), new ArgumentMetadata('foo', null, false, false, null)], |
| 42 | + 'Argument type is not a class' => [false, new Request([], [], ['foo' => (string) $uuidV4]), new ArgumentMetadata('foo', 'string', false, false, null)], |
| 43 | + 'Argument type is not a subclass of AbstractUid' => [false, new Request([], [], ['foo' => (string) $uuidV4]), new ArgumentMetadata('foo', UlidFactory::class, false, false, null)], |
| 44 | + 'AbstractUid is not supported' => [false, new Request([], [], ['foo' => (string) $uuidV4]), new ArgumentMetadata('foo', AbstractUid::class, false, false, null)], |
| 45 | + 'Custom abstract subclass is supported but will fail in resolve' => [true, new Request([], [], ['foo' => (string) $uuidV4]), new ArgumentMetadata('foo', TestAbstractCustomUid::class, false, false, null)], |
| 46 | + 'Known subclass' => [true, new Request([], [], ['foo' => (string) $uuidV4]), new ArgumentMetadata('foo', UuidV4::class, false, false, null)], |
| 47 | + 'Format does not matter' => [true, new Request([], [], ['foo' => (string) $uuidV4]), new ArgumentMetadata('foo', Ulid::class, false, false, null)], |
| 48 | + 'Custom subclass' => [true, new Request([], [], ['foo' => '01FPND7BD15ZV07X5VGDXAJ8VD']), new ArgumentMetadata('foo', TestCustomUid::class, false, false, null)], |
| 49 | + ]; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @dataProvider provideResolveOK |
| 54 | + */ |
| 55 | + public function testResolveOK(AbstractUid $expected, string $requestUid) |
| 56 | + { |
| 57 | + $this->assertEquals([$expected], (new UidValueResolver())->resolve( |
| 58 | + new Request([], [], ['id' => $requestUid]), |
| 59 | + new ArgumentMetadata('id', \get_class($expected), false, false, null) |
| 60 | + )); |
| 61 | + } |
| 62 | + |
| 63 | + public function provideResolveOK() |
| 64 | + { |
| 65 | + return [ |
| 66 | + [$uuidV1 = new UuidV1(), (string) $uuidV1], |
| 67 | + [$uuidV1, $uuidV1->toBase58()], |
| 68 | + [$uuidV1, $uuidV1->toBase32()], |
| 69 | + [$ulid = Ulid::fromBase32('01FQC6Y03WDZ73DQY9RXQMPHB1'), (string) $ulid], |
| 70 | + [$ulid, $ulid->toBase58()], |
| 71 | + [$ulid, $ulid->toRfc4122()], |
| 72 | + [$customUid = new TestCustomUid(), (string) $customUid], |
| 73 | + [$customUid, $customUid->toBase58()], |
| 74 | + [$customUid, $customUid->toBase32()], |
| 75 | + ]; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @dataProvider provideResolveKO |
| 80 | + */ |
| 81 | + public function testResolveKO(string $requestUid, string $argumentType) |
| 82 | + { |
| 83 | + $this->expectException(NotFoundHttpException::class); |
| 84 | + $this->expectExceptionMessage('The uid for the "id" parameter is invalid.'); |
| 85 | + |
| 86 | + (new UidValueResolver())->resolve( |
| 87 | + new Request([], [], ['id' => $requestUid]), |
| 88 | + new ArgumentMetadata('id', $argumentType, false, false, null) |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + public function provideResolveKO() |
| 93 | + { |
| 94 | + return [ |
| 95 | + 'Bad value for UUID' => ['ccc', UuidV1::class], |
| 96 | + 'Bad value for ULID' => ['ccc', Ulid::class], |
| 97 | + 'Bad value for custom UID' => ['ccc', TestCustomUid::class], |
| 98 | + 'Bad UUID version' => [(string) new UuidV4(), UuidV1::class], |
| 99 | + ]; |
| 100 | + } |
| 101 | + |
| 102 | + public function testResolveAbstractClass() |
| 103 | + { |
| 104 | + $this->expectException(\Error::class); |
| 105 | + $this->expectExceptionMessage('Cannot instantiate abstract class Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\TestAbstractCustomUid'); |
| 106 | + |
| 107 | + (new UidValueResolver())->resolve( |
| 108 | + new Request([], [], ['id' => (string) new UuidV1()]), |
| 109 | + new ArgumentMetadata('id', TestAbstractCustomUid::class, false, false, null) |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +class TestCustomUid extends UuidV1 |
| 115 | +{ |
| 116 | +} |
| 117 | + |
| 118 | +abstract class TestAbstractCustomUid extends UuidV1 |
| 119 | +{ |
| 120 | +} |
0 commit comments