Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Sep 13, 2020
1 parent 456ce84 commit 355bee7
Showing 1 changed file with 147 additions and 0 deletions.
147 changes: 147 additions & 0 deletions tests/JsonLd/Serializer/ObjectNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\JsonLd\Serializer;

use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\JsonLd\AnonymousContextBuilderInterface;
use ApiPlatform\Core\JsonLd\Serializer\ObjectNormalizer;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use ApiPlatform\Core\Tests\ProphecyTrait;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @author Kévin Dunglas <[email protected]>
*/
class ObjectNormalizerTest extends TestCase
{
use ProphecyTrait;

public function testNormalize()
{
$dummy = new Dummy();
$dummy->setName('hello');

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->willImplement(NormalizerInterface::class);
$serializerProphecy->normalize($dummy, null, Argument::type('array'))->willReturn(['name' => 'hello']);

$contextBuilderProphecy = $this->prophesize(AnonymousContextBuilderInterface::class);
$contextBuilderProphecy->getAnonymousResourceContext($dummy, Argument::type('array'))->shouldBeCalled()->willReturn([
'@context' => [],
'@type' => 'Dummy',
'@id' => '_:1234',
]);

$normalizer = new ObjectNormalizer(
$serializerProphecy->reveal(),
$iriConverterProphecy->reveal(),
$contextBuilderProphecy->reveal()
);

$expected = [
'@context' => [],
'@id' => '_:1234',
'@type' => 'Dummy',
'name' => 'hello',
];
$this->assertEquals($expected, $normalizer->normalize($dummy));
}

public function testNormalizeEmptyArray()
{
$dummy = new Dummy();
$dummy->setName('hello');

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->willImplement(NormalizerInterface::class);
$serializerProphecy->normalize($dummy, null, Argument::type('array'))->willReturn([]);

$contextBuilderProphecy = $this->prophesize(AnonymousContextBuilderInterface::class);
$contextBuilderProphecy->getAnonymousResourceContext($dummy, Argument::type('array'))->shouldNotBeCalled();

$normalizer = new ObjectNormalizer(
$serializerProphecy->reveal(),
$iriConverterProphecy->reveal(),
$contextBuilderProphecy->reveal()
);

$this->assertEquals([], $normalizer->normalize($dummy));
}

public function testNormalizeWithOutput()
{
$dummy = new Dummy();
$dummy->setName('hello');

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummy/1234');

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->willImplement(NormalizerInterface::class);
$serializerProphecy->normalize($dummy, null, Argument::type('array'))->willReturn(['name' => 'hello']);

$contextBuilderProphecy = $this->prophesize(AnonymousContextBuilderInterface::class);
$contextBuilderProphecy->getAnonymousResourceContext($dummy, ['iri' => '/dummy/1234', 'api_resource' => $dummy])->shouldBeCalled()->willReturn(['@id' => '/dummy/1234', '@type' => 'Dummy', '@context' => []]);

$normalizer = new ObjectNormalizer(
$serializerProphecy->reveal(),
$iriConverterProphecy->reveal(),
$contextBuilderProphecy->reveal()
);

$expected = [
'@context' => [],
'@id' => '/dummy/1234',
'@type' => 'Dummy',
'name' => 'hello',
];
$this->assertEquals($expected, $normalizer->normalize($dummy, null, ['api_resource' => $dummy]));
}

public function testNormalizeWithContext()
{
$dummy = new Dummy();
$dummy->setName('hello');

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummy/1234');

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->willImplement(NormalizerInterface::class);
$serializerProphecy->normalize($dummy, null, Argument::type('array'))->willReturn(['name' => 'hello']);

$contextBuilderProphecy = $this->prophesize(AnonymousContextBuilderInterface::class);
$contextBuilderProphecy->getAnonymousResourceContext($dummy, ['iri' => '/dummy/1234', 'api_resource' => $dummy, 'has_context' => true])->shouldBeCalled()->willReturn(['@id' => '/dummy/1234', '@type' => 'Dummy']);

$normalizer = new ObjectNormalizer(
$serializerProphecy->reveal(),
$iriConverterProphecy->reveal(),
$contextBuilderProphecy->reveal()
);

$expected = [
'@id' => '/dummy/1234',
'@type' => 'Dummy',
'name' => 'hello',
];
$this->assertEquals($expected, $normalizer->normalize($dummy, null, ['api_resource' => $dummy, 'jsonld_has_context' => true]));
}
}

0 comments on commit 355bee7

Please sign in to comment.