1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Doctrine\Tests; |
4
|
|
|
|
5
|
|
|
use Bankiru\Api\Doctrine\Test\Entity\CompositeKeyEntity; |
6
|
|
|
use Bankiru\Api\Doctrine\Test\Entity\Sub\SubEntity; |
7
|
|
|
use Bankiru\Api\Doctrine\Test\Entity\TestEntity; |
8
|
|
|
use ScayTrase\Api\Rpc\RpcRequestInterface; |
9
|
|
|
|
10
|
|
|
class EntityFactoryTest extends AbstractEntityManagerTest |
11
|
|
|
{ |
12
|
|
|
public function testEntityLoading() |
13
|
|
|
{ |
14
|
|
|
$repository = $this->getManager()->getRepository(TestEntity::class); |
15
|
|
|
|
16
|
|
|
$this->getClient()->push( |
17
|
|
|
$this->getResponseMock( |
18
|
|
|
true, |
19
|
|
|
(object)[ |
20
|
|
|
'id' => '1', |
21
|
|
|
'payload' => 'test-payload', |
22
|
|
|
] |
23
|
|
|
), |
24
|
|
|
function (RpcRequestInterface $request) { |
25
|
|
|
self::assertEquals('test-entity/find', $request->getMethod()); |
26
|
|
|
self::assertEquals(['id' => 1], $request->getParameters()); |
27
|
|
|
|
28
|
|
|
return true; |
29
|
|
|
} |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
/** @var TestEntity $entity */ |
33
|
|
|
$entity = $repository->find(1); |
34
|
|
|
|
35
|
|
|
self::assertInstanceOf(TestEntity::class, $entity); |
36
|
|
|
self::assertEquals(1, $entity->getId()); |
37
|
|
|
self::assertInternalType('int', $entity->getId()); |
38
|
|
|
self::assertEquals('test-payload', $entity->getPayload()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function testInheritanceLoading() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$repository = $this->getManager()->getRepository(SubEntity::class); |
44
|
|
|
$this->getClient()->push( |
45
|
|
|
$this->getResponseMock( |
46
|
|
|
true, |
47
|
|
|
(object)[ |
48
|
|
|
'id' => 2, |
49
|
|
|
'payload' => 'test-payload', |
50
|
|
|
'sub-payload' => 'sub-payload', |
51
|
|
|
] |
52
|
|
|
), |
53
|
|
|
function (RpcRequestInterface $request) { |
54
|
|
|
self::assertEquals('test-entity/find', $request->getMethod()); |
55
|
|
|
self::assertEquals(['id' => 2], $request->getParameters()); |
56
|
|
|
|
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
/** @var SubEntity $entity */ |
62
|
|
|
$entity = $repository->find(2); |
63
|
|
|
|
64
|
|
|
self::assertInstanceOf(SubEntity::class, $entity); |
65
|
|
|
self::assertEquals(2, $entity->getId()); |
66
|
|
|
self::assertEquals('test-payload', $entity->getPayload()); |
67
|
|
|
self::assertEquals('sub-payload', $entity->getSubPayload()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testCompositeKeyLoading() |
71
|
|
|
{ |
72
|
|
|
$repository = $this->getManager()->getRepository(CompositeKeyEntity::class); |
73
|
|
|
$this->getClient()->push( |
74
|
|
|
$this->getResponseMock( |
75
|
|
|
true, |
76
|
|
|
(object)[ |
77
|
|
|
'first_key' => 2, |
78
|
|
|
'second_key' => 'test', |
79
|
|
|
'payload' => 'test-payload', |
80
|
|
|
] |
81
|
|
|
), |
82
|
|
|
function (RpcRequestInterface $request) { |
83
|
|
|
self::assertEquals('composite-key-entity/find', $request->getMethod()); |
84
|
|
|
self::assertEquals(['first_key' => 2, 'second_key' => 'test'], $request->getParameters()); |
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
/** @var CompositeKeyEntity $entity */ |
91
|
|
|
$entity = $repository->find(['firstKey' => '2', 'secondKey' => 'test']); |
92
|
|
|
|
93
|
|
|
self::assertInstanceOf(CompositeKeyEntity::class, $entity); |
94
|
|
|
self::assertEquals(2, $entity->getFirstKey()); |
95
|
|
|
self::assertEquals('test', $entity->getSecondKey()); |
96
|
|
|
self::assertEquals('test-payload', $entity->getPayload()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function getClientNames() |
100
|
|
|
{ |
101
|
|
|
return array_merge(parent::getClientNames(), ['test-reference-client']); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
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.