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

ServiceRepositoryTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
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 Doctrine\ORM\EntityRepository;
22
use Fixtures\Bundles\RepositoryServiceBundle\Entity\TestCustomRepoEntity;
23
use Fixtures\Bundles\RepositoryServiceBundle\Entity\TestDefaultRepoEntity;
24
use Fixtures\Bundles\RepositoryServiceBundle\RepositoryServiceBundle;
25
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomRepoRepository;
26
use Symfony\Component\DependencyInjection\ContainerBuilder;
27
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
28
29
class ServiceRepositoryTest extends TestCase
30
{
31
    protected function setUp()
32
    {
33
        parent::setUp();
34
35
        if (!class_exists('Doctrine\\ORM\\Version')) {
36
            $this->markTestSkipped('Doctrine ORM is not available.');
37
        }
38
    }
39
40
    public function testRepositoryServiceWiring()
41
    {
42
        $container = new ContainerBuilder(new ParameterBag(array(
43
            'kernel.name' => 'app',
44
            'kernel.debug' => false,
45
            'kernel.bundles' => array('RepositoryServiceBundle' => RepositoryServiceBundle::class),
46
            'kernel.cache_dir' => sys_get_temp_dir(),
47
            'kernel.environment' => 'test',
48
            'kernel.root_dir' => __DIR__.'/../../../../', // src dir
49
        )));
50
        $container->set('annotation_reader', new AnnotationReader());
51
        $extension = new DoctrineExtension();
52
        $container->registerExtension($extension);
53
        $extension->load(array(array(
54
            'dbal' => array(
55
                'driver' => 'pdo_mysql',
56
                'charset' => 'UTF8',
57
            ),
58
            'orm' => array(
59
                'mappings' => array('RepositoryServiceBundle' => array(
60
                    'type' => 'annotation',
61
                    'dir' => __DIR__.'/DependencyInjection/Fixtures/Bundles/RepositoryServiceBundle/Entity',
62
                    'prefix' => 'Fixtures\Bundles\RepositoryServiceBundle\Entity',
63
                )),
64
                'use_service_repositories' => true,
65
            ),
66
        )), $container);
67
68
        $container->register(TestCustomRepoRepository::class)
69
            ->addTag('doctrine.repository_service');
70
71
        $container->getCompilerPassConfig()->addPass(new ServiceRepositoryCompilerPass());
72
        $container->compile();
73
74
        $em = $container->get('doctrine.orm.default_entity_manager');
75
        $customRepo = $em->getRepository(TestCustomRepoEntity::class);
76
        $this->assertSame($customRepo, $container->get(TestCustomRepoRepository::class));
77
78
        $genericRepository = $em->getRepository(TestDefaultRepoEntity::class);
79
        $this->assertInstanceOf(DefaultServiceRepository::class, $genericRepository);
80
        $this->assertSame($genericRepository, $genericRepository = $em->getRepository(TestDefaultRepoEntity::class));
81
    }
82
}
83