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

RepositoryTest   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 336
Duplicated Lines 31.25 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 25
c 1
b 0
f 0
lcom 1
cbo 4
dl 105
loc 336
rs 10

21 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 19 2
A testInheritance() 0 5 1
A testCreateDataSourceBuilder() 0 7 1
A testCreateQueryBuilder() 0 6 1
A testCreateQueryForCollectionBuilder() 0 6 1
A testFindOneByWithNullValue() 16 16 1
A testFindOneByWithScalarValue() 16 16 1
A testFindOneByWithArrayValue() 16 16 1
B testFindBy() 0 33 1
A testFindForIndex() 9 9 1
A testFindForShow() 16 16 1
A testFindForUpdate() 16 16 1
A testFindForDelete() 16 16 1
B setUpQueryBuilder() 0 34 4
A createDocumentManagerMock() 0 6 1
A createUnitOfWorkMock() 0 6 1
A createClassMetadataMock() 0 6 1
A createResourceMock() 0 4 1
A createQueryBuilderMock() 0 6 1
A createQueryMock() 0 6 1
A createIteratorMock() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
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\MongoDB;
13
14
use Doctrine\MongoDB\Iterator;
15
use Doctrine\ODM\MongoDB\DocumentManager;
16
use Doctrine\ODM\MongoDB\DocumentRepository;
17
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
18
use Doctrine\ODM\MongoDB\Query\Builder;
19
use Doctrine\ODM\MongoDB\Query\Query;
20
use Doctrine\ODM\MongoDB\UnitOfWork;
21
use Lug\Component\Grid\DataSource\Doctrine\MongoDB\DataSourceBuilder;
22
use Lug\Component\Resource\Model\ResourceInterface;
23
use Lug\Component\Resource\Repository\Doctrine\MongoDB\Repository;
24
use Lug\Component\Resource\Repository\RepositoryInterface;
25
use Pagerfanta\Adapter\DoctrineODMMongoDBAdapter;
26
use Pagerfanta\Pagerfanta;
27
28
/**
29
 * @author GeLo <[email protected]>
30
 */
