Completed
Pull Request — master (#727)
by
unknown
02:10
created

ContainerRepositoryFactoryTest::createContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
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\ORM\Configuration;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\EntityRepository;
9
use Doctrine\ORM\Mapping\ClassMetadata;
10
use PHPUnit\Framework\TestCase;
11
use Psr\Container\ContainerInterface;
12
13
class ContainerRepositoryFactoryTest extends TestCase
14
{
15
    public function testGetRepositoryReturnsService()
16
    {
17
        $em = $this->createEntityManager(array(
18
            'Foo\CoolEntity' => 'my_repo'
19
        ));
20
        $repo = new StubRepository($em, new ClassMetadata(''));
21
        $container = $this->createContainer(array(
22
            'my_repo' => $repo
23
        ));
24
25
        $factory = new ContainerRepositoryFactory($container);
26
        $this->assertSame($repo, $factory->getRepository($em, 'Foo\CoolEntity'));
27
    }
28
29
    public function testGetRepositoryReturnsEntityRepository()
30
    {
31
        $container = $this->createContainer(array());
32
        $em = $this->createEntityManager(array(
33
            'Foo\BoringEntity' => null
34
        ));
35
36
        $factory = new ContainerRepositoryFactory($container);
37
        $actualRepo = $factory->getRepository($em, 'Foo\BoringEntity');
38
        $this->assertInstanceOf(EntityRepository::class, $actualRepo);
39
        // test the same instance is returned
40
        $this->assertSame($actualRepo, $factory->getRepository($em, 'Foo\BoringEntity'));
41
    }
42
43
    /**
44
     * @expectedException \RuntimeException
45
     * @expectedExceptionMessage The service "my_repo" must extend EntityRepository (or a base class, like ServiceEntityRepository).
46
     */
47 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...
48
    {
49
        $repo = new \stdClass();
50
        $container = $this->createContainer(array(
51
            'my_repo' => $repo
52
        ));
53
        $em = $this->createEntityManager(array(
54
            'Foo\CoolEntity' => 'my_repo'
55
        ));
56
57
        $factory = new ContainerRepositoryFactory($container);
58
        $factory->getRepository($em, 'Foo\CoolEntity');
59
    }
60
61
    /**
62
     * @expectedException \RuntimeException
63
     * @expectedExceptionMessage Could not find the repository service for the "Foo\CoolEntity" entity. Make sure the "my_repo" service exists and is tagged with "doctrine.repository_service".
64
     */
65 View Code Duplication
    public function testIfServiceDoesNotExist()
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...
66
    {
67
        $container = $this->createContainer(array());
68
        $em = $this->createEntityManager(array(
69
            'Foo\CoolEntity' => 'my_repo'
70
        ));
71
72
        $factory = new ContainerRepositoryFactory($container);
73
        $factory->getRepository($em, 'Foo\CoolEntity');
74
    }
75
76
    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...
77
    {
78
        $container = $this->getMockBuilder(ContainerInterface::class)->getMock();
79
        $container->expects($this->any())
80
            ->method('has')
81
            ->willReturnCallback(function($id) use ($services) {
82
                return isset($services[$id]);
83
            });
84
        $container->expects($this->any())
85
            ->method('get')
86
            ->willReturnCallback(function($id) use ($services) {
87
                return $services[$id];
88
            });
89
90
        return $container;
91
    }
92
93
    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...
94
    {
95
        $classMetadatas = array();
96
        foreach ($entityRepositoryClasses as $entityClass => $entityRepositoryClass) {
97
            $metadata = new ClassMetadata($entityClass);
98
            $metadata->customRepositoryClassName = $entityRepositoryClass;
99
100
            $classMetadatas[$entityClass] = $metadata;
101
        }
102
103
        $em = $this->getMockBuilder(EntityManagerInterface::class)->getMock();
104
        $em->expects($this->any())
105
            ->method('getClassMetadata')
106
            ->willReturnCallback(function($class) use ($classMetadatas) {
107
                return $classMetadatas[$class];
108
            });
109
110
        $em->expects($this->any())
111
            ->method('getConfiguration')
112
            ->willReturn(new Configuration());
113
114
        return $em;
115
    }
116
}
117
118
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...
119
{
120
}
121