Skip to content

Commit

Permalink
Update attributed test controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
Zakhar Shokel committed Apr 26, 2024
1 parent 9cd3615 commit 800cf0f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 90 deletions.
3 changes: 2 additions & 1 deletion src/DependencyInjection/PayseraApiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'])]
Expand All @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,81 +12,66 @@
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
{
// should fail as we don't pass any body
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
{
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
{
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
{
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
Expand All @@ -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
Expand All @@ -106,132 +89,104 @@ 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
{
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
{
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
{
// should fail before calling controller
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
{
// should fail validation
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
{
// should fail validation
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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<import resource="../../Controller/Attribute/" type="annotation"/>
<import resource="../../Controller/Attribute/" type="attribute"/>
</routes>

0 comments on commit 800cf0f

Please sign in to comment.