Completed
Push — refonte ( 225502...fa24e9 )
by Arnaud
01:45
created

ORMDataProviderTest::testSaveItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 9.28
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace LAG\AdminBundle\Tests\Bridge\Doctrine\ORM\DataProvider;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\AbstractQuery;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\EntityRepository;
9
use Doctrine\ORM\QueryBuilder;
10
use LAG\AdminBundle\Admin\AdminInterface;
11
use LAG\AdminBundle\Bridge\Doctrine\ORM\DataProvider\ORMDataProvider;
12
use LAG\AdminBundle\Configuration\AdminConfiguration;
13
use LAG\AdminBundle\Event\AdminEvents;
14
use LAG\AdminBundle\Event\DoctrineOrmFilterEvent;
15
use LAG\AdminBundle\Exception\Exception;
16
use LAG\AdminBundle\Tests\AdminTestBase;
17
use LAG\AdminBundle\Tests\Fixtures\EntityFixture;
18
use PHPUnit\Framework\MockObject\MockObject;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
use Symfony\Component\HttpFoundation\RequestStack;
21
22
class ORMDataProviderTest extends AdminTestBase
23
{
24
    public function testGetCollection()
25
    {
26
        // Create an admin configuration to test the collection method
27
        $configuration = $this->createMock(AdminConfiguration::class);
28
        $configuration
29
            ->expects($this->atLeastOnce())
30
            ->method('getParameter')
31
            ->willReturnMap([
32
                ['entity', 'MyClass'],
33
                ['pager', null],
34
                ['page_parameter', 'page'],
35
            ])
36
        ;
37
38
        /** @var AdminInterface|MockObject $admin */
39
        $admin = $this->createMock(AdminInterface::class);
40
        $admin
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in LAG\AdminBundle\Admin\AdminInterface.

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...
41
            ->expects($this->atLeastOnce())
42
            ->method('getConfiguration')
43
            ->willReturn($configuration)
44
        ;
45
46
        // Create fake entities to return
47
        $entities = [
48
            new EntityFixture(uniqid()),
49
            new EntityFixture(uniqid()),
50
        ];
51
52
        // The query should return entities
53
        $query = $this->createMock(AbstractQuery::class);
54
        $query
55
            ->expects($this->once())
56
            ->method('getResult')
57
            ->willReturn($entities)
58
        ;
59
60
        // The query builder should call the getQuery() method
61
        $queryBuilder = $this->createMock(QueryBuilder::class);
62
        $queryBuilder
63
            ->expects($this->once())
64
            ->method('getQuery')
65
            ->willReturn($query)
66
        ;
67
68
        // The repository should return a query builder
69
        $repository = $this->createMock(EntityRepository::class);
70
        $repository
71
            ->expects($this->once())
72
            ->method('createQueryBuilder')
73
            ->with('entity')
74
            ->willReturn($queryBuilder)
75
        ;
76
77
        // The entity manager should return a repository
78
        /** @var EntityManagerInterface|MockObject $entityManager */
79
        $entityManager = $this->createMock(EntityManagerInterface::class);
80
        $entityManager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Doctrine\ORM\EntityManagerInterface.

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...
81
            ->expects($this->once())
82
            ->method('getRepository')
83
            ->willReturn($repository)
84
        ;
85
86
        // The event dispatcher should dispatched once a ORM_FILTER event
87
        /** @var EventDispatcherInterface|MockObject $eventDispatcher */
88
        $eventDispatcher = $this->createMock(EventDispatcherInterface::class);
89
        $eventDispatcher
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Symfony\Component\EventD...ventDispatcherInterface.

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...
90
            ->expects($this->once())
91
            ->method('dispatch')
92
            ->willReturnCallback(function ($eventName, $event) use ($queryBuilder, $admin) {
93
                $this->assertEquals(AdminEvents::DOCTRINE_ORM_FILTER, $eventName);
94
                /** @var DoctrineOrmFilterEvent $event */
95
                $this->assertInstanceOf(DoctrineOrmFilterEvent::class, $event);
96
                $this->assertEquals($queryBuilder, $event->getQueryBuilder());
97
                $this->assertEquals($admin, $event->getAdmin());
98
                $this->assertEquals([], $event->getFilters());
99
            })
100
        ;
101
        /** @var RequestStack|MockObject $requestStack */
102
        $requestStack = $this->createMock(RequestStack::class);
103
104
        $provider = new ORMDataProvider(
105
            $entityManager,
0 ignored issues
show
Bug introduced by
It seems like $entityManager defined by $this->createMock(\Doctr...anagerInterface::class) on line 79 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, LAG\AdminBundle\Bridge\D...Provider::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
106
            $eventDispatcher,
0 ignored issues
show
Bug introduced by
It seems like $eventDispatcher defined by $this->createMock(\Symfo...atcherInterface::class) on line 88 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, LAG\AdminBundle\Bridge\D...Provider::__construct() does only seem to accept object<Symfony\Component...entDispatcherInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
107
            $requestStack
0 ignored issues
show
Bug introduced by
It seems like $requestStack defined by $this->createMock(\Symfo...on\RequestStack::class) on line 102 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, LAG\AdminBundle\Bridge\D...Provider::__construct() does only seem to accept object<Symfony\Component...oundation\RequestStack>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
108
        );
109
        $returnedEntities = $provider->getCollection($admin);
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->createMock(\LAG\A...\AdminInterface::class) on line 39 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, LAG\AdminBundle\Bridge\D...ovider::getCollection() does only seem to accept object<LAG\AdminBundle\Admin\AdminInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
110
111
        // The returned entities should be the same as those provided by the repository
112
        $this->assertEquals($entities, $returnedEntities);
113
    }
114
115
    public function testGetItem()
116
    {
117
        /** @var EventDispatcherInterface|MockObject $eventDispatcher */
118
        $eventDispatcher = $this->createMock(EventDispatcherInterface::class);
119
120
        /** @var RequestStack|MockObject $requestStack */
121
        $requestStack = $this->createMock(RequestStack::class);
122
123
        $repository = $this->createMock(EntityRepository::class);
124
        $repository
125
            ->expects($this->atLeastOnce())
126
            ->method('find')
127
            ->with(42)
128
            ->willReturn(new EntityFixture(42))
129
        ;
130
131
        /** @var EntityManagerInterface|MockObject $entityManager */
132
        $entityManager = $this->createMock(EntityManagerInterface::class);
133
        $entityManager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Doctrine\ORM\EntityManagerInterface.

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...
134
            ->expects($this->atLeastOnce())
135
            ->method('getRepository')
136
            ->with('MyClass')
137
            ->willReturn($repository)
138
        ;
139
140
        $configuration = $this->createMock(AdminConfiguration::class);
141
        $configuration
142
            ->expects($this->atLeastOnce())
143
            ->method('getParameter')
144
            ->willReturnMap([
145
                ['entity', 'MyClass'],
146
                ['pager', null],
147
                ['page_parameter', 'page'],
148
            ])
149
        ;
150
151
        /** @var AdminInterface|MockObject $admin */
152
        $admin = $this->createMock(AdminInterface::class);
153
        $admin
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in LAG\AdminBundle\Admin\AdminInterface.

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...
154
            ->expects($this->atLeastOnce())
155
            ->method('getConfiguration')
156
            ->willReturn($configuration)
157
        ;
158
159
        $provider = new ORMDataProvider(
160
            $entityManager,
0 ignored issues
show
Bug introduced by
It seems like $entityManager defined by $this->createMock(\Doctr...anagerInterface::class) on line 132 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, LAG\AdminBundle\Bridge\D...Provider::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
161
            $eventDispatcher,
0 ignored issues
show
Bug introduced by
It seems like $eventDispatcher defined by $this->createMock(\Symfo...atcherInterface::class) on line 118 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, LAG\AdminBundle\Bridge\D...Provider::__construct() does only seem to accept object<Symfony\Component...entDispatcherInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
162
            $requestStack
0 ignored issues
show
Bug introduced by
It seems like $requestStack defined by $this->createMock(\Symfo...on\RequestStack::class) on line 121 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, LAG\AdminBundle\Bridge\D...Provider::__construct() does only seem to accept object<Symfony\Component...oundation\RequestStack>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
163
        );
164
165
        $item = $provider->getItem($admin, 42);
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->createMock(\LAG\A...\AdminInterface::class) on line 152 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, LAG\AdminBundle\Bridge\D...DataProvider::getItem() does only seem to accept object<LAG\AdminBundle\Admin\AdminInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
166
167
        $this->assertEquals(42, $item->getId());
168
    }
169
170
    public function testGetItemWithException()
171
    {
172
        /** @var EventDispatcherInterface|MockObject $eventDispatcher */
173
        $eventDispatcher = $this->createMock(EventDispatcherInterface::class);
174
175
        /** @var RequestStack|MockObject $requestStack */
176
        $requestStack = $this->createMock(RequestStack::class);
177
178
        $repository = $this->createMock(EntityRepository::class);
179
        $repository
180
            ->expects($this->atLeastOnce())
181
            ->method('find')
182
            ->with(42)
183
            ->willReturn(null)
184
        ;
185
186
        /** @var EntityManagerInterface|MockObject $entityManager */
187
        $entityManager = $this->createMock(EntityManagerInterface::class);
188
        $entityManager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Doctrine\ORM\EntityManagerInterface.

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...
189
            ->expects($this->atLeastOnce())
190
            ->method('getRepository')
191
            ->with('MyClass')
192
            ->willReturn($repository)
193
        ;
194
195
        $configuration = $this->createMock(AdminConfiguration::class);
196
        $configuration
197
            ->expects($this->atLeastOnce())
198
            ->method('getParameter')
199
            ->willReturnMap([
200
                ['entity', 'MyClass'],
201
                ['pager', null],
202
                ['page_parameter', 'page'],
203
            ])
204
        ;
205
206
        /** @var AdminInterface|MockObject $admin */
207
        $admin = $this->createMock(AdminInterface::class);
208
        $admin
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in LAG\AdminBundle\Admin\AdminInterface.

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...
209
            ->expects($this->atLeastOnce())
210
            ->method('getConfiguration')
211
            ->willReturn($configuration)
212
        ;
213
214
        $provider = new ORMDataProvider(
215
            $entityManager,
0 ignored issues
show
Bug introduced by
It seems like $entityManager defined by $this->createMock(\Doctr...anagerInterface::class) on line 187 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, LAG\AdminBundle\Bridge\D...Provider::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
216
            $eventDispatcher,
0 ignored issues
show
Bug introduced by
It seems like $eventDispatcher defined by $this->createMock(\Symfo...atcherInterface::class) on line 173 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, LAG\AdminBundle\Bridge\D...Provider::__construct() does only seem to accept object<Symfony\Component...entDispatcherInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
217
            $requestStack
0 ignored issues
show
Bug introduced by
It seems like $requestStack defined by $this->createMock(\Symfo...on\RequestStack::class) on line 176 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, LAG\AdminBundle\Bridge\D...Provider::__construct() does only seem to accept object<Symfony\Component...oundation\RequestStack>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
218
        );
219
220
        $this->assertExceptionRaised(Exception::class, function () use ($provider, $admin) {
221
            $provider->getItem($admin, 42);
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->createMock(\LAG\A...\AdminInterface::class) on line 207 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, LAG\AdminBundle\Bridge\D...DataProvider::getItem() does only seem to accept object<LAG\AdminBundle\Admin\AdminInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
222
        });
223
    }
224
225
    public function testSaveItem()
226
    {
227
        /** @var EventDispatcherInterface|MockObject $eventDispatcher */
228
        $eventDispatcher = $this->createMock(EventDispatcherInterface::class);
229
230
        /** @var RequestStack|MockObject $requestStack */
231
        $requestStack = $this->createMock(RequestStack::class);
232
233
        $entity =new EntityFixture(42);
234
235
        /** @var EntityManagerInterface|MockObject $entityManager */
236
        $entityManager = $this->createMock(EntityManagerInterface::class);
237
        $entityManager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Doctrine\ORM\EntityManagerInterface.

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->atLeastOnce())
239
            ->method('persist')
240
            ->with($entity)
241
        ;
242
        $entityManager
243
            ->expects($this->atLeastOnce())
244
            ->method('flush')
245
        ;
246
247
        /** @var AdminInterface|MockObject $admin */
248
        $admin = $this->createMock(AdminInterface::class);
249
        $admin
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in LAG\AdminBundle\Admin\AdminInterface.

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...
250
            ->expects($this->atLeastOnce())
251
            ->method('getEntities')
252
            ->willReturn(new ArrayCollection([
253
                $entity,
254
            ]))
255
        ;
256
257
        $provider = new ORMDataProvider(
258
            $entityManager,
0 ignored issues
show
Bug introduced by
It seems like $entityManager defined by $this->createMock(\Doctr...anagerInterface::class) on line 236 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, LAG\AdminBundle\Bridge\D...Provider::__construct() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
259
            $eventDispatcher,
0 ignored issues
show
Bug introduced by
It seems like $eventDispatcher defined by $this->createMock(\Symfo...atcherInterface::class) on line 228 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, LAG\AdminBundle\Bridge\D...Provider::__construct() does only seem to accept object<Symfony\Component...entDispatcherInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
260
            $requestStack
0 ignored issues
show
Bug introduced by
It seems like $requestStack defined by $this->createMock(\Symfo...on\RequestStack::class) on line 231 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, LAG\AdminBundle\Bridge\D...Provider::__construct() does only seem to accept object<Symfony\Component...oundation\RequestStack>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
261
        );
262
263
        $provider->saveItem($admin);
0 ignored issues
show
Bug introduced by
It seems like $admin defined by $this->createMock(\LAG\A...\AdminInterface::class) on line 248 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, LAG\AdminBundle\Bridge\D...ataProvider::saveItem() does only seem to accept object<LAG\AdminBundle\Admin\AdminInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
264
    }
265
}
266