Completed
Push — master ( 74c3c7...591b88 )
by Eric
08:09
created

TranslatableRepositoryTest::testFindOneBy()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 167
Code Lines 132

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 167
rs 8.2857
cc 1
eloc 132
nc 1
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 Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Component\Translation\Tests\Repository\Doctrine\ORM;
13
14
use Doctrine\ORM\EntityManager;
15
use Doctrine\ORM\Mapping\ClassMetadata;
16
use Doctrine\ORM\Query;
17
use Doctrine\ORM\Query\Expr;
18
use Doctrine\ORM\Query\Expr\Join;
19
use Doctrine\ORM\QueryBuilder;
20
use Lug\Component\Resource\Model\ResourceInterface;
21
use Lug\Component\Translation\Context\LocaleContextInterface;
22
use Lug\Component\Translation\Model\TranslatableInterface;
23
use Lug\Component\Translation\Model\TranslatableTrait;
24
use Lug\Component\Translation\Model\TranslationInterface;
25
use Lug\Component\Translation\Model\TranslationTrait;
26
use Lug\Component\Translation\Repository\Doctrine\ORM\TranslatableRepository;
27
28
/**
29
 * @author GeLo <[email protected]>
30
 */
31
class TranslatableRepositoryTest extends \PHPUnit_Framework_TestCase
32
{
33
    /**
34
     * @var TranslatableRepository
35
     */
36
    private $translatableRepository;
37
38
    /**
39
     * @var \PHPUnit_Framework_MockObject_MockObject|EntityManager
40
     */
41
    private $entityManager;
42
43
    /**
44
     * @var \PHPUnit_Framework_MockObject_MockObject|ClassMetadata
45
     */
46
    private $classMetadata;
47
48
    /**
49
     * @var \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
50
     */
51
    private $resource;
52
53
    /**
54
     * @var string
55
     */
56
    private $class;
57
58
    /**
59
     * @var \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface
60
     */
61
    private $localeContext;
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected function setUp()
67
    {
68
        $this->entityManager = $this->createEntityManagerMock();
69
        $this->classMetadata = $this->createClassMetadataMock();
70
        $this->resource = $this->createResourceMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createResourceMock() can also be of type object<Lug\Component\Res...odel\ResourceInterface>. However, the property $resource is declared as type object<PHPUnit_Framework_MockObject_MockObject>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
71
        $this->localeContext = $this->createLocaleContextMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createLocaleContextMock() can also be of type object<Lug\Component\Tra...LocaleContextInterface>. However, the property $localeContext is declared as type object<PHPUnit_Framework_MockObject_MockObject>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
72
        $this->class = $this->classMetadata->name = TranslatableTest::class;
73
74
        $this->translatableRepository = new TranslatableRepository(
75
            $this->entityManager,
76
            $this->classMetadata,
77
            $this->resource,
78
            $this->localeContext
79
        );
80
    }
81
82
    public function testCreateQueryBuilder()
83
    {
84
        $this->classMetadata
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Mapping\ClassMetadata.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
85
            ->expects($this->once())
86
            ->method('getFieldNames')
87
            ->will($this->returnValue([]));
88
89
        $this->classMetadata
90
            ->expects($this->once())
91
            ->method('getAssociationMapping')
92
            ->with($this->identicalTo('translations'))
93
            ->will($this->returnValue(['targetEntity' => $translationClass = TranslationTest::class]));
94
95
        $translationClassMetadata = $this->createClassMetadataMock();
96
        $translationClassMetadata
97
            ->expects($this->once())
98
            ->method('getFieldNames')
99
            ->will($this->returnValue([]));
100
101
        $this->entityManager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\EntityManager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
102
            ->expects($this->once())
103
            ->method('getClassMetadata')
104
            ->with($this->identicalTo($translationClass))
105
            ->will($this->returnValue($translationClassMetadata));
106
107
        $this->resource
108
            ->expects($this->once())
109
            ->method('getName')
110
            ->will($this->returnValue($name = 'resource'));
111
112
        $this->entityManager
113
            ->expects($this->once())
114
            ->method('createQueryBuilder')
115
            ->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock()));
116
117
        $queryBuilder
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\QueryBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
118
            ->expects($this->once())
119
            ->method('select')
120
            ->with($this->identicalTo($name))
121
            ->will($this->returnSelf());
122
123
        $queryBuilder
124
            ->expects($this->once())
125
            ->method('from')
126
            ->with(
127
                $this->identicalTo($this->class),
128
                $this->identicalTo($name),
129
                $this->isNull()
130
            )
131
            ->will($this->returnSelf());
132
133
        $queryBuilder
134
            ->expects($this->once())
135
            ->method('addSelect')
136
            ->with($this->identicalTo('resource_translation'))
137
            ->will($this->returnSelf());
138
139
        $queryBuilder
140
            ->expects($this->exactly(2))
141
            ->method('getRootAliases')
142
            ->will($this->returnValue([$name]));
143
144
        $queryBuilder
145
            ->expects($this->once())
146
            ->method('leftJoin')
147
            ->with(
148
                $this->identicalTo($name.'.translations'),
149
                $this->identicalTo('resource_translation'),
150
                $this->isNull(),
151
                $this->isNull(),
152
                $this->isNull()
153
            )
154
            ->will($this->returnSelf());
155
156
        $this->assertSame($queryBuilder, $this->translatableRepository->createQueryBuilder());
157
    }
158
159
    public function testCreateQueryBuilderForCollection()
160
    {
161
        $this->classMetadata
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Mapping\ClassMetadata.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
162
            ->expects($this->once())
163
            ->method('getFieldNames')
164
            ->will($this->returnValue([]));
165
166
        $this->classMetadata
167
            ->expects($this->once())
168
            ->method('getAssociationMapping')
169
            ->with($this->identicalTo('translations'))
170
            ->will($this->returnValue(['targetEntity' => $translationClass = TranslationTest::class]));
171
172
        $translationClassMetadata = $this->createClassMetadataMock();
173
        $translationClassMetadata
174
            ->expects($this->once())
175
            ->method('getFieldNames')
176
            ->will($this->returnValue([]));
177
178
        $this->entityManager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\EntityManager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
179
            ->expects($this->once())
180
            ->method('getClassMetadata')
181
            ->with($this->identicalTo($translationClass))
182
            ->will($this->returnValue($translationClassMetadata));
183
184
        $this->resource
185
            ->expects($this->once())
186
            ->method('getName')
187
            ->will($this->returnValue($name = 'resource'));
188
189
        $this->entityManager
190
            ->expects($this->once())
191
            ->method('createQueryBuilder')
192
            ->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock()));
193
194
        $queryBuilder
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\QueryBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
195
            ->expects($this->once())
196
            ->method('select')
197
            ->with($this->identicalTo($name))
198
            ->will($this->returnSelf());
199
200
        $queryBuilder
201
            ->expects($this->once())
202
            ->method('from')
203
            ->with(
204
                $this->identicalTo($this->class),
205
                $this->identicalTo($name),
206
                $this->isNull()
207
            )
208
            ->will($this->returnSelf());
209
210
        $queryBuilder
211
            ->expects($this->once())
212
            ->method('addSelect')
213
            ->with($this->identicalTo('resource_translation'))
214
            ->will($this->returnSelf());
215
216
        $queryBuilder
217
            ->expects($this->exactly(3))
218
            ->method('getRootAliases')
219
            ->will($this->returnValue([$name]));
220
221
        $this->localeContext
222
            ->expects($this->once())
223
            ->method('getLocales')
224
            ->will($this->returnValue($locales = ['fr']));
225
226
        $this->localeContext
227
            ->expects($this->once())
228
            ->method('getFallbackLocale')
229
            ->will($this->returnValue($fallbackLocale = 'en'));
230
231
        $queryBuilder
232
            ->expects($this->once())
233
            ->method('expr')
234
            ->will($this->returnValue($expr = $this->createExprMock()));
235
236
        $expr
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Query\Expr.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
237
            ->expects($this->once())
238
            ->method('in')
239
            ->with(
240
                $this->identicalTo('resource.locale'),
241
                $this->identicalTo(array_merge($locales, [$fallbackLocale]))
242
            )
243
            ->will($this->returnValue($expression = 'expression'));
244
245
        $queryBuilder
246
            ->expects($this->once())
247
            ->method('innerJoin')
