From 800cf0fe7577e55a470cd040d0fa9607e26ba1c2 Mon Sep 17 00:00:00 2001 From: Zakhar Shokel Date: Fri, 26 Apr 2024 11:50:55 +0300 Subject: [PATCH] Update attributed test controllers --- .../PayseraApiExtension.php | 3 +- ...utedClassRequiredPermissionsController.php | 10 +- .../AttributedClassValidationController.php | 9 +- .../Attribute/AttributedController.php | 103 +++++------------- .../Resources/config/attributed_routing.xml | 2 +- 5 files changed, 37 insertions(+), 90 deletions(-) diff --git a/src/DependencyInjection/PayseraApiExtension.php b/src/DependencyInjection/PayseraApiExtension.php index 19728fc..2a1db01 100644 --- a/src/DependencyInjection/PayseraApiExtension.php +++ b/src/DependencyInjection/PayseraApiExtension.php @@ -28,7 +28,8 @@ public function load(array $configs, ContainerBuilder $container) $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config/services')); class_exists(AttributeRouteControllerLoader::class) ? $loader->load('attributes.xml') - : $loader->load('annotations.xml'); + : $loader->load('annotations.xml') + ; $container->setParameter('paysera_api.locales', $config['locales']); if (count($config['locales']) === 0) { diff --git a/tests/Functional/Fixtures/FixtureTestBundle/Controller/Attribute/AttributedClassRequiredPermissionsController.php b/tests/Functional/Fixtures/FixtureTestBundle/Controller/Attribute/AttributedClassRequiredPermissionsController.php index be275b5..f93ebd4 100644 --- a/tests/Functional/Fixtures/FixtureTestBundle/Controller/Attribute/AttributedClassRequiredPermissionsController.php +++ b/tests/Functional/Fixtures/FixtureTestBundle/Controller/Attribute/AttributedClassRequiredPermissionsController.php @@ -4,15 +4,15 @@ namespace Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Controller\Attribute; use Paysera\Bundle\ApiBundle\Attribute\RequiredPermissions; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; #[RequiredPermissions(permissions: ['ROLE_USER'])] class AttributedClassRequiredPermissionsController { - /** - * @Route(path="/attributed/class/testRequiredPermissions", methods={"GET"}) - */ + + #[Route(path: '/attributed/class/testRequiredPermissions', methods: Request::METHOD_GET)] #[RequiredPermissions(permissions: ['ROLE_USER'])] #[RequiredPermissions(permissions: ['ROLE_ADMIN'])] #[RequiredPermissions(permissions: ['ROLE_USER'])] @@ -21,9 +21,7 @@ public function test(): Response return new Response('OK'); } - /** - * @Route(path="/attributed/class/simpleAction", methods={"GET"}) - */ + #[Route(path: '/attributed/class/simpleAction', methods: Request::METHOD_GET)] public function simpleAction(): Response { return new Response('OK'); diff --git a/tests/Functional/Fixtures/FixtureTestBundle/Controller/Attribute/AttributedClassValidationController.php b/tests/Functional/Fixtures/FixtureTestBundle/Controller/Attribute/AttributedClassValidationController.php index 8d0ff9b..63d74dd 100644 --- a/tests/Functional/Fixtures/FixtureTestBundle/Controller/Attribute/AttributedClassValidationController.php +++ b/tests/Functional/Fixtures/FixtureTestBundle/Controller/Attribute/AttributedClassValidationController.php @@ -6,15 +6,14 @@ use Paysera\Bundle\ApiBundle\Attribute\Body; use Paysera\Bundle\ApiBundle\Attribute\Validation; use Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Entity\MyObject; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; #[Validation(groups: ['internal_field1_email'], violationPathMap: ['internalField1' => 'internal.field1'])] class AttributedClassValidationController { - /** - * @Route(path="/attributed/class/testValidation", methods={"POST"}) - */ + #[Route(path: '/attributed/class/testValidation', methods: Request::METHOD_POST)] #[Body(parameterName: 'resource')] #[Validation(groups: ['field1_email'], violationPathMap: ['field1' => 'my_mapped_key'])] public function testValidation(MyObject $resource): Response @@ -23,9 +22,7 @@ public function testValidation(MyObject $resource): Response return new Response('FAIL'); } - /** - * @Route(path="/attributed/class/testValidationFromClass", methods={"POST"}) - */ + #[Route(path: '/attributed/class/testValidationFromClass', methods: Request::METHOD_POST)] #[Body(parameterName: 'resource')] public function testValidationFromClass(MyObject $resource): Response { diff --git a/tests/Functional/Fixtures/FixtureTestBundle/Controller/Attribute/AttributedController.php b/tests/Functional/Fixtures/FixtureTestBundle/Controller/Attribute/AttributedController.php index c8fe1ce..6544ab7 100644 --- a/tests/Functional/Fixtures/FixtureTestBundle/Controller/Attribute/AttributedController.php +++ b/tests/Functional/Fixtures/FixtureTestBundle/Controller/Attribute/AttributedController.php @@ -12,32 +12,27 @@ use Paysera\Bundle\ApiBundle\Attribute\Validation; use Paysera\Bundle\ApiBundle\Tests\Functional\Fixtures\FixtureTestBundle\Entity\MyObject; use Paysera\Pagination\Entity\Pager; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; class AttributedController { - /** - * @Route(path="/attributed/testBodyNormalizationWithExtractedKeyValue", methods={"POST"}) - */ + #[Route(path: '/attributed/testBodyNormalizationWithExtractedKeyValue', methods: Request::METHOD_POST)] #[Body(parameterName: 'keyValueInBody', denormalizationType: 'extract:key')] public function testBodyNormalizationWithExtractedKeyValue(string $keyValueInBody = 'default'): Response { return new Response($keyValueInBody); } - /** - * @Route(path="/attributed/testBodyNormalizationWithDenormalizationGroup", methods={"POST"}) - */ + #[Route(path: '/attributed/testBodyNormalizationWithDenormalizationGroup', methods: Request::METHOD_POST)] #[Body(parameterName: 'keyValueInBody', denormalizationType: 'extract:key', denormalizationGroup: 'custom')] public function testBodyNormalizationWithDenormalizationGroup(string $keyValueInBody = 'default'): Response { return new Response($keyValueInBody); } - /** - * @Route(path="/attributed/testBodyNormalizationWithRequiredBody", methods={"POST"}) - */ + #[Route(path: '/attributed/testBodyNormalizationWithRequiredBody', methods: Request::METHOD_POST)] #[Body(parameterName: 'body', denormalizationType: 'extract:key')] public function testBodyNormalizationWithRequiredBody(string $body): Response { @@ -45,18 +40,14 @@ public function testBodyNormalizationWithRequiredBody(string $body): Response return new Response('FAIL'); } - /** - * @Route(path="/attributed/testBodyAndResponseNormalization", methods={"POST"}) - */ + #[Route(path: '/attributed/testBodyAndResponseNormalization', methods: Request::METHOD_POST)] #[Body(parameterName: 'resource')] public function testBodyAndResponseNormalization(MyObject $resource): MyObject { return $resource; } - /** - * @Route(path="/attributed/testBodyNormalizationWithCustomContentType", methods={"POST"}) - */ + #[Route(path: '/attributed/testBodyNormalizationWithCustomContentType', methods: Request::METHOD_POST)] #[Body(parameterName: 'body', denormalizationType: 'prefixed')] #[BodyContentType(supportedContentTypes: ['text/plain'])] public function testBodyNormalizationWithCustomContentType(string $body): Response @@ -64,9 +55,7 @@ public function testBodyNormalizationWithCustomContentType(string $body): Respon return new Response($body); } - /** - * @Route(path="/attributed/testBodyNormalizationWithCustomContentTypeAndJsonDecode", methods={"POST"}) - */ + #[Route(path: '/attributed/testBodyNormalizationWithCustomContentTypeAndJsonDecode', methods: Request::METHOD_POST)] #[Body(parameterName: 'keyValueInBody', denormalizationType: 'extract:key')] #[BodyContentType(supportedContentTypes: ['text/plain'], jsonEncodedBody: true)] public function testBodyNormalizationWithCustomContentTypeAndJsonDecode(string $keyValueInBody): Response @@ -74,9 +63,7 @@ public function testBodyNormalizationWithCustomContentTypeAndJsonDecode(string $ return new Response($keyValueInBody); } - /** - * @Route(path="/attributed/testBodyNormalizationWithSemiContentTypeRestriction", methods={"POST"}) - */ + #[Route(path: '/attributed/testBodyNormalizationWithSemiContentTypeRestriction', methods: Request::METHOD_POST)] #[Body(parameterName: 'body', denormalizationType: 'prefixed')] #[BodyContentType(supportedContentTypes: ['image/jpeg', 'text/*'])] public function testBodyNormalizationWithSemiContentTypeRestriction(string $body): Response @@ -84,9 +71,7 @@ public function testBodyNormalizationWithSemiContentTypeRestriction(string $body return new Response($body); } - /** - * @Route(path="/attributed/testBodyNormalizationWithValidation", methods={"POST"}) - */ + #[Route(path: '/attributed/testBodyNormalizationWithValidation', methods: Request::METHOD_POST)] #[Body(parameterName: 'resource')] #[Validation(groups: ['field1_email'], violationPathMap: ['field1' => 'my_mapped_key'])] public function testBodyNormalizationWithValidation(MyObject $resource): Response @@ -95,9 +80,7 @@ public function testBodyNormalizationWithValidation(MyObject $resource): Respons return new Response('FAIL'); } - /** - * @Route(path="/attributed/testBodyNormalizationWithInnerTypeValidation", methods={"POST"}) - */ + #[Route(path: '/attributed/testBodyNormalizationWithInnerTypeValidation', methods: Request::METHOD_POST)] #[Body(parameterName: 'resource')] #[Validation(groups: ['internal_field1_email'])] public function testBodyNormalizationWithInnerTypeValidation(MyObject $resource): Response @@ -106,9 +89,7 @@ public function testBodyNormalizationWithInnerTypeValidation(MyObject $resource) return new Response('FAIL'); } - /** - * @Route(path="/attributed/testBodyValidationCanBeTurnedOff", methods={"POST"}) - */ + #[Route(path: '/attributed/testBodyValidationCanBeTurnedOff', methods: Request::METHOD_POST)] #[Body(parameterName: 'resource')] #[Validation(enabled: false)] public function testBodyValidationCanBeTurnedOff(MyObject $resource): Response @@ -116,9 +97,7 @@ public function testBodyValidationCanBeTurnedOff(MyObject $resource): Response return new Response('OK'); } - /** - * @Route(path="/attributed/testBodyValidationCanBeTurnedOffWithEmptyGroups", methods={"POST"}) - */ + #[Route(path: '/attributed/testBodyValidationCanBeTurnedOffWithEmptyGroups', methods: Request::METHOD_POST)] #[Body(parameterName: 'resource')] #[Validation(groups: [])] public function testBodyValidationCanBeTurnedOffWithEmptyGroups(MyObject $resource): Response @@ -126,28 +105,22 @@ public function testBodyValidationCanBeTurnedOffWithEmptyGroups(MyObject $resour return new Response('OK'); } - /** - * @Route(path="/attributed/testPathAttribute/{id}", methods={"GET"}) - * @Route(path="/attributed/testPathAttribute", methods={"GET"}) - */ + #[Route(path: '/attributed/testPathAttribute/{id}', methods: Request::METHOD_GET)] + #[Route(path: '/attributed/testPathAttribute', methods: Request::METHOD_GET)] #[PathAttribute(parameterName: 'parameter', pathPartName: 'id', resolverType: 'prefixed')] public function testPathAttribute(string $parameter = 'default'): Response { return new Response($parameter); } - /** - * @Route(path="/attributed/testPathAttributeWithFindingObject/{id}", methods={"GET"}) - */ + #[Route(path: '/attributed/testPathAttributeWithFindingObject/{id}', methods: Request::METHOD_GET)] #[PathAttribute(parameterName: 'myObject', pathPartName: 'id')] public function testPathAttributeWithFindingObject(MyObject $myObject): Response { return new Response($myObject->getField1()); } - /** - * @Route(path="/attributed/testPathAttributeWithFailedResolution/{id}", methods={"GET"}) - */ + #[Route(path: '/attributed/testPathAttributeWithFailedResolution/{id}', methods: Request::METHOD_GET)] #[PathAttribute(parameterName: 'myObject', pathPartName: 'id', resolverType: 'always_null')] public function testPathAttributeWithFailedResolution(MyObject $myObject): Response { @@ -155,36 +128,28 @@ public function testPathAttributeWithFailedResolution(MyObject $myObject): Respo return new Response('FAIL'); } - /** - * @Route(path="/attributed/testQueryResolver", methods={"GET"}) - */ + #[Route(path: '/attributed/testQueryResolver', methods: Request::METHOD_GET)] #[Query(parameterName: 'parameter', denormalizationType: 'extract:parameter')] public function testQueryResolver(string $parameter): Response { return new Response($parameter); } - /** - * @Route(path="/attributed/testQueryResolverWithDenormalizationGroup", methods={"GET"}) - */ + #[Route(path: '/attributed/testQueryResolverWithDenormalizationGroup', methods: Request::METHOD_GET)] #[Query(parameterName: 'parameter', denormalizationType: 'extract:parameter', denormalizationGroup: 'custom')] public function testQueryResolverWithDenormalizationGroup(string $parameter): Response { return new Response($parameter); } - /** - * @Route(path="/attributed/testQueryResolverPagerLimitIs42", methods={"GET"}) - */ + #[Route(path: '//attributed/testQueryResolverPagerLimitIs42', methods: Request::METHOD_GET)] #[Query(parameterName: 'pager')] public function testQueryResolverPagerLimitIs42(Pager $pager): Response { return new Response($pager->getLimit() === 42 ? 'OK' : 'FAIL'); } - /** - * @Route(path="/attributed/testQueryResolverHasDefaultValidation", methods={"GET"}) - */ + #[Route(path: '/attributed/testQueryResolverHasDefaultValidation', methods: Request::METHOD_GET)] #[Query(parameterName: 'myObject')] public function testQueryResolverHasDefaultValidation(MyObject $myObject): Response { @@ -192,27 +157,21 @@ public function testQueryResolverHasDefaultValidation(MyObject $myObject): Respo return new Response('FAIL'); } - /** - * @Route(path="/attributed/testQueryResolverCanTurnOffValidation", methods={"GET"}) - */ + #[Route(path: '/attributed/testQueryResolverCanTurnOffValidation', methods: Request::METHOD_GET)] #[Query(parameterName: 'myObject', validation: new Validation(enabled: false))] public function testQueryResolverCanTurnOffValidation(MyObject $myObject): Response { return new Response('OK'); } - /** - * @Route(path="/attributed/testQueryResolverCanTurnOffValidationWithEmptyGroups", methods={"GET"}) - */ + #[Route(path: '/attributed/testQueryResolverCanTurnOffValidationWithEmptyGroups', methods: Request::METHOD_GET)] #[Query(parameterName: 'myObject', validation: new Validation(groups: []))] public function testQueryResolverCanTurnOffValidationWithEmptyGroups(MyObject $myObject): Response { return new Response('OK'); } - /** - * @Route(path="/attributed/testQueryResolverValidationWithInvalidData", methods={"GET"}) - */ + #[Route(path: '/attributed/testQueryResolverValidationWithInvalidData', methods: Request::METHOD_GET)] #[Query(parameterName: 'myObject', validation: new Validation(groups: ['field1_email'], violationPathMap: ['field1' => 'mapped_key']))] public function testQueryResolverValidationWithInvalidData(MyObject $myObject): Response { @@ -220,18 +179,14 @@ public function testQueryResolverValidationWithInvalidData(MyObject $myObject): return new Response('FAIL'); } - /** - * @Route(path="/attributed/testRequiredPermissions", methods={"GET"}) - */ + #[Route(path: '/attributed/testRequiredPermissions', methods: Request::METHOD_GET)] #[RequiredPermissions(permissions: ['ROLE_USER', 'ROLE_ADMIN'])] public function testRequiredPermissions(): Response { return new Response('OK'); } - /** - * @Route(path="/attributed/testResponseNormalization", methods={"GET"}) - */ + #[Route(path: '/attributed/testResponseNormalization', methods: Request::METHOD_GET)] #[ResponseNormalization(normalizationType: 'my_object_custom')] public function testResponseNormalization(): MyObject { @@ -240,9 +195,7 @@ public function testResponseNormalization(): MyObject ; } - /** - * @Route(path="/attributed/testResponseNormalizationWithNormalizationGroup", methods={"GET"}) - */ + #[Route(path: '/attributed/testResponseNormalizationWithNormalizationGroup', methods: Request::METHOD_GET)] #[ResponseNormalization(normalizationGroup: 'custom')] public function testResponseNormalizationWithNormalizationGroup(): MyObject { @@ -251,9 +204,7 @@ public function testResponseNormalizationWithNormalizationGroup(): MyObject ; } - /** - * @Route(path="/attributed/testResponseNormalizationWithGuessedNormalizer", methods={"GET"}) - */ + #[Route(path: '/attributed/testResponseNormalizationWithGuessedNormalizer', methods: Request::METHOD_GET)] #[ResponseNormalization] public function testResponseNormalizationWithGuessedNormalizer(): MyObject { diff --git a/tests/Functional/Fixtures/FixtureTestBundle/Resources/config/attributed_routing.xml b/tests/Functional/Fixtures/FixtureTestBundle/Resources/config/attributed_routing.xml index 754665c..253529a 100644 --- a/tests/Functional/Fixtures/FixtureTestBundle/Resources/config/attributed_routing.xml +++ b/tests/Functional/Fixtures/FixtureTestBundle/Resources/config/attributed_routing.xml @@ -4,5 +4,5 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/routing https://symfony.com/schema/routing/routing-1.0.xsd"> - +