Skip to content

Commit cb1691b

Browse files
Add test
1 parent ae34227 commit cb1691b

File tree

4 files changed

+309
-0
lines changed

4 files changed

+309
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Doctrine;
4+
5+
use PHPStan\Type\Doctrine\CustomObjectManager\MyDocument;
6+
use PHPStan\Type\Doctrine\CustomObjectManager\MyEntity;
7+
8+
final class ObjectMetadataResolverTest extends \PHPStan\Testing\PHPStanTestCase
9+
{
10+
11+
public function testGetRepositoryClassWithCustomObjectManager(): void
12+
{
13+
$objectMetadataResolver = new ObjectMetadataResolver(
14+
$this->createReflectionProvider(),
15+
__DIR__ . '/data/CustomObjectManager/custom-object-manager.php',
16+
null
17+
);
18+
19+
self::assertSame('Doctrine\ODM\MongoDB\Repository\DocumentRepository', $objectMetadataResolver->getRepositoryClass(MyDocument::class));
20+
self::assertSame('Doctrine\ORM\EntityRepository', $objectMetadataResolver->getRepositoryClass(MyEntity::class));
21+
}
22+
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Doctrine\CustomObjectManager;
4+
5+
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
6+
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;
7+
8+
/**
9+
* @Document
10+
*/
11+
class MyDocument
12+
{
13+
14+
/**
15+
* @Id(strategy="NONE", type="string")
16+
*
17+
* @var string
18+
*/
19+
private $id;
20+
21+
public function doSomethingElse(): void
22+
{
23+
}
24+
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Doctrine\CustomObjectManager;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
/**
8+
* @ORM\Entity
9+
*/
10+
class MyEntity
11+
{
12+
13+
/**
14+
* @ORM\Id()
15+
* @ORM\GeneratedValue()
16+
* @ORM\Column(type="integer")
17+
*
18+
* @var int
19+
*/
20+
private $id;
21+
22+
public function doSomethingElse(): void
23+
{
24+
}
25+
26+
}
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
<?php declare(strict_types = 1);
2+
3+
use Doctrine\Common\Annotations\AnnotationReader;
4+
use Doctrine\Common\Cache\ArrayCache;
5+
use Doctrine\ODM\MongoDB;
6+
use Doctrine\ORM;
7+
use Doctrine\Persistence\Mapping\ClassMetadata;
8+
use Doctrine\Persistence\Mapping\ClassMetadataFactory;
9+
use Doctrine\Persistence\ObjectManager;
10+
use Doctrine\Persistence\ObjectRepository;
11+
12+
$config = new MongoDB\Configuration();
13+
$config->setProxyDir(__DIR__);
14+
$config->setProxyNamespace('PHPstan\Doctrine\OdmProxies');
15+
$config->setMetadataCacheImpl(new ArrayCache());
16+
$config->setHydratorDir(__DIR__);
17+
$config->setHydratorNamespace('PHPstan\Doctrine\OdmHydrators');
18+
19+
$config->setMetadataDriverImpl(
20+
new MongoDB\Mapping\Driver\AnnotationDriver(
21+
new AnnotationReader(),
22+
[__DIR__ . '/ObjectMetadataResolverTest.php']
23+
)
24+
);
25+
26+
$documentManager = MongoDB\DocumentManager::create(
27+
null,
28+
$config
29+
);
30+
31+
$config = new ORM\Configuration();
32+
$config->setProxyDir(__DIR__);
33+
$config->setProxyNamespace('PHPstan\Doctrine\OrmProxies');
34+
$config->setMetadataCacheImpl(new ArrayCache());
35+
36+
$config->setMetadataDriverImpl(
37+
new ORM\Mapping\Driver\AnnotationDriver(
38+
new AnnotationReader(),
39+
[__DIR__ . '/ObjectMetadataResolverTest.php']
40+
)
41+
);
42+
43+
$entityManager = ORM\EntityManager::create(
44+
[
45+
'driver' => 'pdo_sqlite',
46+
'memory' => true,
47+
],
48+
$config
49+
);
50+
51+
$metadataFactory = new class($documentManager, $entityManager) implements ClassMetadataFactory
52+
{
53+
54+
/** @var MongoDB\DocumentManager */
55+
private $documentManager;
56+
57+
/** @var ORM\EntityManager */
58+
private $entityManager;
59+
60+
public function __construct(MongoDB\DocumentManager $documentManager, ORM\EntityManager $entityManager)
61+
{
62+
$this->documentManager = $documentManager;
63+
$this->entityManager = $entityManager;
64+
}
65+
66+
public function getAllMetadata(): array
67+
{
68+
return array_merge(
69+
$this->documentManager->getMetadataFactory()->getAllMetadata(),
70+
$this->entityManager->getMetadataFactory()->getAllMetadata()
71+
);
72+
}
73+
74+
/**
75+
* @param string $className
76+
*/
77+
public function getMetadataFor($className): void
78+
{
79+
throw new \Exception(__FILE__);
80+
}
81+
82+
/**
83+
* @param string $className
84+
*/
85+
public function isTransient($className): void
86+
{
87+
throw new \Exception(__FILE__);
88+
}
89+
90+
/**
91+
* @param string $className
92+
*/
93+
public function hasMetadataFor($className): void
94+
{
95+
throw new \Exception(__FILE__);
96+
}
97+
98+
/**
99+
* @param string $className
100+
* @param ClassMetadata $class
101+
*/
102+
public function setMetadataFor($className, $class): void
103+
{
104+
throw new \Exception(__FILE__);
105+
}
106+
107+
};
108+
109+
return new class($documentManager, $entityManager, $metadataFactory) implements ObjectManager
110+
{
111+
112+
/** @var MongoDB\DocumentManager */
113+
private $documentManager;
114+
115+
/** @var ORM\EntityManager */
116+
private $entityManager;
117+
118+
/** @var ClassMetadataFactory */
119+
private $classMetadataFactory;
120+
121+
public function __construct(MongoDB\DocumentManager $documentManager, ORM\EntityManager $entityManager, ClassMetadataFactory $classMetadataFactory)
122+
{
123+
$this->documentManager = $documentManager;
124+
$this->entityManager = $entityManager;
125+
$this->classMetadataFactory = $classMetadataFactory;
126+
}
127+
128+
/**
129+
* @param string $className
130+
*/
131+
public function getRepository($className): ObjectRepository
132+
{
133+
if (strpos($className, 'Entity') !== false) {
134+
return $this->entityManager->getRepository($className);
135+
}
136+
137+
return $this->documentManager->getRepository($className);
138+
}
139+
140+
/**
141+
* @param string $className
142+
*/
143+
public function getClassMetadata($className): ClassMetadata
144+
{
145+
if (strpos($className, 'Entity') !== false) {
146+
return $this->entityManager->getClassMetadata($className);
147+
}
148+
149+
return $this->documentManager->getClassMetadata($className);
150+
}
151+
152+
public function getMetadataFactory(): ClassMetadataFactory
153+
{
154+
return $this->classMetadataFactory;
155+
}
156+
157+
/**
158+
* @param string $className
159+
* @param mixed $id
160+
*/
161+
public function find($className, $id): void
162+
{
163+
throw new \Exception(__FILE__);
164+
}
165+
166+
/**
167+
* @param object $object
168+
*/
169+
public function persist($object): void
170+
{
171+
throw new \Exception(__FILE__);
172+
}
173+
174+
/**
175+
* @param object $object
176+
*/
177+
public function remove($object): void
178+
{
179+
throw new \Exception(__FILE__);
180+
}
181+
182+
/**
183+
* @param object $object
184+
*/
185+
public function merge($object): void
186+
{
187+
throw new \Exception(__FILE__);
188+
}
189+
190+
/**
191+
* @param string|null $objectName
192+
*/
193+
public function clear($objectName = null): void
194+
{
195+
throw new \Exception(__FILE__);
196+
}
197+
198+
/**
199+
* @param object $object
200+
*/
201+
public function detach($object): void
202+
{
203+
throw new \Exception(__FILE__);
204+
}
205+
206+
/**
207+
* @param object $object
208+
*/
209+
public function refresh($object): void
210+
{
211+
throw new \Exception(__FILE__);
212+
}
213+
214+
public function flush(): void
215+
{
216+
throw new \Exception(__FILE__);
217+
}
218+
219+
/**
220+
* @param object $object
221+
*/
222+
public function initializeObject($object): void
223+
{
224+
throw new \Exception(__FILE__);
225+
}
226+
227+
/**
228+
* @param object $object
229+
*/
230+
public function contains($object): bool
231+
{
232+
throw new \Exception(__FILE__);
233+
}
234+
235+
};

0 commit comments

Comments
 (0)