Passed
Push — merge-1.1.x ( d7c3d1 )
by Jonathan
04:14
created

testGetManagerForProxyInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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