Completed
Pull Request — master (#727)
by
unknown
11:47
created

testServiceRepositoriesMustExtendEntityRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 0
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
    public function testGetRepositoryReturnsService()
17
    {
18
        if (!interface_exists(ContainerInterface::class)) {
19
            $this->markTestSkipped('The Psr\Container\ContainerInterface (supplied by Symfony 3.3) is needed for this feature.');
20
        }
21
22
        $em = $this->createEntityManager(array(
23
            'Foo\CoolEntity' => 'my_repo'
24
        ));
25
        $repo = new StubRepository($em, new ClassMetadata(''));
26
        $container = $this->createContainer(array(
27
            'my_repo' => $repo
28
        ));
29
30
        $factory = new ContainerRepositoryFactory($container);
31
        $this->assertSame($repo, $factory->getRepository($em, 'Foo\CoolEntity'));
32
    }
33
34 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...
35
    {
36
        if (!interface_exists(ContainerInterface::class)) {
37
            $this->markTestSkipped('The Psr\Container\ContainerInterface (supplied by Symfony 3.3) is needed for this feature.');
38
        }
39
40
        $container = $this->createContainer(array());
41
        $em = $this->createEntityManager(array(
42
            'Foo\BoringEntity' => null
43
        ));
44
45
        $factory = new ContainerRepositoryFactory($container);
46
        $actualRepo = $factory->getRepository($em, 'Foo\BoringEntity');
47
        $this->assertInstanceOf(EntityRepository::class, $actualRepo);
48
        // test the same instance is returned
49
        $this->assertSame($actualRepo, $factory->getRepository($em, 'Foo\BoringEntity'));
50
    }
51
52 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...
53
    {
54
        if (!interface_exists(ContainerInterface::class)) {
55
            $this->markTestSkipped('The Psr\Container\ContainerInterface (supplied by Symfony 3.3) is needed for this feature.');
56
        }
57
58
        $container = $this->createContainer(array());
59
        $em = $this->createEntityManager(array(
60
            'Foo\CustomNormalRepoEntity' => StubRepository::class
61
        ));
62
63
        $factory = new ContainerRepositoryFactory($container);
64
        $actualRepo = $factory->getRepository($em, 'Foo\CustomNormalRepoEntity');
65
        $this->assertInstanceOf(StubRepository::class, $actualRepo);
66
        // test the same instance is returned
67
        $this->assertSame($actualRepo, $factory->getRepository($em, 'Foo\CustomNormalRepoEntity'));
68
    }
69
70
    /**
71
     * @expectedException \RuntimeException
72
     * @expectedExceptionMessage The service "my_repo" must extend EntityRepository (or a base class, like ServiceEntityRepository).
73
     */
74
    public function testServiceRepositoriesMustExtendEntityRepository()
75
    {
76
        $repo = new \stdClass();
77
78
        if (interface_exists(ContainerInterface::class)) {
79
            $container = $this->createContainer(array(
80
                'my_repo' => $repo
81
            ));
82
        } else {
83
            // Symfony 3.2 and lower support
84
            $container = null;
85
        }
86
87
        $em = $this->createEntityManager(array(
88
            'Foo\CoolEntity' => 'my_repo'
89
        ));
90
91
        $factory = new ContainerRepositoryFactory($container);
92
        $factory->getRepository($em, 'Foo\CoolEntity');
93
    }
94
95
    /**
96
     * @expectedException \RuntimeException
97
     * @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".
98
     */
99 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...
100
    {
101
        if (interface_exists(ContainerInterface::class)) {
102
            $container = $this->createContainer(array());
103
        } else {
104
            // Symfony 3.2 and lower support
105
            $container = null;
106
        }
107
108
        $em = $this->createEntityManager(array(
109
            'Foo\CoolEntity' => StubServiceRepository::class
110
        ));
111
112
        $factory = new ContainerRepositoryFactory($container);
113
        $factory->getRepository($em, 'Foo\CoolEntity');
114
    }
115
116
    /**
117
     * @expectedException \RuntimeException
118
     * @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".
119
     */
120 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...
121
    {
122
        if (interface_exists(ContainerInterface::class)) {
123
            $container = $this->createContainer(array());
124
        } else {
125
            // Symfony 3.2 and lower support
126
            $container = null;
127
        }
128
129
        $em = $this->createEntityManager(array(
130
            'Foo\CoolEntity' => 'not_a_real_class'
131
        ));
132
133
        $factory = new ContainerRepositoryFactory($container);
134
        $factory->getRepository($em, 'Foo\CoolEntity');
135
    }
136
137
    private function createContainer(array $services)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
138
    {
139
        $container = $this->getMockBuilder(ContainerInterface::class)->getMock();
140
        $container->expects($this->any())
141
            ->method('has')
142
            ->willReturnCallback(function($id) use ($services) {
143
                return isset($services[$id]);
144
            });
145
        $container->expects($this->any())
146
            ->method('get')
147
            ->willReturnCallback(function($id) use ($services) {
148
                return $services[$id];
149
            });
150
151
        return $container;
152
    }
153
154
    private function createEntityManager(array $entityRepositoryClasses)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
155
    {
156
        $classMetadatas = array();
157
        foreach ($entityRepositoryClasses as $entityClass => $entityRepositoryClass) {
158
            $metadata = new ClassMetadata($entityClass);
159
            $metadata->customRepositoryClassName = $entityRepositoryClass;
160
161
            $classMetadatas[$entityClass] = $metadata;
162
        }
163
164
        $em = $this->getMockBuilder(EntityManagerInterface::class)->getMock();
165
        $em->expects($this->any())
166
            ->method('getClassMetadata')
167
            ->willReturnCallback(function($class) use ($classMetadatas) {
168
                return $classMetadatas[$class];
169
            });
170
171
        $em->expects($this->any())
172
            ->method('getConfiguration')
173
            ->willReturn(new Configuration());
174
175
        return $em;
176
    }
177
}
178
179
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...
180
{
181
}
182
183
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...
184
{
185
}