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

ReopeningEntityManagerTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

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

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 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