Failed Conditions
Push — types ( 30d5cd...98b391 )
by Jonathan
02:28
created

ManagerRegistryTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 84
dl 0
loc 160
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A testGetRepositoryWithManagerDetection() 0 29 1
A testGetManagerForAliasedClass() 0 5 1
A getManagerFactory() 0 16 2
A testGetManagerForClass() 0 5 1
A testGetManagerForInvalidAliasedClass() 0 6 1
A testResetManager() 0 7 1
A testGetManagerForProxyInterface() 0 3 1
A testGetManagerForInvalidClass() 0 6 1
A testGetRepository() 0 13 1
A testGetRepositoryWithSpecificManagerName() 0 29 1
1
<?php
2
3
namespace Doctrine\Tests\Persistence;
4
5
use Doctrine\Persistence\AbstractManagerRegistry;
6
use Doctrine\Persistence\Mapping\ClassMetadata;
7
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
8
use Doctrine\Persistence\ObjectManager;
9
use Doctrine\Persistence\ObjectManagerAware;
10
use Doctrine\Persistence\ObjectRepository;
11
use Doctrine\Persistence\Proxy;
12
use Doctrine\Tests\DoctrineTestCase;
13
use Doctrine\Tests\Persistence\Mapping\TestClassMetadataFactory;
14
use PHPUnit\Framework\MockObject\MockObject;
15
use ReflectionException;
16
use function call_user_func;
17
18
/**
19
 * @uses Doctrine\Tests\Persistence\TestObject
20
 *
21
 * @groups DCOM-270
22
 */
