Skip to content

Commit

Permalink
Adding TransactionalManagerInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
eerison committed Aug 12, 2022
1 parent 99e5a29 commit b3c97ba
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Entity/BaseEntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Sonata\Doctrine\Exception\TransactionException;
use Sonata\Doctrine\Model\BaseManager;
use Sonata\Doctrine\Model\TransactionalManagerInterface;

/**
* @author Sylvain Deloux <[email protected]>
*
* @phpstan-template T of object
* @phpstan-extends BaseManager<T>
*/
abstract class BaseEntityManager extends BaseManager
abstract class BaseEntityManager extends BaseManager implements TransactionalManagerInterface
{
/**
* Make sure the code is compatible with legacy code.
Expand Down Expand Up @@ -78,6 +80,25 @@ public function getEntityManager()
return $objectManager;
}

public function beginTransaction(): void
{
$this->getEntityManager()->beginTransaction();
}

public function commit(): void
{
try {
$this->getEntityManager()->commit();
} catch (\Throwable $exception) {
throw new TransactionException($exception->getMessage(), (int) $exception->getCode(), $exception);
}
}

public function rollBack(): void
{
$this->getEntityManager()->rollback();
}

/**
* @phpstan-return EntityRepository<T>
*/
Expand Down
21 changes: 21 additions & 0 deletions src/Exception/TransactionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\Doctrine\Exception;

/**
* @author Erison Silva <[email protected]>
*/
final class TransactionException extends \RuntimeException
{
}
31 changes: 31 additions & 0 deletions src/Model/TransactionalManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\Doctrine\Model;

use Sonata\Doctrine\Exception\TransactionException;

/**
* @author Erison Silva <[email protected]>
*/
interface TransactionalManagerInterface
{
public function beginTransaction(): void;

/**
* @throws TransactionException
*/
public function commit(): void;

public function rollBack(): void;
}
55 changes: 55 additions & 0 deletions tests/Entity/BaseEntityManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sonata\Doctrine\Entity\BaseEntityManager;
use Sonata\Doctrine\Exception\TransactionException;
use Sonata\Doctrine\Model\TransactionalManagerInterface;

final class BaseEntityManagerTest extends TestCase
{
Expand Down Expand Up @@ -99,4 +101,57 @@ public function testGetRepository(): void

static::assertInstanceOf(EntityRepository::class, $m->invoke($this->manager));
}

public function testTransactionsFields(): void
{
// Mocks
$entityManagerMock = $this->createMock(EntityManagerInterface::class);
$entityManagerMock
->expects(static::once())
->method('beginTransaction');

$entityManagerMock
->expects(static::once())
->method('commit');

$entityManagerMock
->expects(static::once())
->method('rollback');

$baseEntityManagerMock = $this->createPartialMock(BaseEntityManager::class, ['getEntityManager']);
$baseEntityManagerMock
->method('getEntityManager')
->willReturn($entityManagerMock);

// Run code
$baseEntityManagerMock->beginTransaction();
$baseEntityManagerMock->commit();
$baseEntityManagerMock->rollBack();

// Asserts
static::assertInstanceOf(TransactionalManagerInterface::class, $baseEntityManagerMock);
}

public function testTransactionException(): void
{
// Asserts exception
static::expectException(TransactionException::class);
static::expectExceptionMessage('Foo message');
static::expectExceptionCode(123);

// Mocks
$entityManagerMock = $this->createMock(EntityManagerInterface::class);
$entityManagerMock
->expects(static::once())
->method('commit')
->willThrowException(new TransactionException('Foo message', 123, new \Exception()));

$baseEntityManagerMock = $this->createPartialMock(BaseEntityManager::class, ['getEntityManager']);
$baseEntityManagerMock
->method('getEntityManager')
->willReturn($entityManagerMock);

// Run code
$baseEntityManagerMock->commit();
}
}

0 comments on commit b3c97ba

Please sign in to comment.