Skip to content

Commit

Permalink
PHPCS fixes and test structure refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
michnovka committed Sep 11, 2022
1 parent 3da2eef commit 1914ba7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -2209,7 +2209,7 @@ public function refresh($entity, $lockMode = null)
*/
private function doRefresh($entity, array &$visited, $lockMode = null): void
{
switch(true) {
switch (true) {
case $lockMode === LockMode::PESSIMISTIC_READ:
case $lockMode === LockMode::PESSIMISTIC_WRITE:
if (! $this->em->getConnection()->isTransactionActive()) {
Expand Down
17 changes: 0 additions & 17 deletions tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,23 +390,6 @@ public function testIdentityMappedOptimisticLockUnversionedEntityThrowsException
$this->_em->find(CmsUser::class, $userId, LockMode::OPTIMISTIC);
}

/**
* @group locking
*/
public function testRefreshWithLockThrowsException(): void
{
$user = new CmsUser();
$user->name = 'Roman';
$user->username = 'romanb';
$user->status = 'freak';
$this->_em->persist($user);
$this->_em->flush();

$this->expectException(TransactionRequiredException::class);

$this->_em->refresh($user, LockMode::PESSIMISTIC_WRITE);
}

/** @group DDC-819 */
public function testFindMagicCallByNullValue(): void
{
Expand Down
52 changes: 52 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Locking/LockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ public function testLockPessimisticWriteNoTransactionThrowsException(): void
$this->_em->lock($article, LockMode::PESSIMISTIC_WRITE);
}

/**
* @group locking
*/
public function testRefreshWithLockPessimisticWriteNoTransactionThrowsException(): void
{
$article = new CmsArticle();
$article->text = 'my article';
$article->topic = 'Hello';

$this->_em->persist($article);
$this->_em->flush();

$this->expectException(TransactionRequiredException::class);

$this->_em->refresh($article, LockMode::PESSIMISTIC_WRITE);
}

/**
* @group DDC-178
* @group locking
Expand Down Expand Up @@ -180,6 +197,41 @@ public function testLockPessimisticWrite(): void
self::assertStringContainsString($writeLockSql, $lastLoggedQuery);
}

/**
* @group locking
*/
public function testRefreshWithLockPessimisticWrite(): void
{
$writeLockSql = $this->_em->getConnection()->getDatabasePlatform()->getWriteLockSQL();

if (! $writeLockSql) {
self::markTestSkipped('Database Driver has no Write Lock support.');
}

$article = new CmsArticle();
$article->text = 'my article';
$article->topic = 'Hello';

$this->_em->persist($article);
$this->_em->flush();

$this->_em->beginTransaction();
try {
$this->_em->refresh($article, LockMode::PESSIMISTIC_WRITE);
$this->_em->commit();
} catch (Exception $e) {
$this->_em->rollback();
}

$lastLoggedQuery = $this->getLastLoggedQuery()['sql'];
// DBAL 2 logs a commit as last query.
if ($lastLoggedQuery === '"COMMIT"') {
$lastLoggedQuery = $this->getLastLoggedQuery(1)['sql'];
}

self::assertStringContainsString($writeLockSql, $lastLoggedQuery);
}

/** @group DDC-178 */
public function testLockPessimisticRead(): void
{
Expand Down

0 comments on commit 1914ba7

Please sign in to comment.