Completed
Pull Request — master (#719)
by
unknown
02:11
created

testRepositoryServiceWiring()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 34
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Doctrine Bundle
5
 *
6
 * The code was originally distributed inside the Symfony framework.
7
 *
8
 * (c) Fabien Potencier <[email protected]>
9
 * (c) Doctrine Project, Benjamin Eberlei <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Doctrine\Bundle\DoctrineBundle\Tests;
16
17
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
18
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
19
use Doctrine\Bundle\DoctrineBundle\Repository\DefaultServiceRepository;
20
use Doctrine\Common\Annotations\AnnotationReader;
21
use Fixtures\Bundles\RepositoryServiceBundle\Entity\TestCustomRepoEntity;
22
use Fixtures\Bundles\RepositoryServiceBundle\Entity\TestDefaultRepoEntity;
23
use Fixtures\Bundles\RepositoryServiceBundle\RepositoryServiceBundle;
24
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomRepoRepository;
25
use Symfony\Component\DependencyInjection\ContainerBuilder;
26
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
27
28
class ServiceRepositoryTest extends TestCase
29
{
30
    protected function setUp()
31
    {
32
        parent::setUp();
33
34
        if (!class_exists('Doctrine\\ORM\\Version')) {
35
            $this->markTestSkipped('Doctrine ORM is not available.');
36
        }
37
    }
38
39
    public function testRepositoryServiceWiring()
40
    {
41
        $container = new ContainerBuilder(new ParameterBag(array(
42
            'kernel.name' => 'app',
43
            'kernel.debug' => false,
44
            'kernel.bundles' => array('RepositoryServiceBundle' => RepositoryServiceBundle::class),
45
            'kernel.cache_dir' => sys_get_temp_dir(),
46
            'kernel.environment' => 'test',
47
            'kernel.root_dir' => __DIR__.'/../../../../', // src dir
48
        )));
49
        $container->set('annotation_reader', new AnnotationReader());
50
        $extension = new DoctrineExtension();
51
        $container->registerExtension($extension);
52
        $extension->load(array(array(
53
            'dbal' => array(
54
                'driver' => 'pdo_mysql',
55
                'charset' => 'UTF8',
56
            ),
57
            'orm' => array(
58
                'mappings' => array('RepositoryServiceBundle' => array(
59
                    'type' => 'annotation',
60
                    'dir' => __DIR__.'/DependencyInjection/Fixtures/Bundles/RepositoryServiceBundle/Entity',
61
                    'prefix' => 'Fixtures\Bundles\RepositoryServiceBundle\Entity',
62
                )),
63
                'use_service_repositories' => true,
64
            ),
65
        )), $container);
66
67
        $container->register(TestCustomRepoRepository::class)
68
            ->addTag('doctrine.repository_service');
69
70
        $container->getDefinition('doctrine.orm.container_repository_factory')
71
            ->setPublic(true);
72
        $container->getCompilerPassConfig()->addPass(new ServiceRepositoryCompilerPass());
73
        $container->compile();
74
75
        $em = $container->get('doctrine.orm.default_entity_manager');
76
        $customRepo = $em->getRepository(TestCustomRepoEntity::class);
77
        $this->assertSame($customRepo, $container->get(TestCustomRepoRepository::class));
78
79
        $genericRepository = $em->getRepository(TestDefaultRepoEntity::class);
80
        $this->assertInstanceOf(DefaultServiceRepository::class, $genericRepository);
81
        $this->assertSame($genericRepository, $genericRepository = $em->getRepository(TestDefaultRepoEntity::class));
82
    }
83
}
84