Completed
Pull Request — master (#727)
by
unknown
01:56
created

testRepositoryServiceWiring()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 53
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 9.5797
c 0
b 0
f 0
cc 2
eloc 39
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Doctrine\ORM\QueryBuilder;
23
use Fixtures\Bundles\RepositoryServiceBundle\Entity\TestCustomRepoEntity;
24
use Fixtures\Bundles\RepositoryServiceBundle\Entity\TestDefaultRepoEntity;
25
use Fixtures\Bundles\RepositoryServiceBundle\RepositoryServiceBundle;
26
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomRepoRepository;
27
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
28
use Symfony\Component\DependencyInjection\ContainerBuilder;
29
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
30
use Symfony\Component\DependencyInjection\Reference;
31
32
class ServiceRepositoryTest extends TestCase
33
{
34
    protected function setUp()
35
    {
36
        parent::setUp();
37
38
        if (!class_exists('Doctrine\\ORM\\Version')) {
39
            $this->markTestSkipped('Doctrine ORM is not available.');
40
        }
41
    }
42
43
    public function testRepositoryServiceWiring()
44
    {
45
        if (!class_exists(ServiceLocatorTagPass::class)) {
46
            $this->markTestSkipped();
47
        }
48
49
        $container = new ContainerBuilder(new ParameterBag(array(
50
            'kernel.name' => 'app',
51
            'kernel.debug' => false,
52
            'kernel.bundles' => array('RepositoryServiceBundle' => RepositoryServiceBundle::class),
53
            'kernel.cache_dir' => sys_get_temp_dir(),
54
            'kernel.environment' => 'test',
55
            'kernel.root_dir' => __DIR__.'/../../../../', // src dir
56
        )));
57
        $container->set('annotation_reader', new AnnotationReader());
58
        $extension = new DoctrineExtension();
59
        $container->registerExtension($extension);
60
        $extension->load(array(array(
61
            'dbal' => array(
62
                'driver' => 'pdo_mysql',
63
                'charset' => 'UTF8',
64
            ),
65
            'orm' => array(
66
                'mappings' => array('RepositoryServiceBundle' => array(
67
                    'type' => 'annotation',
68
                    'dir' => __DIR__.'/DependencyInjection/Fixtures/Bundles/RepositoryServiceBundle/Entity',
69
                    'prefix' => 'Fixtures\Bundles\RepositoryServiceBundle\Entity',
70
                )),
71
                'use_service_repositories' => true,
72
            ),
73
        )), $container);
74
75
        $container->register(TestCustomRepoRepository::class)
76
            ->setAutowired(true)
77
            ->setPublic(false)
78
            ->addTag('doctrine.repository_service');
79
80
        $container->getCompilerPassConfig()->addPass(new ServiceRepositoryCompilerPass());
81
        $container->compile();
82
83
        $em = $container->get('doctrine.orm.default_entity_manager');
84
        $customRepo = $em->getRepository(TestCustomRepoEntity::class);
85
        $this->assertSame($customRepo, $container->get(TestCustomRepoRepository::class));
86
        // a smoke test, trying some methods
87
        $this->assertSame(TestCustomRepoEntity::class, $customRepo->getClassName());
88
        $this->assertInstanceOf(QueryBuilder::class, $customRepo->createQueryBuilder('tc'));
89
90
        $genericRepository = $em->getRepository(TestDefaultRepoEntity::class);
91
        $this->assertInstanceOf(EntityRepository::class, $genericRepository);
92
        $this->assertSame($genericRepository, $genericRepository = $em->getRepository(TestDefaultRepoEntity::class));
93
        // a smoke test, trying one of the methods
94
        $this->assertSame(TestDefaultRepoEntity::class, $genericRepository->getClassName());
95
    }
96
}
97