diff --git a/.gitattributes b/.gitattributes index 60aab89..144cd42 100644 --- a/.gitattributes +++ b/.gitattributes @@ -6,10 +6,7 @@ /.scrutinizer.yml export-ignore /.styleci.yml export-ignore /.travis.yml export-ignore -/phpspec.ci.yml export-ignore -/phpspec.yml.dist export-ignore /phpstan-baseline.neon export-ignore /phpstan.neon.dist export-ignore /phpunit.xml.dist export-ignore -/spec/ export-ignore /tests/ export-ignore diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 83a85d5..ce2c38b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5'] + php: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5', '8.6'] steps: - name: Checkout code @@ -27,6 +27,9 @@ jobs: - name: Install dependencies run: composer update --prefer-dist --no-interaction --no-progress + env: + # PHPUnit 7.5 is the last version supporting PHP 7.1, it has a security advisory that blocks installation + COMPOSER_NO_BLOCKING: ${{ matrix.php == '7.1' && '1' || '' }} - name: Execute tests run: composer test @@ -36,7 +39,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: ['7.1', '7.4', '8.0', '8.5'] + php: ['7.1', '7.4', '8.0', '8.5', '8.6'] steps: - name: Checkout code @@ -50,9 +53,10 @@ jobs: coverage: none - name: Install dependencies - run: | - composer require "sebastian/comparator:^3.0.2" --no-interaction --no-update - composer update --prefer-dist --prefer-stable --prefer-lowest --no-interaction --no-progress + run: composer update --prefer-dist --prefer-stable --prefer-lowest --no-interaction --no-progress + env: + # PHPUnit 7.5 is the last version supporting PHP 7.1, it has a security advisory that blocks installation + COMPOSER_NO_BLOCKING: ${{ matrix.php == '7.1' && '1' || '' }} - name: Execute tests run: composer test @@ -68,14 +72,12 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '7.4' + php-version: '8.5' tools: composer:v2 coverage: xdebug - name: Install dependencies - run: | - composer require "friends-of-phpspec/phpspec-code-coverage:^4.3.2" --no-interaction --no-update - composer update --prefer-dist --no-interaction --no-progress + run: composer update --prefer-dist --no-interaction --no-progress - name: Execute tests run: composer test-ci diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index bbf2666..c86dc1e 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -15,9 +15,6 @@ jobs: - name: Checkout code uses: actions/checkout@v7 - - name: Remove phpspec - run: composer remove --dev friends-of-phpspec/phpspec-code-coverage phpspec/phpspec - - name: PHPStan uses: docker://oskarstark/phpstan-ga with: diff --git a/.gitignore b/.gitignore index 8bca981..6196be4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .php-cs-fixer.cache /build/ /composer.lock -/phpspec.yml /phpunit.xml +/.phpunit.cache/ +/.phpunit.result.cache /vendor/ diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index eead1b7..5ff0c88 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -1,7 +1,7 @@ in(__DIR__.'/src') + ->in([__DIR__.'/src', __DIR__.'/tests']) ->name('*.php') ; diff --git a/composer.json b/composer.json index c5e5e93..b519fed 100644 --- a/composer.json +++ b/composer.json @@ -25,16 +25,20 @@ "psr/http-message": "^1.0 || ^2.0" }, "require-dev": { - "friends-of-phpspec/phpspec-code-coverage": "^4.1 || ^5.0 || ^6.0 || ^7.0", - "phpspec/phpspec": "^5.1 || ^6.0 || ^7.0 || ^8.3" + "phpunit/phpunit": "^7.5 || ^8.5 || ^9.6 || ^10.5 || ^11.5 || ^12.5" }, "autoload": { "psr-4": { "Http\\Client\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "Http\\Client\\Tests\\": "tests/" + } + }, "scripts": { - "test": "vendor/bin/phpspec run", - "test-ci": "vendor/bin/phpspec run -c phpspec.ci.yml" + "test": "vendor/bin/phpunit", + "test-ci": "vendor/bin/phpunit --coverage-filter src --coverage-clover build/coverage.xml" } } diff --git a/phpspec.ci.yml b/phpspec.ci.yml deleted file mode 100644 index 65c53d0..0000000 --- a/phpspec.ci.yml +++ /dev/null @@ -1,10 +0,0 @@ -suites: - client_suite: - namespace: Http\Client - psr4_prefix: Http\Client -formatter.name: pretty -extensions: - FriendsOfPhpSpec\PhpSpec\CodeCoverage\CodeCoverageExtension: ~ -code_coverage: - format: clover - output: build/coverage.xml diff --git a/phpspec.yml.dist b/phpspec.yml.dist deleted file mode 100644 index 89c8de5..0000000 --- a/phpspec.yml.dist +++ /dev/null @@ -1,5 +0,0 @@ -suites: - client_suite: - namespace: Http\Client - psr4_prefix: Http\Client -formatter.name: pretty diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index c1629a6..aab4991 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,21 +1,2 @@ parameters: - ignoreErrors: - - - message: "#^Method Http\\\\Client\\\\Exception\\\\HttpException\\:\\:create\\(\\) has no return type specified\\.$#" - count: 1 - path: src/Exception/HttpException.php - - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" - count: 1 - path: src/Exception/HttpException.php - - - - message: "#^Method Http\\\\Client\\\\Exception\\\\NetworkException\\:\\:setRequest\\(\\) has no return type specified\\.$#" - count: 1 - path: src/Exception/NetworkException.php - - - - message: "#^Method Http\\\\Client\\\\Exception\\\\RequestException\\:\\:setRequest\\(\\) has no return type specified\\.$#" - count: 1 - path: src/Exception/RequestException.php + ignoreErrors: [] diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..cb1a1e5 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,12 @@ + + + + + tests/ + + + diff --git a/spec/Exception/HttpExceptionSpec.php b/spec/Exception/HttpExceptionSpec.php deleted file mode 100644 index 20eb58e..0000000 --- a/spec/Exception/HttpExceptionSpec.php +++ /dev/null @@ -1,45 +0,0 @@ -getStatusCode()->willReturn(500); - - $this->beConstructedWith('message', $request, $response); - } - - function it_is_initializable() - { - $this->shouldHaveType('Http\Client\Exception\HttpException'); - } - - function it_is_a_request_exception() - { - $this->shouldHaveType('Http\Client\Exception\RequestException'); - } - - function it_has_a_response(ResponseInterface $response) - { - $this->getResponse()->shouldReturn($response); - } - - function it_creates_an_http_exception(RequestInterface $request, ResponseInterface $response) - { - $request->getRequestTarget()->willReturn('/uri'); - $request->getMethod()->willReturn('GET'); - $response->getStatusCode()->willReturn(100); - $response->getReasonPhrase()->willReturn('Continue'); - - $e = $this->create($request, $response); - - $e->shouldHaveType('Http\Client\Exception\HttpException'); - $e->getMessage()->shouldReturn('[url] /uri [http method] GET [status code] 100 [reason phrase] Continue'); - } -} diff --git a/spec/Exception/NetworkExceptionSpec.php b/spec/Exception/NetworkExceptionSpec.php deleted file mode 100644 index c628b82..0000000 --- a/spec/Exception/NetworkExceptionSpec.php +++ /dev/null @@ -1,44 +0,0 @@ -beConstructedWith('message', $request); - } - - function it_is_initializable() - { - $this->shouldHaveType('Http\Client\Exception\NetworkException'); - } - - function it_is_a_transfer_exception() - { - $this->shouldHaveType('Http\Client\Exception\TransferException'); - } - - function it_implements_psr_network_exception_interface() - { - $this->shouldHaveType('Psr\Http\Client\NetworkExceptionInterface'); - } - - function it_does_not_implement_psr_request_exception_interface() - { - $this->shouldNotHaveType('Psr\Http\Client\RequestExceptionInterface'); - } - - function it_is_not_a_request_exception() - { - $this->shouldNotHaveType('Http\Client\Exception\RequestException'); - } - - function it_has_a_request(RequestInterface $request) - { - $this->getRequest()->shouldReturn($request); - } -} diff --git a/spec/Exception/RequestExceptionSpec.php b/spec/Exception/RequestExceptionSpec.php deleted file mode 100644 index 5ac7a10..0000000 --- a/spec/Exception/RequestExceptionSpec.php +++ /dev/null @@ -1,44 +0,0 @@ -beConstructedWith('message', $request); - } - - function it_is_initializable() - { - $this->shouldHaveType('Http\Client\Exception\RequestException'); - } - - function it_is_a_transfer_exception() - { - $this->shouldHaveType('Http\Client\Exception\TransferException'); - } - - function it_implements_psr_request_exception_interface() - { - $this->shouldHaveType('Psr\Http\Client\RequestExceptionInterface'); - } - - function it_does_not_implement_psr_network_exception_interface() - { - $this->shouldNotHaveType('Psr\Http\Client\NetworkExceptionInterface'); - } - - function it_is_not_a_network_exception() - { - $this->shouldNotHaveType('Http\Client\Exception\NetworkException'); - } - - function it_has_a_request(RequestInterface $request) - { - $this->getRequest()->shouldReturn($request); - } -} diff --git a/spec/Exception/TransferExceptionSpec.php b/spec/Exception/TransferExceptionSpec.php deleted file mode 100644 index 03fe243..0000000 --- a/spec/Exception/TransferExceptionSpec.php +++ /dev/null @@ -1,18 +0,0 @@ -shouldHaveType('RuntimeException'); - } - - function it_is_an_exception() - { - $this->shouldImplement('Http\Client\Exception'); - } -} diff --git a/spec/Promise/HttpFulfilledPromiseSpec.php b/spec/Promise/HttpFulfilledPromiseSpec.php deleted file mode 100644 index b58da8c..0000000 --- a/spec/Promise/HttpFulfilledPromiseSpec.php +++ /dev/null @@ -1,76 +0,0 @@ -beConstructedWith($response); - } - - function it_is_initializable() - { - $this->shouldHaveType('Http\Client\Promise\HttpFulfilledPromise'); - } - - function it_is_a_promise() - { - $this->shouldImplement('Http\Promise\Promise'); - } - - function it_returns_a_http_fulfilled_promise() - { - $promise = $this->then(function ($result) { - return $result; - }); - $promise->shouldHaveType('Http\Promise\Promise'); - $promise->shouldHaveType('Http\Client\Promise\HttpFulfilledPromise'); - $promise->getState()->shouldReturn(Promise::FULFILLED); - $promise->wait()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface'); - } - - function it_returns_a_http_rejected_promise() - { - $exception = new TransferException(); - $promise = $this->then(function () use ($exception) { - throw $exception; - }); - $promise->shouldHaveType('Http\Promise\Promise'); - $promise->shouldHaveType('Http\Client\Promise\HttpRejectedPromise'); - $promise->getState()->shouldReturn(Promise::REJECTED); - $promise->shouldThrow($exception)->duringWait(); - } - - function it_returns_itself_when_no_on_fulfilled_callback_is_passed() - { - $this->then()->shouldReturn($this); - } - - function it_is_in_fulfilled_state() - { - $this->getState()->shouldReturn(Promise::FULFILLED); - } - - function it_has_a_response() - { - $this->wait()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface'); - } - - function it_does_not_unwrap_a_response() - { - $this->wait(false)->shouldNotReturnAnInstanceOf('Psr\Http\Message\ResponseInterface'); - } - - function it_throws_not_http_exception() - { - $this->shouldThrow('Exception')->duringThen(function () { - throw new \Exception(); - }, null); - } -} diff --git a/spec/Promise/HttpRejectedPromiseSpec.php b/spec/Promise/HttpRejectedPromiseSpec.php deleted file mode 100644 index 5bc3f82..0000000 --- a/spec/Promise/HttpRejectedPromiseSpec.php +++ /dev/null @@ -1,106 +0,0 @@ -beConstructedWith(new TransferException()); - } - - function it_is_initializable() - { - $this->shouldHaveType('Http\Client\Promise\HttpRejectedPromise'); - } - - function it_is_a_promise() - { - $this->shouldImplement('Http\Promise\Promise'); - } - - function it_returns_a_fulfilled_promise(ResponseInterface $response) - { - $exception = new TransferException(); - $this->beConstructedWith($exception); - - $promise = $this->then(null, function (Exception $exceptionReceived) use($exception, $response) { - return $response->getWrappedObject(); - }); - - $promise->shouldHaveType('Http\Promise\Promise'); - $promise->shouldHaveType('Http\Client\Promise\HttpFulfilledPromise'); - $promise->getState()->shouldReturn(Promise::FULFILLED); - $promise->wait()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface'); - } - - function it_returns_a_rejected_promise() - { - $exception = new TransferException(); - $this->beConstructedWith($exception); - - $promise = $this->then(null, function (Exception $exceptionReceived) use($exception) { - if (Argument::is($exceptionReceived)->scoreArgument($exception)) { - throw $exception; - } - }); - - $promise->shouldHaveType('Http\Promise\Promise'); - $promise->shouldHaveType('Http\Client\Promise\HttpRejectedPromise'); - $promise->getState()->shouldReturn(Promise::REJECTED); - $promise->shouldThrow($exception)->duringWait(); - } - - function it_returns_itself_when_no_on_rejected_callback_is_passed() - { - $this->then()->shouldReturn($this); - } - - function it_is_in_rejected_state() - { - $this->getState()->shouldReturn(Promise::REJECTED); - } - - function it_returns_an_exception() - { - $exception = new TransferException(); - - $this->beConstructedWith($exception); - $this->shouldThrow($exception)->duringWait(); - } - - function it_does_not_unwrap_a_value() - { - $this->shouldNotThrow('Http\Client\Exception')->duringWait(false); - } - - function it_throws_not_http_exception() - { - $this->shouldThrow('Exception')->duringThen(null, function () { - throw new \Exception(); - }); - } - - function it_returns_a_promise_when_rejected(ResponseInterface $response) - { - $exception = new TransferException(); - $this->beConstructedWith($exception); - - $promise = $this->then(null, function (Exception $exceptionReceived) use($exception, $response) { - return new HttpFulfilledPromise($response->getWrappedObject()); - }); - - $promise->shouldHaveType('Http\Promise\Promise'); - $promise->shouldHaveType('Http\Client\Promise\HttpFulfilledPromise'); - $promise->getState()->shouldReturn(Promise::FULFILLED); - $promise->wait()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface'); - } -} diff --git a/src/Exception/HttpException.php b/src/Exception/HttpException.php index 8af32f1..442d290 100644 --- a/src/Exception/HttpException.php +++ b/src/Exception/HttpException.php @@ -11,6 +11,8 @@ * In addition to the request, this exception always provides access to the response object. * * @author Márk Sági-Kazár + * + * @phpstan-consistent-constructor */ class HttpException extends RequestException { @@ -46,6 +48,8 @@ public function getResponse() /** * Factory method to create a new exception with a normalized error message. + * + * @return static */ public static function create( RequestInterface $request, diff --git a/src/Exception/RequestAwareTrait.php b/src/Exception/RequestAwareTrait.php index f507982..6679871 100644 --- a/src/Exception/RequestAwareTrait.php +++ b/src/Exception/RequestAwareTrait.php @@ -11,7 +11,7 @@ trait RequestAwareTrait */ private $request; - private function setRequest(RequestInterface $request) + private function setRequest(RequestInterface $request): void { $this->request = $request; } diff --git a/tests/CreatesStubs.php b/tests/CreatesStubs.php new file mode 100644 index 0000000..6ed4e1f --- /dev/null +++ b/tests/CreatesStubs.php @@ -0,0 +1,27 @@ += 8.4) and falls back to createMock() for PHPUnit 7.5 (PHP 7.1). + */ +trait CreatesStubs +{ + /** + * @template T of object + * + * @param class-string $className + * + * @return T&Stub + */ + private function stub(string $className) + { + if (method_exists($this, 'createStub')) { + return $this->createStub($className); + } + + return $this->createMock($className); + } +} diff --git a/tests/Exception/HttpExceptionTest.php b/tests/Exception/HttpExceptionTest.php new file mode 100644 index 0000000..c39e442 --- /dev/null +++ b/tests/Exception/HttpExceptionTest.php @@ -0,0 +1,77 @@ +stub(RequestInterface::class), $this->createResponse(500)); + + $this->assertInstanceOf(RequestException::class, $exception); + } + + public function testHasARequestAndAResponse(): void + { + $request = $this->stub(RequestInterface::class); + $response = $this->createResponse(500); + + $exception = new HttpException('message', $request, $response); + + $this->assertSame('message', $exception->getMessage()); + $this->assertSame($request, $exception->getRequest()); + $this->assertSame($response, $exception->getResponse()); + } + + public function testUsesTheResponseStatusCodeAsCode(): void + { + $exception = new HttpException('message', $this->stub(RequestInterface::class), $this->createResponse(503)); + + $this->assertSame(503, $exception->getCode()); + } + + public function testKeepsThePreviousException(): void + { + $previous = new \Exception(); + + $exception = new HttpException('message', $this->stub(RequestInterface::class), $this->createResponse(500), $previous); + + $this->assertSame($previous, $exception->getPrevious()); + } + + public function testCreatesAnHttpExceptionWithANormalizedMessage(): void + { + $request = $this->stub(RequestInterface::class); + $request->method('getRequestTarget')->willReturn('/uri'); + $request->method('getMethod')->willReturn('GET'); + $response = $this->createResponse(100, 'Continue'); + $previous = new \Exception(); + + $exception = HttpException::create($request, $response, $previous); + + $this->assertInstanceOf(HttpException::class, $exception); + $this->assertSame('[url] /uri [http method] GET [status code] 100 [reason phrase] Continue', $exception->getMessage()); + $this->assertSame(100, $exception->getCode()); + $this->assertSame($request, $exception->getRequest()); + $this->assertSame($response, $exception->getResponse()); + $this->assertSame($previous, $exception->getPrevious()); + } + + private function createResponse(int $statusCode, string $reasonPhrase = ''): ResponseInterface + { + $response = $this->stub(ResponseInterface::class); + $response->method('getStatusCode')->willReturn($statusCode); + $response->method('getReasonPhrase')->willReturn($reasonPhrase); + + return $response; + } +} diff --git a/tests/Exception/NetworkExceptionTest.php b/tests/Exception/NetworkExceptionTest.php new file mode 100644 index 0000000..7433d8d --- /dev/null +++ b/tests/Exception/NetworkExceptionTest.php @@ -0,0 +1,63 @@ +request = $this->stub(RequestInterface::class); + $this->exception = new NetworkException('message', $this->request); + } + + public function testIsATransferException(): void + { + $this->assertInstanceOf(TransferException::class, $this->exception); + } + + public function testImplementsPsrNetworkExceptionInterface(): void + { + $this->assertInstanceOf(NetworkExceptionInterface::class, $this->exception); + } + + public function testDoesNotImplementPsrRequestExceptionInterface(): void + { + $this->assertNotInstanceOf(RequestExceptionInterface::class, $this->exception); + } + + public function testIsNotARequestException(): void + { + $this->assertNotInstanceOf(RequestException::class, $this->exception); + } + + public function testHasAMessage(): void + { + $this->assertSame('message', $this->exception->getMessage()); + } + + public function testHasARequest(): void + { + $this->assertSame($this->request, $this->exception->getRequest()); + } +} diff --git a/tests/Exception/RequestExceptionTest.php b/tests/Exception/RequestExceptionTest.php new file mode 100644 index 0000000..c121217 --- /dev/null +++ b/tests/Exception/RequestExceptionTest.php @@ -0,0 +1,63 @@ +request = $this->stub(RequestInterface::class); + $this->exception = new RequestException('message', $this->request); + } + + public function testIsATransferException(): void + { + $this->assertInstanceOf(TransferException::class, $this->exception); + } + + public function testImplementsPsrRequestExceptionInterface(): void + { + $this->assertInstanceOf(RequestExceptionInterface::class, $this->exception); + } + + public function testDoesNotImplementPsrNetworkExceptionInterface(): void + { + $this->assertNotInstanceOf(NetworkExceptionInterface::class, $this->exception); + } + + public function testIsNotANetworkException(): void + { + $this->assertNotInstanceOf(NetworkException::class, $this->exception); + } + + public function testHasAMessage(): void + { + $this->assertSame('message', $this->exception->getMessage()); + } + + public function testHasARequest(): void + { + $this->assertSame($this->request, $this->exception->getRequest()); + } +} diff --git a/tests/Exception/TransferExceptionTest.php b/tests/Exception/TransferExceptionTest.php new file mode 100644 index 0000000..20b3a49 --- /dev/null +++ b/tests/Exception/TransferExceptionTest.php @@ -0,0 +1,20 @@ +assertInstanceOf(\RuntimeException::class, new TransferException()); + } + + public function testIsAnHttplugException(): void + { + $this->assertInstanceOf(Exception::class, new TransferException()); + } +} diff --git a/tests/Promise/HttpFulfilledPromiseTest.php b/tests/Promise/HttpFulfilledPromiseTest.php new file mode 100644 index 0000000..32ab833 --- /dev/null +++ b/tests/Promise/HttpFulfilledPromiseTest.php @@ -0,0 +1,105 @@ +response = $this->stub(ResponseInterface::class); + $this->promise = new HttpFulfilledPromise($this->response); + } + + public function testIsAPromise(): void + { + $this->assertInstanceOf(Promise::class, $this->promise); + } + + public function testIsInFulfilledState(): void + { + $this->assertSame(Promise::FULFILLED, $this->promise->getState()); + } + + public function testReturnsTheResponseWhenWaiting(): void + { + $this->assertSame($this->response, $this->promise->wait()); + } + + public function testDoesNotUnwrapTheResponse(): void + { + $this->assertNull($this->promise->wait(false)); + } + + public function testReturnsItselfWhenNoOnFulfilledCallbackIsPassed(): void + { + $this->assertSame($this->promise, $this->promise->then()); + $this->assertSame($this->promise, $this->promise->then(null, function () { + $this->fail('The onRejected callback must not be called.'); + })); + } + + public function testReturnsAFulfilledPromiseWithTheCallbackResult(): void + { + $newResponse = $this->stub(ResponseInterface::class); + $received = null; + + $promise = $this->promise->then(function (ResponseInterface $response) use (&$received, $newResponse) { + $received = $response; + + return $newResponse; + }); + + $this->assertSame($this->response, $received); + $this->assertInstanceOf(HttpFulfilledPromise::class, $promise); + $this->assertSame(Promise::FULFILLED, $promise->getState()); + $this->assertSame($newResponse, $promise->wait()); + } + + public function testReturnsARejectedPromiseWhenTheCallbackThrowsAnHttplugException(): void + { + $exception = new TransferException(); + + $promise = $this->promise->then(function () use ($exception) { + throw $exception; + }); + + $this->assertInstanceOf(HttpRejectedPromise::class, $promise); + $this->assertSame(Promise::REJECTED, $promise->getState()); + + try { + $promise->wait(); + $this->fail('Expected exception was not thrown.'); + } catch (TransferException $e) { + $this->assertSame($exception, $e); + } + } + + public function testRethrowsExceptionsThatAreNotHttplugExceptions(): void + { + $this->expectException(\LogicException::class); + + $this->promise->then(function () { + throw new \LogicException(); + }); + } +} diff --git a/tests/Promise/HttpRejectedPromiseTest.php b/tests/Promise/HttpRejectedPromiseTest.php new file mode 100644 index 0000000..5225065 --- /dev/null +++ b/tests/Promise/HttpRejectedPromiseTest.php @@ -0,0 +1,123 @@ +exception = new TransferException(); + $this->promise = new HttpRejectedPromise($this->exception); + } + + public function testIsAPromise(): void + { + $this->assertInstanceOf(Promise::class, $this->promise); + } + + public function testIsInRejectedState(): void + { + $this->assertSame(Promise::REJECTED, $this->promise->getState()); + } + + public function testThrowsTheExceptionWhenWaiting(): void + { + try { + $this->promise->wait(); + $this->fail('Expected exception was not thrown.'); + } catch (TransferException $e) { + $this->assertSame($this->exception, $e); + } + } + + public function testDoesNotUnwrapTheException(): void + { + $this->assertNull($this->promise->wait(false)); + } + + public function testReturnsItselfWhenNoOnRejectedCallbackIsPassed(): void + { + $this->assertSame($this->promise, $this->promise->then()); + $this->assertSame($this->promise, $this->promise->then(function () { + $this->fail('The onFulfilled callback must not be called.'); + })); + } + + public function testReturnsAFulfilledPromiseWithTheCallbackResult(): void + { + $response = $this->stub(ResponseInterface::class); + $received = null; + + $promise = $this->promise->then(null, function (Exception $exception) use (&$received, $response) { + $received = $exception; + + return $response; + }); + + $this->assertSame($this->exception, $received); + $this->assertInstanceOf(HttpFulfilledPromise::class, $promise); + $this->assertSame(Promise::FULFILLED, $promise->getState()); + $this->assertSame($response, $promise->wait()); + } + + public function testReturnsThePromiseReturnedByTheCallback(): void + { + $returnedPromise = new HttpFulfilledPromise($this->stub(ResponseInterface::class)); + + $promise = $this->promise->then(null, function () use ($returnedPromise) { + return $returnedPromise; + }); + + $this->assertSame($returnedPromise, $promise); + } + + public function testReturnsARejectedPromiseWhenTheCallbackThrowsAnHttplugException(): void + { + $exception = new TransferException(); + + $promise = $this->promise->then(null, function () use ($exception) { + throw $exception; + }); + + $this->assertInstanceOf(HttpRejectedPromise::class, $promise); + $this->assertNotSame($this->promise, $promise); + $this->assertSame(Promise::REJECTED, $promise->getState()); + + try { + $promise->wait(); + $this->fail('Expected exception was not thrown.'); + } catch (TransferException $e) { + $this->assertSame($exception, $e); + } + } + + public function testRethrowsExceptionsThatAreNotHttplugExceptions(): void + { + $this->expectException(\LogicException::class); + + $this->promise->then(null, function () { + throw new \LogicException(); + }); + } +}