Completed
Pull Request — master (#1051)
by Karel
07:07
created

RepositoryManagerTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 139
Duplicated Lines 41.01 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 9
c 3
b 1
f 0
lcom 1
cbo 4
dl 57
loc 139
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A createFinderMock() 0 8 1
A createReaderMock() 0 8 1
A testThatGetRepositoryReturnsDefaultRepository() 12 12 1
A testThatGetRepositoryReturnsCustomRepository() 12 12 1
A testThatGetRepositoryThrowsExceptionIfEntityNotConfigured() 11 11 1
A testThatGetRepositoryThrowsExceptionIfCustomRepositoryNotFound() 11 11 1
A testThatGetRepositoryThrowsExceptionIfEntityDoesNotExist() 11 11 1
A testThatGetRepositoryCachesRepositoryInstances() 0 16 1
B testGetRepositoryNameCanReadFromClassAnnotation() 0 24 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace FOS\ElasticaBundle\Tests\Manager;
4
5
use FOS\ElasticaBundle\Configuration\Search;
6
use FOS\ElasticaBundle\Manager\RepositoryManager;
7
8
class CustomRepository
9
{
10
}
11
12
class Entity
13
{
14
}
15
16
/**
17
 * @author Richard Miller <[email protected]>
18
 */
19
class RepositoryManagerTest extends \PHPUnit_Framework_TestCase
20
{
21
    /**
22
     * @return \PHPUnit_Framework_MockObject_MockObject|\FOS\ElasticaBundle\Finder\TransformedFinder
23
     */
24
    private function createFinderMock()
25
    {
26
        $finderMock = $this->getMockBuilder('FOS\ElasticaBundle\Finder\TransformedFinder')
27
            ->disableOriginalConstructor()
28
            ->getMock();
29
30
        return $finderMock;
31
    }
32
33
    /**
34
     * @return \PHPUnit_Framework_MockObject_MockObject|\Doctrine\Common\Annotations\Reader
35
     */
36
    private function createReaderMock()
37
    {
38
        $readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader')
39
            ->disableOriginalConstructor()
40
            ->getMock();
41
42
        return $readerMock;
43
    }
44
45 View Code Duplication
    public function testThatGetRepositoryReturnsDefaultRepository()
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...
46
    {
47
        $finderMock = $this->createFinderMock();
48
        $readerMock = $this->createReaderMock();
49
50
        $entityName = 'FOS\ElasticaBundle\Tests\Manager\Entity';
51
52
        $manager = new RepositoryManager($readerMock);
53
        $manager->addEntity($entityName, $finderMock);
54
        $repository = $manager->getRepository($entityName);
55
        $this->assertInstanceOf('FOS\ElasticaBundle\Repository', $repository);
56
    }
57
58 View Code Duplication
    public function testThatGetRepositoryReturnsCustomRepository()
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...
59
    {
60
        $finderMock = $this->createFinderMock();
61
        $readerMock = $this->createReaderMock();
62
63
        $entityName = 'FOS\ElasticaBundle\Tests\Manager\Entity';
64
65
        $manager = new RepositoryManager($readerMock);
66
        $manager->addEntity($entityName, $finderMock, 'FOS\ElasticaBundle\Tests\Manager\CustomRepository');
67
        $repository = $manager->getRepository($entityName);
68
        $this->assertInstanceOf('FOS\ElasticaBundle\Tests\Manager\CustomRepository', $repository);
69
    }
70
71
    /**
72
     * @expectedException \RuntimeException
73
     */
74 View Code Duplication
    public function testThatGetRepositoryThrowsExceptionIfEntityNotConfigured()
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...
75
    {
76
        $finderMock = $this->createFinderMock();
77
        $readerMock = $this->createReaderMock();
78
79
        $entityName = 'FOS\ElasticaBundle\Tests\Manager\Entity';
80
81
        $manager = new RepositoryManager($readerMock);
82
        $manager->addEntity($entityName, $finderMock);
83
        $manager->getRepository('Missing Entity');
84
    }
85
86
    /**
87
     * @expectedException \RuntimeException
88
     */
89 View Code Duplication
    public function testThatGetRepositoryThrowsExceptionIfCustomRepositoryNotFound()
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...
90
    {
91
        $finderMock = $this->createFinderMock();
92
        $readerMock = $this->createReaderMock();
93
94
        $entityName = 'FOS\ElasticaBundle\Tests\Manager\Entity';
95
96
        $manager = new RepositoryManager($readerMock);
97
        $manager->addEntity($entityName, $finderMock, 'FOS\ElasticaBundle\Tests\MissingRepository');
98
        $manager->getRepository('FOS\ElasticaBundle\Tests\Manager\Entity');
99
    }
100
101
    /**
102
     * @expectedException \RuntimeException
103
     */
104 View Code Duplication
    public function testThatGetRepositoryThrowsExceptionIfEntityDoesNotExist()
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...
105
    {
106
        $finderMock = $this->createFinderMock();
107
        $readerMock = $this->createReaderMock();
108
109
        $entityName = 'FOS\ElasticaBundle\Tests\Manager\Entity';
110
111
        $manager = new RepositoryManager($readerMock);
112
        $manager->addEntity($entityName, $finderMock, 'FOS\ElasticaBundle\Tests\MissingRepository');
113
        $manager->getRepository('Missing Entity');
114
    }
115
116
    public function testThatGetRepositoryCachesRepositoryInstances()
117
    {
118
        $finderMock = $this->createFinderMock();
119
        $readerMock = $this->createReaderMock();
120
121
        $entityName = 'FOS\ElasticaBundle\Tests\Manager\Entity';
122
123
        $manager = new RepositoryManager($readerMock);
124
        $manager->addEntity($entityName, $finderMock, 'FOS\ElasticaBundle\Tests\Manager\CustomRepository');
125
        $repository = $manager->getRepository($entityName);
126
        $this->assertInstanceOf('FOS\ElasticaBundle\Tests\Manager\CustomRepository', $repository);
127
128
        $repository2 = $manager->getRepository($entityName);
129
        $this->assertInstanceOf('FOS\ElasticaBundle\Tests\Manager\CustomRepository', $repository2);
130
        $this->assertSame($repository, $repository2);
131
    }
132
133
    public function testGetRepositoryNameCanReadFromClassAnnotation()
134
    {
135
        $repositoryClass = 'FOS\ElasticaBundle\Tests\Manager\CustomRepository';
136
137
        $annotation = new Search;
0 ignored issues
show
Deprecated Code introduced by
The class FOS\ElasticaBundle\Configuration\Search has been deprecated with message: Use FOS\ElasticaBundle\Annotation\Search instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
138
        $annotation->repositoryClass = $repositoryClass;
139
140
        $finderMock = $this->createFinderMock();
141
        $readerMock = $this->createReaderMock();
142
143
        $readerMock
144
            ->expects($this->once())
145
            ->method('getClassAnnotation')
146
            ->will($this->returnValue($annotation));
147
148
        $entityName = 'FOS\ElasticaBundle\Tests\Manager\Entity';
149
150
        $manager = new RepositoryManager($readerMock);
151
        $manager->addEntity($entityName, $finderMock);
152
153
        $repository = $manager->getRepository($entityName);
154
155
        $this->assertInstanceOf($repositoryClass, $repository);
156
    }
157
}
158