|
14 | 14 | namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Model; |
15 | 15 |
|
16 | 16 | use Doctrine\ODM\MongoDB\DocumentManager; |
| 17 | +use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
17 | 18 | use Doctrine\ODM\MongoDB\Query\Builder; |
18 | 19 | use Doctrine\ODM\MongoDB\Repository\DocumentRepository; |
19 | 20 | use PHPUnit\Framework\MockObject\Stub; |
@@ -148,6 +149,47 @@ public function testSupportsQuery(bool $expected, object $object): void |
148 | 149 | static::assertSame($expected, $modelManager->supportsQuery($object)); |
149 | 150 | } |
150 | 151 |
|
| 152 | + public function testGetRealClassWithProxyObject(): void |
| 153 | + { |
| 154 | + $proxyClass = TestDocument::class; |
| 155 | + /** @var class-string $baseClass */ |
| 156 | + $baseClass = 'BaseTestDocument'; |
| 157 | + |
| 158 | + $classMetadata = $this->createMock(ClassMetadata::class); |
| 159 | + $classMetadata->expects(static::once()) |
| 160 | + ->method('getName') |
| 161 | + ->willReturn($baseClass); |
| 162 | + |
| 163 | + $documentManager = $this->createMock(DocumentManager::class); |
| 164 | + $documentManager->expects(static::once()) |
| 165 | + ->method('getClassMetadata') |
| 166 | + ->with($proxyClass) |
| 167 | + ->willReturn($classMetadata); |
| 168 | + |
| 169 | + $registry = $this->createMock(ManagerRegistry::class); |
| 170 | + $registry->expects(static::once()) |
| 171 | + ->method('getManagerForClass') |
| 172 | + ->with($proxyClass) |
| 173 | + ->willReturn($documentManager); |
| 174 | + |
| 175 | + $modelManager = new ModelManager($registry, $this->propertyAccessor); |
| 176 | + |
| 177 | + static::assertSame($baseClass, $modelManager->getRealClass(new TestDocument())); |
| 178 | + } |
| 179 | + |
| 180 | + public function testGetRealClassWithNonProxyObject(): void |
| 181 | + { |
| 182 | + $registry = $this->createMock(ManagerRegistry::class); |
| 183 | + $registry->expects(static::once()) |
| 184 | + ->method('getManagerForClass') |
| 185 | + ->with(\DateTime::class) |
| 186 | + ->willReturn(null); |
| 187 | + |
| 188 | + $modelManager = new ModelManager($registry, $this->propertyAccessor); |
| 189 | + |
| 190 | + static::assertSame(\DateTime::class, $modelManager->getRealClass(new \DateTime())); |
| 191 | + } |
| 192 | + |
151 | 193 | /** |
152 | 194 | * @phpstan-return iterable<array{bool, object}> |
153 | 195 | */ |
|
0 commit comments