Skip to content

Commit

Permalink
Added object typehinting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen van den Nieuwenhuisen committed Nov 25, 2020
1 parent e94d742 commit 0e66165
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
21 changes: 15 additions & 6 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,13 @@ final protected function trans($id, array $parameters = [], $domain = null, $loc
protected function handleXmlHttpRequestErrorResponse(Request $request, FormInterface $form): ?JsonResponse
{
if (empty(array_intersect(['application/json', '*/*'], $request->getAcceptableContentTypes()))) {
@trigger_error(sprintf('"%s" is not supported since sonata-project/admin-bundle 3.x and the status code 406 will be set in the returned response in 4.0. Use `application/json`.', implode('", "', $request->getAcceptableContentTypes())));
@trigger_error(sprintf(
'None of the passed values ("%s") in the "Accept" header when requesting %s %s is supported since sonata-project/admin-bundle 3.x.'
.' It will result in a response with the status code 406 (Not Acceptable) in 4.0. You must add "application/json".',
implode('", "', $request->getAcceptableContentTypes()),
$request->getMethod(),
$request->getUri()
), E_USER_DEPRECATED);

return null;
}
Expand All @@ -1574,13 +1580,16 @@ protected function handleXmlHttpRequestErrorResponse(Request $request, FormInter
], Response::HTTP_BAD_REQUEST);
}

/**
* @param object $object
*/
protected function handleXmlHttpRequestSuccessResponse(Request $request, $object): JsonResponse
protected function handleXmlHttpRequestSuccessResponse(Request $request, object $object): JsonResponse
{
if (empty(array_intersect(['application/json', '*/*'], $request->getAcceptableContentTypes()))) {
@trigger_error(sprintf('"%s" is not supported since sonata-project/admin-bundle 3.x and the status code 406 will be set in the returned response in 4.0. Use `application/json`.', implode('", "', $request->getAcceptableContentTypes())));
@trigger_error(sprintf(
'None of the passed values ("%s") in the "Accept" header when requesting %s %s is supported since sonata-project/admin-bundle 3.x.'
.' It will result in a response with the status code 406 (Not Acceptable) in 4.0. You must add "application/json".',
implode('", "', $request->getAcceptableContentTypes()),
$request->getMethod(),
$request->getUri()
), E_USER_DEPRECATED);
}

return $this->renderJson([
Expand Down
16 changes: 14 additions & 2 deletions tests/Controller/CRUDControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,13 @@ public function testEditActionAjaxErrorWithoutAcceptApplicationJson(): void
->method('trans')
->willReturn('flash message');

$this->expectDeprecation('In next major version response will return 406 NOT ACCEPTABLE without `Accept: application/json` or `Accept: */*`');
$this->expectDeprecation(sprintf(
'None of the passed values ("%s") in the "Accept" header when requesting %s %s is supported since sonata-project/admin-bundle 3.x.'
.' It will result in a response with the status code 406 (Not Acceptable) in 4.0. You must add "application/json".',
implode('", "', $this->request->getAcceptableContentTypes()),
$this->request->getMethod(),
$this->request->getUri()
));
$this->assertInstanceOf(Response::class, $response = $this->controller->editAction(null));
$this->assertSame($this->admin, $this->parameters['admin']);
$this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']);
Expand Down Expand Up @@ -2447,7 +2453,13 @@ public function testCreateActionAjaxErrorWithoutAcceptApplicationJson(): void
->method('trans')
->willReturn('flash message');

$this->expectDeprecation('In next major version response will return 406 NOT ACCEPTABLE without `Accept: application/json` or `Accept: */*`');
$this->expectDeprecation(sprintf(
'None of the passed values ("%s") in the "Accept" header when requesting %s %s is supported since sonata-project/admin-bundle 3.x.'
.' It will result in a response with the status code 406 (Not Acceptable) in 4.0. You must add "application/json".',
implode('", "', $this->request->getAcceptableContentTypes()),
$this->request->getMethod(),
$this->request->getUri()
));
$this->assertInstanceOf(Response::class, $response = $this->controller->createAction());
$this->assertSame($this->admin, $this->parameters['admin']);
$this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']);
Expand Down

0 comments on commit 0e66165

Please sign in to comment.