31
class RepositoryTest extends \PHPUnit_Framework_TestCase
32
{
33
    /**
34
     * @var Repository
35
     */
36
    private $repository;
37
38
    /**
39
     * @var \PHPUnit_Framework_MockObject_MockObject|DocumentManager
40
     */
41
    private $documentManager;
42
43
    /**
44
     * @var \PHPUnit_Framework_MockObject_MockObject|UnitOfWork
45
     */
46
    private $unitOfWork;
47
48
    /**
49
     * @var \PHPUnit_Framework_MockObject_MockObject|ClassMetadata
50
     */
51
    private $classMetadata;
52
53
    /**
54
     * @var \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
55
     */
56
    private $resource;
57
58
    /**
59
     * @var string
60
     */
61
    private $class;
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected function setUp()
67
    {
68
        if (!class_exists(DocumentManager::class)) {
69
            $this->markTestSkipped();
70
        }
71
72
        $this->documentManager = $this->createDocumentManagerMock();
73
        $this->unitOfWork = $this->createUnitOfWorkMock();
74
        $this->classMetadata = $this->createClassMetadataMock();
75
        $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...
76
        $this->class = $this->classMetadata->name = \stdClass::class;
77
78
        $this->repository = new Repository(
79
            $this->documentManager,
80
            $this->unitOfWork,
81
            $this->classMetadata,
82
            $this->resource
83
        );
84
    }
85
86
    public function testInheritance()
87
    {
88
        $this->assertInstanceOf(RepositoryInterface::class, $this->repository);
89
        $this->assertInstanceOf(DocumentRepository::class, $this->repository);
90
    }
91
92
    public function testCreateDataSourceBuilder()
93
    {
94
        $this->setUpQueryBuilder();
95
        $dataSourceBuilder = $this->repository->createDataSourceBuilder();
96
97
        $this->assertInstanceOf(DataSourceBuilder::class, $dataSourceBuilder);
98
    }
99
100
    public function testCreateQueryBuilder()
101
    {
102
        $queryBuilder = $this->setUpQueryBuilder();
103
104
        $this->assertSame($queryBuilder, $this->repository->createQueryBuilder());
105
    }
106
107
    public function testCreateQueryForCollectionBuilder()
108
    {
109
        $queryBuilder = $this->setUpQueryBuilder();
110
111
        $this->assertSame($queryBuilder, $this->repository->createQueryBuilderForCollection());
112
    }
113
114 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...
115
    {
116
        $queryBuilder = $this->setUpQueryBuilder($value = null);
117
118
        $queryBuilder
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Query\Builder.

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...
119
            ->expects($this->once())
120
            ->method('getQuery')
121
            ->will($this->returnValue($query = $this->createQueryMock()));
122
123
        $query
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Query\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...
124
            ->expects($this->once())
125
            ->method('getSingleResult')
126
            ->will($this->returnValue($result = 'result'));
127
128
        $this->assertSame($result, $this->repository->findOneBy(['foo' => $value], ['baz' => 'ASC']));
129
    }
130
131 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...
132
    {
133
        $queryBuilder = $this->setUpQueryBuilder($value = 'bar');
134
135
        $queryBuilder
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Query\Builder.

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('getQuery')
138
            ->will($this->returnValue($query = $this->createQueryMock()));
139
140
        $query
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Query\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...
141
            ->expects($this->once())
142
            ->method('getSingleResult')
143
            ->will($this->returnValue($result = 'result'));
144
145
        $this->assertSame($result, $this->repository->findOneBy(['foo' => $value], ['baz' => 'ASC']));
146
    }
147
148 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...
149
    {
150
        $queryBuilder = $this->setUpQueryBuilder($value = ['bar']);
151
152
        $queryBuilder
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Query\Builder.

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('getQuery')
155
            ->will($this->returnValue($query = $this->createQueryMock()));
156
157
        $query
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Query\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...
158
            ->expects($this->once())
159
            ->method('getSingleResult')
160
            ->will($this->returnValue($result = 'result'));
161
162
        $this->assertSame($result, $this->repository->findOneBy(['foo' => $value], ['baz' => 'ASC']));
163
    }
164
165
    public function testFindBy()
166
    {
167
        $queryBuilder = $this->setUpQueryBuilder($value = 'bar');
168
169
        $queryBuilder
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Query\Builder.

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('limit')
172
            ->with($this->identicalTo($limit = 5))
173
            ->will($this->returnSelf());
174
175
        $queryBuilder
176
            ->expects($this->once())
177
            ->method('skip')
178
            ->with($this->identicalTo($offset = 10))
179
            ->will($this->returnSelf());
180
181
        $queryBuilder
182
            ->expects($this->once())
183
            ->method('getQuery')
184
            ->will($this->returnValue($query = $this->createQueryMock()));
185
186
        $query
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Query\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...
187
            ->expects($this->once())
188
            ->method('getIterator')
189
            ->will($this->returnValue($iterator = $this->createIteratorMock()));
190
191
        $iterator
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\MongoDB\Iterator.

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...
192
            ->expects($this->once())
193
            ->method('toArray')
194
            ->will($this->returnValue($result = ['result']));
195
196
        $this->assertSame($result, $this->repository->findBy(['foo' => $value], ['baz' => 'ASC'], $limit, $offset));
197
    }
198
199 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...
200
    {
201
        $this->setUpQueryBuilder($value = 'bar');
202
203
        $pager = $this->repository->findForIndex(['foo' => $value], ['baz' => 'ASC']);
204
205
        $this->assertInstanceOf(Pagerfanta::class, $pager);
206
        $this->assertInstanceOf(DoctrineODMMongoDBAdapter::class, $pager->getAdapter());
207
    }
208
209 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...
210
    {
211
        $queryBuilder = $this->setUpQueryBuilder($value = 'bar');
212
213
        $queryBuilder
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Query\Builder.

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...
214
            ->expects($this->once())
215
            ->method('getQuery')
216
            ->will($this->returnValue($query = $this->createQueryMock()));
217
218
        $query
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Query\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...
219
            ->expects($this->once())
220
            ->method('getSingleResult')
221
            ->will($this->returnValue($result = 'result'));
222
223
        $this->assertSame($result, $this->repository->findForShow(['foo' => $value], ['baz' => 'ASC']));
224
    }
225
226 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...
227
    {
228
        $queryBuilder = $this->setUpQueryBuilder($value = 'bar');
229
230
        $queryBuilder
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Query\Builder.

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...
231
            ->expects($this->once())
232
            ->method('getQuery')
233
            ->will($this->returnValue($query = $this->createQueryMock()));
234
235
        $query
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Query\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...
236
            ->expects($this->once())
237
            ->method('getSingleResult')
238
            ->will($this->returnValue($result = 'result'));
239
240
        $this->assertSame($result, $this->repository->findForUpdate(['foo' => $value], ['baz' => 'ASC']));
241
    }
242
243 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...
244
    {
245
        $queryBuilder = $this->setUpQueryBuilder($value = 'bar');
246
247
        $queryBuilder
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Query\Builder.

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...
248
            ->expects($this->once())
249
            ->method('getQuery')
250
            ->will($this->returnValue($query = $this->createQueryMock()));
251
252
        $query
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Query\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...
253
            ->expects($this->once())
254
            ->method('getSingleResult')
255
            ->will($this->returnValue($result = 'result'));
256
257
        $this->assertSame($result, $this->repository->findForDelete(['foo' => $value], ['baz' => 'ASC']));
258
    }
259
260
    /**
261
     * @param mixed       $value
262
     * @param string|null $alias
263
     *
264
     * @return \PHPUnit_Framework_MockObject_MockObject|Builder
265
     */
266
    private function setUpQueryBuilder($value = false, $alias = null)
0 ignored issues
show
Unused Code introduced by
The parameter $alias is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
267
    {
268
        $this->documentManager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\DocumentManager.

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...
269
            ->expects($this->once())
270
            ->method('createQueryBuilder')
271
            ->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock()));
