Skip to content

Commit

Permalink
Merge e3b5c61 into e22f0da
Browse files Browse the repository at this point in the history
  • Loading branch information
laryjulien committed Nov 9, 2021
2 parents e22f0da + e3b5c61 commit 55a2329
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/Api/IdentifiersExtractor.php
Expand Up @@ -15,10 +15,10 @@

use ApiPlatform\Core\Api\ResourceClassResolverInterface;
use ApiPlatform\Core\Identifier\CompositeIdentifierParser;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Core\Util\ResourceClassInfoTrait;
use ApiPlatform\Exception\RuntimeException;
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
Expand All @@ -36,8 +36,13 @@ final class IdentifiersExtractor implements IdentifiersExtractorInterface
private $propertyMetadataFactory;
private $propertyAccessor;

public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, PropertyAccessorInterface $propertyAccessor = null)
{
public function __construct(
ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory,
ResourceClassResolverInterface $resourceClassResolver,
PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory,
PropertyMetadataFactoryInterface $propertyMetadataFactory,
PropertyAccessorInterface $propertyAccessor = null
) {
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->resourceClassResolver = $resourceClassResolver;
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
Expand Down
94 changes: 94 additions & 0 deletions tests/Api/IdentifiersExtractorTest.php
@@ -0,0 +1,94 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Tests\Api;

use ApiPlatform\Api\IdentifiersExtractor;
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
use ApiPlatform\Core\Tests\ProphecyTrait;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\UriVariable;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
use PHPUnit\Framework\TestCase;

/**
* @author Tomasz Grochowski <tg@urias.it>
*/
class IdentifiersExtractorTest extends TestCase
{
use ProphecyTrait;

public function testGetIdentifiersFromItem()
{
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);

$resourceClassResolver = $resourceClassResolverProphecy->reveal();

$identifiersExtractor = new IdentifiersExtractor(
$resourceMetadataFactoryProphecy->reveal(),
$resourceClassResolver,
$propertyNameCollectionFactoryProphecy->reveal(),
$propertyMetadataFactoryProphecy->reveal()
);

$operation = $this->prophesize(Operation::class);
$item = new Dummy();
$resourceClass = Dummy::class;
$context = [
'operation' => $operation->reveal(),
];

$resourceClassResolverProphecy->getResourceClass($item)->willReturn($resourceClass);
$operation->getUriVariables()->willReturn([]);

$this->assertEquals([], $identifiersExtractor->getIdentifiersFromItem($item, 'operation', $context));
}

public function testGetIdentifiersFromItemWithId()
{
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);

$resourceClassResolver = $resourceClassResolverProphecy->reveal();

$identifiersExtractor = new IdentifiersExtractor(
$resourceMetadataFactoryProphecy->reveal(),
$resourceClassResolver,
$propertyNameCollectionFactoryProphecy->reveal(),
$propertyMetadataFactoryProphecy->reveal()
);

$operation = $this->prophesize(Operation::class);
$item = new Dummy();
$item->setId(1);
$resourceClass = Dummy::class;
$context = [
'operation' => $operation->reveal(),
];

$resourceClassResolverProphecy->getResourceClass($item)->willReturn($resourceClass);
$uriVariable = new UriVariable();
$uriVariable = $uriVariable->withIdentifiers(['id'])->withTargetClass(Dummy::class);
$operation->getUriVariables()->willReturn(['id' => $uriVariable]);

$this->assertEquals(['id' => '1'], $identifiersExtractor->getIdentifiersFromItem($item, 'operation', $context));
}
}
Expand Up @@ -26,4 +26,13 @@ public function testTransform()
$normalizer = new DateTimeUriVariableTransformer();
$normalizer->transform('not valid', [\DateTimeImmutable::class]);
}

public function testSupportsTransformationForDateTime()
{
$normalizer = new DateTimeUriVariableTransformer();

$this->assertTrue($normalizer->supportsTransformation(1, [\DateTime::class]));
$this->assertTrue($normalizer->supportsTransformation(2, [\DateTimeImmutable::class]));
$this->assertTrue($normalizer->supportsTransformation(3, [\DateTimeInterface::class]));
}
}
76 changes: 76 additions & 0 deletions tests/Core/Hal/Serializer/ObjectNormalizerTest.php
@@ -0,0 +1,76 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Hal\Serializer;

use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\Hal\Serializer\ObjectNormalizer;
use ApiPlatform\Core\Tests\ProphecyTrait;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* @author Tomasz Grochowski <tg@urias.it>
* @group legacy
*/
class ObjectNormalizerTest extends TestCase
{
use ProphecyTrait;

/**
* @group legacy
*/
public function testDoesNotSupportDenormalization()
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('jsonhal is a read-only format.');

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

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

$this->assertFalse($normalizer->supportsDenormalization('foo', 'type', 'format'));
$normalizer->denormalize(['foo'], 'Foo');
}

/**
* @group legacy
*/
public function testSupportsNormalization()
{
$std = new \stdClass();
$dummy = new Dummy();
$dummy->setDescription('hello');

$normalizerInterfaceProphecy = $this->prophesize(NormalizerInterface::class);
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$normalizer = new ObjectNormalizer(
$normalizerInterfaceProphecy->reveal(),
$iriConverterProphecy->reveal()
);

$normalizerInterfaceProphecy->supportsNormalization($dummy, 'xml', [])->willReturn(true);
$normalizerInterfaceProphecy->supportsNormalization($std, ObjectNormalizer::FORMAT, [])->willReturn(true);
$normalizerInterfaceProphecy->supportsNormalization($dummy, ObjectNormalizer::FORMAT, [])->willReturn(true);

$this->assertFalse($normalizer->supportsNormalization($dummy, 'xml'));
$this->assertTrue($normalizer->supportsNormalization($std, ObjectNormalizer::FORMAT));
$this->assertTrue($normalizer->supportsNormalization($dummy, ObjectNormalizer::FORMAT));
}
}
4 changes: 2 additions & 2 deletions tests/Fixtures/TestBundle/Entity/OperationResource.php
Expand Up @@ -34,8 +34,8 @@
class OperationResource
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
#[ApiProperty(identifier: true)]
Expand Down

0 comments on commit 55a2329

Please sign in to comment.