Skip to content

Commit

Permalink
Make SuluContentBundle 0.1 compatible with Sulu 2.0.2 (#58)
Browse files Browse the repository at this point in the history
* Adjust dependencies in composer.json to be compatible with Sulu 2.0.2

* Automatically fix coding style with prettyci

* Remove php-cs-fixer from composer json as we are using PrettyCI

* Adjust functional test cases to changed method signature of ->getResponse()->getContent()

* Adjust ContentViewSerializationSubscriber to new SerializationVisitorInterface

* Handle HandlerFailedException in controllers
  • Loading branch information
niklasnatter authored and wachterjohannes committed Dec 11, 2019
1 parent edbea89 commit c75a8bc
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 144 deletions.
5 changes: 5 additions & 0 deletions .prettyci.composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16"
}
}
7 changes: 6 additions & 1 deletion Controller/AbstractContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Sulu\Bundle\ContentBundle\Model\Content\Query\FindContentQuery;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\MessageBusInterface;

abstract class AbstractContentController implements ClassResourceInterface
Expand Down Expand Up @@ -60,7 +61,11 @@ public function getAction(Request $request, string $id): Response
$message = new FindContentQuery($this->getContentResourceKey(), $id, $request->query->get('locale'));
$this->messageBus->dispatch($message);
$content = $message->getContent();
} catch (ContentNotFoundException $exception) {
} catch (HandlerFailedException $exception) {
if (!$exception->getPrevious() instanceof ContentNotFoundException) {
throw $exception;
}

// need to return an empty content-view object because the sulu frontend does not expect any errors here
// TODO: review this code when subresource handling is implemented in the sulu frontend
$content = new ContentView(
Expand Down
7 changes: 6 additions & 1 deletion Controller/AbstractExcerptController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Sulu\Bundle\ContentBundle\Model\Excerpt\Query\FindExcerptQuery;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\MessageBusInterface;

abstract class AbstractExcerptController implements ClassResourceInterface
Expand Down Expand Up @@ -54,7 +55,11 @@ public function getAction(Request $request, string $id): Response
$message = new FindExcerptQuery($this->getExcerptResourceKey(), $id, $request->query->get('locale'));
$this->messageBus->dispatch($message);
$excerpt = $message->getExcerpt();
} catch (ExcerptNotFoundException $exception) {
} catch (HandlerFailedException $exception) {
if (!$exception->getPrevious() instanceof ExcerptNotFoundException) {
throw $exception;
}

// need to return an empty excerpt-view object because the sulu frontend does not expect any errors here
// TODO: review this code when subresource handling is implemented in the sulu frontend
$excerpt = new ExcerptView($this->getExcerptResourceKey(), $id, $request->query->get('locale'));
Expand Down
7 changes: 6 additions & 1 deletion Controller/AbstractSeoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Sulu\Bundle\ContentBundle\Model\Seo\SeoView;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\MessageBusInterface;

abstract class AbstractSeoController implements ClassResourceInterface
Expand Down Expand Up @@ -54,7 +55,11 @@ public function getAction(Request $request, string $id): Response
$message = new FindSeoQuery($this->getSeoResourceKey(), $id, $request->query->get('locale'));
$this->messageBus->dispatch($message);
$seo = $message->getSeo();
} catch (SeoNotFoundException $exception) {
} catch (HandlerFailedException $exception) {
if (!$exception->getPrevious() instanceof SeoNotFoundException) {
throw $exception;
}

// need to return an empty seo-view object because the sulu frontend does not expect any errors here
// TODO: review this code when subresource handling is implemented in the sulu frontend
$seo = new SeoView($this->getSeoResourceKey(), $id, $request->query->get('locale'));
Expand Down
39 changes: 22 additions & 17 deletions EventSubscriber/ContentViewSerializationSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
use JMS\Serializer\EventDispatcher\Events;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use JMS\Serializer\JsonSerializationVisitor;
use JMS\Serializer\Metadata\StaticPropertyMetadata;
use JMS\Serializer\Visitor\SerializationVisitorInterface;
use Sulu\Bundle\ContentBundle\Model\Content\ContentViewInterface;
use Sulu\Component\Content\Compat\StructureInterface;
use Sulu\Component\Content\Compat\StructureManagerInterface;
use Sulu\Component\Content\ContentTypeManagerInterface;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
use Sulu\Component\Serializer\ArraySerializationVisitor;

class ContentViewSerializationSubscriber implements EventSubscriberInterface
{
Expand Down Expand Up @@ -83,21 +83,16 @@ public function onPostSerializeJson(ObjectEvent $event): void
return;
}

/** @var JsonSerializationVisitor $visitor */
/** @var SerializationVisitorInterface $visitor */
$visitor = $event->getVisitor();
foreach ($metadata->getProperties() as $property) {
$name = $property->getName();
if (\is_float($name)) {
$name = (string) $name;
}

if (\array_key_exists($name, $data)) {
$visitor->setData((string) $name, $data[$name]);
$name = (string) $property->getName();
$value = $data[$name] ?? null;

continue;
}

$visitor->setData((string) $name, null);
$visitor->visitProperty(
new StaticPropertyMetadata('', $name, $value),
$value
);
}
}

Expand All @@ -115,10 +110,20 @@ public function onPostSerializeArray(ObjectEvent $event): void

$data = $object->getData() ?? [];

/** @var ArraySerializationVisitor $visitor */
/** @var SerializationVisitorInterface $visitor */
$visitor = $event->getVisitor();
$visitor->setData('content', $this->resolveContent($structure, $data));
$visitor->setData('view', $this->resolveView($structure, $data));

$content = $this->resolveContent($structure, $data);
$visitor->visitProperty(
new StaticPropertyMetadata('', 'content', $content),
$content
);

$view = $this->resolveView($structure, $data);
$visitor->visitProperty(
new StaticPropertyMetadata('', 'view', $view),
$view
);
}

