ReopeningEntityManagerTest::provideWrapped()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Common\Doctrine;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use PHPUnit\Framework\TestCase;
9
use Prophecy\PhpUnit\ProphecyTrait;
10
use Shlinkio\Shlink\Common\Doctrine\ReopeningEntityManager;
11
12
class ReopeningEntityManagerTest extends TestCase
13
{
14
    use ProphecyTrait;
15
16
    /**
17
     * @test
18
     * @dataProvider provideWrapped
19
     */
20
    public function wrappedEntityManagerIsOnlyRecreatedWhenCurrentOneIsClosed(
21
        EntityManagerInterface $wrapped,
22
        bool $shouldRecreate
23
    ): void {
24
        $factoryCalls = 0;
25
        $reopeningEm = new ReopeningEntityManager(static function () use ($wrapped, &$factoryCalls) {
26
            $factoryCalls++;
27
            return $wrapped;
28
        });
29
30
        $reopeningEm->open();
31
32
        $this->assertEquals($shouldRecreate, $factoryCalls === 2);
33
    }
34
35
    public function provideWrapped(): iterable
36
    {
37
        $createEmMock = function (bool $isOpen): EntityManagerInterface {
38
            $em = $this->prophesize(EntityManagerInterface::class);
39
            $em->isOpen()->willReturn($isOpen);
40
41
            return $em->reveal();
42
        };
43
44
        yield [$createEmMock(true), false];
45
        yield [$createEmMock(false), true];
46
    }
47
}
48