Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Commit

Permalink
III-2200: Json document transformer to return the embedded json-ld
Browse files Browse the repository at this point in the history
  • Loading branch information
bertramakers committed Jul 4, 2017
1 parent 7d39946 commit c94e3fd
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/JsonDocument/JsonLdEmbeddingJsonDocumentTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace CultuurNet\UDB3\Search\ElasticSearch\JsonDocument;

use CultuurNet\UDB3\ReadModel\JsonDocument;
use CultuurNet\UDB3\Search\JsonDocument\JsonDocumentTransformerInterface;

class JsonLdEmbeddingJsonDocumentTransformer implements JsonDocumentTransformerInterface
{
/**
* @param JsonDocument $jsonDocument
* @return JsonDocument
*/
public function transform(JsonDocument $jsonDocument)
{
$body = $jsonDocument->getBody();

return new JsonDocument(
$jsonDocument->getId(),
$body->originalEmbeddedJsonLd
);
}
}
81 changes: 81 additions & 0 deletions tests/JsonDocument/JsonLdEmbeddingJsonDocumentTransformerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace CultuurNet\UDB3\Search\ElasticSearch\JsonDocument;

use CultuurNet\UDB3\ReadModel\JsonDocument;

class JsonLdEmbeddingJsonDocumentTransformerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var JsonLdEmbeddingJsonDocumentTransformer
*/
private $transformer;

public function setUp()
{
$this->transformer = new JsonLdEmbeddingJsonDocumentTransformer();
}

/**
* @test
*/
public function it_should_return_a_document_with_only_the_embedded_json_ld()
{
$id = '8ea290f6-deb2-426e-820a-68eeefde9c4d';

$jsonLd = (object) [
'@id' => 'https://io.uitdatabank.be/events/8ea290f6-deb2-426e-820a-68eeefde9c4d',
'@type' => 'Event',
'location' => (object) [
'@id' => 'https://io.uitdatabank.be/places/9361008e-4e5b-4060-ad49-0866c8fa1860',
'@type' => 'Place',
'address' => (object) [
'nl' => (object) [
'streetAddress' => 'Eenmeilaan 35',
'postalCode' => '3010',
'addressLocality' => 'Kessel-Lo',
'addressCountry' => 'BE',
],
],
],
];

$encodedJsonLd = json_encode($jsonLd);

$indexed = (object) [
'@id' => 'https://io.uitdatabank.be/events/8ea290f6-deb2-426e-820a-68eeefde9c4d',
'@type' => 'Event',
'regions' => ['gem-leuven', 'prv-vlaams-brabant'],
'address' => [
'nl' => (object) [
'streetAddress' => 'Eenmeilaan 35',
'postalCode' => '3010',
'addressLocality' => 'Kessel-Lo',
'addressCountry' => 'BE',
],
],
'location' => (object) [
'@id' => 'https://io.uitdatabank.be/places/9361008e-4e5b-4060-ad49-0866c8fa1860',
'@type' => 'Place',
'address' => [
'nl' => (object) [
'streetAddress' => 'Eenmeilaan 35',
'postalCode' => '3010',
'addressLocality' => 'Kessel-Lo',
'addressCountry' => 'BE',
],
],
],
'foo' => 'bar',
'originalEmbeddedJsonLd' => $encodedJsonLd,
];

$expectedJsonLdDocument = new JsonDocument($id, $encodedJsonLd);
$indexedDocument = new JsonDocument($id, json_encode($indexed));

$actualJsonLdDocument = $this->transformer->transform($indexedDocument);

$this->assertEquals($expectedJsonLdDocument, $actualJsonLdDocument);
$this->assertEquals($jsonLd, $actualJsonLdDocument->getBody());
}
}

0 comments on commit c94e3fd

Please sign in to comment.