|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Doctrine\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Bankiru\Api\Doctrine\Test\Entity\ToManyAsArrayEntity; |
|
6
|
|
|
use ScayTrase\Api\Rpc\RpcRequestInterface; |
|
7
|
|
|
|
|
8
|
|
|
class ToManyFetchTest extends AbstractEntityManagerTest |
|
9
|
|
|
{ |
|
10
|
|
|
public function testEntityManagerLoadsToManyFromArray() |
|
11
|
|
|
{ |
|
12
|
|
|
$repository = $this->getManager()->getRepository(ToManyAsArrayEntity::class); |
|
13
|
|
|
|
|
14
|
|
|
$this->getClient('test-client')->push( |
|
15
|
|
|
$this->getResponseMock( |
|
16
|
|
|
true, |
|
17
|
|
|
(object)[ |
|
18
|
|
|
'id' => 3, |
|
19
|
|
|
'payload' => 'test-payload-3', |
|
20
|
|
|
'references' => [1, 2], |
|
21
|
|
|
] |
|
22
|
|
|
), |
|
23
|
|
|
function (RpcRequestInterface $request) { |
|
24
|
|
|
self::assertEquals('array-entity/find', $request->getMethod()); |
|
25
|
|
|
self::assertEquals(['id' => 3], $request->getParameters()); |
|
26
|
|
|
|
|
27
|
|
|
return true; |
|
28
|
|
|
} |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
$this->getClient('test-client')->push( |
|
32
|
|
|
$this->getResponseMock( |
|
33
|
|
|
true, |
|
34
|
|
|
[ |
|
35
|
|
|
(object)[ |
|
36
|
|
|
'id' => 1, |
|
37
|
|
|
'payload' => 'test-payload-1', |
|
38
|
|
|
], |
|
39
|
|
|
(object)[ |
|
40
|
|
|
'id' => 2, |
|
41
|
|
|
'payload' => 'test-payload-2', |
|
42
|
|
|
], |
|
43
|
|
|
] |
|
44
|
|
|
), |
|
45
|
|
View Code Duplication |
function (RpcRequestInterface $request) { |
|
|
|
|
|
|
46
|
|
|
self::assertEquals('array-entity/search', $request->getMethod()); |
|
47
|
|
|
self::assertEquals( |
|
48
|
|
|
[ |
|
49
|
|
|
'criteria' => ['id' => [1, 2]], |
|
50
|
|
|
'order' => [], |
|
51
|
|
|
'limit' => null, |
|
52
|
|
|
'offset' => null, |
|
53
|
|
|
], |
|
54
|
|
|
$request->getParameters() |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
return true; |
|
58
|
|
|
} |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
/** @var ToManyAsArrayEntity $entity */ |
|
62
|
|
|
$entity = $repository->find(3); |
|
63
|
|
|
self::assertSame(3, $entity->getId()); |
|
64
|
|
|
self::assertSame('test-payload-3', $entity->getPayload()); |
|
65
|
|
|
/** @var ToManyAsArrayEntity[] $references */ |
|
66
|
|
|
$references = $entity->getChildren()->toArray(); |
|
67
|
|
|
|
|
68
|
|
|
self::assertCount(2, $references); |
|
69
|
|
|
foreach ($references as $reference) { |
|
70
|
|
|
self::assertSame('test-payload-' . $reference->getId(), $reference->getPayload()); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
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.