|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Bankiru\Api\Doctrine\Proxy\ApiCollection; |
|
6
|
|
|
use Bankiru\Api\Test\Entity\Sub\SubEntity; |
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
8
|
|
|
use GuzzleHttp\Psr7\Response; |
|
9
|
|
|
|
|
10
|
|
|
class CollectionLoadingTest extends AbstractEntityManagerTest |
|
11
|
|
|
{ |
|
12
|
2 |
|
protected function getClientNames() |
|
13
|
|
|
{ |
|
14
|
2 |
|
return [self::DEFAULT_CLIENT, 'test-reference-client']; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
1 |
|
public function testLazyCollections() |
|
18
|
|
|
{ |
|
19
|
1 |
|
$repository = $this->getManager()->getRepository(SubEntity::class); |
|
20
|
|
|
/** @var SubEntity[]|ArrayCollection|ApiCollection $entities */ |
|
21
|
1 |
|
$entities = $repository->findBy(['subPayload' => 'sub-payload']); |
|
22
|
|
|
|
|
23
|
1 |
|
self::assertInstanceOf(ApiCollection::class, $entities); |
|
24
|
1 |
|
self::assertFalse($entities->isInitialized()); |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
try { |
|
27
|
1 |
|
$entities->count(); |
|
28
|
1 |
|
} catch (\OutOfBoundsException $exception) { |
|
29
|
1 |
|
self::assertEquals('Mock queue is empty', $exception->getMessage()); |
|
30
|
|
|
} |
|
31
|
1 |
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
public function testFindBy() |
|
34
|
|
|
{ |
|
35
|
1 |
|
$repository = $this->getManager()->getRepository(SubEntity::class); |
|
36
|
|
|
|
|
37
|
1 |
|
$this->getResponseMock()->append( |
|
38
|
1 |
|
new Response( |
|
39
|
1 |
|
200, |
|
40
|
1 |
|
[], |
|
41
|
1 |
|
json_encode( |
|
42
|
|
|
[ |
|
43
|
1 |
|
'jsonrpc' => '2.0', |
|
44
|
1 |
|
'id' => 'test', |
|
45
|
|
|
'result' => [ |
|
46
|
|
|
[ |
|
47
|
1 |
|
'id' => '1', |
|
48
|
1 |
|
'payload' => 'test-payload-1', |
|
49
|
1 |
|
'sub-payload' => 'sub-payload', |
|
50
|
1 |
|
'string-payload' => null, |
|
51
|
1 |
|
], |
|
52
|
|
|
[ |
|
53
|
1 |
|
'id' => '2', |
|
54
|
1 |
|
'payload' => 'test-payload-2', |
|
55
|
1 |
|
'sub-payload' => 'sub-payload', |
|
56
|
1 |
|
'string-payload' => 123456, |
|
57
|
1 |
|
], |
|
58
|
|
|
[ |
|
59
|
1 |
|
'id' => '3', |
|
60
|
1 |
|
'payload' => 'test-payload-3', |
|
61
|
1 |
|
'sub-payload' => 'sub-payload', |
|
62
|
1 |
|
'string-payload' => 'sub-payload', |
|
63
|
1 |
|
], |
|
64
|
1 |
|
], |
|
65
|
|
|
] |
|
66
|
1 |
|
) |
|
67
|
1 |
|
) |
|
68
|
1 |
|
); |
|
69
|
|
|
|
|
70
|
|
|
/** @var SubEntity[]|ArrayCollection $entities */ |
|
71
|
1 |
|
$entities = $repository->findBy(['subPayload' => 'sub-payload']); |
|
72
|
1 |
|
self::assertInstanceOf(\Countable::class, $entities); |
|
73
|
1 |
|
self::assertCount(3, $entities); |
|
74
|
|
|
|
|
75
|
1 |
View Code Duplication |
foreach ($entities as $entity) { |
|
|
|
|
|
|
76
|
1 |
|
self::assertInternalType('int', $entity->getId()); |
|
77
|
1 |
|
self::assertInstanceOf(SubEntity::class, $entity); |
|
78
|
1 |
|
self::assertEquals('test-payload-' . $entity->getId(), $entity->getPayload()); |
|
79
|
2 |
|
self::assertEquals('sub-payload', $entity->getSubPayload()); |
|
80
|
|
|
|
|
81
|
1 |
|
if (null !== $entity->getStringPayload()) { |
|
82
|
1 |
|
self::assertInternalType('string', $entity->getStringPayload()); |
|
83
|
1 |
|
} |
|
84
|
1 |
|
} |
|
85
|
1 |
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: