Skip to content

Commit d34a8ea

Browse files
Add test
1 parent ae34227 commit d34a8ea

File tree

2 files changed

+246
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)