Completed
Pull Request — master (#1203)
by
unknown
04:38
created

ContainerRepositoryFactoryTest::createContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\Tests\Repository;
4
5
use Doctrine\Bundle\DoctrineBundle\Repository\ContainerRepositoryFactory;
6
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface;
7
use Doctrine\ORM\Configuration;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Doctrine\ORM\EntityRepository;
10
use Doctrine\ORM\Mapping\ClassMetadata;
11
use Doctrine\Persistence\ObjectRepository;
12
use PHPUnit\Framework\MockObject\MockObject;
13
use PHPUnit\Framework\TestCase;
14
use Psr\Container\ContainerInterface;
15
use stdClass;
16
17
class ContainerRepositoryFactoryTest extends TestCase
18
{
19
    public static function setUpBeforeClass() : void
20
    {
21
        if (interface_exists(EntityManagerInterface::class)) {
22
            return;
23
        }
24
25
        self::markTestSkipped('This test requires ORM');
26
    }
27
28
    public function testGetRepositoryReturnsService() : void
29
    {
30
        $em        = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']);
31
        $repo      = new StubRepository();
32
        $container = $this->createContainer(['my_repo' => $repo]);
33
34
        $factory = new ContainerRepositoryFactory($container);
0 ignored issues
show
Documentation introduced by
$container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Container\ContainerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
        $this->assertSame($repo, $factory->getRepository($em, 'Foo\CoolEntity'));
0 ignored issues
show
Documentation introduced by
$em is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
    }
37
38
    public function testGetRepositoryReturnsEntityRepository() : void
39
    {
40
        $container = $this->createContainer([]);
41
        $em        = $this->createEntityManager(['Foo\BoringEntity' => null]);
42
43
        $factory    = new ContainerRepositoryFactory($container);
0 ignored issues
show
Documentation introduced by
$container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Container\ContainerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
        $actualRepo = $factory->getRepository($em, 'Foo\BoringEntity');
0 ignored issues
show
Documentation introduced by
$em is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
        $this->assertInstanceOf(EntityRepository::class, $actualRepo);
46
        // test the same instance is returned
47
        $this->assertSame($actualRepo, $factory->getRepository($em, 'Foo\BoringEntity'));
0 ignored issues
show
Documentation introduced by
$em is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
    }
49
50
    public function testCustomRepositoryIsReturned() : void
51
    {
52
        $container = $this->createContainer([]);
53
        $em        = $this->createEntityManager([
54
            'Foo\CustomNormalRepoEntity' => StubRepository::class,
55
        ]);
56
57
        $factory    = new ContainerRepositoryFactory($container);
0 ignored issues
show
Documentation introduced by
$container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Container\ContainerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
        $actualRepo = $factory->getRepository($em, 'Foo\CustomNormalRepoEntity');
0 ignored issues
show
Documentation introduced by
$em is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
        $this->assertInstanceOf(StubRepository::class, $actualRepo);
60
        // test the same instance is returned
61
        $this->assertSame($actualRepo, $factory->getRepository($em, 'Foo\CustomNormalRepoEntity'));
0 ignored issues
show
Documentation introduced by
$em is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
    }
63
64
    /**
65
     * @expectedException \RuntimeException
66
     * @expectedExceptionMessage The service "my_repo" must implement ObjectRepository (or extend a base class, like ServiceEntityRepository).
67
     */
68
    public function testServiceRepositoriesMustExtendObjectRepository() : void
69
    {
70
        $repo = new stdClass();
71
72
        $container = $this->createContainer(['my_repo' => $repo]);
73
74
        $em = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']);
75
76
        $factory = new ContainerRepositoryFactory($container);
0 ignored issues
show
Documentation introduced by
$container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Container\ContainerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
        $factory->getRepository($em, 'Foo\CoolEntity');
0 ignored issues
show
Documentation introduced by
$em is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
    }
79
80
    public function testServiceRepositoriesCanNotExtendsEntityRepository() : void
81
    {
82
        $repo = $this->getMockBuilder(ObjectRepository::class)->getMock();
83
84
        $container = $this->createContainer(['my_repo' => $repo]);
85
86
        $em = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']);
87
88
        $factory = new ContainerRepositoryFactory($container);
0 ignored issues
show
Documentation introduced by
$container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Container\ContainerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
        $factory->getRepository($em, 'Foo\CoolEntity');
0 ignored issues
show
Documentation introduced by
$em is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
        $actualRepo = $factory->getRepository($em, 'Foo\CoolEntity');
0 ignored issues
show
Documentation introduced by
$em is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
        $this->assertSame($repo, $actualRepo);
92
    }
93
94
    /**
95
     * @expectedException \RuntimeException
96
     * @expectedExceptionMessage The "Doctrine\Bundle\DoctrineBundle\Tests\Repository\StubServiceRepository" entity repository implements "Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface", but its service could not be found. Make sure the service exists and is tagged with "doctrine.repository_service".
97
     */
98
    public function testRepositoryMatchesServiceInterfaceButServiceNotFound() : void
99
    {
100
        $container = $this->createContainer([]);
101
102
        $em = $this->createEntityManager([
103
            'Foo\CoolEntity' => StubServiceRepository::class,
104
        ]);
105
106
        $factory = new ContainerRepositoryFactory($container);
0 ignored issues
show
Documentation introduced by
$container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Container\ContainerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
        $factory->getRepository($em, 'Foo\CoolEntity');
0 ignored issues
show
Documentation introduced by
$em is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
    }
109
110
    /**
111
     * @expectedException \RuntimeException
112
     * @expectedExceptionMessage The "Foo\CoolEntity" entity has a repositoryClass set to "not_a_real_class", but this is not a valid class. Check your class naming. If this is meant to be a service id, make sure this service exists and is tagged with "doctrine.repository_service".
113
     */
114
    public function testCustomRepositoryIsNotAValidClass() : void
115
    {
116
        $container = $this->createContainer([]);
117
118
        $em = $this->createEntityManager(['Foo\CoolEntity' => 'not_a_real_class']);
119
120
        $factory = new ContainerRepositoryFactory($container);
0 ignored issues
show
Documentation introduced by
$container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Container\ContainerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
        $factory->getRepository($em, 'Foo\CoolEntity');
0 ignored issues
show
Documentation introduced by
$em is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
122
    }
123
124
    private function createContainer(array $services) : MockObject
125
    {
126
        $container = $this->getMockBuilder(ContainerInterface::class)->getMock();
127
        $container->expects($this->any())
128
            ->method('has')
129
            ->willReturnCallback(static function ($id) use ($services) {
130
                return isset($services[$id]);
131
            });
132
        $container->expects($this->any())
133
            ->method('get')
134
            ->willReturnCallback(static function ($id) use ($services) {
135
                return $services[$id];
136
            });
137
138
        return $container;
139
    }
140
141
    private function createEntityManager(array $entityRepositoryClasses) : MockObject
142
    {
143
        $classMetadatas = [];
144
        foreach ($entityRepositoryClasses as $entityClass => $entityRepositoryClass) {
145
            $metadata                            = new ClassMetadata($entityClass);
146
            $metadata->customRepositoryClassName = $entityRepositoryClass;
147
148
            $classMetadatas[$entityClass] = $metadata;
149
        }
150
151
        $em = $this->getMockBuilder(EntityManagerInterface::class)->getMock();
152
        $em->expects($this->any())
153
            ->method('getClassMetadata')
154
            ->willReturnCallback(static function ($class) use ($classMetadatas) {
155
                return $classMetadatas[$class];
156
            });
157
158
        $em->expects($this->any())
159
            ->method('getConfiguration')
160
            ->willReturn(new Configuration());
161
162
        return $em;
163
    }
164
}
165
166
/**
167
 * Repository implementing non-deprecated interface, as current interface implemented in ORM\EntityRepository
168
 * uses deprecated one and Composer autoload triggers deprecations that can't be silenced by @group legacy
169
 */
170
class NonDeprecatedRepository implements ObjectRepository
171
{
172
    /**
173
     * {@inheritDoc}
174
     */
175
    public function find($id)
176
    {
177
        return null;
178
    }
179
180
    public function findAll() : array
181
    {
182
        return [];
183
    }
184
185
    public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null) : array
186
    {
187
        return [];
188
    }
189
190
    /**
191
     * {@inheritDoc}
192
     */
193
    public function findOneBy(array $criteria)
194
    {
195
        return null;
196
    }
197
198
    public function getClassName() : string
199
    {
200
        return '';
201
    }
202
}
203
204
class StubRepository extends NonDeprecatedRepository
205
{
206
}
207
208
class StubServiceRepository extends NonDeprecatedRepository implements ServiceEntityRepositoryInterface
209
{
210
}
211