2020-10-25 07:30:18 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Util;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-23 13:32:13 -05:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2020-10-25 07:30:18 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use RuntimeException;
|
|
|
|
use Shlinkio\Shlink\Core\Util\DoctrineBatchHelper;
|
|
|
|
|
|
|
|
class DoctrineBatchHelperTest extends TestCase
|
|
|
|
{
|
|
|
|
private DoctrineBatchHelper $helper;
|
2022-10-24 12:53:13 -05:00
|
|
|
private MockObject & EntityManagerInterface $em;
|
2020-10-25 07:30:18 -05:00
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
2022-10-23 13:32:13 -05:00
|
|
|
$this->em = $this->createMock(EntityManagerInterface::class);
|
|
|
|
$this->helper = new DoctrineBatchHelper($this->em);
|
2020-10-25 07:30:18 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideIterables')]
|
2020-10-25 07:30:18 -05:00
|
|
|
public function entityManagerIsFlushedAndClearedTheExpectedAmountOfTimes(
|
|
|
|
array $iterable,
|
|
|
|
int $batchSize,
|
2021-05-23 05:31:10 -05:00
|
|
|
int $expectedCalls,
|
2020-10-25 07:30:18 -05:00
|
|
|
): void {
|
2022-10-23 13:32:13 -05:00
|
|
|
$this->em->expects($this->once())->method('beginTransaction');
|
|
|
|
$this->em->expects($this->once())->method('commit');
|
|
|
|
$this->em->expects($this->never())->method('rollback');
|
|
|
|
$this->em->expects($this->exactly($expectedCalls))->method('flush');
|
|
|
|
$this->em->expects($this->exactly($expectedCalls))->method('clear');
|
|
|
|
|
2020-10-25 07:30:18 -05:00
|
|
|
$wrappedIterable = $this->helper->wrapIterable($iterable, $batchSize);
|
|
|
|
|
|
|
|
foreach ($wrappedIterable as $item) {
|
|
|
|
// Iterable needs to be iterated for the logic to be invoked
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideIterables(): iterable
|
2020-10-25 07:30:18 -05:00
|
|
|
{
|
|
|
|
yield [[], 100, 1];
|
|
|
|
yield [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3, 4];
|
|
|
|
yield [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 11, 1];
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2020-10-25 07:30:18 -05:00
|
|
|
public function transactionIsRolledBackWhenAnErrorOccurs(): void
|
|
|
|
{
|
2022-10-23 13:32:13 -05:00
|
|
|
$this->em->expects($this->once())->method('flush')->willThrowException(new RuntimeException());
|
|
|
|
$this->em->expects($this->once())->method('beginTransaction');
|
|
|
|
$this->em->expects($this->never())->method('commit');
|
|
|
|
$this->em->expects($this->once())->method('rollback');
|
2020-10-25 07:30:18 -05:00
|
|
|
|
|
|
|
$wrappedIterable = $this->helper->wrapIterable([1, 2, 3], 1);
|
|
|
|
|
|
|
|
self::expectException(RuntimeException::class);
|
|
|
|
|
|
|
|
foreach ($wrappedIterable as $item) {
|
|
|
|
// Iterable needs to be iterated for the logic to be invoked
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|