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

RepositoryTest::setUpQueryBuilder()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 82
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 82
rs 8.3768
cc 5
eloc 64
nc 6
nop 3

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\Resource\Tests\Repository\Doctrine\ORM;
13
14
use Doctrine\ORM\EntityManager;
15
use Doctrine\ORM\EntityRepository;
16
use Doctrine\ORM\Mapping\ClassMetadata;
17
use Doctrine\ORM\Query;
18
use Doctrine\ORM\Query\Expr;
19
use Doctrine\ORM\QueryBuilder;
20
use Lug\Component\Grid\DataSource\Doctrine\ORM\DataSourceBuilder;
21
use Lug\Component\Resource\Model\ResourceInterface;
22
use Lug\Component\Resource\Repository\Doctrine\ORM\Repository;
23
use Lug\Component\Resource\Repository\RepositoryInterface;
24
use Pagerfanta\Adapter\DoctrineORMAdapter;
25
use Pagerfanta\Pagerfanta;
26
27
/**
28
 * @author GeLo <[email protected]>
29
 */
30
class RepositoryTest extends \PHPUnit_Framework_TestCase
31
{
32
    /**
33
     * @var Repository
34
     */
35
    private $repository;
36
37
    /**
38
     * @var \PHPUnit_Framework_MockObject_MockObject|EntityManager
39
     */
40
    private $entityManager;
41
42
    /**
43
     * @var \PHPUnit_Framework_MockObject_MockObject|ClassMetadata
44
     */
45
    private $classMetadata;
46
47
    /**
48
     * @var \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
49
     */
50
    private $resource;
51
52
    /**
53
     * @var string
54
     */
55
    private $class;
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected function setUp()
61
    {
62
        $this->entityManager = $this->createEntityManagerMock();
63
        $this->classMetadata = $this->createClassMetadataMock();
64
        $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...
65
        $this->class = $this->classMetadata->name = \stdClass::class;
66
67
        $this->repository = new Repository($this->entityManager, $this->classMetadata, $this->resource);
68
    }
69
70
    public function testInheritance()
71
    {
72
        $this->assertInstanceOf(RepositoryInterface::class, $this->repository);
73
        $this->assertInstanceOf(EntityRepository::class, $this->repository);
74
    }
75
76
    public function testCreateDataSourceBuilder()
77
    {
78
        $this->setUpQueryBuilder();
79
        $dataSourceBuilder = $this->repository->createDataSourceBuilder();
80
81
        $this->assertInstanceOf(DataSourceBuilder::class, $dataSourceBuilder);
82
    }
83
84
    public function testCreateQueryBuilder()
85
    {
86
        $queryBuilder = $this->setUpQueryBuilder();
87
88
        $this->assertSame($queryBuilder, $this->repository->createQueryBuilder());
89
    }
90
91
    public function testCreateQueryBuilderWithAlias()
92
    {
93
        $queryBuilder = $this->setUpQueryBuilder(false, $alias = 'foo');
94
95
        $this->assertSame($queryBuilder, $this->repository->createQueryBuilder($alias));
96
    }
97
98
    public function testCreateQueryBuilderWithIndexBy()
99
    {
100
        $queryBuilder = $this->setUpQueryBuilder(false, null, $indexBy = 'index');
101
102
        $this->assertSame($queryBuilder, $this->repository->createQueryBuilder(null, $indexBy));
103
    }
104
105
    public function testCreateQueryForCollectionBuilder()
106
    {
107
        $queryBuilder = $this->setUpQueryBuilder();
108
109
        $this->assertSame($queryBuilder, $this->repository->createQueryBuilderForCollection());
110
    }
111
112
    public function testCreateQueryBuilderForCollectionWithAlias()
113
    {
114
        $queryBuilder = $this->setUpQueryBuilder(false, $alias = 'foo');
115
116
        $this->assertSame($queryBuilder, $this->repository->createQueryBuilderForCollection($alias));
117
    }
118
119
    public function testCreateQueryBuilderForCollectionWithIndexBy()
120
    {
121
        $queryBuilder = $this->setUpQueryBuilder(false, null, $indexBy = 'index');
122
123
        $this->assertSame($queryBuilder, $this->repository->createQueryBuilderForCollection(null, $indexBy));
124
    }
125
126 View Code Duplication
    public function testFindOneByWithNullValue()
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...
127
    {
128
        $queryBuilder = $this->setUpQueryBuilder($value = null);
129
130
        $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...
131
            ->expects($this->once())
132
            ->method('getQuery')
133
            ->will($this->returnValue($query = $this->createQueryMock()));
134
135
        $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...
136
            ->expects($this->once())
137
            ->method('getOneOrNullResult')
138
            ->will($this->returnValue($result = 'result'));
139
140
        $this->assertSame($result, $this->repository->findOneBy(['foo' => $value], ['baz' => 'ASC']));
141
    }
142
143 View Code Duplication
    public function testFindOneByWithScalarValue()
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...
144
    {
145
        $queryBuilder = $this->setUpQueryBuilder($value = 'bar');
146
147
        $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...
148
            ->expects($this->once())
149
            ->method('getQuery')
150
            ->will($this->returnValue($query = $this->createQueryMock()));
151
152
        $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...
153
            ->expects($this->once())
154
            ->method('getOneOrNullResult')
155
            ->will($this->returnValue($result = 'result'));
156
157
        $this->assertSame($result, $this->repository->findOneBy(['foo' => $value], ['baz' => 'ASC']));
158
    }
159
160 View Code Duplication
    public function testFindOneByWithArrayValue()
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...
161
    {
162
        $queryBuilder = $this->setUpQueryBuilder($value = ['bar']);
163
164
        $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...
165
            ->expects($this->once())
166
            ->method('getQuery')
167
            ->will($this->returnValue($query = $this->createQueryMock()));
168
169
        $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...
170
            ->expects($this->once())
171
            ->method('getOneOrNullResult')
172
            ->will($this->returnValue($result = 'result'));
173
174
        $this->assertSame($result, $this->repository->findOneBy(['foo' => $value], ['baz' => 'ASC']));
175
    }
176
177
    public function testFindBy()
178
    {
179
        $queryBuilder = $this->setUpQueryBuilder($value = 'bar');
180
181
        $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...
182
            ->expects($this->once())
183
            ->method('setMaxResults')
184
            ->with($this->identicalTo($limit = 5))
185
            ->will($this->returnSelf());
186
187
        $queryBuilder
188
            ->expects($this->once())
189
            ->method('setFirstResult')
190
            ->with($this->identicalTo($offset = 10))
191
            ->will($this->returnSelf());
192
193
        $queryBuilder
194
            ->expects($this->once())
195
            ->method('getQuery')
196
            ->will($this->returnValue($query = $this->createQueryMock()));
197
198
        $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...
199
            ->expects($this->once())
200
            ->method('getResult')
201
            ->will($this->returnValue($result = ['result']));
202
203
        $this->assertSame($result, $this->repository->findBy(['foo' => $value], ['baz' => 'ASC'], $limit, $offset));
204
    }
205
206 View Code Duplication
    public function testFindForIndex()
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...
207
    {
208
        $this->setUpQueryBuilder($value = 'bar');
209
210
        $pager = $this->repository->findForIndex(['foo' => $value], ['baz' => 'ASC']);
211
212
        $this->assertInstanceOf(Pagerfanta::class, $pager);
213
        $this->assertInstanceOf(DoctrineORMAdapter::class, $pager->getAdapter());
214
    }
215
216 View Code Duplication
    public function testFindForShow()
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...
217
    {
218
        $queryBuilder = $this->setUpQueryBuilder($value = 'bar');
219
220
        $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...
221
            ->expects($this->once())
222
            ->method('getQuery')
223
            ->will($this->returnValue($query = $this->createQueryMock()));
224
225
        $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...
226
            ->expects($this->once())
227
            ->method('getOneOrNullResult')
228
            ->will($this->returnValue($result = 'result'));
229
230
        $this->assertSame($result, $this->repository->findForShow(['foo' => $value], ['baz' => 'ASC']));
231
    }
232
233 View Code Duplication
    public function testFindForUpdate()
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...
234
    {
235
        $queryBuilder = $this->setUpQueryBuilder($value = 'bar');
236
237
        $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...
238
            ->expects($this->once())
239
            ->method('getQuery')
240
            ->will($this->returnValue($query = $this->createQueryMock()));
241
242
        $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...
243
            ->expects($this->once())
244
            ->method('getOneOrNullResult')
245
            ->will($this->returnValue($result = 'result'));
246
247
        $this->assertSame($result, $this->repository->findForUpdate(['foo' => $value], ['baz' => 'ASC']));
248
    }
249
250 View Code Duplication
    public function testFindForDelete()
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...
251
    {
252
        $queryBuilder = $this->setUpQueryBuilder($value = 'bar');
253
254
        $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...
255
            ->expects($this->once())
256
            ->method('getQuery')
257
            ->will($this->returnValue($query = $this->createQueryMock()));
258
259
        $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...
260
            ->expects($this->once())
261
            ->method('getOneOrNullResult')
262
            ->will($this->returnValue($result = 'result'));
263
264
        $this->assertSame($result, $this->repository->findForDelete(['foo' => $value], ['baz' => 'ASC']));
265
    }
266
267
    /**
268
     * @param mixed       $value
269
     * @param string|null $alias
270
     * @param string|null $indexBy
271
     *
272
     * @return \PHPUnit_Framework_MockObject_MockObject|QueryBuilder
273
     */
274
    private function setUpQueryBuilder($value = false, $alias = null, $indexBy = null)
275
    {
276
        if ($alias === null) {
277
            $this->resource
278
                ->expects($this->once())
279
                ->method('getName')
280
                ->will($this->returnValue($resource = 'resource'));
281
        } else {
282
            $resource = $alias;
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('createQueryBuilder')
288
            ->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock()));
289
290
        $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...
291
            ->expects($this->once())
292
            ->method('select')
293
            ->with($this->identicalTo($resource))
294
            ->will($this->returnSelf());
295
296
        $queryBuilder
297
            ->expects($this->once())
298
            ->method('from')
299
            ->with($this->identicalTo($this->class), $this->identicalTo($resource), $this->identicalTo($indexBy))
300
            ->will($this->returnSelf());
301
302
        $queryBuilder
303
            ->expects($this->any())
304
            ->method('getRootAliases')
305
            ->will($this->returnValue([$resource]));
306
307
        if ($value === false) {
308
            return $queryBuilder;
309
        }
310
311
        $queryBuilder
312
            ->expects($this->once())
313
            ->method('expr')
314
            ->will($this->returnValue($expr = $this->createExprMock()));
315
316
        if ($value === null) {
317
            $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...
318
                ->expects($this->once())
319
                ->method('isNull')
320
                ->with($this->identicalTo($resource.'.'.($property = 'foo')))
321
                ->will($this->returnValue($expression = 'expression'));
322
        } else {
323
            $expr
324
                ->expects($this->once())
325
                ->method(is_string($value) ? 'eq' : 'in')
326
                ->with(
327
                    $this->identicalTo($resource.'.'.($property = 'foo')),
328
                    $this->matchesRegularExpression('/:'.$resource.'_'.$property.'_[a-z0-9]{22}/')
329
                )
330
                ->will($this->returnValue($expression = 'expression'));
331
332
            $queryBuilder
333
                ->expects($this->once())
334
                ->method('setParameter')
335
                ->with(
336
                    $this->matchesRegularExpression('/'.$resource.'_'.$property.'_[a-z0-9]{22}/'),
337
                    $this->identicalTo($value)
338
                )
339
                ->will($this->returnSelf());
340
        }
341
342
        $queryBuilder
343
            ->expects($this->once())
344
            ->method('andWhere')
345
            ->with($this->identicalTo($expression))
346
            ->will($this->returnSelf());
347
348
        $queryBuilder
349
            ->expects($this->once())
350
            ->method('addOrderBy')
351
            ->with($this->identicalTo($resource.'.'.($order = 'baz')), $this->identicalTo($sort = 'ASC'))
352
            ->will($this->returnSelf());
353
354
        return $queryBuilder;
355
    }
