Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
190 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters