Completed
Push — master ( b17efa...63e741 )
by
unknown
04:17 queued 01:40
created

testBuildingChoicesFromRepositoryMethod()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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