248
            ->with(
249
                $this->identicalTo($name.'.translations'),
250
                $this->identicalTo('resource_translation'),
251
                $this->identicalTo(Join::WITH),
252
                $this->identicalTo($expression),
253
                $this->isNull()
254
            )
255
            ->will($this->returnSelf());
256
257
        $this->assertSame($queryBuilder, $this->translatableRepository->createQueryBuilderForCollection());
258
    }
259
260
    public function testFindOneBy()
261
    {
262
        $this->classMetadata
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Mapping\ClassMetadata.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
263
            ->expects($this->once())
264
            ->method('getFieldNames')
265
            ->will($this->returnValue([
266
                $translatableField = 'translatable_field',
267
                $bothField = 'both_field',
268
            ]));
269
270
        $this->classMetadata
271
            ->expects($this->once())
272
            ->method('getAssociationMapping')
273
            ->with($this->identicalTo('translations'))
274
            ->will($this->returnValue(['targetEntity' => $translationClass = TranslationTest::class]));
275
276
        $translationClassMetadata = $this->createClassMetadataMock();
277
        $translationClassMetadata
278
            ->expects($this->once())
279
            ->method('getFieldNames')
280
            ->will($this->returnValue([
281
                $translationField = 'translation_field',
282
                $bothField,
283
            ]));
284
285
        $this->entityManager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\EntityManager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
286
            ->expects($this->once())
287
            ->method('getClassMetadata')
288
            ->with($this->identicalTo($translationClass))
289
            ->will($this->returnValue($translationClassMetadata));
290
291
        $this->resource
292
            ->expects($this->once())
293
            ->method('getName')
294
            ->will($this->returnValue($name = 'resource'));
295
296
        $this->entityManager
297
            ->expects($this->once())
298
            ->method('createQueryBuilder')
299
            ->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock()));
300
301
        $queryBuilder
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\QueryBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
302
            ->expects($this->once())
303
            ->method('select')
304
            ->with($this->identicalTo($name))
305
            ->will($this->returnSelf());
306
307
        $queryBuilder
308
            ->expects($this->once())
309
            ->method('from')
310
            ->with(
311
                $this->identicalTo($this->class),
312
                $this->identicalTo($name),
313
                $this->isNull()
314
            )
315
            ->will($this->returnSelf());
316
317
        $queryBuilder
318
            ->expects($this->once())
319
            ->method('addSelect')
320
            ->with($this->identicalTo('resource_translation'))
321
            ->will($this->returnSelf());
322
323
        $queryBuilder
324
            ->expects($this->exactly(5))
325
            ->method('getRootAliases')
326
            ->will($this->returnValue([$name]));
327
328
        $queryBuilder
329
            ->expects($this->once())
330
            ->method('leftJoin')
331
            ->with(
332
                $this->identicalTo($name.'.translations'),
333
                $this->identicalTo($translation = 'resource_translation'),
334
                $this->isNull(),
335
                $this->isNull(),
336
                $this->isNull()
337
            )
338
            ->will($this->returnSelf());
339
340
        $queryBuilder
341
            ->expects($this->exactly(3))
342
            ->method('expr')
343
            ->will($this->returnValue($expr = $this->createExprMock()));
344
345
        $expr
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Query\Expr.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
346
            ->expects($this->at(0))
347
            ->method('eq')
348
            ->with(
349
                $this->identicalTo($name.'.'.$translatableField),
350
                $this->matchesRegularExpression('/:'.$name.'_'.$translatableField.'_[a-z0-9]{22}/')
351
            )
352
            ->will($this->returnValue($translatableWhere = 'translatable_where'));
353
354
        $expr
355
            ->expects($this->at(1))
356
            ->method('eq')
357
            ->with(
358
                $this->identicalTo($translation.'.'.$translationField),
359
                $this->matchesRegularExpression('/:'.$translation.'_'.$translationField.'_[a-z0-9]{22}/')
360
            )
361
            ->will($this->returnValue($translationWhere = 'translation_where'));
362
363
        $expr
364
            ->expects($this->at(2))
365
            ->method('eq')
366
            ->with(
367
                $this->identicalTo($name.'.'.$bothField),
368
                $this->matchesRegularExpression('/:'.$name.'_'.$bothField.'_[a-z0-9]{22}/')
369
            )
370
            ->will($this->returnValue($bothWhere = 'both_where'));
371
372
        $queryBuilder
373
            ->expects($this->exactly(3))
374
            ->method('andWhere')
375
            ->will($this->returnValueMap([
376
                [$translatableWhere, $queryBuilder],
377
                [$translationWhere, $queryBuilder],
378
                [$bothWhere, $queryBuilder],
379
            ]));
380
381
        $queryBuilder
382
            ->expects($this->at(9))
383
            ->method('setParameter')
384
            ->with(
385
                $this->matchesRegularExpression('/'.$name.'_'.$translatableField.'_[a-z0-9]{22}/'),
386
                $this->identicalTo($translatableValue = 'translatable_value'),
387
                $this->isNull()
388
            )
389
            ->will($this->returnSelf());
390
391
        $queryBuilder
392
            ->expects($this->at(13))
393
            ->method('setParameter')
394
            ->with(
395
                $this->matchesRegularExpression('/'.$translation.'_'.$translationField.'_[a-z0-9]{22}/'),
396
                $this->identicalTo($translationValue = 'translation_value'),
397
                $this->isNull()
398
            )
399
            ->will($this->returnSelf());
400
401
        $queryBuilder
402
            ->expects($this->at(17))
403
            ->method('setParameter')
404
            ->with(
405
                $this->matchesRegularExpression('/'.$name.'_'.$bothField.'_[a-z0-9]{22}/'),
406
                $this->identicalTo($bothValue = 'both_value'),
407
                $this->isNull()
408
            )
409
            ->will($this->returnSelf());
410
411
        $queryBuilder
412
            ->expects($this->once())
413
            ->method('getQuery')
414
            ->will($this->returnValue($query = $this->createQueryMock()));
415
416
        $query
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ORM\Query.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
417
            ->expects($this->once())
418
            ->method('getOneOrNullResult')
419
            ->will($this->returnValue($result = 'result'));
420
421
        $this->assertSame($result, $this->translatableRepository->findOneBy([
422
            $translatableField => $translatableValue,
423
            $translationField  => $translationValue,
424
            $bothField         => $bothValue,
425
        ]));
426
    }
427
428
    /**
429
     * @return \PHPUnit_Framework_MockObject_MockObject|EntityManager
430
     */
431
    private function createEntityManagerMock()
432
    {
433
        return $this->getMockBuilder(EntityManager::class)
434
            ->disableOriginalConstructor()
435
            ->getMock();
436
    }
437
438
    /**
439
     * @return \PHPUnit_Framework_MockObject_MockObject|ClassMetadata
440
     */
441
    private function createClassMetadataMock()
442
    {
443
        return $this->getMockBuilder(ClassMetadata::class)
444
            ->disableOriginalConstructor()
445
            ->getMock();
446
    }
447
448
    /**
449
     * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
450
     */
451
    private function createResourceMock()
452
    {
453
        return $this->getMock(ResourceInterface::class);
454
    }
455
456
    /**
457
     * @return \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface
458
     */
459
    private function createLocaleContextMock()
460
    {
461
        return $this->getMock(LocaleContextInterface::class);
462
    }
463
464
    /**
465
     * @return \PHPUnit_Framework_MockObject_MockObject|QueryBuilder
466
     */
467
    private function createQueryBuilderMock()
468
    {
469
        return $this->getMockBuilder(QueryBuilder::class)
470
            ->disableOriginalConstructor()
471
            ->getMock();
472
    }
473
474
    /**
475
     * @return \PHPUnit_Framework_MockObject_MockObject|Expr
476
     */
477
    private function createExprMock()
478
    {
479
        return $this->getMock(Expr::class);
480
    }
481
482
    /**
483
     * @return \PHPUnit_Framework_MockObject_MockObject|Query
484
     */
485
    private function createQueryMock()
486
    {
487
        return $this->getMock(\stdClass::class, ['getOneOrNullResult']);
488
    }
489
}
490
491
/**
492
 * @author GeLo <[email protected]>
493
 */
494
class TranslatableTest implements TranslatableInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
495
{
496
    use TranslatableTrait;
497
}
498
499
/**
500
 * @author GeLo <[email protected]>
501
 */
502
class TranslationTest implements TranslationInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
503
{
504
    use TranslationTrait;
505
}
506