Completed
Pull Request — master (#1101)
by
unknown
16:28
created

NonDeprecatedRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 33
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 4 1
A findAll() 0 4 1
A findBy() 0 4 1
A findOneBy() 0 4 1
A getClassName() 0 4 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\TestCase;
13
use Psr\Container\ContainerInterface;
14
use stdClass;
15
16
class ContainerRepositoryFactoryTest extends TestCase
17
{
18
    public function testGetRepositoryReturnsService()
19
    {
20
        $em        = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']);
21
        $repo      = new StubRepository();
22
        $container = $this->createContainer(['my_repo' => $repo]);
23
24
        $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...
25
        $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...
26
    }
27
28
    public function testGetRepositoryReturnsEntityRepository()
29
    {
30
        $container = $this->createContainer([]);
31
        $em        = $this->createEntityManager(['Foo\BoringEntity' => null]);
32
33
        $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...
34
        $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...
35
        $this->assertInstanceOf(EntityRepository::class, $actualRepo);
36
        // test the same instance is returned
37
        $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...
38
    }
39
40
    public function testCustomRepositoryIsReturned()
41
    {
42
        $container = $this->createContainer([]);
43
        $em        = $this->createEntityManager([
44
            'Foo\CustomNormalRepoEntity' => StubRepository::class,
45
        ]);
46
47
        $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...
48
        $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...
49
        $this->assertInstanceOf(StubRepository::class, $actualRepo);
50
        // test the same instance is returned
51
        $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...
52
    }
53
54
    /**
55
     * @expectedException \RuntimeException
56
     * @expectedExceptionMessage The service "my_repo" must implement ObjectRepository (or extend a base class, like ServiceEntityRepository).
57
     */
58
    public function testServiceRepositoriesMustExtendObjectRepository()
59
    {
60
        $repo = new stdClass();
61
62
        $container = $this->createContainer(['my_repo' => $repo]);
63
64
        $em = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']);
65
66
        $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...
67
        $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...
68
    }
69
70
    public function testServiceRepositoriesCanNotExtendsEntityRepository()
71
    {
72
        $repo = $this->getMockBuilder(ObjectRepository::class)->getMock();
73
74
        $container = $this->createContainer(['my_repo' => $repo]);
75
76
        $em = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']);
77
78
        $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...
79
        $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...
80
        $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...
81
        $this->assertSame($repo, $actualRepo);
82
    }
83
84
    /**
85
     * @expectedException \RuntimeException
86
     * @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".
87
     */
88
    public function testRepositoryMatchesServiceInterfaceButServiceNotFound()
89
    {
90
        $container = $this->createContainer([]);
91
92
        $em = $this->createEntityManager([
93
            'Foo\CoolEntity' => StubServiceRepository::class,
94
        ]);
95
96
        $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...
97
        $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...
98
    }
99
100
    /**
101
     * @expectedException \RuntimeException
102
     * @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".
103
     */
104
    public function testCustomRepositoryIsNotAValidClass()
105
    {
106
        $container = $this->createContainer([]);
107
108
        $em = $this->createEntityManager(['Foo\CoolEntity' => 'not_a_real_class']);
109
110
        $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...
111
        $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...
112
    }
113
114
    private function createContainer(array $services)
115
    {
116
        $container = $this->getMockBuilder(ContainerInterface::class)->getMock();
117
        $container->expects($this->any())
118
            ->method('has')
119
            ->willReturnCallback(static function ($id) use ($services) {
120
                return isset($services[$id]);
121
            });
122
        $container->expects($this->any())
123
            ->method('get')
124
            ->willReturnCallback(static function ($id) use ($services) {
125
                return $services[$id];
126
            });
127
128
        return $container;
129
    }
130
131
    private function createEntityManager(array $entityRepositoryClasses)
132
    {
133
        $classMetadatas = [];
134
        foreach ($entityRepositoryClasses as $entityClass => $entityRepositoryClass) {
135
            $metadata                            = new ClassMetadata($entityClass);
136
            $metadata->customRepositoryClassName = $entityRepositoryClass;
137
138
            $classMetadatas[$entityClass] = $metadata;
139
        }
140
141
        $em = $this->getMockBuilder(EntityManagerInterface::class)->getMock();
142
        $em->expects($this->any())
143
            ->method('getClassMetadata')
144
            ->willReturnCallback(static function ($class) use ($classMetadatas) {
145
                return $classMetadatas[$class];
146
            });
147
148
        $em->expects($this->any())
149
            ->method('getConfiguration')
150
            ->willReturn(new Configuration());
151
152
        return $em;
153
    }
154
}
155
156
/**
157
 * Repository implementing non-deprecated interface, as current interface implemented in ORM\EntityRepository
158
 * uses deprecated one and Composer autoload triggers deprecations that can't be silenced by @group legacy
159
 */
160
class NonDeprecatedRepository implements ObjectRepository
161
{
162
    /**
163
     * {@inheritDoc}
164
     */
165
    public function find($id)
166
    {
167
        return null;
168
    }
169
170
    public function findAll() : array
171
    {
172
        return [];
173
    }
174
175
    public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null) : array
176
    {
177
        return [];
178
    }
179
180
    /**
181
     * {@inheritDoc}
182
     */
183
    public function findOneBy(array $criteria)
184
    {
185
        return null;
186
    }
187
188
    public function getClassName() : string
189
    {
190
        return '';
191
    }
192
}
193
194
class StubRepository extends NonDeprecatedRepository
195
{
196
}
197
198
class StubServiceRepository extends NonDeprecatedRepository implements ServiceEntityRepositoryInterface
199
{
200
}
201