ReopeningEntityManagerTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
eloc 14
c 2
b 0
f 0
dl 0
loc 34
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A provideWrapped() 0 11 1
A wrappedEntityManagerIsOnlyRecreatedWhenCurrentOneIsClosed() 0 13 1
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