Skip to content

Commit

Permalink
Refactor test controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
Zakhar Shokel committed Apr 22, 2024
1 parent 6721a38 commit 8d73ff6
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,16 @@ class AnnotatedClassRequiredPermissionsController
* @RequiredPermissions(permissions={"ROLE_USER"})
* @RequiredPermissions(permissions={"ROLE_ADMIN"})
* @RequiredPermissions(permissions={"ROLE_USER"})
*
* @return Response
*/
public function test()
public function test(): Response
{
return new Response('OK');
}

/**
* @Route(path="/annotated/class/simpleAction", methods={"GET"})
*
* @return Response
*/
public function simpleAction()
public function simpleAction(): Response
{
return new Response('OK');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ class AnnotatedClassValidationController
*
* @Body(parameterName="resource")
* @Validation(groups={"field1_email"}, violationPathMap={"field1": "my_mapped_key"})
*
* @param MyObject $resource
* @return Response
*/
public function testValidation(MyObject $resource)
public function testValidation(MyObject $resource): Response
{
// should fail validation
return new Response('FAIL');
Expand All @@ -33,11 +30,8 @@ public function testValidation(MyObject $resource)
* @Route(path="/annotated/class/testValidationFromClass", methods={"POST"})
*
* @Body(parameterName="resource")
*
* @param MyObject $resource
* @return Response
*/
public function testValidationFromClass(MyObject $resource)
public function testValidationFromClass(MyObject $resource): Response
{
// should fail validation
return new Response('FAIL');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ class AnnotatedController
* @Route(path="/annotated/testBodyNormalizationWithExtractedKeyValue", methods={"POST"})
*
* @Body(parameterName="keyValueInBody", denormalizationType="extract:key")
*
* @param string $keyValueInBody
* @return Response
*/
public function testBodyNormalizationWithExtractedKeyValue(string $keyValueInBody = 'default')
public function testBodyNormalizationWithExtractedKeyValue(string $keyValueInBody = 'default'): Response
{
return new Response($keyValueInBody);
}
Expand All @@ -34,11 +31,8 @@ public function testBodyNormalizationWithExtractedKeyValue(string $keyValueInBod
* @Route(path="/annotated/testBodyNormalizationWithDenormalizationGroup", methods={"POST"})
*
* @Body(parameterName="keyValueInBody", denormalizationType="extract:key", denormalizationGroup="custom")
*
* @param string $keyValueInBody
* @return Response
*/
public function testBodyNormalizationWithDenormalizationGroup(string $keyValueInBody = 'default')
public function testBodyNormalizationWithDenormalizationGroup(string $keyValueInBody = 'default'): Response
{
return new Response($keyValueInBody);
}
Expand All @@ -47,11 +41,8 @@ public function testBodyNormalizationWithDenormalizationGroup(string $keyValueIn
* @Route(path="/annotated/testBodyNormalizationWithRequiredBody", methods={"POST"})
*
* @Body(parameterName="body", denormalizationType="extract:key")
*
* @param string $body
* @return Response
*/
public function testBodyNormalizationWithRequiredBody(string $body)
public function testBodyNormalizationWithRequiredBody(string $body): Response
{
// should fail as we don't pass any body
return new Response('FAIL');
Expand All @@ -61,11 +52,8 @@ public function testBodyNormalizationWithRequiredBody(string $body)
* @Route(path="/annotated/testBodyAndResponseNormalization", methods={"POST"})
*
* @Body(parameterName="resource")
*
* @param MyObject $resource
* @return MyObject
*/
public function testBodyAndResponseNormalization(MyObject $resource)
public function testBodyAndResponseNormalization(MyObject $resource): MyObject
{
return $resource;
}
Expand All @@ -75,11 +63,8 @@ public function testBodyAndResponseNormalization(MyObject $resource)
*
* @Body(parameterName="body", denormalizationType="prefixed")
* @BodyContentType(supportedContentTypes={"text/plain"})
*
* @param string $body
* @return Response
*/
public function testBodyNormalizationWithCustomContentType(string $body)
public function testBodyNormalizationWithCustomContentType(string $body): Response
{
return new Response($body);
}
Expand All @@ -89,11 +74,8 @@ public function testBodyNormalizationWithCustomContentType(string $body)
*
* @Body(parameterName="keyValueInBody", denormalizationType="extract:key")
* @BodyContentType(supportedContentTypes={"text/plain"}, jsonEncodedBody=true)
*
* @param string $keyValueInBody
* @return Response
*/
public function testBodyNormalizationWithCustomContentTypeAndJsonDecode(string $keyValueInBody)
public function testBodyNormalizationWithCustomContentTypeAndJsonDecode(string $keyValueInBody): Response
{
return new Response($keyValueInBody);
}
Expand All @@ -103,11 +85,8 @@ public function testBodyNormalizationWithCustomContentTypeAndJsonDecode(string $
*
* @Body(parameterName="body", denormalizationType="prefixed")
* @BodyContentType(supportedContentTypes={"image/jpeg", "text/*"})
*
* @param string $body
* @return Response
*/
public function testBodyNormalizationWithSemiContentTypeRestriction(string $body)
public function testBodyNormalizationWithSemiContentTypeRestriction(string $body): Response
{
return new Response($body);
}
Expand All @@ -117,11 +96,8 @@ public function testBodyNormalizationWithSemiContentTypeRestriction(string $body
*
* @Body(parameterName="resource")
* @Validation(groups={"field1_email"}, violationPathMap={"field1": "my_mapped_key"})
*
* @param MyObject $resource
* @return Response
*/
public function testBodyNormalizationWithValidation(MyObject $resource)
public function testBodyNormalizationWithValidation(MyObject $resource): Response
{
// should fail validation
return new Response('FAIL');
Expand All @@ -132,11 +108,8 @@ public function testBodyNormalizationWithValidation(MyObject $resource)
*
* @Body(parameterName="resource")
* @Validation(groups={"internal_field1_email"})
*
* @param MyObject $resource
* @return Response
*/
public function testBodyNormalizationWithInnerTypeValidation(MyObject $resource)
public function testBodyNormalizationWithInnerTypeValidation(MyObject $resource): Response
{
// should fail validation
return new Response('FAIL');
Expand All @@ -147,11 +120,8 @@ public function testBodyNormalizationWithInnerTypeValidation(MyObject $resource)
*
* @Body(parameterName="resource")
* @Validation(enabled=false)
*
* @param MyObject $resource
* @return Response
*/
public function testBodyValidationCanBeTurnedOff(MyObject $resource)
public function testBodyValidationCanBeTurnedOff(MyObject $resource): Response
{
return new Response('OK');
}
Expand All @@ -161,11 +131,8 @@ public function testBodyValidationCanBeTurnedOff(MyObject $resource)
*
* @Body(parameterName="resource")
* @Validation(groups={})
*
* @param MyObject $resource
* @return Response
*/
public function testBodyValidationCanBeTurnedOffWithEmptyGroups(MyObject $resource)
public function testBodyValidationCanBeTurnedOffWithEmptyGroups(MyObject $resource): Response
{
return new Response('OK');
}
Expand All @@ -175,11 +142,8 @@ public function testBodyValidationCanBeTurnedOffWithEmptyGroups(MyObject $resour
* @Route(path="/annotated/testPathAttribute", methods={"GET"})
*
* @PathAttribute(parameterName="parameter", pathPartName="id", resolverType="prefixed")
*
* @param string $parameter
* @return Response
*/
public function testPathAttribute(string $parameter = 'default')
public function testPathAttribute(string $parameter = 'default'): Response
{
return new Response($parameter);
}
Expand All @@ -188,11 +152,8 @@ public function testPathAttribute(string $parameter = 'default')
* @Route(path="/annotated/testPathAttributeWithFindingObject/{id}", methods={"GET"})
*
* @PathAttribute(parameterName="myObject", pathPartName="id")
*
* @param MyObject $myObject
* @return Response
*/
public function testPathAttributeWithFindingObject(MyObject $myObject)
public function testPathAttributeWithFindingObject(MyObject $myObject): Response
{
return new Response($myObject->getField1());
}
Expand All @@ -201,11 +162,8 @@ public function testPathAttributeWithFindingObject(MyObject $myObject)
* @Route(path="/annotated/testPathAttributeWithFailedResolution/{id}", methods={"GET"})
*
* @PathAttribute(parameterName="myObject", pathPartName="id", resolverType="always_null")
*
* @param MyObject $myObject
* @return Response
*/
public function testPathAttributeWithFailedResolution(MyObject $myObject)
public function testPathAttributeWithFailedResolution(MyObject $myObject): Response
{
// should fail before calling controller
return new Response('FAIL');
Expand All @@ -215,11 +173,8 @@ public function testPathAttributeWithFailedResolution(MyObject $myObject)
* @Route(path="/annotated/testQueryResolver", methods={"GET"})
*
* @Query(parameterName="parameter", denormalizationType="extract:parameter")
*
* @param string $parameter
* @return Response
*/
public function testQueryResolver(string $parameter)
public function testQueryResolver(string $parameter): Response
{
return new Response($parameter);
}
Expand All @@ -228,11 +183,8 @@ public function testQueryResolver(string $parameter)
* @Route(path="/annotated/testQueryResolverWithDenormalizationGroup", methods={"GET"})
*
* @Query(parameterName="parameter", denormalizationType="extract:parameter", denormalizationGroup="custom")
*
* @param string $parameter
* @return Response
*/
public function testQueryResolverWithDenormalizationGroup(string $parameter)
public function testQueryResolverWithDenormalizationGroup(string $parameter): Response
{
return new Response($parameter);
}
Expand All @@ -241,11 +193,8 @@ public function testQueryResolverWithDenormalizationGroup(string $parameter)
* @Route(path="/annotated/testQueryResolverPagerLimitIs42", methods={"GET"})
*
* @Query(parameterName="pager")
*
* @param Pager $pager
* @return Response
*/
public function testQueryResolverPagerLimitIs42(Pager $pager)
public function testQueryResolverPagerLimitIs42(Pager $pager): Response
{
return new Response($pager->getLimit() === 42 ? 'OK' : 'FAIL');
}
Expand All @@ -254,11 +203,8 @@ public function testQueryResolverPagerLimitIs42(Pager $pager)
* @Route(path="/annotated/testQueryResolverHasDefaultValidation", methods={"GET"})
*
* @Query(parameterName="myObject")
*
* @param MyObject $myObject
* @return Response
*/
public function testQueryResolverHasDefaultValidation(MyObject $myObject)
public function testQueryResolverHasDefaultValidation(MyObject $myObject): Response
{
// should fail validation
return new Response('FAIL');
Expand All @@ -268,11 +214,8 @@ public function testQueryResolverHasDefaultValidation(MyObject $myObject)
* @Route(path="/annotated/testQueryResolverCanTurnOffValidation", methods={"GET"})
*
* @Query(parameterName="myObject", validation=@Validation(enabled=false))
*
* @param MyObject $myObject
* @return Response
*/
public function testQueryResolverCanTurnOffValidation(MyObject $myObject)
public function testQueryResolverCanTurnOffValidation(MyObject $myObject): Response
{
return new Response('OK');
}
Expand All @@ -281,11 +224,8 @@ public function testQueryResolverCanTurnOffValidation(MyObject $myObject)
* @Route(path="/annotated/testQueryResolverCanTurnOffValidationWithEmptyGroups", methods={"GET"})
*
* @Query(parameterName="myObject", validation=@Validation(groups={}))
*
* @param MyObject $myObject
* @return Response
*/
public function testQueryResolverCanTurnOffValidationWithEmptyGroups(MyObject $myObject)
public function testQueryResolverCanTurnOffValidationWithEmptyGroups(MyObject $myObject): Response
{
return new Response('OK');
}
Expand All @@ -297,11 +237,8 @@ public function testQueryResolverCanTurnOffValidationWithEmptyGroups(MyObject $m
* groups={"field1_email"},
* violationPathMap={"field1": "mapped_key"}
* ))
*
* @param MyObject $myObject
* @return Response
*/
public function testQueryResolverValidationWithInvalidData(MyObject $myObject)
public function testQueryResolverValidationWithInvalidData(MyObject $myObject): Response
{
// should fail validation
return new Response('FAIL');
Expand All @@ -311,10 +248,8 @@ public function testQueryResolverValidationWithInvalidData(MyObject $myObject)
* @Route(path="/annotated/testRequiredPermissions", methods={"GET"})
*
* @RequiredPermissions(permissions={"ROLE_USER", "ROLE_ADMIN"})
*
* @return Response
*/
public function testRequiredPermissions()
public function testRequiredPermissions(): Response
{
return new Response('OK');
}
Expand All @@ -323,35 +258,35 @@ public function testRequiredPermissions()
* @Route(path="/annotated/testResponseNormalization", methods={"GET"})
*
* @ResponseNormalization(normalizationType="my_object_custom")
*
* @return string
*/
public function testResponseNormalization()
public function testResponseNormalization(): MyObject
{
return (new MyObject())->setField1('hi');
return (new MyObject())
->setField1('hi')
;
}

/**
* @Route(path="/annotated/testResponseNormalizationWithNormalizationGroup", methods={"GET"})
*
* @ResponseNormalization(normalizationGroup="custom")
*
* @return string
*/
public function testResponseNormalizationWithNormalizationGroup()
public function testResponseNormalizationWithNormalizationGroup(): MyObject
{
return (new MyObject())->setField1('hi');
return (new MyObject())
->setField1('hi')
;
}

/**
* @Route(path="/annotated/testResponseNormalizationWithGuessedNormalizer", methods={"GET"})
*
* @ResponseNormalization()
*
* @return string
*/
public function testResponseNormalizationWithGuessedNormalizer()
public function testResponseNormalizationWithGuessedNormalizer(): MyObject
{
return (new MyObject())->setField1('hi');
return (new MyObject())
->setField1('hi')
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public function actionWithMultipleParameters($parameter1, $parameter2): Response

public function actionWithReturn(): MyObject
{
return (new MyObject())->setField1('field from controller');
return (new MyObject())
->setField1('field from controller')
;
}
}
Loading

0 comments on commit 8d73ff6

Please sign in to comment.