Skip to content

Commit a958d71

Browse files
authored
Merge pull request #773 from soyuka/test-doctrine-metadata
test DoctrineOrmPropertyMetadataFactory
2 parents 0f88647 + bb3af99 commit a958d71

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ApiPlatform\Core\Tests\Bridge\Doctrine\Orm\Metadata\Property;
13+
14+
use ApiPlatform\Core\Bridge\Doctrine\Orm\Metadata\Property\DoctrineOrmPropertyMetadataFactory;
15+
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
16+
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
17+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
18+
use Doctrine\Common\Persistence\ManagerRegistry;
19+
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
20+
use Doctrine\Common\Persistence\ObjectManager;
21+
use Doctrine\ORM\Mapping\ClassMetadataInfo;
22+
23+
/**
24+
* @author Antoine Bluchet <[email protected]>
25+
*/
26+
class DoctrineOrmPropertyMetadataFactoryTest extends \PHPUnit_Framework_TestCase
27+
{
28+
public function testCreateNoManager()
29+
{
30+
$propertyMetadata = new PropertyMetadata();
31+
$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
32+
$propertyMetadataFactory->create(Dummy::class, 'id', [])->shouldBeCalled()->willReturn($propertyMetadata);
33+
34+
$managerRegistry = $this->prophesize(ManagerRegistry::class);
35+
$managerRegistry->getManagerForClass(Dummy::class)->willReturn(null);
36+
37+
$doctrineOrmPropertyMetadataFactory = new DoctrineOrmPropertyMetadataFactory($managerRegistry->reveal(), $propertyMetadataFactory->reveal());
38+
39+
$this->assertEquals($doctrineOrmPropertyMetadataFactory->create(Dummy::class, 'id', []), $propertyMetadata);
40+
}
41+
42+
public function testCreateNoClassMetadata()
43+
{
44+
$propertyMetadata = new PropertyMetadata();
45+
$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
46+
$propertyMetadataFactory->create(Dummy::class, 'id', [])->shouldBeCalled()->willReturn($propertyMetadata);
47+
48+
$objectManager = $this->prophesize(ObjectManager::class);
49+
$objectManager->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn(null);
50+
51+
$managerRegistry = $this->prophesize(ManagerRegistry::class);
52+
$managerRegistry->getManagerForClass(Dummy::class)->shouldBeCalled()->willReturn($objectManager->reveal());
53+
54+
$doctrineOrmPropertyMetadataFactory = new DoctrineOrmPropertyMetadataFactory($managerRegistry->reveal(), $propertyMetadataFactory->reveal());
55+
56+
$this->assertEquals($doctrineOrmPropertyMetadataFactory->create(Dummy::class, 'id', []), $propertyMetadata);
57+
}
58+
59+
public function testCreateIsIdentifier()
60+
{
61+
$propertyMetadata = new PropertyMetadata();
62+
$propertyMetadata = $propertyMetadata->withIdentifier(true);
63+
64+
$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
65+
$propertyMetadataFactory->create(Dummy::class, 'id', [])->shouldBeCalled()->willReturn($propertyMetadata);
66+
67+
$classMetadata = $this->prophesize(ClassMetadataInfo::class);
68+
69+
$objectManager = $this->prophesize(ObjectManager::class);
70+
$objectManager->getClassMetadata(Dummy::class)->shouldNotBeCalled()->willReturn($classMetadata->reveal());
71+
72+
$managerRegistry = $this->prophesize(ManagerRegistry::class);
73+
$managerRegistry->getManagerForClass(Dummy::class)->shouldNotBeCalled()->willReturn($objectManager->reveal());
74+
75+
$doctrineOrmPropertyMetadataFactory = new DoctrineOrmPropertyMetadataFactory($managerRegistry->reveal(), $propertyMetadataFactory->reveal());
76+
77+
$this->assertEquals($doctrineOrmPropertyMetadataFactory->create(Dummy::class, 'id', []), $propertyMetadata);
78+
}
79+
80+
public function testCreateClassMetadataInfo()
81+
{
82+
$propertyMetadata = new PropertyMetadata();
83+
$propertyMetadata = $propertyMetadata->withWritable(true);
84+
85+
$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
86+
$propertyMetadataFactory->create(Dummy::class, 'id', [])->shouldBeCalled()->willReturn($propertyMetadata);
87+
88+
$classMetadata = $this->prophesize(ClassMetadataInfo::class);
89+
$classMetadata->getIdentifier()->shouldBeCalled()->willReturn(['id']);
90+
$classMetadata->isIdentifierNatural()->shouldBeCalled()->willReturn(true);
91+
92+
$objectManager = $this->prophesize(ObjectManager::class);
93+
$objectManager->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadata->reveal());
94+
95+
$managerRegistry = $this->prophesize(ManagerRegistry::class);
96+
$managerRegistry->getManagerForClass(Dummy::class)->shouldBeCalled()->willReturn($objectManager->reveal());
97+
98+
$doctrineOrmPropertyMetadataFactory = new DoctrineOrmPropertyMetadataFactory($managerRegistry->reveal(), $propertyMetadataFactory->reveal());
99+
100+
$doctrinePropertyMetadata = $doctrineOrmPropertyMetadataFactory->create(Dummy::class, 'id', []);
101+
102+
$this->assertEquals($doctrinePropertyMetadata->isIdentifier(), true);
103+
$this->assertEquals($doctrinePropertyMetadata->isWritable(), true);
104+
}
105+
106+
public function testCreateClassMetadata()
107+
{
108+
$propertyMetadata = new PropertyMetadata();
109+
$propertyMetadata = $propertyMetadata->withWritable(true);
110+
111+
$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
112+
$propertyMetadataFactory->create(Dummy::class, 'id', [])->shouldBeCalled()->willReturn($propertyMetadata);
113+
114+
$classMetadata = $this->prophesize(ClassMetadata::class);
115+
$classMetadata->getIdentifier()->shouldBeCalled()->willReturn(['id']);
116+
117+
$objectManager = $this->prophesize(ObjectManager::class);
118+
$objectManager->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadata->reveal());
119+
120+
$managerRegistry = $this->prophesize(ManagerRegistry::class);
121+
$managerRegistry->getManagerForClass(Dummy::class)->shouldBeCalled()->willReturn($objectManager->reveal());
122+
123+
$doctrineOrmPropertyMetadataFactory = new DoctrineOrmPropertyMetadataFactory($managerRegistry->reveal(), $propertyMetadataFactory->reveal());
124+
125+
$doctrinePropertyMetadata = $doctrineOrmPropertyMetadataFactory->create(Dummy::class, 'id', []);
126+
127+
$this->assertEquals($doctrinePropertyMetadata->isIdentifier(), true);
128+
$this->assertEquals($doctrinePropertyMetadata->isWritable(), false);
129+
}
130+
}

0 commit comments

Comments
 (0)