|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\Tests\ORM\Internal\UnitOfWork; |
| 6 | + |
| 7 | +use Doctrine\ORM\EntityManagerInterface; |
| 8 | +use Doctrine\ORM\Id\AssignedGenerator; |
| 9 | +use Doctrine\ORM\Id\IdentityGenerator; |
| 10 | +use Doctrine\ORM\Internal\UnitOfWork\InsertBatch; |
| 11 | +use Doctrine\ORM\Mapping\ClassMetadata; |
| 12 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 13 | +use PHPUnit\Framework\Attributes\Group; |
| 14 | +use PHPUnit\Framework\MockObject\Stub; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +#[CoversClass(InsertBatch::class)] |
| 18 | +#[Group('#11977')] |
| 19 | +final class InsertBatchTest extends TestCase |
| 20 | +{ |
| 21 | + private EntityManagerInterface&Stub $entityManager; |
| 22 | + |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + $this->entityManager = $this->createStub(EntityManagerInterface::class); |
| 26 | + |
| 27 | + $entityAMetadata = new ClassMetadata(EntityA::class); |
| 28 | + $entityBMetadata = new ClassMetadata(EntityB::class); |
| 29 | + $entityCMetadata = new ClassMetadata(EntityC::class); |
| 30 | + |
| 31 | + $entityAMetadata->idGenerator = new AssignedGenerator(); |
| 32 | + $entityBMetadata->idGenerator = new AssignedGenerator(); |
| 33 | + $entityCMetadata->idGenerator = new IdentityGenerator(); |
| 34 | + |
| 35 | + $this->entityManager->method('getClassMetadata') |
| 36 | + ->willReturnMap([ |
| 37 | + [EntityA::class, $entityAMetadata], |
| 38 | + [EntityB::class, $entityBMetadata], |
| 39 | + [EntityC::class, $entityCMetadata], |
| 40 | + ]); |
| 41 | + } |
| 42 | + |
| 43 | + public function testWillProduceEmptyBatchOnNoGivenEntities(): void |
| 44 | + { |
| 45 | + self::assertEmpty(InsertBatch::batchByEntityType($this->entityManager, [])); |
| 46 | + } |
| 47 | + |
| 48 | + public function testWillBatchSameEntityOperationsInSingleBatch(): void |
| 49 | + { |
| 50 | + $batches = InsertBatch::batchByEntityType( |
| 51 | + $this->entityManager, |
| 52 | + [ |
| 53 | + new EntityA(), |
| 54 | + new EntityA(), |
| 55 | + new EntityA(), |
| 56 | + ], |
| 57 | + ); |
| 58 | + |
| 59 | + self::assertCount(1, $batches); |
| 60 | + self::assertSame(EntityA::class, $batches[0]->class->name); |
| 61 | + self::assertCount(3, $batches[0]->entities); |
| 62 | + } |
| 63 | + |
| 64 | + public function testWillBatchInterleavedEntityOperationsInGroups(): void |
| 65 | + { |
| 66 | + $batches = InsertBatch::batchByEntityType( |
| 67 | + $this->entityManager, |
| 68 | + [ |
| 69 | + new EntityA(), |
| 70 | + new EntityA(), |
| 71 | + new EntityB(), |
| 72 | + new EntityB(), |
| 73 | + new EntityA(), |
| 74 | + new EntityA(), |
| 75 | + ], |
| 76 | + ); |
| 77 | + |
| 78 | + self::assertCount(3, $batches); |
| 79 | + self::assertSame(EntityA::class, $batches[0]->class->name); |
| 80 | + self::assertCount(2, $batches[0]->entities); |
| 81 | + self::assertSame(EntityB::class, $batches[1]->class->name); |
| 82 | + self::assertCount(2, $batches[1]->entities); |
| 83 | + self::assertSame(EntityA::class, $batches[2]->class->name); |
| 84 | + self::assertCount(2, $batches[2]->entities); |
| 85 | + } |
| 86 | + |
| 87 | + public function testWillNotBatchOperationsForAGeneratedIdentifierEntity(): void |
| 88 | + { |
| 89 | + $batches = InsertBatch::batchByEntityType( |
| 90 | + $this->entityManager, |
| 91 | + [ |
| 92 | + new EntityC(), |
| 93 | + new EntityC(), |
| 94 | + new EntityC(), |
| 95 | + ], |
| 96 | + ); |
| 97 | + |
| 98 | + self::assertCount(3, $batches); |
| 99 | + self::assertSame(EntityC::class, $batches[0]->class->name); |
| 100 | + self::assertCount(1, $batches[0]->entities); |
| 101 | + self::assertSame(EntityC::class, $batches[1]->class->name); |
| 102 | + self::assertCount(1, $batches[1]->entities); |
| 103 | + self::assertSame(EntityC::class, $batches[2]->class->name); |
| 104 | + self::assertCount(1, $batches[2]->entities); |
| 105 | + } |
| 106 | + |
| 107 | + public function testWillIsolateBatchesForEntitiesWithGeneratedIdentifiers(): void |
| 108 | + { |
| 109 | + $batches = InsertBatch::batchByEntityType( |
| 110 | + $this->entityManager, |
| 111 | + [ |
| 112 | + new EntityA(), |
| 113 | + new EntityA(), |
| 114 | + new EntityC(), |
| 115 | + new EntityC(), |
| 116 | + new EntityA(), |
| 117 | + new EntityA(), |
| 118 | + ], |
| 119 | + ); |
| 120 | + |
| 121 | + self::assertCount(4, $batches); |
| 122 | + self::assertSame(EntityA::class, $batches[0]->class->name); |
| 123 | + self::assertCount(2, $batches[0]->entities); |
| 124 | + self::assertSame(EntityC::class, $batches[1]->class->name); |
| 125 | + self::assertCount(1, $batches[1]->entities); |
| 126 | + self::assertSame(EntityC::class, $batches[2]->class->name); |
| 127 | + self::assertCount(1, $batches[2]->entities); |
| 128 | + self::assertSame(EntityA::class, $batches[3]->class->name); |
| 129 | + self::assertCount(2, $batches[3]->entities); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +class EntityA |
| 134 | +{ |
| 135 | +} |
| 136 | + |
| 137 | +class EntityB |
| 138 | +{ |
| 139 | +} |
| 140 | + |
| 141 | +class EntityC |
| 142 | +{ |
| 143 | +} |
0 commit comments