Skip to content

Commit 259f83b

Browse files
committed
#11977 added test coverage verifying that persisters are being used to batch INSERTs
1 parent 4a24860 commit 259f83b

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

src/UnitOfWork.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,11 +1038,9 @@ public function recomputeSingleEntityChangeSet(ClassMetadata $class, object $ent
10381038
*/
10391039
private function executeInserts(): void
10401040
{
1041-
$eventsToDispatch = [];
1042-
// @TODO #11977 test this by verifying the number of executed SQL queries in a flush operation?
1043-
// @TODO #11977 could this be applied also to batch updates? If so, let's raise a new issue.
10441041
/** @var list<InsertBatch<object>> $batchedByType */
1045-
$batchedByType = InsertBatch::batchByEntityType($this->em, $this->computeInsertExecutionOrder());
1042+
$batchedByType = InsertBatch::batchByEntityType($this->em, $this->computeInsertExecutionOrder());
1043+
$eventsToDispatch = [];
10461044

10471045
foreach ($batchedByType as $batch) {
10481046
$class = $batch->class;

tests/Tests/Mocks/EntityPersisterMock.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
class EntityPersisterMock extends BasicEntityPersister
1515
{
16+
/** @var int<0, max> */
17+
private int $countOfExecuteInsertCalls = 0;
1618
private array $inserts = [];
1719
private array $updates = [];
1820
private array $deletes = [];
@@ -40,6 +42,8 @@ public function addInsert(object $entity): void
4042

4143
public function executeInserts(): void
4244
{
45+
$this->countOfExecuteInsertCalls += 1;
46+
4347
foreach ($this->postInsertIds as $item) {
4448
$this->em->getUnitOfWork()->assignPostInsertId($item['entity'], $item['generatedId']);
4549
}
@@ -86,6 +90,7 @@ public function getDeletes(): array
8690

8791
public function reset(): void
8892
{
93+
$this->countOfExecuteInsertCalls = 0;
8994
$this->existsCalled = false;
9095
$this->identityColumnValueCounter = 0;
9196
$this->inserts = [];
@@ -97,4 +102,10 @@ public function isExistsCalled(): bool
97102
{
98103
return $this->existsCalled;
99104
}
105+
106+
/** @return int<0, max> */
107+
public function countOfExecuteInsertCalls(): int
108+
{
109+
return $this->countOfExecuteInsertCalls;
110+
}
100111
}

tests/Tests/ORM/Internal/UnitOfWork/InsertBatchTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
use Doctrine\ORM\Internal\UnitOfWork\InsertBatch;
1111
use Doctrine\ORM\Mapping\ClassMetadata;
1212
use PHPUnit\Framework\Attributes\CoversClass;
13+
use PHPUnit\Framework\Attributes\Group;
1314
use PHPUnit\Framework\MockObject\Stub;
1415
use PHPUnit\Framework\TestCase;
1516

1617
#[CoversClass(InsertBatch::class)]
18+
#[Group('#11977')]
1719
final class InsertBatchTest extends TestCase
1820
{
1921
private EntityManagerInterface&Stub $entityManager;

tests/Tests/ORM/UnitOfWorkTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Doctrine\Tests\Models\CMS\CmsUser;
3333
use Doctrine\Tests\Models\Forum\ForumAvatar;
3434
use Doctrine\Tests\Models\Forum\ForumUser;
35+
use Doctrine\Tests\Models\MixedToOneIdentity\Country;
3536
use Doctrine\Tests\OrmTestCase;
3637
use Exception as BaseException;
3738
use PHPUnit\Framework\Attributes\DataProvider;
@@ -141,6 +142,25 @@ public function testSavingSingleEntityWithIdentityColumnForcesInsert(): void
141142
self::assertIsNumeric($user->id);
142143
}
143144

145+
#[Group('#11977')]
146+
public function testMultipleInsertsAreBatchedInThePersister(): void
147+
{
148+
$userPersister = new EntityPersisterMock($this->_emMock, $this->_emMock->getClassMetadata(Country::class));
149+
$this->_unitOfWork->setEntityPersister(Country::class, $userPersister);
150+
151+
$country1 = new Country();
152+
$country1->country = 'Italy';
153+
$country2 = new Country();
154+
$country2->country = 'Germany';
155+
156+
$this->_unitOfWork->persist($country1);
157+
$this->_unitOfWork->persist($country2);
158+
$this->_unitOfWork->commit();
159+
160+
self::assertCount(2, $userPersister->getInserts());
161+
self::assertSame(1, $userPersister->countOfExecuteInsertCalls());
162+
}
163+
144164
/**
145165
* Tests a scenario where a save() operation is cascaded from a ForumUser
146166
* to its associated ForumAvatar, both entities using IDENTITY id generation.

0 commit comments

Comments
 (0)