Completed
Push — master ( 57a48c...37b82a )
by Alejandro
14s queued 13s
created

wrappedEntityManagerIsOnlyRecreatedWhenCurrentOneIsClosed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 13
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 Shlinkio\Shlink\Common\Doctrine\ReopeningEntityManager;
10
11
class ReopeningEntityManagerTest extends TestCase
12
{
13
    /**
14
     * @test
15
     * @dataProvider provideWrapped
16
     */
17
    public function wrappedEntityManagerIsOnlyRecreatedWhenCurrentOneIsClosed(
18
        EntityManagerInterface $wrapped,
19
        bool $shouldRecreate
20
    ): void {
21
        $factoryCalls = 0;
22
        $reopeningEm = new ReopeningEntityManager(static function () use ($wrapped, &$factoryCalls) {
23
            $factoryCalls++;
24
            return $wrapped;
25
        });
26
27
        $reopeningEm->open();
28
29
        $this->assertEquals($shouldRecreate, $factoryCalls === 2);
30
    }
31
32
    public function provideWrapped(): iterable
33
    {
34
        $createEmMock = function (bool $isOpen): EntityManagerInterface {
35
            $em = $this->prophesize(EntityManagerInterface::class);
36
            $em->isOpen()->willReturn($isOpen);
37
38
            return $em->reveal();
39
        };
40
41
        yield [$createEmMock(true), false];
42
        yield [$createEmMock(false), true];
43
    }
44
}
45