356
357
    /**
358
     * @return \PHPUnit_Framework_MockObject_MockObject|EntityManager
359
     */
360
    private function createEntityManagerMock()
361
    {
362
        return $this->getMockBuilder(EntityManager::class)
363
            ->disableOriginalConstructor()
364
            ->getMock();
365
    }
366
367
    /**
368
     * @return \PHPUnit_Framework_MockObject_MockObject|ClassMetadata
369
     */
370
    private function createClassMetadataMock()
371
    {
372
        return $this->getMockBuilder(ClassMetadata::class)
373
            ->disableOriginalConstructor()
374
            ->getMock();
375
    }
376
377
    /**
378
     * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
379
     */
380
    private function createResourceMock()
381
    {
382
        return $this->getMock(ResourceInterface::class);
383
    }
384
385
    /**
386
     * @return \PHPUnit_Framework_MockObject_MockObject|QueryBuilder
387
     */
388
    private function createQueryBuilderMock()
389
    {
390
        return $this->getMockBuilder(QueryBuilder::class)
391
            ->disableOriginalConstructor()
392
            ->getMock();
393
    }
394
395
    /**
396
     * @return \PHPUnit_Framework_MockObject_MockObject|Expr
397
     */
398
    private function createExprMock()
399
    {
400
        return $this->getMock(Expr::class);
401
    }
402
403
    /**
404
     * @return \PHPUnit_Framework_MockObject_MockObject|Query
405
     */
406
    private function createQueryMock()
407
    {
408
        return $this->getMock(\stdClass::class, ['getOneOrNullResult', 'getResult']);
409
    }
410
}
411