|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sysix\LexOffice\Tests; |
| 6 | + |
| 7 | +use GuzzleHttp\Psr7\Response; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Sysix\LexOffice\Utils; |
| 10 | + |
| 11 | +class UtilsTest extends TestCase |
| 12 | +{ |
| 13 | + public function testGetJsonFromResponseWithoutAnHeader(): void |
| 14 | + { |
| 15 | + $response = new Response(); |
| 16 | + $json = Utils::getJsonFromResponse($response); |
| 17 | + |
| 18 | + $this->assertNull($json); |
| 19 | + } |
| 20 | + |
| 21 | + public function testGetJsonFromResponseWithInvalidHeader(): void |
| 22 | + { |
| 23 | + $response = new Response(200, [ |
| 24 | + 'Content-Type' => 'application/xml' |
| 25 | + ]); |
| 26 | + $json = Utils::getJsonFromResponse($response); |
| 27 | + |
| 28 | + $this->assertNull($json); |
| 29 | + } |
| 30 | + |
| 31 | + public function testGetJsonFromResponseWithValidHeader(): void |
| 32 | + { |
| 33 | + $response = new Response(200, [ |
| 34 | + 'Content-Type' => 'application/json' |
| 35 | + ], (string) json_encode([ |
| 36 | + 'test' => true |
| 37 | + ])); |
| 38 | + $json = Utils::getJsonFromResponse($response); |
| 39 | + |
| 40 | + $this->assertEquals((object)[ |
| 41 | + 'test' => true |
| 42 | + ], $json); |
| 43 | + } |
| 44 | + |
| 45 | + public function testGetJsonFromResponseWithValidCharsetHeader(): void |
| 46 | + { |
| 47 | + $response = new Response(200, [ |
| 48 | + 'Content-Type' => 'application/json; charset=utf-8' |
| 49 | + ], (string) json_encode([ |
| 50 | + 'test' => true |
| 51 | + ])); |
| 52 | + $json = Utils::getJsonFromResponse($response); |
| 53 | + |
| 54 | + $this->assertEquals((object)[ |
| 55 | + 'test' => true |
| 56 | + ], $json); |
| 57 | + } |
| 58 | +} |
0 commit comments