Skip to content

Commit

Permalink
Make Result::assertErrorFree() return void
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia authored Oct 10, 2024
1 parent 6a42958 commit e5d12bb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## v0.34.0

### Changed

- Make `Result::assertErrorFree()` return void

## v0.33.1

### Fixed
Expand Down
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,25 +198,35 @@ $result = \Example\Api\Operations\HelloSailor::execute();
```

The returned `$result` is going to be a class that extends `\Spawnia\Sailor\Result` and
holds the decoded response returned from the server. You can just grab the `$data`, `$errors`
or `$extensions` off of it:
holds the decoded response returned from the server.
You can just grab the `$data`, `$errors` or `$extensions` off of it:

```php
$catchResult->data // `null` or a generated subclass of `\Spawnia\Sailor\ObjectLike`
$catchResult->errors // `null` or a list of `\Spawnia\Sailor\Error\Error`
$catchResult->extensions // `null` or an arbitrary map
$result->data // `null` or a generated subclass of `\Spawnia\Sailor\ObjectLike`
$result->errors // `null` or a list of `\Spawnia\Sailor\Error\Error`
$result->extensions // `null` or an arbitrary map
```

### Error handling

You can ensure your query returned the proper data and contained no errors:
You can ensure an operation returned the proper data and contained no errors:

```php
$errorFreeResult = \Example\Api\Operations\HelloSailor::execute()
->errorFree(); // Throws if there are errors or returns an error free result
```

If you don't need any data, but want to ensure a mutation succeeded:
The `$errorFreeResult` is going to be a class that extends `\Spawnia\Sailor\ErrorFreeResult`.
Given it can only be obtained by going through validation,
it is guaranteed to have non-null `$data` and does not have `$errors`:

```php
$errorFreeResult->data // a generated subclass of `\Spawnia\Sailor\ObjectLike`
$errorFreeResult->extensions // `null` or an arbitrary map
```

If you do not need to access the data and just want to ensure a mutation was successful,
the following is more efficient as it does not instantiate a new object:

```php
\Example\Api\Operations\SomeMutation::execute()
Expand Down
6 changes: 1 addition & 5 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,11 @@ static function (\stdClass $raw) use ($endpoint): Error {
* Throw an exception if errors are present in the result.
*
* @throws ResultErrorsException
*
* @return $this
*/
public function assertErrorFree(): self
public function assertErrorFree(): void
{
if (isset($this->errors)) {
throw new ResultErrorsException($this->errors, static::config(), static::endpoint());
}

return $this;
}
}
18 changes: 13 additions & 5 deletions tests/Unit/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
final class ResultTest extends TestCase
{
/** @dataProvider isClientSafe */
public function testThrowErrors(bool $isClientSafe): void
public function testAssertErrorFree(bool $isClientSafe): void
{
$endpoint = \Mockery::mock(EndpointConfig::class);
$endpoint->expects('errorsAreClientSafe')
Expand Down Expand Up @@ -56,14 +56,22 @@ public function testErrorFree(bool $isClientSafe): void
->andReturn($isClientSafe);
Configuration::setEndpointFor(MyScalarQueryResult::class, $endpoint);

$result = new MyScalarQueryResult();
$result->data = MyScalarQuery::make(
$data = MyScalarQuery::make(
/* scalarWithArg: */
null,
);
$extensions = (object) [
'foo' => 'bar',
];

$result = new MyScalarQueryResult();
$result->data = $data;
$result->extensions = $extensions;

// No errors
$result->errorFree();
$errorFreeResult = $result->errorFree();
self::assertSame($data, $errorFreeResult->data);
self::assertSame($extensions, $errorFreeResult->extensions);

$result->errors = [new Error('foo')];

Expand All @@ -78,7 +86,7 @@ public function testErrorFree(bool $isClientSafe): void
self::assertSame($isClientSafe, $exception->isClientSafe());
}

public function testWithErrors(): void
public function testFromStdClass(): void
{
$endpoint = \Mockery::mock(EndpointConfig::class)->makePartial();
Configuration::setEndpointFor(MyScalarQueryResult::class, $endpoint);
Expand Down

0 comments on commit e5d12bb

Please sign in to comment.