23
class ManagerRegistryTest extends DoctrineTestCase
24
{
25
    /** @var TestManagerRegistry */
26
    private $mr;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function setUp() : void
32
    {
33
        $this->mr = new TestManagerRegistry(
34
            'ORM',
35
            ['default' => 'default_connection'],
36
            ['default' => 'default_manager'],
37
            'default',
38
            'default',
39
            Proxy::class,
40
            $this->getManagerFactory()
41
        );
42
    }
43
44
    public function testGetManagerForClass()
45
    {
46
        self::assertInstanceOf(
47
            ObjectManager::class,
48
            $this->mr->getManagerForClass(TestObject::class)
49
        );
50
    }
51
52
    public function testGetManagerForProxyInterface()
53
    {
54
        self::assertNull($this->mr->getManagerForClass(ObjectManagerAware::class));
55
    }
56
57
    public function testGetManagerForInvalidClass()
58
    {
59
        $this->expectException(ReflectionException::class);
60
        $this->expectExceptionMessage('Class Doctrine\Tests\Persistence\TestObjectInexistent does not exist');
61
62
        $this->mr->getManagerForClass('prefix:TestObjectInexistent');
63
    }
64
65
    public function testGetManagerForAliasedClass()
66
    {
67
        self::assertInstanceOf(
68
            ObjectManager::class,
69
            $this->mr->getManagerForClass('prefix:TestObject')
70
        );
71
    }
72
73
    public function testGetManagerForInvalidAliasedClass()
74
    {
75
        $this->expectException(ReflectionException::class);
76
        $this->expectExceptionMessage('Class Doctrine\Tests\Persistence\TestObject:Foo does not exist');
77
78
        $this->mr->getManagerForClass('prefix:TestObject:Foo');
79
    }
80
81
    public function testResetManager()
82
    {
83
        $manager    = $this->mr->getManager();
84
        $newManager = $this->mr->resetManager();
85
86
        self::assertInstanceOf(ObjectManager::class, $newManager);
87
        self::assertNotSame($manager, $newManager);
88
    }
89
90
    public function testGetRepository()
91
    {
92
        $repository = $this->createMock(ObjectRepository::class);
93
94
        /** @var MockObject $defaultManager */
95
        $defaultManager = $this->mr->getManager();
96
        $defaultManager
97
            ->expects($this->once())
98
            ->method('getRepository')
99
            ->with($this->equalTo(TestObject::class))
100
            ->will($this->returnValue($repository));
101
102
        self::assertSame($repository, $this->mr->getRepository(TestObject::class));
103
    }
104
105
    public function testGetRepositoryWithSpecificManagerName()
106
    {
107
        $this->mr = new TestManagerRegistry(
108
            'ORM',
109
            ['default' => 'default_connection'],
110
            ['default' => 'default_manager', 'other' => 'other_manager'],
111
            'default',
112
            'default',
113
            ObjectManagerAware::class,
114
            $this->getManagerFactory()
115
        );
116
117
        $repository = $this->createMock(ObjectRepository::class);
118
119
        /** @var MockObject $defaultManager */
120
        $defaultManager = $this->mr->getManager();
121
        $defaultManager
122
            ->expects($this->never())
123
            ->method('getRepository');
124
125
        /** @var MockObject $otherManager */
126
        $otherManager = $this->mr->getManager('other');
127
        $otherManager
128
            ->expects($this->once())
129
            ->method('getRepository')
130
            ->with($this->equalTo(TestObject::class))
131
            ->will($this->returnValue($repository));
132
133
        self::assertSame($repository, $this->mr->getRepository(TestObject::class, 'other'));
134
    }
135
136
    public function testGetRepositoryWithManagerDetection()
137
    {
138
        $this->mr = new TestManagerRegistry(
139
            'ORM',
140
            ['default' => 'default_connection'],
141
            ['default' => 'default_manager', 'other' => 'other_manager'],
142
            'default',
143
            'default',
144
            Proxy::class,
145
            $this->getManagerFactory()
146
        );
147
148
        $repository = $this->createMock(ObjectRepository::class);
149
150
        /** @var MockObject $defaultManager */
151
        $defaultManager = $this->mr->getManager();
152
        $defaultManager
153
            ->expects($this->never())
154
            ->method('getRepository');
155
156
        /** @var MockObject $otherManager */
157
        $otherManager = $this->mr->getManager('other');
158
        $otherManager
159
            ->expects($this->once())
160
            ->method('getRepository')
161
            ->with($this->equalTo(OtherTestObject::class))
162
            ->will($this->returnValue($repository));
163
164
        self::assertSame($repository, $this->mr->getRepository(OtherTestObject::class));
165
    }
166
167
    private function getManagerFactory()
168
    {
169
        return function (string $name) {
170
            $mock = $this->createMock(ObjectManager::class);
171
172
            $driver   = $this->createMock(MappingDriver::class);
173
            $metadata = $this->createMock(ClassMetadata::class);
174
175
            $metadata
176
                ->expects($this->any())
177
                ->method('getName')
178
                ->willReturn($name === 'other_manager' ? OtherTestObject::class : TestObject::class);
179
180
            $mock->method('getMetadataFactory')->willReturn(new TestClassMetadataFactory($driver, $metadata));
181
182
            return $mock;
183
        };
184
    }
185
}
186
187
class TestManagerRegistry extends AbstractManagerRegistry
188
{
189
    /** @var object[] */
190
    private $services;
191
192
    /** @var callable */
193
    private $managerFactory;
194
195
    /**
196
     * @param string[] $connections
197
     * @param string[] $managers
198
     */
199
    public function __construct(
200
        $name,
201
        array $connections,
202
        array $managers,
203
        $defaultConnection,
204
        $defaultManager,
205
        $proxyInterfaceName,
206
        callable $managerFactory
207
    ) {
208
        $this->managerFactory = $managerFactory;
209
210
        parent::__construct($name, $connections, $managers, $defaultConnection, $defaultManager, $proxyInterfaceName);
211
    }
212
213
    protected function getService(string $name) : object
214
    {
215
        if (! isset($this->services[$name])) {
216
            $this->services[$name] = call_user_func($this->managerFactory, $name);
217
        }
218
219
        return $this->services[$name];
220
    }
221
222
    protected function resetService(string $name) : void
223
    {
224
        unset($this->services[$name]);
225
    }
226
227
    public function getAliasNamespace(string $alias) : string
228
    {
229
        return __NAMESPACE__;
230
    }
231
}
232