272
273
        if ($value === false) {
274
            return $queryBuilder;
275
        }
276
277
        $queryBuilder
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\MongoDB\Query\Builder.

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...
278
            ->expects($this->once())
279
            ->method('field')
280
            ->with($this->identicalTo('foo'))
281
            ->will($this->returnSelf());
282
283
        $queryBuilder
284
            ->expects($this->once())
285
            ->method($value === null || is_string($value) ? 'equals' : 'in')
286
            ->with($this->identicalTo($value))
287
            ->will($this->returnSelf());
288
289
        $queryBuilder
290
            ->expects($this->once())
291
            ->method('sort')
292
            ->with(
293
                $this->identicalTo('baz'),
294
                $this->identicalTo('ASC')
295
            )
296
            ->will($this->returnSelf());
297
298
        return $queryBuilder;
299
    }
300
301
    /**
302
     * @return \PHPUnit_Framework_MockObject_MockObject|DocumentManager
303
     */
304
    private function createDocumentManagerMock()
305
    {
306
        return $this->getMockBuilder(DocumentManager::class)
307
            ->disableOriginalConstructor()
308
            ->getMock();
309
    }
310
311
    /**
312
     * @return \PHPUnit_Framework_MockObject_MockObject|UnitOfWork
313
     */
314
    private function createUnitOfWorkMock()
315
    {
316
        return $this->getMockBuilder(UnitOfWork::class)
317
            ->disableOriginalConstructor()
318
            ->getMock();
319
    }
320
321
    /**
322
     * @return \PHPUnit_Framework_MockObject_MockObject|ClassMetadata
323
     */
324
    private function createClassMetadataMock()
325
    {
326
        return $this->getMockBuilder(ClassMetadata::class)
327
            ->disableOriginalConstructor()
328
            ->getMock();
329
    }
330
331
    /**
332
     * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
333
     */
334
    private function createResourceMock()
335
    {
336
        return $this->getMock(ResourceInterface::class);
337
    }
338
339
    /**
340
     * @return \PHPUnit_Framework_MockObject_MockObject|Builder
341
     */
342
    private function createQueryBuilderMock()
343
    {
344
        return $this->getMockBuilder(Builder::class)
345
            ->disableOriginalConstructor()
346
            ->getMock();
347
    }
348
349
    /**
350
     * @return \PHPUnit_Framework_MockObject_MockObject|Query
351
     */
352
    private function createQueryMock()
353
    {
354
        return $this->getMockBuilder(Query::class)
355
            ->disableOriginalConstructor()
356
            ->getMock();
357
    }
358
359
    /**
360
     * @return \PHPUnit_Framework_MockObject_MockObject|Iterator
361
     */
362
    private function createIteratorMock()
363
    {
364
        return $this->getMock(Iterator::class);
365
    }
366
}
367