Skip to content

Commit

Permalink
Remove real request in test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jenky committed Jun 16, 2023
1 parent bb4cd54 commit c0f8ff9
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 8 deletions.
52 changes: 44 additions & 8 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Jenky\Atlas\Body\MultipartResource;
use Jenky\Atlas\Exception\HttpException;
use Jenky\Atlas\Mock\MockClient;
use Jenky\Atlas\Mock\MockResponse;
use Jenky\Atlas\Response;
use Jenky\Atlas\Tests\Services\HTTPBin\Connector;
use Jenky\Atlas\Tests\Services\HTTPBin\DTO\Uuid;
Expand Down Expand Up @@ -45,20 +47,28 @@ public function test_sending_request_directly(): void

public function test_sending_request_from_connector(): void
{
$response = $this->connector->send(new GetHeadersRequest());
$client = new MockClient();
$connector = $this->connector->withClient($client);

$response = $connector->send(new GetHeadersRequest());

$this->assertTrue($response->ok());
}

public function test_request_headers(): void
{
$client = new MockClient(
MockResponse::fixture(__DIR__.'/fixtures/headers.json')
);
$connector = $this->connector->withClient($client);

$request = new GetHeadersRequest();

$request->headers()
->with('Accept', 'application/json')
->with('X-Foo', 'bar');

$response = $this->connector->send($request);
$response = $connector->send($request);

$this->assertTrue($response->ok());
$this->assertSame('bar', $response->data()['headers']['X-Foo'] ?? null);
Expand All @@ -67,9 +77,14 @@ public function test_request_headers(): void

public function test_cast_response_to_dto(): void
{
$client = new MockClient(
MockResponse::create(['uuid' => '01b67779-4690-4094-8e83-624cc496e1ef'])
);
$connector = $this->connector->withClient($client);

$request = new GetUuidRequest();

$response = $this->connector->send($request);
$response = $connector->send($request);

$this->assertTrue($response->ok());
$this->assertInstanceOf(Uuid::class, $dto = Uuid::fromResponse($response));
Expand All @@ -78,13 +93,18 @@ public function test_cast_response_to_dto(): void

public function test_request_body(): void
{
$client = new MockClient(
MockResponse::fixture(__DIR__.'/fixtures/anything.json')
);
$connector = $this->connector->withClient($client);

$request = new PostAnythingRequest();

$request->body()
->with('hello', 'world')
->merge(['foo' => 'bar'], ['buzz' => 'quiz']);

$response = $this->connector->send($request);
$response = $connector->send($request);

$this->assertTrue($response->ok());
$this->assertSame('bar', $response['json']['foo'] ?? null);
Expand All @@ -94,11 +114,16 @@ public function test_request_body(): void

public function test_request_multipart(): void
{
$client = new MockClient(
MockResponse::fixture(__DIR__.'/fixtures/multipart.json')
);
$connector = $this->connector->withClient($client);

$request = new PostRequest('John', '[email protected]');
$request->body()
->with('img', MultipartResource::from(__DIR__.'/fixtures/1x1.png'));

$response = $this->connector->send($request);
$response = $connector->send($request);

$this->assertFalse($response->failed());

Expand All @@ -111,9 +136,14 @@ public function test_request_multipart(): void

public function test_response_xml_decoder(): void
{
$client = new MockClient(
MockResponse::fixture(__DIR__.'/fixtures/slideshow.xml', 200, ['Content-Type' => 'text/xml'])
);
$connector = $this->connector->withClient($client);

$request = new GetXmlRequest();

$response = $this->connector->send($request);
$response = $connector->send($request);

$this->assertTrue($response->ok());

Expand All @@ -123,14 +153,20 @@ public function test_response_xml_decoder(): void

public function test_response_exception(): void
{
$client = new MockClient([
MockResponse::create('', 400),
MockResponse::create(''),
]);
$connector = $this->connector->withClient($client);

$request = new GetStatusRequest(400);

$this->expectException(HttpException::class);

$this->connector->send($request)->throwIf(function (Response $response) {
$connector->send($request)->throwIf(function (Response $response) {
return $response->failed();
});

$this->connector->send($request->withStatus(200))->throwIf(true);
$connector->send($request->withStatus(200))->throwIf(true);
}
}
26 changes: 26 additions & 0 deletions tests/fixtures/anything.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"args": {},
"data": {
"hello": "world",
"foo": "bar",
"buzz": "quiz"
},
"files": {},
"form": {},
"headers": {
"Content-Length": "43",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "GuzzleHttp/7",
"X-Amzn-Trace-Id": "Root=1-648be8e7-22eb348849e631df01a655d9",
"X-From": "atlas"
},
"json": {
"buzz": "quiz",
"foo": "bar",
"hello": "world"
},
"method": "POST",
"origin": "222.252.14.21",
"url": "https://httpbin.org/anything"
}
11 changes: 11 additions & 0 deletions tests/fixtures/headers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"headers": {
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "GuzzleHttp/7",
"X-Amzn-Trace-Id": "Root=1-648be83a-1a58dcdc0ee57cf561dd6de9",
"X-Foo": "bar",
"X-From": "atlas"
}
}
22 changes: 22 additions & 0 deletions tests/fixtures/multipart.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"args": {},
"data": "",
"files": {
"img": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII="
},
"form": {
"email": "[email protected]",
"name": "John"
},
"headers": {
"Content-Length": "548",
"Content-Type": "multipart/form-data; boundary=3a257f6bae64997ef39d9f6ce690da26f85c6fa8",
"Host": "httpbin.org",
"User-Agent": "GuzzleHttp/7",
"X-Amzn-Trace-Id": "Root=1-648be978-61653b9e74646cd85f2fc9fc",
"X-From": "atlas"
},
"json": null,
"origin": "222.252.14.21",
"url": "https://httpbin.org/post"
}
24 changes: 24 additions & 0 deletions tests/fixtures/slideshow.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version='1.0' encoding='us-ascii'?>

<!-- A SAMPLE set of slides -->

<slideshow
title="Sample Slide Show"
date="Date of publication"
author="Yours Truly"
>

<!-- TITLE SLIDE -->
<slide type="all">
<title>Wake up to WonderWidgets!</title>
</slide>

<!-- OVERVIEW -->
<slide type="all">
<title>Overview</title>
<item>Why <em>WonderWidgets</em> are great</item>
<item/>
<item>Who <em>buys</em> WonderWidgets</item>
</slide>

</slideshow>

0 comments on commit c0f8ff9

Please sign in to comment.