Skip to content

Commit 480c027

Browse files
authored
Merge pull request #155 from Sammyjo20/fix/v1-encoded-files-in-fixtures
Fix | V1 Encoded Files In Fixtures
2 parents 1905cdb + 2dc424c commit 480c027

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

src/Data/FixtureData.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class FixtureData implements JsonSerializable
1616
* @param mixed $data
1717
*/
1818
public function __construct(
19-
public int $statusCode,
19+
public int $statusCode,
2020
public array $headers = [],
2121
public mixed $data = null,
2222
) {
@@ -34,10 +34,16 @@ public static function fromFile(string $contents): static
3434
{
3535
$fileData = json_decode($contents, true, 512, JSON_THROW_ON_ERROR);
3636

37+
$data = $fileData['data'];
38+
39+
if (isset($fileData['encoding']) && $fileData['encoding'] === 'base64') {
40+
$data = base64_decode($data);
41+
}
42+
3743
return new static(
3844
statusCode: $fileData['statusCode'],
3945
headers: $fileData['headers'],
40-
data: $fileData['data']
46+
data: $data
4147
);
4248
}
4349

@@ -84,10 +90,17 @@ public function toMockResponse(): MockResponse
8490
*/
8591
public function jsonSerialize(): array
8692
{
87-
return [
93+
$response = [
8894
'statusCode' => $this->statusCode,
8995
'headers' => $this->headers,
9096
'data' => $this->data,
9197
];
98+
99+
if (mb_check_encoding($response['data'], 'UTF-8') === false) {
100+
$response['data'] = base64_encode($response['data']);
101+
$response['encoding'] = 'base64';
102+
}
103+
104+
return $response;
92105
}
93106
}

tests/Feature/ConnectorRequestTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
$connector = new InvalidServiceRequestConnector();
126126

127127
$this->expectException(InvalidRequestKeyException::class);
128-
$this->expectDeprecationMessage('Request groups must be keyed.');
128+
$this->expectExceptionMessage('Request groups must be keyed.');
129129

130130
$connector->custom();
131131
});

tests/Feature/MockRequestTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Sammyjo20\Saloon\Tests\Fixtures\Requests\ErrorRequest;
1111
use Sammyjo20\Saloon\Tests\Fixtures\Connectors\TestConnector;
1212
use Sammyjo20\Saloon\Tests\Fixtures\Mocking\CallableMockResponse;
13+
use Sammyjo20\Saloon\Tests\Fixtures\Requests\FileDownloadRequest;
1314
use Sammyjo20\Saloon\Exceptions\SaloonNoMockResponsesProvidedException;
1415
use Sammyjo20\Saloon\Tests\Fixtures\Connectors\QueryParameterConnector;
1516
use Sammyjo20\Saloon\Tests\Fixtures\Requests\DifferentServiceUserRequest;
@@ -464,3 +465,19 @@ function (SaloonRequest $request): MockResponse {
464465
'message' => 'Fake Error',
465466
]);
466467
});
468+
469+
test('a fixture can record the file data from a request that returns a file download', function () {
470+
$mockClient = new MockClient([
471+
FileDownloadRequest::class => MockResponse::fixture('file'),
472+
]);
473+
474+
$requestA = new FileDownloadRequest;
475+
$responseA = $requestA->send($mockClient);
476+
477+
expect($responseA->body())->toEqual(file_get_contents('tests/Fixtures/Files/test.pdf'));
478+
479+
$requestB = new FileDownloadRequest;
480+
$responseB = $requestB->send($mockClient);
481+
482+
expect($responseB->body())->toEqual(file_get_contents('tests/Fixtures/Files/test.pdf'));
483+
});

tests/Fixtures/Files/test.pdf

74.5 KB
Binary file not shown.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Sammyjo20\Saloon\Tests\Fixtures\Requests;
4+
5+
use Sammyjo20\Saloon\Constants\Saloon;
6+
use Sammyjo20\Saloon\Http\SaloonRequest;
7+
use Sammyjo20\Saloon\Tests\Fixtures\Connectors\TestConnector;
8+
9+
class FileDownloadRequest extends SaloonRequest
10+
{
11+
/**
12+
* Define the method that the request will use.
13+
*
14+
* @var string|null
15+
*/
16+
protected ?string $method = Saloon::GET;
17+
18+
/**
19+
* The connector.
20+
*
21+
* @var string|null
22+
*/
23+
protected ?string $connector = TestConnector::class;
24+
25+
/**
26+
* Define the endpoint for the request.
27+
*
28+
* @return string
29+
*/
30+
public function defineEndpoint(): string
31+
{
32+
return '/download-test-pdf';
33+
}
34+
}

0 commit comments

Comments
 (0)