private function getStructure(ContentViewInterface $contentView): ?StructureInterface
Expand Down
15 changes: 3 additions & 12 deletions Model/Excerpt/MessageHandler/ModifyExcerptMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,7 @@ private function findTagByName(string $tagName): TagInterface
{
$tag = $this->tagRepository->findTagByName($tagName);
if (!$tag) {
throw EntityNotFoundException::fromClassNameAndIdentifier(
TagInterface::class,
['name' => $tagName]
);
throw EntityNotFoundException::fromClassNameAndIdentifier(TagInterface::class, ['name' => $tagName]);
}

return $tag;
Expand All @@ -244,10 +241,7 @@ private function findCategoryById(int $categoryId): CategoryInterface
{
$category = $this->categoryRepository->findCategoryById($categoryId);
if (!$category) {
throw EntityNotFoundException::fromClassNameAndIdentifier(
CategoryInterface::class,
['id' => (string) $categoryId]
);
throw EntityNotFoundException::fromClassNameAndIdentifier(CategoryInterface::class, ['id' => (string) $categoryId]);
}

return $category;
Expand All @@ -258,10 +252,7 @@ private function findMediaById(int $mediaId): MediaInterface
/** @var ?MediaInterface */
$media = $this->mediaRepository->findMediaById($mediaId);
if (!$media) {
throw EntityNotFoundException::fromClassNameAndIdentifier(
MediaInterface::class,
['id' => (string) $mediaId]
);
throw EntityNotFoundException::fromClassNameAndIdentifier(MediaInterface::class, ['id' => (string) $mediaId]);
}

return $media;
Expand Down
52 changes: 41 additions & 11 deletions Tests/Functional/Controller/AbstractContentControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ public function testGet(): void
$client->request('GET', '/api/test-resource-contents/test-resource-1?locale=en');

$response = $client->getResponse();
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertSame(200, $response->getStatusCode());
$this->assertHttpStatusCode(200, $response);

$responseContent = $response->getContent();
if (!$responseContent) {
$this->fail();
}

$result = json_decode($responseContent, true);

$this->assertSame(
[
Expand All @@ -64,8 +70,14 @@ public function testGetAbsent(): void
$client->request('GET', '/api/test-resource-contents/absent-resource?locale=en');

$response = $client->getResponse();
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertSame(200, $response->getStatusCode());
$this->assertHttpStatusCode(200, $response);

$responseContent = $response->getContent();
if (!$responseContent) {
$this->fail();
}

$result = json_decode($responseContent, true);

$this->assertSame(
[
Expand Down Expand Up @@ -94,8 +106,14 @@ public function testPut(): void
$client->request('PUT', '/api/test-resource-contents/test-resource-1?locale=en', $payload);

$response = $client->getResponse();
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertSame(200, $response->getStatusCode());
$this->assertHttpStatusCode(200, $response);

$responseContent = $response->getContent();
if (!$responseContent) {
$this->fail();
}

$result = json_decode($responseContent, true);

$this->assertSame(
[
Expand Down Expand Up @@ -124,8 +142,14 @@ public function testPutAbsent(): void
$client->request('PUT', '/api/test-resource-contents/absent-resource?locale=en', $payload);

$response = $client->getResponse();
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertSame(200, $response->getStatusCode());
$this->assertHttpStatusCode(200, $response);

$responseContent = $response->getContent();
if (!$responseContent) {
$this->fail();
}

$result = json_decode($responseContent, true);

$this->assertSame(
[
Expand Down Expand Up @@ -156,8 +180,14 @@ public function testPutWithPublishAction(): void
$client->request('PUT', '/api/test-resource-contents/test-resource-1?locale=en&action=publish', $payload);

$response = $client->getResponse();
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertSame(200, $response->getStatusCode());
$this->assertHttpStatusCode(200, $response);

$responseContent = $response->getContent();
if (!$responseContent) {
$this->fail();
}

$result = json_decode($responseContent, true);

$this->assertSame(
[
Expand Down Expand Up @@ -185,6 +215,6 @@ public function testDelete(): void
$client->request('DELETE', '/api/test-resource-contents/test-resource-1?locale=en');

$response = $client->getResponse();
$this->assertSame(204, $response->getStatusCode());
$this->assertHttpStatusCode(204, $response);
}
}
52 changes: 41 additions & 11 deletions Tests/Functional/Controller/AbstractExcerptControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,14 @@ public function testGet(): void
$client->request('GET', '/api/test-resource-excerpts/test-resource-1?locale=en');

$response = $client->getResponse();
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertSame(200, $response->getStatusCode());
$this->assertHttpStatusCode(200, $response);

$responseContent = $response->getContent();
if (!$responseContent) {
$this->fail();
}

$result = json_decode($responseContent, true);

$this->assertSame(
[
Expand All @@ -133,8 +139,14 @@ public function testGetAbsent(): void
$client->request('GET', '/api/test-resource-excerpts/absent-resource?locale=en');

$response = $client->getResponse();
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertSame(200, $response->getStatusCode());
$this->assertHttpStatusCode(200, $response);

$responseContent = $response->getContent();
if (!$responseContent) {
$this->fail();
}

$result = json_decode($responseContent, true);

$this->assertSame(
[
Expand Down Expand Up @@ -196,8 +208,14 @@ public function testPut(): void
$client->request('PUT', '/api/test-resource-excerpts/test-resource-1?locale=en', $payload);

$response = $client->getResponse();
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertSame(200, $response->getStatusCode());
$this->assertHttpStatusCode(200, $response);

$responseContent = $response->getContent();
if (!$responseContent) {
$this->fail();
}

$result = json_decode($responseContent, true);

$this->assertSame(
[
Expand Down Expand Up @@ -246,8 +264,14 @@ public function testPutAbsent(): void
$client->request('PUT', '/api/test-resource-excerpts/absent-resource?locale=en', $payload);

$response = $client->getResponse();
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertSame(200, $response->getStatusCode());
$this->assertHttpStatusCode(200, $response);

$responseContent = $response->getContent();
if (!$responseContent) {
$this->fail();
}

$result = json_decode($responseContent, true);

$this->assertSame(
[
Expand Down Expand Up @@ -298,8 +322,14 @@ public function testPutWithPublishAction(): void
$client->request('PUT', '/api/test-resource-excerpts/test-resource-1?locale=en&action=publish', $payload);

$response = $client->getResponse();
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertSame(200, $response->getStatusCode());
$this->assertHttpStatusCode(200, $response);

$responseContent = $response->getContent();
if (!$responseContent) {
$this->fail();
}

$result = json_decode($responseContent, true);

$this->assertSame(
[
Expand Down Expand Up @@ -335,6 +365,6 @@ public function testDelete(): void
$client->request('DELETE', '/api/test-resource-excerpts/test-resource-1?locale=en');

$response = $client->getResponse();
$this->assertSame(204, $response->getStatusCode());
$this->assertHttpStatusCode(204, $response);
}
}
Loading

0 comments on commit c75a8bc

Please sign in to comment.