Skip to content

Commit

Permalink
Allowing to post subresources as form content
Browse files Browse the repository at this point in the history
  • Loading branch information
philipsorst committed Nov 21, 2018
1 parent cf93e21 commit 5d332ee
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 18 deletions.
1 change: 0 additions & 1 deletion Controller/AbstractCrudServiceRestResourceController.php
Expand Up @@ -3,7 +3,6 @@
namespace Dontdrinkandroot\RestBundle\Controller;

use Dontdrinkandroot\RestBundle\Metadata\RestMetadataFactory;
use Dontdrinkandroot\RestBundle\Service\Normalizer;
use Dontdrinkandroot\Service\CrudServiceInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand Down
33 changes: 27 additions & 6 deletions Controller/AbstractRestResourceController.php
Expand Up @@ -265,12 +265,7 @@ public function postSubresourceAction(Request $request, $id, string $subresource
$parent = $this->fetchEntity($id);
$this->assertSubResourceMethodGranted(Method::POST, $parent, $subresource);

$entity = $this->serializer->deserialize(
$request->getContent(),
$this->getSubResourceEntityClass($subresource),
'json',
[RestDenormalizer::DDR_REST_METHOD => Method::POST]
);
$entity = $this->getSubresourcePostedEntity($request, $subresource);

$entity = $this->buildAssociation($parent, $subresource, $entity);
$entity = $this->postProcessSubResourcePostedEntity($parent, $subresource, $entity);
Expand Down Expand Up @@ -609,4 +604,30 @@ abstract protected function addAssociation($parent, string $subresource, $subId)
* @return mixed
*/
abstract protected function removeAssociation($parent, string $subresource, $subId = null);

/**
* @param Request $request
* @param string $subresource
*
* @return mixed
*/
protected function getSubresourcePostedEntity(Request $request, string $subresource)
{
$content = null;
$requestParameters = $request->request->all();
if (!empty($requestParameters)) {
$content = json_encode($requestParameters);
} else {
$content = $request->getContent();
}

$entity = $this->serializer->deserialize(
$content,
$this->getSubResourceEntityClass($subresource),
'json',
[RestDenormalizer::DDR_REST_METHOD => Method::POST]
);

return $entity;
}
}
1 change: 0 additions & 1 deletion Tests/Functional/PuttablePostableTest.php
Expand Up @@ -2,7 +2,6 @@

namespace Dontdrinkandroot\RestBundle\Tests\Functional\TestBundle\Entity;

use Dontdrinkandroot\RestBundle\Security\AbstractAccessTokenAuthenticator;
use Dontdrinkandroot\RestBundle\Tests\Functional\FunctionalTestCase;
use Dontdrinkandroot\RestBundle\Tests\Functional\TestBundle\Fixtures\ORM\PuttablePostableAnnotationEntities;
use Dontdrinkandroot\RestBundle\Tests\Functional\TestBundle\Fixtures\ORM\Users;
Expand Down
8 changes: 4 additions & 4 deletions Tests/Functional/ReferenceTest.php
Expand Up @@ -2,8 +2,6 @@

namespace Dontdrinkandroot\RestBundle\Tests\Functional;

use Dontdrinkandroot\RestBundle\Security\AbstractAccessTokenAuthenticator;
use Dontdrinkandroot\RestBundle\Tests\Functional\TestBundle\Entity\AccessToken;
use Dontdrinkandroot\RestBundle\Tests\Functional\TestBundle\Fixtures\ORM\SubResourceEntities;
use Dontdrinkandroot\RestBundle\Tests\Functional\TestBundle\Fixtures\ORM\Users;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -47,7 +45,8 @@ public function testPostByReference()
'roles' => [
'ROLE_USER'
]
]
],
'text' => null
],
$content
);
Expand Down Expand Up @@ -90,7 +89,8 @@ public function testPutByReference()
'roles' => [
'ROLE_USER'
]
]
],
'text' => null
],
$content
);
Expand Down
58 changes: 53 additions & 5 deletions Tests/Functional/SecuredEnvironmentTest.php
Expand Up @@ -418,7 +418,8 @@ public function testAddParent()
'parentEntity' => [
'id' => $parent->getId(),
'uuid' => $parent->getUuid()
]
],
'text' => null
],
$content,
false
Expand Down Expand Up @@ -506,7 +507,8 @@ public function testRemoveParent()
$this->assertContentEquals(
[
'id' => $child->getId(),
'parentEntity' => null
'parentEntity' => null,
'text' => null
],
$content,
false
Expand Down Expand Up @@ -570,12 +572,58 @@ public function testPostSubresource()
$client,
sprintf('/rest/secured/%s/subresources', $entity->getId()),
[],
[],
['text' => 'TestText']
);
$content = $this->assertJsonResponse($response, Response::HTTP_CREATED);
$this->assertHasKeyAndUnset('id', $content);
$this->assertContentEquals(
[
'parentEntity' => [
'id' => $entity->getId(),
'uuid' => $entity->getUuid()
],
'text' => 'TestText'
],
$content,
false
);
}

public function testPostSubresourceAsForm()
{
$referenceRepository = $this->loadFixtures([Users::class, SecuredEntities::class])->getReferenceRepository();

$client = $this->makeClient(
false,
[
'PHP_AUTH_USER' => 'admin',
'PHP_AUTH_PW' => 'admin',
]
);

/** @var SecuredEntity $entity */
$entity = $referenceRepository->getReference('secured-entity-1');

$response = $this->performPost(
$client,
sprintf('/rest/secured/%s/subresources', $entity->getId()),
['text' => 'TestText'],
[]
);
$content = $this->assertJsonResponse($response, Response::HTTP_CREATED);
$this->assertNotNull($content['id']);
$this->assertNotNull($content['parentEntity']);
$this->assertEquals($entity->getId(), $content['parentEntity']['id']);
$this->assertHasKeyAndUnset('id', $content);
$this->assertContentEquals(
[
'parentEntity' => [
'id' => $entity->getId(),
'uuid' => $entity->getUuid()
],
'text' => 'TestText'
],
$content,
false
);
}

public function testGetInheritedEntity()
Expand Down
24 changes: 23 additions & 1 deletion Tests/Functional/TestBundle/Entity/SubResourceEntity.php
Expand Up @@ -3,7 +3,6 @@
namespace Dontdrinkandroot\RestBundle\Tests\Functional\TestBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Dontdrinkandroot\RestBundle\Metadata\Annotation as REST;

/**
* @author Philip Washington Sorst <philip@sorst.net>
Expand Down Expand Up @@ -38,6 +37,13 @@ class SubResourceEntity
*/
private $creator;

/**
* @ORM\Column(type="string", nullable=true)
*
* @var string|null
*/
private $text;

/**
* @return int
*/
Expand Down Expand Up @@ -71,4 +77,20 @@ public function setCreator($creator)
{
$this->creator = $creator;
}

/**
* @return null|string
*/
public function getText(): ?string
{
return $this->text;
}

/**
* @param null|string $text
*/
public function setText(?string $text): void
{
$this->text = $text;
}
}
Expand Up @@ -43,3 +43,5 @@ Dontdrinkandroot\RestBundle\Tests\Functional\TestBundle\Entity\SubResourceEntity
byReference: true
puttable:
byReference: true
text:
postable: true

0 comments on commit 5d332ee

Please sign in to comment.