Completed
Push — master ( 298f36...c6c82d )
by Mihail
06:19 queued 10s
created

JsonSerializableTest::test_json_failed_encoding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 19
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 28
rs 9.6333
1
<?php
2
3
namespace Koded\Stdlib;
4
5
use PHPUnit\Framework\TestCase;
6
7
class JsonSerializableTest extends TestCase
8
{
9
10
    public function test_json_serializable_interface_implementation()
11
    {
12
        $SUT = new Arguments(['foo' => new Arguments(['bar' => 'gir'])]);
13
        $json = json_encode($SUT);
14
        $this->assertSame('{"foo":{"bar":"gir"}}', $json);
15
    }
16
17
    public function test_json_failed_encoding()
18
    {
19
        $stdClass = new \stdClass;
20
        $stdClass->hello = 'there';
21
22
        $args = [
23
            'integer' => 123,
24
            'float' => 0.17,
25
            'string' => 'fubar',
26
            'bool' => true,
27
            'null' => null,
28
            'array' => ['foo' => 'bar'],
29
            'stdClass' => $stdClass
30
        ];
31
32
        $SUT = new Immutable($args);
33
34
        $json = $SUT->toJSON();
35
        $this->assertSame('{"integer":123,"float":0.17,"string":"fubar","bool":true,"null":null,"array":{"foo":"bar"},"stdClass":{"hello":"there"}}', $json);
36
37
        $expected = $args;
38
        $expected['array'] = (object)['foo' => 'bar'];
39
        $this->assertEquals((object)$expected, json_decode($json), 'The arrays are lost after json_decode($, false)');
40
41
        $expected['array'] = $args['array'];
42
        $expected['stdClass'] = (array)$args['stdClass'];
43
44
        $this->assertEquals($expected, json_decode($json, true), 'The objects are lost after json_decode($, true)');
45
    }
46
}
47