ChoicesBuilderTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 271
Duplicated Lines 27.68 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 75
loc 271
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testBuildingChoicesWithInvalidConfig() 12 12 1
A testBuildingChoicesFromRepositoryField() 0 52 1
B testBuildingChoicesFromRepositoryMethod() 0 37 1
B testBuildingChoicesFromRepositoryMethodWhenMethodDoesNotExist() 24 26 1
B testBuildingChoicesFromRepositoryMethodWhenMethodDoesNotReturnAnArray() 25 27 1
B testBuildingChoicesFromRepositoryWhenSpecifyingBothFieldAndMethod() 0 24 1
A testBuildingChoicesFromRepositoryWithoutSpecifyingFieldOrMethod() 0 22 1
A testBuildingChoicesFromCallable() 0 18 1
A testBuildingChoicesFromCallableWhenResultIsNotAnArray() 14 14 1
A prepareEntityManagerMock() 0 9 1
A prepareRepositoryMock() 0 9 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 Netdudes\DataSourceryBundle\Tests\DataSource\Util;
4
5
use Netdudes\DataSourceryBundle\DataSource\Util\ChoicesBuilder;
6
use PHPUnit\Framework\TestCase;
7
8
class ChoicesBuilderTest extends TestCase
9
{
10 View Code Duplication
    public function testBuildingChoicesWithInvalidConfig()
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...
11
    {
12
        $invalidConfig = 'invalid configuration';
13
14
        $emMock = $this->prepareEntityManagerMock();
15
16
        $builder = new ChoicesBuilder($emMock);
17
18
        $this->expectException(\Exception::class);
19
        $this->expectExceptionMessage('No usable configuration was found');
20
        $builder->build($invalidConfig);
21
    }
22
23
    public function testBuildingChoicesFromRepositoryField()
24
    {
25
        $fieldName = 'a_field';
26
        $repositoryName = 'a_test_repository';
27
28
        $repositoryChoices = [
29
            [$fieldName => 'choice 1'],
30
            [$fieldName => 'choice 2'],
31
            [$fieldName => 'choice 3'],
32
        ];
33
34
        $queryMock = $this->getMockBuilder('Doctrine\ORM\AbstractQuery')// because Query is final
35
        ->disableOriginalConstructor()
36
            ->getMock();
37
        $queryMock->expects($this->once())
38
            ->method('getArrayResult')
39
            ->willReturn($repositoryChoices);
40
41
        $queryBuilderMock = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
42
            ->disableOriginalConstructor()
43
            ->setMethods(['select', 'orderBy', 'getQuery'])
44
            ->getMock();
45
        $queryBuilderMock->method('select')->will($this->returnSelf());
46
        $queryBuilderMock->method('orderBy')->will($this->returnSelf());
47
        $queryBuilderMock->method('getQuery')->willReturn($queryMock);
48
49
        $repositoryMock = $this->prepareRepositoryMock();
50
        $repositoryMock->expects($this->once())
51
            ->method('createQueryBuilder')
52
            ->willReturn($queryBuilderMock);
53
54
        $emMock = $this->prepareEntityManagerMock();
55
        $emMock->expects($this->once())
56
            ->method('getRepository')
57
            ->with($this->equalTo($repositoryName))
58
            ->willReturn($repositoryMock);
59
60
        $builder = new ChoicesBuilder($emMock);
61
        $choices = $builder->build(
62
            [
63
                'repository' => $repositoryName,
64
                'field' => $fieldName,
65
            ]
66
        );
67
68
        $expectedChoices = [
69
            'choice 1' => 'choice 1',
70
            'choice 2' => 'choice 2',
71
            'choice 3' => 'choice 3',
72
        ];
73
        $this->assertSame($expectedChoices, $choices);
74
    }
75
76
    public function testBuildingChoicesFromRepositoryMethod()
77
    {
78
        $methodName = 'a_method';
79
        $repositoryName = 'a_test_repository';
80
81
        $repositoryChoices = [
82
            'choice 1',
83
            'choice 2',
84
            'choice 3',
85
        ];
86
87
        $repositoryMock = $this->prepareRepositoryMock([$methodName]);
88
        $repositoryMock->expects($this->once())
89
            ->method($methodName)
90
            ->willReturn($repositoryChoices);
91
92
        $emMock = $this->prepareEntityManagerMock();
93
        $emMock->expects($this->once())
94
            ->method('getRepository')
95
            ->with($this->equalTo($repositoryName))
96
            ->willReturn($repositoryMock);
97
98
        $builder = new ChoicesBuilder($emMock);
99
        $choices = $builder->build(
100
            [
101
                'repository' => $repositoryName,
102
                'method' => $methodName,
103
            ]
104
        );
105
106
        $expectedChoices = [
107
            'choice 1',
108
            'choice 2',
109
            'choice 3',
110
        ];
111
        $this->assertSame($expectedChoices, $choices);
112
    }
113
114 View Code Duplication
    public function testBuildingChoicesFromRepositoryMethodWhenMethodDoesNotExist()
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...
115
    {
116
        $methodName = 'a_method';
117
        $repositoryName = 'a_test_repository';
118
119
        $repositoryMock = $this->prepareRepositoryMock([$methodName]);
120
        $repositoryMock->expects($this->never())
121
            ->method($methodName);
122
123
        $emMock = $this->prepareEntityManagerMock();
124
        $emMock->expects($this->once())
125
            ->method('getRepository')
126
            ->with($this->equalTo($repositoryName))
127
            ->willReturn($repositoryMock);
128
129
        $builder = new ChoicesBuilder($emMock);
130
131
        $this->expectException(\Exception::class);
132
        $this->expectExceptionMessage("Specified repository does not have 'other_method' method");
133
        $builder->build(
134
            [
135
                'repository' => $repositoryName,
136
                'method' => 'other_method',
137
            ]
138
        );
139
    }
140
141 View Code Duplication
    public function testBuildingChoicesFromRepositoryMethodWhenMethodDoesNotReturnAnArray()
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...
142
    {
143
        $methodName = 'a_method';
144
        $repositoryName = 'a_test_repository';
145
146
        $repositoryMock = $this->prepareRepositoryMock([$methodName]);
147
        $repositoryMock->expects($this->once())
148
            ->method($methodName)
149
            ->willReturn('not an array');
150
151
        $emMock = $this->prepareEntityManagerMock();
152
        $emMock->expects($this->once())
153
            ->method('getRepository')
154
            ->with($this->equalTo($repositoryName))
155
            ->willReturn($repositoryMock);
156
157
        $builder = new ChoicesBuilder($emMock);
158
159
        $this->expectException(\Exception::class);
160
        $this->expectExceptionMessage("Repository method $methodName must return an array of choices");
161
        $builder->build(
162
            [
163
                'repository' => $repositoryName,
164
                'method' => $methodName,
165
            ]
166
        );
167
    }
168
169
    public function testBuildingChoicesFromRepositoryWhenSpecifyingBothFieldAndMethod()
170
    {
171
        $repositoryName = 'a_test_repository';
172
173
        $repositoryMock = $this->prepareRepositoryMock();
174
175
        $emMock = $this->prepareEntityManagerMock();
176
        $emMock->expects($this->once())
177
            ->method('getRepository')
178
            ->with($this->equalTo($repositoryName))
179
            ->willReturn($repositoryMock);
180
181
        $builder = new ChoicesBuilder($emMock);
182
183
        $this->expectException(\Exception::class);
184
        $this->expectExceptionMessage('Repository source expects field or method parameter, but not both');
185
        $builder->build(
186
            [
187
                'repository' => $repositoryName,
188
                'field' => 'a_field',
189
                'method' => 'a_method',
190
            ]
191
        );
192
    }
193
194
    public function testBuildingChoicesFromRepositoryWithoutSpecifyingFieldOrMethod()
195
    {
196
        $repositoryName = 'a_test_repository';
197
198
        $repositoryMock = $this->prepareRepositoryMock();
199
200
        $emMock = $this->prepareEntityManagerMock();
201
        $emMock->expects($this->once())
202
            ->method('getRepository')
203
            ->with($this->equalTo($repositoryName))
204
            ->willReturn($repositoryMock);
205
206
        $builder = new ChoicesBuilder($emMock);
207
208
        $this->expectException(\Exception::class);
209
        $this->expectExceptionMessage('Repository source expects field or method parameter');
210
        $builder->build(
211
            [
212
                'repository' => $repositoryName,
213
            ]
214
        );
215
    }
216
217
    public function testBuildingChoicesFromCallable()
218
    {
219
        $aCallable = function () {
220
            return ['choice 1', 'choice 2', 'choice 3'];
221
        };
222
223
        $emMock = $this->prepareEntityManagerMock();
224
225
        $builder = new ChoicesBuilder($emMock);
226
        $choices = $builder->build($aCallable);
227
228
        $expectedChoices = [
229
            'choice 1',
230
            'choice 2',
231
            'choice 3',
232
        ];
233
        $this->assertSame($expectedChoices, $choices);
234
    }
235
236 View Code Duplication
    public function testBuildingChoicesFromCallableWhenResultIsNotAnArray()
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...
237
    {
238
        $aCallable = function () {
239
            return 'not an array';
240
        };
241
242
        $emMock = $this->prepareEntityManagerMock();
243
244
        $builder = new ChoicesBuilder($emMock);
245
246
        $this->expectException(\Exception::class);
247
        $this->expectExceptionMessage('The provided choice callback must return an array of choices');
248
        $builder->build($aCallable);
249
    }
250
251
    /**
252
     * @return \PHPUnit_Framework_MockObject_MockObject
253
     */
254
    private function prepareEntityManagerMock()
255
    {
256
        $mock = $this->getMockBuilder('Doctrine\ORM\EntityManager')
257
            ->disableOriginalConstructor()
258
            ->setMethods(['getRepository'])
259
            ->getMock();
260
261
        return $mock;
262
    }
263
264
    /**
265
     * @param array $extraMethods
266
     *
267
     * @return \PHPUnit_Framework_MockObject_MockObject
268
     */
269
    private function prepareRepositoryMock(array $extraMethods = [])
270
    {
271
        $mock = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
272
            ->setMethods(array_merge(['createQueryBuilder'], $extraMethods))
273
            ->disableOriginalConstructor()
274
            ->getMock();
275
276
        return $mock;
277
    }
278
}
279