Passed
Pull Request — master (#437)
by Alejandro
09:16
created

ReopeningEntityManagerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 31
c 1
b 0
f 0
dl 0
loc 63
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A wrappedInstanceIsTransparentlyCalledWhenItIsNotClosed() 0 12 1
A provideMethodNames() 0 7 1
A wrappedInstanceIsRecreatedWhenItIsClosed() 0 12 1
A setUp() 0 10 1
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Common\Doctrine;
5
6
use Doctrine\Common\EventManager;
7
use Doctrine\DBAL\Connection;
8
use Doctrine\ORM\Configuration;
9
use Doctrine\ORM\EntityManagerInterface;
10
use PHPUnit\Framework\TestCase;
11
use Prophecy\Argument;
12
use Prophecy\Prophecy\ObjectProphecy;
13
use Shlinkio\Shlink\Common\Doctrine\ReopeningEntityManager;
14
use stdClass;
15
16
class ReopeningEntityManagerTest extends TestCase
17
{
18
    /** @var ReopeningEntityManager */
19
    private $decoratorEm;
20
    /** @var ObjectProphecy */
21
    private $wrapped;
22
23
    public function setUp(): void
24
    {
25
        $this->wrapped = $this->prophesize(EntityManagerInterface::class);
26
        $this->wrapped->getConnection()->willReturn($this->prophesize(Connection::class));
27
        $this->wrapped->getConfiguration()->willReturn($this->prophesize(Configuration::class));
28
        $this->wrapped->getEventManager()->willReturn($this->prophesize(EventManager::class));
29
30
        $wrappedMock = $this->wrapped->reveal();
31
        $this->decoratorEm = new ReopeningEntityManager($wrappedMock, function () use ($wrappedMock) {
32
            return $wrappedMock;
33
        });
34
    }
35
36
    /**
37
     * @test
38
     * @dataProvider provideMethodNames
39
     */
40
    public function wrappedInstanceIsTransparentlyCalledWhenItIsNotClosed(string $methodName): void
41
    {
42
        $method = $this->wrapped->__call($methodName, [Argument::cetera()])->willReturnArgument();
43
        $isOpen = $this->wrapped->isOpen()->willReturn(true);
44
45
        $this->decoratorEm->{$methodName}(new stdClass());
46
47
        $method->shouldHaveBeenCalledOnce();
48
        $isOpen->shouldHaveBeenCalledOnce();
49
        $this->wrapped->getConnection()->shouldNotHaveBeenCalled();
50
        $this->wrapped->getConfiguration()->shouldNotHaveBeenCalled();
51
        $this->wrapped->getEventManager()->shouldNotHaveBeenCalled();
52
    }
53
54
    /**
55
     * @test
56
     * @dataProvider provideMethodNames
57
     */
58
    public function wrappedInstanceIsRecreatedWhenItIsClosed(string $methodName): void
59
    {
60
        $method = $this->wrapped->__call($methodName, [Argument::cetera()])->willReturnArgument();
61
        $isOpen = $this->wrapped->isOpen()->willReturn(false);
62
63
        $this->decoratorEm->{$methodName}(new stdClass());
64
65
        $method->shouldHaveBeenCalledOnce();
66
        $isOpen->shouldHaveBeenCalledOnce();
67
        $this->wrapped->getConnection()->shouldHaveBeenCalledOnce();
68
        $this->wrapped->getConfiguration()->shouldHaveBeenCalledOnce();
69
        $this->wrapped->getEventManager()->shouldHaveBeenCalledOnce();
70
    }
71
72
    public function provideMethodNames(): iterable
73
    {
74
        yield 'flush' => ['flush'];
75
        yield 'persist' => ['persist'];
76
        yield 'remove' => ['remove'];
77
        yield 'refresh' => ['refresh'];
78
        yield 'merge' => ['merge'];
79
    }
80
}
81