From bb3af99ed8a60251bcd4b61453718306cdfeaf19 Mon Sep 17 00:00:00 2001 From: abluchet Date: Wed, 28 Sep 2016 15:45:46 +0200 Subject: [PATCH] test DoctrineOrmPropertyMetadataFactory --- ...DoctrineOrmPropertyMetadataFactoryTest.php | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 tests/Bridge/Doctrine/Orm/Metadata/Property/DoctrineOrmPropertyMetadataFactoryTest.php diff --git a/tests/Bridge/Doctrine/Orm/Metadata/Property/DoctrineOrmPropertyMetadataFactoryTest.php b/tests/Bridge/Doctrine/Orm/Metadata/Property/DoctrineOrmPropertyMetadataFactoryTest.php new file mode 100644 index 00000000000..57268ddcc6e --- /dev/null +++ b/tests/Bridge/Doctrine/Orm/Metadata/Property/DoctrineOrmPropertyMetadataFactoryTest.php @@ -0,0 +1,130 @@ + + * + * 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 + */ +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); + } +}