Skip to content

Commit f1c0ac0

Browse files
committed
ParsesApiReponse tests
1 parent 60d8e7b commit f1c0ac0

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Tests\Traits;
4+
5+
use Illuminate\Http\Client\Response as HttpResponse;
6+
use Illuminate\Http\JsonResponse;
7+
use Illuminate\Support\Collection;
8+
use Kroderdev\LaravelMicroserviceCore\Traits\ParsesApiResponse;
9+
use Orchestra\Testbench\TestCase;
10+
11+
class DummyParser
12+
{
13+
use ParsesApiResponse;
14+
15+
public static function parse(mixed $data): array
16+
{
17+
return static::parseResponse($data);
18+
}
19+
}
20+
21+
class ArrayableStub
22+
{
23+
public function toArray(): array
24+
{
25+
return ['foo' => 'bar'];
26+
}
27+
}
28+
29+
class ParsesApiResponseTest extends TestCase
30+
{
31+
/** @test */
32+
public function it_returns_empty_array_for_null()
33+
{
34+
$this->assertSame([], DummyParser::parse(null));
35+
}
36+
37+
/** @test */
38+
public function it_parses_http_response()
39+
{
40+
$response = new HttpResponse(new \GuzzleHttp\Psr7\Response(200, [], '{"foo":"bar"}'));
41+
$this->assertSame(['foo' => 'bar'], DummyParser::parse($response));
42+
}
43+
44+
/** @test */
45+
public function it_parses_json_response()
46+
{
47+
$response = new JsonResponse(['foo' => 'bar']);
48+
$this->assertSame(['foo' => 'bar'], DummyParser::parse($response));
49+
}
50+
51+
/** @test */
52+
public function it_parses_array()
53+
{
54+
$this->assertSame(['foo' => 'bar'], DummyParser::parse(['foo' => 'bar']));
55+
}
56+
57+
/** @test */
58+
public function it_parses_collection()
59+
{
60+
$collection = new Collection(['foo' => 'bar']);
61+
$this->assertSame(['foo' => 'bar'], DummyParser::parse($collection));
62+
}
63+
64+
/** @test */
65+
public function it_parses_json_string()
66+
{
67+
$json = '{"foo":"bar"}';
68+
$this->assertSame(['foo' => 'bar'], DummyParser::parse($json));
69+
}
70+
71+
/** @test */
72+
public function it_parses_object_with_to_array()
73+
{
74+
$object = new ArrayableStub();
75+
$this->assertSame(['foo' => 'bar'], DummyParser::parse($object));
76+
}
77+
}

0 commit comments

Comments
 (0)