Completed
Pull Request — 1.4.x (#106)
by Grégoire
02:25
created

ManagerRegistryTest::testResetManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
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\Common\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
            $driver   = $this->createMock(MappingDriver::class);
166
            $metadata = $this->createMock(ClassMetadata::class);
167
168
            $metadata
169
                ->expects($this->any())
170
                ->method('getName')
171
                ->willReturn($name === 'other_manager' ? OtherTestObject::class : TestObject::class);
172
173
            $mock->method('getMetadataFactory')->willReturn(new TestClassMetadataFactory($driver, $metadata));
174
175
            return $mock;
176
        };
177
    }
178
}
179
180
class TestManagerRegistry extends AbstractManagerRegistry
181
{
182
    /** @var object[] */
183
    private $services;
184
185
    /** @var callable */
186
    private $managerFactory;
187
188
    /**
189
     * @param string[] $connections
190
     * @param string[] $managers
191
     */
192
    public function __construct(
193
        $name,
194
        array $connections,
195
        array $managers,
196
        $defaultConnection,
197
        $defaultManager,
198
        $proxyInterfaceName,
199
        callable $managerFactory
200
    ) {
201
        $this->managerFactory = $managerFactory;
202
203
        parent::__construct($name, $connections, $managers, $defaultConnection, $defaultManager, $proxyInterfaceName);
204
    }
205
206
    protected function getService($name)
207
    {
208
        if (! isset($this->services[$name])) {
209
            $this->services[$name] = call_user_func($this->managerFactory, $name);
210
        }
211
212
        return $this->services[$name];
213
    }
214
215
    protected function resetService($name)
216
    {
217
        unset($this->services[$name]);
218
    }
219
220
    public function getAliasNamespace($alias)
221
    {
222
        return __NAMESPACE__;
223
    }
224
}
225