|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\Tests\ORM\Functional; |
| 6 | + |
| 7 | +use Doctrine\Tests\Models\Issue7877\Issue7877ApplicationGenerated; |
| 8 | +use Doctrine\Tests\Models\Issue7877\Issue7877DatabaseGenerated; |
| 9 | +use Doctrine\Tests\Models\Issue7877\Issue7877Interface; |
| 10 | +use Doctrine\Tests\OrmFunctionalTestCase; |
| 11 | + |
| 12 | +class SelfReferencingTest extends OrmFunctionalTestCase |
| 13 | +{ |
| 14 | + protected function setUp() : void |
| 15 | + { |
| 16 | + parent::setUp(); |
| 17 | + try { |
| 18 | + $classMetadatas = [ |
| 19 | + $this->em->getClassMetadata(Issue7877ApplicationGenerated::class), |
| 20 | + $this->em->getClassMetadata(Issue7877DatabaseGenerated::class), |
| 21 | + ]; |
| 22 | + // We first drop the schema to avoid collision between tests |
| 23 | + $this->schemaTool->dropSchema($classMetadatas); |
| 24 | + $this->schemaTool->createSchema($classMetadatas); |
| 25 | + } catch (Exception $e) { |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + public function providerDifferentEntity() |
| 30 | + { |
| 31 | + yield [Issue7877ApplicationGenerated::class]; |
| 32 | + yield [Issue7877DatabaseGenerated::class]; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @dataProvider providerDifferentEntity |
| 37 | + */ |
| 38 | + public function testDifferentEntity(string $class) |
| 39 | + { |
| 40 | + $count = count($this->sqlLoggerStack->queries); |
| 41 | + |
| 42 | + /** @var Issue7877Interface $parent */ |
| 43 | + $parent = new $class($parentId = 1); |
| 44 | + $this->em->persist($parent); |
| 45 | + |
| 46 | + /** @var Issue7877Interface $child */ |
| 47 | + $child = new $class($childId = 2); |
| 48 | + $child->setParent($parent); |
| 49 | + $this->em->persist($child); |
| 50 | + |
| 51 | + $this->em->flush(); |
| 52 | + $this->assertCount($count + 4, $this->sqlLoggerStack->queries); |
| 53 | + |
| 54 | + $this->em->clear(); |
| 55 | + |
| 56 | + $child = $this->em->find($class, $childId); |
| 57 | + $this->assertSame($parentId, $child->getParent()->getId()); |
| 58 | + } |
| 59 | + |
| 60 | + public function testSameEntityApplicationGenerated() |
| 61 | + { |
| 62 | + $count = count($this->sqlLoggerStack->queries); |
| 63 | + |
| 64 | + $entity = new Issue7877ApplicationGenerated($entityId = 1); |
| 65 | + $entity->setParent($entity); |
| 66 | + $this->em->persist($entity); |
| 67 | + |
| 68 | + $this->em->flush(); |
| 69 | + $this->assertCount($count + 3, $this->sqlLoggerStack->queries); |
| 70 | + |
| 71 | + $this->em->clear(); |
| 72 | + |
| 73 | + $child = $this->em->find(Issue7877ApplicationGenerated::class, $entityId); |
| 74 | + $this->assertSame($entityId, $child->getParent()->getId()); |
| 75 | + } |
| 76 | + |
| 77 | + public function testSameEntityDatabaseGenerated() |
| 78 | + { |
| 79 | + $count = count($this->sqlLoggerStack->queries); |
| 80 | + |
| 81 | + $entity = new Issue7877DatabaseGenerated(); |
| 82 | + $entity->setParent($entity); |
| 83 | + $this->em->persist($entity); |
| 84 | + |
| 85 | + $this->em->flush(); |
| 86 | + $this->assertCount($count + 4, $this->sqlLoggerStack->queries); |
| 87 | + $entityId = $entity->getId(); |
| 88 | + |
| 89 | + $this->em->clear(); |
| 90 | + |
| 91 | + $child = $this->em->find(Issue7877DatabaseGenerated::class, $entityId); |
| 92 | + $this->assertSame($entityId, $child->getParent()->getId()); |
| 93 | + } |
| 94 | +} |
0 commit comments