|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace HttpSoft\Tests\Basis\Response; |
| 6 | + |
| 7 | +use DateTime; |
| 8 | +use DateTimeZone; |
| 9 | +use HttpSoft\Basis\Response\PrepareJsonDataTrait; |
| 10 | +use JsonSerializable; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use stdClass; |
| 13 | + |
| 14 | +class PrepareJsonDataTraitTest extends TestCase |
| 15 | +{ |
| 16 | + use PrepareJsonDataTrait; |
| 17 | + |
| 18 | + public function testPrepareScalarAndNull(): void |
| 19 | + { |
| 20 | + $this->assertSame(1, $this->prepareJsonData(1)); |
| 21 | + $this->assertSame(1.1, $this->prepareJsonData(1.1)); |
| 22 | + $this->assertSame(null, $this->prepareJsonData(null)); |
| 23 | + $this->assertSame(true, $this->prepareJsonData(true)); |
| 24 | + $this->assertSame(false, $this->prepareJsonData(false)); |
| 25 | + $this->assertSame('string', $this->prepareJsonData('string')); |
| 26 | + } |
| 27 | + |
| 28 | + public function testPrepareSimpleArray(): void |
| 29 | + { |
| 30 | + $this->assertSame([1, 2], $this->prepareJsonData([1, 2])); |
| 31 | + $this->assertSame(['a' => 1, 'b' => 2], $this->prepareJsonData(['a' => 1, 'b' => 2])); |
| 32 | + } |
| 33 | + |
| 34 | + public function testPrepareSimpleObject(): void |
| 35 | + { |
| 36 | + $data = new stdClass(); |
| 37 | + $data->a = 1; |
| 38 | + $data->b = 2; |
| 39 | + $this->assertSame(['a' => 1, 'b' => 2], $this->prepareJsonData($data)); |
| 40 | + } |
| 41 | + |
| 42 | + public function testPrepareEmpty(): void |
| 43 | + { |
| 44 | + $data = new stdClass(); |
| 45 | + $this->assertSame([], $this->prepareJsonData([])); |
| 46 | + $this->assertEquals($data, $this->prepareJsonData($data)); |
| 47 | + $this->assertNotSame($data, $this->prepareJsonData($data)); |
| 48 | + $this->assertInstanceOf(stdClass::class, $this->prepareJsonData($data)); |
| 49 | + } |
| 50 | + |
| 51 | + public function testPrepareJsonSerializable(): void |
| 52 | + { |
| 53 | + $this->assertSame( |
| 54 | + ['a' => 1, 'b' => 'string'], |
| 55 | + $this->prepareJsonData(new class () implements JsonSerializable { |
| 56 | + public function jsonSerialize() |
| 57 | + { |
| 58 | + return ['a' => 1, 'b' => 'string']; |
| 59 | + } |
| 60 | + }), |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + public function testPrepareDateTime(): void |
| 65 | + { |
| 66 | + $this->assertSame( |
| 67 | + [ |
| 68 | + 'date' => '2021-08-08 00:00:00.000000', |
| 69 | + 'timezone_type' => 3, |
| 70 | + 'timezone' => 'UTC', |
| 71 | + ], |
| 72 | + $this->prepareJsonData(new DateTime('August 8, 2021', new DateTimeZone('UTC'))), |
| 73 | + ); |
| 74 | + } |
| 75 | +} |
0 commit comments