Skip to content

Commit 21f61e5

Browse files
committed
test: full test suits for JsonParamEncoder
1 parent 9e3e039 commit 21f61e5

1 file changed

Lines changed: 249 additions & 0 deletions

File tree

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\ValueObjects\Encoders;
6+
7+
use Chargebee\ValueObjects\Encoders\JsonParamEncoder;
8+
use PHPUnit\Framework\TestCase;
9+
10+
final class JsonParamEncoderTest extends TestCase
11+
{
12+
/** Should convert params to JSON string */
13+
/** {"first_name":"John","last_name":"Doe","email":"john@test.com","locale":"fr-CA"} */
14+
public function testEncodeParamsWithSimpleAttributes(): void
15+
{
16+
$params = [
17+
'first_name' => 'John',
18+
'last_name' => 'Doe',
19+
'email' => 'john@test.com',
20+
'locale' => 'fr-CA',
21+
];
22+
23+
$encoded = JsonParamEncoder::encode($params);
24+
25+
$this->assertIsString($encoded);
26+
$this->assertJson($encoded);
27+
$this->assertEquals($params, json_decode($encoded, true));
28+
}
29+
/** Should convert params to JSON string */
30+
/** {"first_name":"John","last_name":"Doe","coupons":["FIFTYOFF","TENOFF"]} */
31+
public function testEncodeParamsWithAttributeAsArrayOfPrimitives(): void
32+
{
33+
$params = [
34+
'first_name' => 'John',
35+
'last_name' => 'Doe',
36+
'coupons' => ['FIFTYOFF', 'TENOFF'],
37+
];
38+
39+
$encoded = JsonParamEncoder::encode($params);
40+
41+
$this->assertIsString($encoded);
42+
$this->assertJson($encoded);
43+
$this->assertEquals($params, json_decode($encoded, true));
44+
}
45+
46+
/** Should convert params to JSON string */
47+
/** {"first_name":"John","last_name":"Doe","billing_address":{"city":"Walnut","state":"California"}} */
48+
public function testEncodeParamsWithAttributeAsSubResourceAttributes(): void
49+
{
50+
$params = [
51+
'first_name' => 'John',
52+
'last_name' => 'Doe',
53+
'billing_address' => [
54+
'city' => 'Walnut',
55+
'state' => 'California',
56+
],
57+
];
58+
59+
$encoded = JsonParamEncoder::encode($params);
60+
61+
$this->assertIsString($encoded);
62+
$this->assertJson($encoded);
63+
$this->assertEquals($params, json_decode($encoded, true));
64+
}
65+
66+
/** Should convert params to JSON string */
67+
/** {"subscription_items":[{"item_price_id":"day-pass-USD","unit_price":100},{"item_price_id":"basic-USD","billing_cycles":2,"quantity":1}],"billing_cycle":1} */
68+
public function testEncodeParamsWithAttributeAsArrayOfSubResources(): void
69+
{
70+
$params = [
71+
'subscription_items' => [
72+
[
73+
'item_price_id' => 'day-pass-USD',
74+
'unit_price' => 100,
75+
],
76+
[
77+
'item_price_id' => 'basic-USD',
78+
'billing_cycles' => 2,
79+
'quantity' => 1,
80+
],
81+
],
82+
'billing_cycle' => 1,
83+
];
84+
85+
$encoded = JsonParamEncoder::encode($params);
86+
87+
$this->assertIsString($encoded);
88+
$this->assertJson($encoded);
89+
$this->assertEquals($params, json_decode($encoded, true));
90+
}
91+
92+
/** Convert params to JSON encoding. Transform empty arrays into empty object {} instead of [] */
93+
/** {} */
94+
public function testEncodeParamsWithEmptyArrayAsEmptyObject(): void
95+
{
96+
$params = [];
97+
98+
$encoded = JsonParamEncoder::encode($params);
99+
100+
$this->assertIsString($encoded);
101+
$this->assertJson($encoded);
102+
$this->assertStringContainsString('{}', $encoded);
103+
}
104+
105+
/** Should convert params to JSON string */
106+
/** {"id":"foo","name":"foo","discount_percentage":10,"apply_on":"each_specified_item","item_constraints":[{"constraint":"specific","item_type":"plan","item_price_ids":["some_price_id_one","some_price_id_two"]}]} */
107+
public function testEncodeParamsWithComplexNestedStructure(): void
108+
{
109+
$params = [
110+
'id' => 'foo',
111+
'name' => 'foo',
112+
'discount_percentage' => 10.0,
113+
'apply_on' => 'each_specified_item',
114+
'item_constraints' => [
115+
[
116+
'constraint' => 'specific',
117+
'item_type' => 'plan',
118+
'item_price_ids' => [
119+
'some_price_id_one',
120+
'some_price_id_two',
121+
],
122+
],
123+
],
124+
];
125+
126+
$encoded = JsonParamEncoder::encode($params);
127+
128+
$this->assertIsString($encoded);
129+
$this->assertJson($encoded);
130+
$this->assertEquals($params, json_decode($encoded, true));
131+
}
132+
133+
/** Should convert params to JSON string with numeric values */
134+
/** {"integer_value":42,"float_value":10.5,"zero_value":0,"negative_value":-15} */
135+
public function testEncodeParamsWithNumericValues(): void
136+
{
137+
$params = [
138+
'integer_value' => 42,
139+
'float_value' => 10.5,
140+
'zero_value' => 0,
141+
'negative_value' => -15,
142+
];
143+
144+
$encoded = JsonParamEncoder::encode($params);
145+
146+
$this->assertIsString($encoded);
147+
$this->assertJson($encoded);
148+
$this->assertEquals($params, json_decode($encoded, true));
149+
}
150+
151+
/** Should convert params to JSON string with null values */
152+
/** {"first_name":"John","middle_name":null,"last_name":"Doe"} */
153+
public function testEncodeParamsWithNullValues(): void
154+
{
155+
$params = [
156+
'first_name' => 'John',
157+
'middle_name' => null,
158+
'last_name' => 'Doe',
159+
];
160+
161+
$encoded = JsonParamEncoder::encode($params);
162+
163+
$this->assertIsString($encoded);
164+
$this->assertJson($encoded);
165+
$this->assertEquals($params, json_decode($encoded, true));
166+
}
167+
168+
/** Should convert params to JSON string with special characters */
169+
/** {"email":"john@test.com","url":"https://example.com/path?query=value","description":"Line 1\nLine 2\tTabbed","quotes":"He said \"hello\""} */
170+
public function testEncodeParamsWithSpecialCharacters(): void
171+
{
172+
$params = [
173+
'email' => 'john@test.com',
174+
'url' => 'https://example.com/path?query=value',
175+
'description' => "Line 1\nLine 2\tTabbed",
176+
'quotes' => 'He said "hello"',
177+
];
178+
179+
$encoded = JsonParamEncoder::encode($params);
180+
181+
$this->assertIsString($encoded);
182+
$this->assertJson($encoded);
183+
$this->assertEquals($params, json_decode($encoded, true));
184+
}
185+
186+
/** Should convert params to JSON string with deeply nested structure */
187+
/** {"level1":{"level2":{"level3":{"level4":{"value":"deep"}}}}} */
188+
public function testEncodeParamsWithDeeplyNestedStructure(): void
189+
{
190+
$params = [
191+
'level1' => [
192+
'level2' => [
193+
'level3' => [
194+
'level4' => [
195+
'value' => 'deep',
196+
],
197+
],
198+
],
199+
],
200+
];
201+
202+
$encoded = JsonParamEncoder::encode($params);
203+
204+
$this->assertIsString($encoded);
205+
$this->assertJson($encoded);
206+
$this->assertEquals($params, json_decode($encoded, true));
207+
}
208+
209+
/** Should convert params to JSON string with mixed types */
210+
/** {"string":"value","integer":123,"float":45.67,"boolean":true,"null":null,"array":[1,2,3],"object":{"key":"value"}} */
211+
public function testEncodeParamsWithMixedTypes(): void
212+
{
213+
$params = [
214+
'string' => 'value',
215+
'integer' => 123,
216+
'float' => 45.67,
217+
'boolean' => true,
218+
'null' => null,
219+
'array' => [1, 2, 3],
220+
'object' => ['key' => 'value'],
221+
];
222+
223+
$encoded = JsonParamEncoder::encode($params);
224+
225+
$this->assertIsString($encoded);
226+
$this->assertJson($encoded);
227+
$this->assertEquals($params, json_decode($encoded, true));
228+
}
229+
230+
/** Should Convert empty array attribute to object & array to json array while converting to JSON */
231+
/** {"first_name":"John","last_name":"Doe","email":"john@test.com","locale":"fr-CA","meta_data":{},"coupons":["FIFTY_OFF","EXTRA_CARD_OFFER"]} */
232+
public function testEncodeParamsWithSimpleAttributesOne(): void
233+
{
234+
$params = [
235+
'first_name' => 'John',
236+
'last_name' => 'Doe',
237+
'email' => 'john@test.com',
238+
'locale' => 'fr-CA',
239+
'meta_data' => [],
240+
'coupons' => ["FIFTY_OFF", "EXTRA_CARD_OFFER"]
241+
];
242+
243+
$encoded = JsonParamEncoder::encode($params);
244+
print($encoded);
245+
$this->assertIsString($encoded);
246+
$this->assertJson($encoded);
247+
$this->assertEquals($params, json_decode($encoded, true));
248+
}
249+
}

0 commit comments

Comments
 (0)