Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support Conflict 409 and Gone 410 #3

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [2.1.0] - 2023-11-01
### Added
- Support for HTTP status codes 409, 410

### Changed
- Deprecated public consts from exception classes for HTTP status codes

## [2.0.0] - 2022-11-21
### Added
- Php 8 support
Expand All @@ -16,4 +23,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [1.0.1] - 2020-09-28

## [1.0.0] - 2020-09-27
## [1.0.0] - 2020-09-27
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ This library provides the base exceptions for the generated clients made by [api

vendor/bin/phpunit

See test reports in `test-reports` directory.

## Contributing

Create a branch and open PR.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"require": {
"php": "^7.4 || ^8.0",
"fig/http-message-util": "^1.1",
"psr/http-client": "^1.0",
"psr/http-message": "^1.0"
},
Expand Down
58 changes: 57 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/BadRequestResponseException.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace DoclerLabs\ApiClientException;

use Fig\Http\Message\StatusCodeInterface;

class BadRequestResponseException extends UnexpectedResponseException
{
public const STATUS_CODE = 400;
/** @deprecated */
public const STATUS_CODE = StatusCodeInterface::STATUS_BAD_REQUEST;
}
9 changes: 9 additions & 0 deletions src/ConflictResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace DoclerLabs\ApiClientException;

class ConflictResponseException extends UnexpectedResponseException
{
}
40 changes: 20 additions & 20 deletions src/Factory/ResponseExceptionFactory.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace DoclerLabs\ApiClientException\Factory;

use DoclerLabs\ApiClientException\BadRequestResponseException;
use DoclerLabs\ApiClientException\ConflictResponseException;
use DoclerLabs\ApiClientException\ForbiddenResponseException;
use DoclerLabs\ApiClientException\GoneResponseException;
use DoclerLabs\ApiClientException\NotFoundResponseException;
use DoclerLabs\ApiClientException\PaymentRequiredResponseException;
use DoclerLabs\ApiClientException\UnauthorizedResponseException;
use DoclerLabs\ApiClientException\UnexpectedResponseException;
use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ResponseInterface;
use ReflectionClass;

class ResponseExceptionFactory
{
/** @var string[] */
private array $responseExceptions;

public function __construct()
{
$this->responseExceptions = [
BadRequestResponseException::STATUS_CODE => BadRequestResponseException::class,
UnauthorizedResponseException::STATUS_CODE => UnauthorizedResponseException::class,
PaymentRequiredResponseException::STATUS_CODE => PaymentRequiredResponseException::class,
ForbiddenResponseException::STATUS_CODE => ForbiddenResponseException::class,
NotFoundResponseException::STATUS_CODE => NotFoundResponseException::class,
];
}
private const RESPONSE_EXCEPTIONS = [
StatusCodeInterface::STATUS_BAD_REQUEST => BadRequestResponseException::class,
StatusCodeInterface::STATUS_UNAUTHORIZED => UnauthorizedResponseException::class,
StatusCodeInterface::STATUS_PAYMENT_REQUIRED => PaymentRequiredResponseException::class,
StatusCodeInterface::STATUS_FORBIDDEN => ForbiddenResponseException::class,
StatusCodeInterface::STATUS_NOT_FOUND => NotFoundResponseException::class,
StatusCodeInterface::STATUS_CONFLICT => ConflictResponseException::class,
StatusCodeInterface::STATUS_GONE => GoneResponseException::class,
];

public function create(string $message, ResponseInterface $response): UnexpectedResponseException
{
if (isset($this->responseExceptions[$response->getStatusCode()])) {
return new $this->responseExceptions[$response->getStatusCode()]($message, $response);
}

return new UnexpectedResponseException($message, $response);
return (isset(self::RESPONSE_EXCEPTIONS[$response->getStatusCode()]))
? (new ReflectionClass(self::RESPONSE_EXCEPTIONS[$response->getStatusCode()]))->newInstance($message, $response)
: new UnexpectedResponseException($message, $response);
}
}
}
9 changes: 7 additions & 2 deletions src/ForbiddenResponseException.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace DoclerLabs\ApiClientException;

use Fig\Http\Message\StatusCodeInterface;

class ForbiddenResponseException extends UnexpectedResponseException
{
public const STATUS_CODE = 403;
/** @deprecated */
public const STATUS_CODE = StatusCodeInterface::STATUS_FORBIDDEN;
}
9 changes: 9 additions & 0 deletions src/GoneResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace DoclerLabs\ApiClientException;

