Completed
Pull Request — master (#772)
by Michael
02:09
created

ContainerRepositoryFactoryTest::createContainer()   D

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 1
Ratio 6.25 %

Importance

Changes 0
Metric Value
dl 1
loc 16
rs 4.8292
c 0
b 0
f 0
cc 1
eloc 11
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 PHPUnit\Framework\TestCase;
12
use Psr\Container\ContainerInterface;
13
14
class ContainerRepositoryFactoryTest extends TestCase
15
{
16 View Code Duplication
    public function testGetRepositoryReturnsService()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
    {
18
        if (! interface_exists(ContainerInterface::class)) {
19
            $this->markTestSkipped('Symfony 3.3 is needed for this feature.');
20
        }
21
22
        $em        = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']);
23
        $repo      = new StubRepository($em, new ClassMetadata(''));
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...
24
        $container = $this->createContainer(['my_repo' => $repo]);
25
26
        $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 null|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...
27
        $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...
28
    }
29
30 View Code Duplication
    public function testGetRepositoryReturnsEntityRepository()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
    {
32
        if (! interface_exists(ContainerInterface::class)) {
33
            $this->markTestSkipped('Symfony 3.3 is needed for this feature.');
34
        }
35
36
        $container = $this->createContainer([]);
37
        $em        = $this->createEntityManager(['Foo\BoringEntity' => null]);
38
39
        $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 null|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...
40
        $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...
41
        $this->assertInstanceOf(EntityRepository::class, $actualRepo);
42
        // test the same instance is returned
43
        $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...
44
    }
45
46 View Code Duplication
    public function testCustomRepositoryIsReturned()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        if (! interface_exists(ContainerInterface::class)) {
49
            $this->markTestSkipped('Symfony 3.3 is needed for this feature.');
50
        }
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 null|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 extend EntityRepository (or a base class, like ServiceEntityRepository).
67
     */
68 View Code Duplication
    public function testServiceRepositoriesMustExtendEntityRepository()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        if (! interface_exists(ContainerInterface::class)) {
71
            $this->markTestSkipped('Symfony 3.3 is needed for this feature.');
72
        }
73
74
        $repo = new \stdClass();
75
76
        $container = $this->createContainer(['my_repo' => $repo]);
77
78
        $em = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']);
79
80
        $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 null|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...
81
        $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...
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 View Code Duplication
    public function testRepositoryMatchesServiceInterfaceButServiceNotFound()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90
        if (! interface_exists(ContainerInterface::class)) {
91
            $this->markTestSkipped('Symfony 3.3 is needed for this feature.');
92
        }
93
94
        $container = $this->createContainer([]);
95
96
        $em = $this->createEntityManager([
97
            'Foo\CoolEntity' => StubServiceRepository::class,
98
        ]);
99
100
        $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 null|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...
101
        $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...
102
    }
103
104
    /**
105
     * @expectedException \RuntimeException
106
     * @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".
107
     */
108 View Code Duplication
    public function testCustomRepositoryIsNotAValidClass()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110
        if (interface_exists(ContainerInterface::class)) {
111
            $container = $this->createContainer([]);
112
        } else {
113
            // Symfony 3.2 and lower support
114
            $container = null;
115
        }
116
117
        $em = $this->createEntityManager(['Foo\CoolEntity' => 'not_a_real_class']);
118
119
        $factory = new ContainerRepositoryFactory($container);
0 ignored issues
show
Bug introduced by
It seems like $container defined by $this->createContainer(array()) on line 111 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Doctrine\Bundle\Doctrine...yFactory::__construct() does only seem to accept null|object<Psr\Container\ContainerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
120
        $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...
121
    }
122
123
    private function createContainer(array $services)
124
    {
125
        $container = $this->getMockBuilder(ContainerInterface::class)->getMock();
126
        $container->expects($this->any())
127
            ->method('has')
128
            ->willReturnCallback(function ($id) use ($services) {
129
                return isset($services[$id]);
130
            });
131
        $container->expects($this->any())
132
            ->method('get')
133
            ->willReturnCallback(function ($id) use ($services) {
134
                return $services[$id];
135
            });
136
137
        return $container;
138
    }
139
140
    private function createEntityManager(array $entityRepositoryClasses)
141
    {
142
        $classMetadatas = [];
143
        foreach ($entityRepositoryClasses as $entityClass => $entityRepositoryClass) {
144
            $metadata                            = new ClassMetadata($entityClass);
145
            $metadata->customRepositoryClassName = $entityRepositoryClass;
146
147
            $classMetadatas[$entityClass] = $metadata;
148
        }
149
150
        $em = $this->getMockBuilder(EntityManagerInterface::class)->getMock();
151
        $em->expects($this->any())
152
            ->method('getClassMetadata')
153
            ->willReturnCallback(function ($class) use ($classMetadatas) {
154
                return $classMetadatas[$class];
155
            });
156
157
        $em->expects($this->any())
158
            ->method('getConfiguration')
159
            ->willReturn(new Configuration());
160
161
        return $em;
162
    }
163
}
164
165
class StubRepository extends EntityRepository
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
166
{
167
}
168
169
class StubServiceRepository extends EntityRepository implements ServiceEntityRepositoryInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
170
{
171
}
172