|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WebFiori\Tests\Json; |
| 4 | + |
| 5 | +use WebFiori\Json\Json; |
| 6 | +use jsonx\tests\Obj0; |
| 7 | +use jsonx\tests\Obj1; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use WebFiori\Json\Property; |
| 10 | +use WebFiori\Json\JsonConverter; |
| 11 | + |
| 12 | +class JsonConverterTest extends TestCase { |
| 13 | + /** |
| 14 | + * @test |
| 15 | + */ |
| 16 | + public function testObjectToJson00() { |
| 17 | + $obj = new Obj0('Hello', 'World', 8, 9, 'Good'); |
| 18 | + $json = JsonConverter::objectToJson($obj); |
| 19 | + $this->assertTrue($json instanceof Json); |
| 20 | + $this->assertEquals('{"Property00":"Hello","Property01":"World","Property02":8,"Property04":"Good"}',$json.''); |
| 21 | + } |
| 22 | + /** |
| 23 | + * @test |
| 24 | + */ |
| 25 | + public function testObjectToJson01() { |
| 26 | + $obj = new Obj1('Hello', 'World', 8, 9, 'Good'); |
| 27 | + $json = JsonConverter::objectToJson($obj); |
| 28 | + $this->assertTrue($json instanceof Json); |
| 29 | + $this->assertEquals('{"property-00":"Hello","property-01":"World","property-02":8}',$json.''); |
| 30 | + } |
| 31 | + /** |
| 32 | + * @test |
| 33 | + */ |
| 34 | + public function testObjectToJson02() { |
| 35 | + $obj = new Json(['hello' => 'world']); |
| 36 | + $json = JsonConverter::objectToJson($obj); |
| 37 | + $this->assertTrue($json instanceof Json); |
| 38 | + $this->assertEquals('{"hello":"world"}',$json.''); |
| 39 | + } |
| 40 | + /** |
| 41 | + * @test |
| 42 | + */ |
| 43 | + public function testPropertyToJsonString00() { |
| 44 | + $prop = new Property('hello', 'world'); |
| 45 | + $this->assertEquals('"hello":"world"', JsonConverter::propertyToJsonString($prop)); |
| 46 | + } |
| 47 | + /** |
| 48 | + * @test |
| 49 | + */ |
| 50 | + public function testPropertyToJsonString01() { |
| 51 | + $prop = new Property('hello', 'world'); |
| 52 | + $this->assertEquals('"hello":"world"', JsonConverter::propertyToJsonString($prop, false)); |
| 53 | + } |
| 54 | + /** |
| 55 | + * @test |
| 56 | + */ |
| 57 | + public function testPropertyToJsonString02() { |
| 58 | + $prop = new Property('hello', 'world'); |
| 59 | + $this->assertEquals('"hello":"world"', JsonConverter::propertyToJsonString($prop, true)); |
| 60 | + } |
| 61 | + /** |
| 62 | + * @test |
| 63 | + */ |
| 64 | + public function testPropertyToJsonXString00() { |
| 65 | + $prop = new Property('hello', 'world'); |
| 66 | + $this->assertEquals('<json:string name="hello">'."\r\n" |
| 67 | + . ' world'."\r\n" |
| 68 | + . '</json:string>', JsonConverter::propertyToJsonXString($prop)); |
| 69 | + } |
| 70 | + /** |
| 71 | + * @test |
| 72 | + */ |
| 73 | + public function testPropertyToJsonXString01() { |
| 74 | + $prop = new Property('hello', 'world'); |
| 75 | + $this->assertEquals('<json:string>'."\r\n" |
| 76 | + . ' world'."\r\n" |
| 77 | + . '</json:string>', JsonConverter::propertyToJsonXString($prop, false)); |
| 78 | + } |
| 79 | +} |
0 commit comments