Completed
Pull Request — master (#4)
by
unknown
02:24
created

testBuildingChoicesFromRepositoryWithoutSpecifyingFieldOrMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4286
cc 1
eloc 12
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 testBuildingChoicesFromRepositoryWithoutSpecifyingFieldOrMethod()
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');
194
        $builder->build([
195
            'repository' => $repositoryName,
196
        ]);
197
    }
198
199
    /**
200
     * @covers Netdudes\DataSourceryBundle\DataSource\Util\ChoicesBuilder::build
201
     * @covers Netdudes\DataSourceryBundle\DataSource\Util\ChoicesBuilder::getChoicesFromCallable
202
     */
203
    public function testBuildingChoicesFromCallable()
204
    {
205
        $aCallable = function () {
206
            return ['choice 1', 'choice 2', 'choice 3'];
207
        };
208
209
        $emMock = $this->prepareEntityManagerMock();
210
211
        $builder = new ChoicesBuilder($emMock);
212
        $choices = $builder->build($aCallable);
213
214
        $expectedChoices = [
215
            'choice 1',
216
            'choice 2',
217
            'choice 3',
218
        ];
219
        $this->assertSame($expectedChoices, $choices);
220
    }
221
222
    /**
223
     * @covers Netdudes\DataSourceryBundle\DataSource\Util\ChoicesBuilder::build
224
     * @covers Netdudes\DataSourceryBundle\DataSource\Util\ChoicesBuilder::getChoicesFromCallable
225
     */
226 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...
227
    {
228
        $aCallable = function () {
229
            return 'not an array';
230
        };
231
232
        $emMock = $this->prepareEntityManagerMock();
233
234
        $builder = new ChoicesBuilder($emMock);
235
236
        $this->setExpectedException('Exception', 'Choices callback defined in table configurations must return array');
237
        $builder->build($aCallable);
238
    }
239
240
    /**
241
     * @return \PHPUnit_Framework_MockObject_MockObject
242
     */
243
    private function prepareEntityManagerMock()
244
    {
245
        $mock = $this->getMockBuilder('Doctrine\ORM\EntityManager')
246
            ->disableOriginalConstructor()
247
            ->setMethods(['getRepository'])
248
            ->getMock();
249
250
        return $mock;
251
    }
252
253
    /**
254
     * @param array $extraMethods
255
     *
256
     * @return \PHPUnit_Framework_MockObject_MockObject
257
     */
258
    private function prepareRepositoryMock(array $extraMethods = [])
259
    {
260
        $mock = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
261
            ->setMethods(array_merge(['createQueryBuilder'], $extraMethods))
262
            ->disableOriginalConstructor()
263
            ->getMock();
264
265
        return $mock;
266
    }
267
}
268