Skip to content

Commit ecfad49

Browse files
Add test
1 parent ae34227 commit ecfad49

File tree

2 files changed

+238
-0
lines changed

2 files changed

+238
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
* @ORM\Id()
30+
* @ORM\GeneratedValue()
31+
* @ORM\Column(type="integer")
32+
*
33+
* @var int
34+
*/
35+
private $id;
36+
37+
public function doSomethingElse(): void
38+
{
39+
}
40+
}
41+
42+
/**
43+
* @Document
44+
*/
45+
class MyDocument
46+
{
47+
/**
48+
* @Id(strategy="NONE", type="string")
49+
*
50+
* @var string
51+
*/
52+
private $id;
53+
54+
public function doSomethingElse(): void
55+
{
56+
}
57+
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
/** @var MongoDB\DocumentManager */
51+
private $documentManager;
52+
53+
/** @var ORM\EntityManager */
54+
private $entityManager;
55+
56+
public function __construct(MongoDB\DocumentManager $documentManager, ORM\EntityManager $entityManager)
57+
{
58+
$this->documentManager = $documentManager;
59+
$this->entityManager = $entityManager;
60+
}
61+
62+
public function getAllMetadata()
63+
{
64+
return array_merge(
65+
$this->documentManager->getMetadataFactory()->getAllMetadata(),
66+
$this->entityManager->getMetadataFactory()->getAllMetadata()
67+
);
68+
}
69+
70+
public function getMetadataFor($className)
71+
{
72+
throw new \Exception(__FILE__);
73+
}
74+
75+
public function isTransient($className)
76+
{
77+
throw new \Exception(__FILE__);
78+
}
79+
80+
public function hasMetadataFor($className)
81+
{
82+
throw new \Exception(__FILE__);
83+
}
84+
85+
public function setMetadataFor($className, $class)
86+
{
87+
throw new \Exception(__FILE__);
88+
}
89+
};
90+
91+
return new class($documentManager, $entityManager, $metadataFactory) implements \Doctrine\Persistence\ObjectManager
92+
{
93+
/** @var MongoDB\DocumentManager */
94+
private $documentManager;
95+
96+
/** @var ORM\EntityManager */
97+
private $entityManager;
98+
99+
/** @var ClassMetadataFactory */
100+
private $classMetadataFactory;
101+
102+
public function __construct(MongoDB\DocumentManager $documentManager, ORM\EntityManager $entityManager, ClassMetadataFactory $classMetadataFactory)
103+
{
104+
$this->documentManager = $documentManager;
105+
$this->entityManager = $entityManager;
106+
$this->classMetadataFactory = $classMetadataFactory;
107+
}
108+
109+
public function getRepository($className)
110+
{
111+
if (strpos($className, 'Entity') !== false) {
112+
return $this->entityManager->getRepository($className);
113+
}
114+
115+
return $this->documentManager->getRepository($className);
116+
}
117+
118+
public function getClassMetadata($className)
119+
{
120+
if (strpos($className, 'Entity') !== false) {
121+
return $this->entityManager->getClassMetadata($className);
122+
}
123+
124+
return $this->documentManager->getClassMetadata($className);
125+
}
126+
127+
public function getMetadataFactory()
128+
{
129+
return $this->classMetadataFactory;
130+
}
131+
132+
public function find($className, $id)
133+
{
134+
throw new \Exception(__FILE__);
135+
}
136+
137+
public function persist($object)
138+
{
139+
throw new \Exception(__FILE__);
140+
}
141+
142+
public function remove($object)
143+
{
144+
throw new \Exception(__FILE__);
145+
}
146+
147+
public function merge($object)
148+
{
149+
throw new \Exception(__FILE__);
150+
}
151+
152+
public function clear($objectName = null)
153+
{
154+
throw new \Exception(__FILE__);
155+
}
156+
157+
public function detach($object)
158+
{
159+
throw new \Exception(__FILE__);
160+
}
161+
162+
public function refresh($object)
163+
{
164+
throw new \Exception(__FILE__);
165+
}
166+
167+
public function flush()
168+
{
169+
throw new \Exception(__FILE__);
170+
}
171+
172+
public function initializeObject($obj)
173+
{
174+
throw new \Exception(__FILE__);
175+
}
176+
177+
public function contains($object)
178+
{
179+
throw new \Exception(__FILE__);
180+
}
181+
};

0 commit comments

Comments
 (0)