Skip to content

Commit

Permalink
Merge pull request #298 from saloonphp/feature/v3-add-all-method-on-body
Browse files Browse the repository at this point in the history
Feature | V3 - Add `all` method back onto body
  • Loading branch information
Sammyjo20 authored Sep 21, 2023
2 parents bfd711d + a4d699f commit 99522c9
Show file tree
Hide file tree
Showing 24 changed files with 70 additions and 98 deletions.
9 changes: 1 addition & 8 deletions src/Contracts/Body/BodyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,10 @@ interface BodyRepository
public function set(mixed $value): static;

/**
* Retrieve the raw data in the repository.
* Get the raw data in the repository.
*/
public function all(): mixed;

/**
* Retrieve the raw data in the repository
*
* Alias of `all()`.
*/
public function get(): mixed;

/**
* Determine if the repository is empty
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/FixtureHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @internal
*/
final class FixtureHelper
class FixtureHelper
{
/**
* Recursively replaces array attributes
Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/URLHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @internal
*/
final class URLHelper
class URLHelper
{
/**
* Check if a URL matches a given pattern
Expand Down
2 changes: 1 addition & 1 deletion src/Http/PendingRequest/MergeBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __invoke(PendingRequest $pendingRequest): PendingRequest
// We'll clone the request body into the connector body so any properties on the request
// body will take priority if they are using a keyed array.

$body = $repository->merge($requestBody->get());
$body = $repository->merge($requestBody->all());
}

// Now we'll check if the body is a MultipartBodyRepository. If it is, then we must
Expand Down
2 changes: 0 additions & 2 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ public function getConnector(): Connector

/**
* Get the original request that created the response.
*
* @deprecated Will be removed in Saloon v4. Use $response->getPendingRequest()->getRequest() instead.
*/
public function getRequest(): Request
{
Expand Down
19 changes: 9 additions & 10 deletions src/Repositories/Body/ArrayBodyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,13 @@ public function add(string|int|null $key = null, mixed $value = null): static
}

/**
* Get a specific key of the array
* Get the raw data in the repository.
*
* @param array-key|null $key
* @return ($key is null ? array<array-key, mixed> : mixed)
* @return array<mixed, mixed>
*/
public function all(string|int|null $key = null, mixed $default = null): mixed
public function all(): array
{
if (is_null($key)) {
return $this->data;
}

return $this->data[$key] ?? $default;
return $this->data;
}

/**
Expand All @@ -103,7 +98,11 @@ public function all(string|int|null $key = null, mixed $default = null): mixed
*/
public function get(string|int|null $key = null, mixed $default = null): mixed
{
return $this->all($key, $default);
if (is_null($key)) {
return $this->all();
}

return $this->all()[$key] ?? $default;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Repositories/Body/FormBodyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class FormBodyRepository extends ArrayBodyRepository implements Stringable
*/
public function __toString(): string
{
return http_build_query($this->get());
return http_build_query($this->all());
}
}
2 changes: 1 addition & 1 deletion src/Repositories/Body/JsonBodyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function getJsonFlags(): int
*/
public function __toString(): string
{
$json = json_encode($this->get(), $this->getJsonFlags());
$json = json_encode($this->all(), $this->getJsonFlags());

return $json === false ? '' : $json;
}
Expand Down
22 changes: 8 additions & 14 deletions src/Repositories/Body/MultipartBodyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,29 +111,23 @@ public function attach(MultipartValue $file): static
}

/**
* Get a specific key of the array
* Get the raw data in the repository.
*
* @return ($key is null ? array<array-key, mixed> : mixed)
* @return array<\Saloon\Data\MultipartValue>
*/
public function all(string|int $key = null, mixed $default = null): mixed
public function all(): array
{
if (is_null($key)) {
return $this->data->get();
}

return $this->data->get($key, $default);
return $this->data->all();
}

/**
* Get a specific key of the array
*
* Alias of `all()`.
*
* @return ($key is null ? array<array-key, mixed> : mixed)
* @param array-key $key
*/
public function get(string|int $key = null, mixed $default = null): mixed
public function get(string|int $key, mixed $default = null): MultipartValue
{
return $this->all($key, $default);
return $this->data->get($key, $default);
}

/**
Expand Down Expand Up @@ -210,6 +204,6 @@ public function toStream(StreamFactoryInterface $streamFactory): StreamInterface
throw new BodyException('Unable to create a multipart body stream because the multipart body factory was not set.');
}

return $this->multipartBodyFactory->create($streamFactory, (array)$this->get(), $this->getBoundary());
return $this->multipartBodyFactory->create($streamFactory, $this->all(), $this->getBoundary());
}
}
6 changes: 1 addition & 5 deletions src/Repositories/Body/StreamBodyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public function set(mixed $value): static

/**
* Retrieve the stream from the repository
*
* @return StreamInterface|resource|null
*/
public function all(): mixed
{
Expand All @@ -61,9 +59,7 @@ public function all(): mixed
/**
* Retrieve the stream from the repository
*
* Alias of `all()`.
*
* @return StreamInterface|resource|null
* Alias of "all" method.
*/
public function get(): mixed
{
Expand Down
12 changes: 1 addition & 11 deletions src/Repositories/Body/StringBodyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,6 @@ public function all(): ?string
return $this->data;
}

/**
* Retrieve all in the repository
*
* Alias of `all()`.
*/
public function get(): ?string
{
return $this->all();
}

/**
* Determine if the repository is empty
*/
Expand All @@ -79,6 +69,6 @@ public function isNotEmpty(): bool
*/
public function __toString(): string
{
return $this->get() ?? '';
return $this->all() ?? '';
}
}
2 changes: 1 addition & 1 deletion tests/Feature/Body/HasFormBodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
test('the default body is loaded with the content type header', function () {
$request = new HasFormBodyRequest();

expect($request->body()->get())->toEqual([
expect($request->body()->all())->toEqual([
'name' => 'Sam',
'catchphrase' => 'Yeehaw!',
]);
Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/Body/HasJsonBodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
test('the default body is loaded with the content type header', function () {
$request = new HasJsonBodyRequest();

expect($request->body()->get())->toEqual([
expect($request->body()->all())->toEqual([
'name' => 'Sam',
'catchphrase' => 'Yeehaw!',
]);
Expand All @@ -43,12 +43,12 @@
$connector = new HasJsonBodyConnector;
$request = new HasJsonBodyRequest;

expect($connector->body()->get())->toEqual([
expect($connector->body()->all())->toEqual([
'name' => 'Gareth',
'drink' => 'Moonshine',
]);

expect($request->body()->get())->toEqual([
expect($request->body()->all())->toEqual([
'name' => 'Sam',
'catchphrase' => 'Yeehaw!',
]);
Expand All @@ -60,7 +60,7 @@

expect($pendingRequestBody)->toBeInstanceOf(JsonBodyRepository::class);

expect($pendingRequestBody->get())->toEqual([
expect($pendingRequestBody->all())->toEqual([
'drink' => 'Moonshine',
'name' => 'Sam',
'catchphrase' => 'Yeehaw!',
Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/Body/HasMultipartBodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
test('the default body is loaded with the content type header', function () {
$request = new HasMultipartBodyRequest();

expect($request->body()->get())->toEqual([
expect($request->body()->all())->toEqual([
'nickname' => new MultipartValue('nickname', 'Sam', 'user.txt', ['X-Saloon' => 'Yee-haw!']),
]);

Expand All @@ -33,12 +33,12 @@
$connector = new HasMultipartBodyConnector;
$request = new HasMultipartBodyRequest;

expect($connector->body()->get())->toEqual([
expect($connector->body()->all())->toEqual([
'nickname' => new MultipartValue('nickname', 'Gareth', 'user.txt', ['X-Saloon' => 'Yee-haw!']),
'drink' => new MultipartValue('drink', 'Moonshine', 'moonshine.txt', ['X-My-Head' => 'Spinning!']),
]);

expect($request->body()->get())->toEqual([
expect($request->body()->all())->toEqual([
'nickname' => new MultipartValue('nickname', 'Sam', 'user.txt', ['X-Saloon' => 'Yee-haw!']),
]);

Expand All @@ -49,7 +49,7 @@

expect($pendingRequestBody)->toBeInstanceOf(MultipartBodyRepository::class);

expect($pendingRequestBody->get())->toEqual([
expect($pendingRequestBody->all())->toEqual([
'nickname' => new MultipartValue('nickname', 'Sam', 'user.txt', ['X-Saloon' => 'Yee-haw!']),
'drink' => new MultipartValue('drink', 'Moonshine', 'moonshine.txt', ['X-My-Head' => 'Spinning!']),
]);
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Body/HasStreamBodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
test('the default body is loaded', function () {
$request = new HasStreamBodyRequest;

expect($request->body()->get())->toBeResource();
expect($request->body()->all())->toBeResource();
});

test('the guzzle sender properly sends it', function () {
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Body/HasStringBodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
test('the default body is loaded', function () {
$request = new HasStringBodyRequest();

expect($request->body()->get())->toEqual('name: Sam');
expect($request->body()->all())->toEqual('name: Sam');
});

test('the guzzle sender properly sends it', function () {
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Body/HasXmlBodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
test('the default body is loaded with the content type header', function () {
$request = new HasXmlBodyRequest();

expect($request->body()->get())->toEqual('<p>Howdy</p>');
expect($request->body()->all())->toEqual('<p>Howdy</p>');

$connector = new TestConnector;
$pendingRequest = $connector->createPendingRequest($request);
Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/Oauth2/ClientCredentialsFlowConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

$mockClient->assertSentCount(1);

expect($mockClient->getLastPendingRequest()->body()->get())->toEqual([
expect($mockClient->getLastPendingRequest()->body()->all())->toEqual([
'grant_type' => 'client_credentials',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
Expand Down Expand Up @@ -94,7 +94,7 @@

$mockClient->assertSentCount(1);

expect($mockClient->getLastPendingRequest()->body()->get())->toEqual([
expect($mockClient->getLastPendingRequest()->body()->all())->toEqual([
'grant_type' => 'client_credentials',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
Expand All @@ -120,7 +120,7 @@

$mockClient->assertSentCount(1);

expect($mockClient->getLastPendingRequest()->body()->get())->toEqual([
expect($mockClient->getLastPendingRequest()->body()->all())->toEqual([
'grant_type' => 'client_credentials',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
Expand All @@ -146,7 +146,7 @@

$mockClient->assertSentCount(1);

expect($mockClient->getLastPendingRequest()->body()->get())->toEqual([
expect($mockClient->getLastPendingRequest()->body()->all())->toEqual([
'grant_type' => 'client_credentials',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@

$pool->withResponseHandler(function (Response $response) use (&$successCount, $mockResponses) {
expect($response)->toBeInstanceOf(Response::class);
expect($response->json())->toEqual($mockResponses[$successCount]->body()->get());
expect($response->json())->toEqual($mockResponses[$successCount]->body()->all());

$successCount++;
});
Expand Down
Loading

0 comments on commit 99522c9

Please sign in to comment.