Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?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.
*/

namespace ApiPlatform\Core\Tests\Bridge\Doctrine\Orm\Metadata\Property;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Metadata\Property\DoctrineOrmPropertyMetadataFactory;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\Mapping\ClassMetadataInfo;

/**
* @author Antoine Bluchet <soyuka@gmail.com>
*/
class DoctrineOrmPropertyMetadataFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testCreateNoManager()
{
$propertyMetadata = new PropertyMetadata();
$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactory->create(Dummy::class, 'id', [])->shouldBeCalled()->willReturn($propertyMetadata);

$managerRegistry = $this->prophesize(ManagerRegistry::class);
$managerRegistry->getManagerForClass(Dummy::class)->willReturn(null);

$doctrineOrmPropertyMetadataFactory = new DoctrineOrmPropertyMetadataFactory($managerRegistry->reveal(), $propertyMetadataFactory->reveal());

$this->assertEquals($doctrineOrmPropertyMetadataFactory->create(Dummy::class, 'id', []), $propertyMetadata);
}

public function testCreateNoClassMetadata()
{
$propertyMetadata = new PropertyMetadata();
$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactory->create(Dummy::class, 'id', [])->shouldBeCalled()->willReturn($propertyMetadata);

$objectManager = $this->prophesize(ObjectManager::class);
$objectManager->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn(null);

$managerRegistry = $this->prophesize(ManagerRegistry::class);
$managerRegistry->getManagerForClass(Dummy::class)->shouldBeCalled()->willReturn($objectManager->reveal());

$doctrineOrmPropertyMetadataFactory = new DoctrineOrmPropertyMetadataFactory($managerRegistry->reveal(), $propertyMetadataFactory->reveal());

$this->assertEquals($doctrineOrmPropertyMetadataFactory->create(Dummy::class, 'id', []), $propertyMetadata);
}

public function testCreateIsIdentifier()
{
$propertyMetadata = new PropertyMetadata();
$propertyMetadata = $propertyMetadata->withIdentifier(true);

$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactory->create(Dummy::class, 'id', [])->shouldBeCalled()->willReturn($propertyMetadata);

$classMetadata = $this->prophesize(ClassMetadataInfo::class);

$objectManager = $this->prophesize(ObjectManager::class);
$objectManager->getClassMetadata(Dummy::class)->shouldNotBeCalled()->willReturn($classMetadata->reveal());

$managerRegistry = $this->prophesize(ManagerRegistry::class);
$managerRegistry->getManagerForClass(Dummy::class)->shouldNotBeCalled()->willReturn($objectManager->reveal());

$doctrineOrmPropertyMetadataFactory = new DoctrineOrmPropertyMetadataFactory($managerRegistry->reveal(), $propertyMetadataFactory->reveal());

$this->assertEquals($doctrineOrmPropertyMetadataFactory->create(Dummy::class, 'id', []), $propertyMetadata);
}

public function testCreateClassMetadataInfo()
{
$propertyMetadata = new PropertyMetadata();
$propertyMetadata = $propertyMetadata->withWritable(true);

$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactory->create(Dummy::class, 'id', [])->shouldBeCalled()->willReturn($propertyMetadata);

$classMetadata = $this->prophesize(ClassMetadataInfo::class);
$classMetadata->getIdentifier()->shouldBeCalled()->willReturn(['id']);
$classMetadata->isIdentifierNatural()->shouldBeCalled()->willReturn(true);

$objectManager = $this->prophesize(ObjectManager::class);
$objectManager->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadata->reveal());

$managerRegistry = $this->prophesize(ManagerRegistry::class);
$managerRegistry->getManagerForClass(Dummy::class)->shouldBeCalled()->willReturn($objectManager->reveal());

$doctrineOrmPropertyMetadataFactory = new DoctrineOrmPropertyMetadataFactory($managerRegistry->reveal(), $propertyMetadataFactory->reveal());

$doctrinePropertyMetadata = $doctrineOrmPropertyMetadataFactory->create(Dummy::class, 'id', []);

$this->assertEquals($doctrinePropertyMetadata->isIdentifier(), true);
$this->assertEquals($doctrinePropertyMetadata->isWritable(), true);
}

public function testCreateClassMetadata()
{
$propertyMetadata = new PropertyMetadata();
$propertyMetadata = $propertyMetadata->withWritable(true);

$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactory->create(Dummy::class, 'id', [])->shouldBeCalled()->willReturn($propertyMetadata);

$classMetadata = $this->prophesize(ClassMetadata::class);
$classMetadata->getIdentifier()->shouldBeCalled()->willReturn(['id']);

$objectManager = $this->prophesize(ObjectManager::class);
$objectManager->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadata->reveal());

$managerRegistry = $this->prophesize(ManagerRegistry::class);
$managerRegistry->getManagerForClass(Dummy::class)->shouldBeCalled()->willReturn($objectManager->reveal());

$doctrineOrmPropertyMetadataFactory = new DoctrineOrmPropertyMetadataFactory($managerRegistry->reveal(), $propertyMetadataFactory->reveal());

$doctrinePropertyMetadata = $doctrineOrmPropertyMetadataFactory->create(Dummy::class, 'id', []);

$this->assertEquals($doctrinePropertyMetadata->isIdentifier(), true);
$this->assertEquals($doctrinePropertyMetadata->isWritable(), false);
}
}