|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bankiru\Api\JsonRpc\Test\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Bankiru\Api\JsonRpc\BankiruJsonRpcServerBundle; |
|
6
|
|
|
use ScayTrase\Api\JsonRpc\SyncResponse; |
|
7
|
|
|
|
|
8
|
|
|
final class EntityConversionTest extends JsonRpcTestCase |
|
9
|
|
|
{ |
|
10
|
1 |
|
public function testEntitySerialization() |
|
11
|
|
|
{ |
|
12
|
1 |
|
$response = $this->sendRequest( |
|
13
|
1 |
|
self::createClient(), |
|
14
|
1 |
|
'/test/', |
|
15
|
|
|
[ |
|
16
|
1 |
|
'jsonrpc' => BankiruJsonRpcServerBundle::JSONRPC_VERSION, |
|
17
|
1 |
|
'method' => 'entity', |
|
18
|
1 |
|
'id' => 'test', |
|
19
|
|
|
'params' => [ |
|
20
|
1 |
|
'payload' => 'my-payload', |
|
21
|
1 |
|
], |
|
22
|
|
|
] |
|
23
|
1 |
|
); |
|
24
|
|
|
|
|
25
|
1 |
|
self::assertTrue($response->isSuccessful()); |
|
26
|
1 |
|
$content = json_decode($response->getContent()); |
|
27
|
1 |
|
self::assertInstanceOf(\stdClass::class, $content); |
|
28
|
|
|
|
|
29
|
1 |
|
$jsonResponse = new SyncResponse($content); |
|
30
|
1 |
|
self::assertTrue($jsonResponse->isSuccessful(), json_encode($content, JSON_PRETTY_PRINT)); |
|
31
|
1 |
|
self::assertTrue(isset($jsonResponse->getBody()->{'sample_payload'}), json_encode($content, JSON_PRETTY_PRINT)); |
|
32
|
1 |
|
self::assertEquals('my-payload', $jsonResponse->getBody()->{'sample_payload'}); |
|
33
|
|
|
|
|
34
|
1 |
|
self::assertFalse( |
|
35
|
1 |
|
isset($jsonResponse->getBody()->{'private_payload'}), |
|
36
|
|
|
json_encode($content, JSON_PRETTY_PRINT) |
|
37
|
1 |
|
); |
|
38
|
|
|
} |
|
39
|
1 |
|
|
|
40
|
1 |
View Code Duplication |
public function testPrivateEntityFields() |
|
|
|
|
|
|
41
|
1 |
|
{ |
|
42
|
|
|
$response = $this->sendRequest( |
|
43
|
1 |
|
self::createClient(), |
|
44
|
1 |
|
'/test/private/', |
|
45
|
1 |
|
[ |
|
46
|
|
|
'jsonrpc' => BankiruJsonRpcServerBundle::JSONRPC_VERSION, |
|
47
|
1 |
|
'method' => 'entity/private', |
|
48
|
1 |
|
'id' => 'test', |
|
49
|
|
|
'params' => [ |
|
50
|
1 |
|
'payload' => 'my-payload', |
|
51
|
|
|
], |
|
52
|
1 |
|
] |
|
53
|
1 |
|
); |
|
54
|
1 |
|
|
|
55
|
|
|
self::assertTrue($response->isSuccessful()); |
|
56
|
1 |
|
$content = json_decode($response->getContent()); |
|
57
|
1 |
|
self::assertInstanceOf(\stdClass::class, $content); |
|
58
|
1 |
|
|
|
59
|
1 |
|
$jsonResponse = new SyncResponse($content); |
|
60
|
|
|
self::assertTrue($jsonResponse->isSuccessful(), json_encode($content, JSON_PRETTY_PRINT)); |
|
61
|
1 |
|
self::assertTrue(isset($jsonResponse->getBody()->{'sample_payload'}), json_encode($content, JSON_PRETTY_PRINT)); |
|
62
|
1 |
|
self::assertEquals( |
|
63
|
1 |
|
'my-payload', |
|
64
|
1 |
|
$jsonResponse->getBody()->{'sample_payload'}, |
|
65
|
1 |
|
json_encode($content, JSON_PRETTY_PRINT) |
|
66
|
1 |
|
); |
|
67
|
|
|
|
|
68
|
|
|
if (static::$kernel->getContainer()->has('jms_serializer')) { |
|
69
|
1 |
|
self::assertTrue( |
|
70
|
|
|
isset($jsonResponse->getBody()->{'private_payload'}), |
|
71
|
1 |
|
json_encode($content, JSON_PRETTY_PRINT) |
|
72
|
1 |
|
); |
|
73
|
1 |
|
self::assertEquals( |
|
74
|
|
|
'secret-payload', |
|
75
|
1 |
|
$jsonResponse->getBody()->{'private_payload'}, |
|
76
|
1 |
|
json_encode($content, JSON_PRETTY_PRINT) |
|
77
|
1 |
|
); |
|
78
|
|
|
} |
|
79
|
1 |
|
} |
|
80
|
1 |
|
|
|
81
|
|
View Code Duplication |
public function testNestedContext() |
|
|
|
|
|
|
82
|
1 |
|
{ |
|
83
|
|
|
$response = $this->sendRequest( |
|
84
|
1 |
|
self::createClient(), |
|
85
|
1 |
|
'/test/private/', |
|
86
|
1 |
|
[ |
|
87
|
|
|
'jsonrpc' => BankiruJsonRpcServerBundle::JSONRPC_VERSION, |
|
88
|
1 |
|
'method' => 'entity/nested', |
|
89
|
1 |
|
'id' => 'test', |
|
90
|
1 |
|
'params' => [ |
|
91
|
1 |
|
'payload' => 'my-payload', |
|
92
|
|
|
], |
|
93
|
1 |
|
] |
|
94
|
1 |
|
); |
|
95
|
1 |
|
|
|
96
|
1 |
|
self::assertTrue($response->isSuccessful()); |
|
97
|
1 |
|
$content = json_decode($response->getContent()); |
|
98
|
1 |
|
self::assertInstanceOf(\stdClass::class, $content); |
|
99
|
|
|
|
|
100
|
|
|
$jsonResponse = new SyncResponse($content); |
|
101
|
|
|
self::assertTrue($jsonResponse->isSuccessful(), json_encode($content, JSON_PRETTY_PRINT)); |
|
102
|
|
|
self::assertTrue(isset($jsonResponse->getBody()->{'sample_payload'}), json_encode($content, JSON_PRETTY_PRINT)); |
|
103
|
|
|
self::assertEquals( |
|
104
|
|
|
'my-payload', |
|
105
|
|
|
$jsonResponse->getBody()->{'sample_payload'}, |
|
106
|
|
|
json_encode($content, JSON_PRETTY_PRINT) |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
if (static::$kernel->getContainer()->has('jms_serializer')) { |
|
110
|
|
|
self::assertTrue( |
|
111
|
|
|
isset($jsonResponse->getBody()->{'private_payload'}), |
|
112
|
|
|
json_encode($content, JSON_PRETTY_PRINT) |
|
113
|
|
|
); |
|
114
|
|
|
self::assertEquals( |
|
115
|
|
|
'secret-payload', |
|
116
|
|
|
$jsonResponse->getBody()->{'private_payload'}, |
|
117
|
|
|
json_encode($content, JSON_PRETTY_PRINT) |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
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.