class GoneResponseException extends UnexpectedResponseException
{
}
9 changes: 7 additions & 2 deletions src/NotFoundResponseException.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace DoclerLabs\ApiClientException;

use Fig\Http\Message\StatusCodeInterface;

class NotFoundResponseException extends UnexpectedResponseException
{
public const STATUS_CODE = 404;
/** @deprecated */
public const STATUS_CODE = StatusCodeInterface::STATUS_NOT_FOUND;
}
9 changes: 7 additions & 2 deletions src/PaymentRequiredResponseException.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace DoclerLabs\ApiClientException;

use Fig\Http\Message\StatusCodeInterface;

class PaymentRequiredResponseException extends UnexpectedResponseException
{
public const STATUS_CODE = 402;
/** @deprecated */
public const STATUS_CODE = StatusCodeInterface::STATUS_PAYMENT_REQUIRED;
}
4 changes: 3 additions & 1 deletion src/RequestValidationException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace DoclerLabs\ApiClientException;

Expand Down
9 changes: 7 additions & 2 deletions src/UnauthorizedResponseException.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace DoclerLabs\ApiClientException;

use Fig\Http\Message\StatusCodeInterface;

class UnauthorizedResponseException extends UnexpectedResponseException
{
public const STATUS_CODE = 401;
/** @deprecated */
public const STATUS_CODE = StatusCodeInterface::STATUS_UNAUTHORIZED;
}
4 changes: 3 additions & 1 deletion src/UnexpectedResponseBodyException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace DoclerLabs\ApiClientException;

Expand Down
18 changes: 7 additions & 11 deletions src/UnexpectedResponseException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace DoclerLabs\ApiClientException;

Expand All @@ -11,16 +13,10 @@ class UnexpectedResponseException extends Exception implements ClientExceptionIn
{
protected ResponseInterface $response;

public function __construct(
string $message,
ResponseInterface $response,
Throwable $previous = null
) {
parent::__construct(
$message,
0,
$previous
);
public function __construct(string $message, ResponseInterface $response, Throwable $previous = null)
{
parent::__construct($message, 0, $previous);

$this->response = $response;
}

Expand Down
27 changes: 16 additions & 11 deletions test/suite/unit/BadRequestResponseExceptionTest.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace DoclerLabs\ApiClientBase\Test\Unit;

use DoclerLabs\ApiClientException\BadRequestResponseException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Throwable;

/**
* @coversDefaultClass \DoclerLabs\ApiClientException\BadRequestResponseException
* @covers \DoclerLabs\ApiClientException\BadRequestResponseException
*/
class BadRequestResponseExceptionTest extends TestCase
{
/**
* @covers ::__construct
* @covers ::getResponse
*/
public function testException(): void
{
$statusCode = 400;
$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn($statusCode);
$sut = new BadRequestResponseException('', $response);

$this->assertInstanceOf(Throwable::class, $sut);
$this->assertEquals($statusCode, $sut->getResponse()->getStatusCode());
/** @var ResponseInterface|MockObject $response */
$response = $this->createMock(ResponseInterface::class);
$response
->expects(self::once())
->method('getStatusCode')
->willReturn($statusCode);

$sut = new BadRequestResponseException('', $response);

self::assertInstanceOf(Throwable::class, $sut);
self::assertEquals($statusCode, $sut->getResponse()->getStatusCode());
}
}
34 changes: 34 additions & 0 deletions test/suite/unit/ConflictResponseExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace DoclerLabs\ApiClientBase\Test\Unit;

use DoclerLabs\ApiClientException\ConflictResponseException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Throwable;

/**
* @covers \DoclerLabs\ApiClientException\ConflictResponseException
*/
class ConflictResponseExceptionTest extends TestCase
{
public function testException(): void
{
$statusCode = 409;

/** @var ResponseInterface|MockObject $response */
$response = $this->createMock(ResponseInterface::class);
$response
->expects(self::once())
->method('getStatusCode')
->willReturn($statusCode);

$sut = new ConflictResponseException('', $response);

self::assertInstanceOf(Throwable::class, $sut);
self::assertEquals($statusCode, $sut->getResponse()->getStatusCode());
}
}
Loading