From c4543e96693c3274e1d6d5bfc6ea36357ade5af3 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Mon, 7 Oct 2019 14:37:55 +0200 Subject: [PATCH] Modernize test suite Our current test suite has been around for a while and to keep it in shape for the future we need to modernize is one in a while. With the increase to PHP 7.1+ in #149 we can also bump the PHPUnit version to 7. --- composer.json | 2 +- phpunit.xml.dist | 1 - tests/FulfilledPromiseTest.php | 8 +- tests/FunctionAllTest.php | 30 ++--- tests/FunctionAnyTest.php | 33 ++--- tests/FunctionCheckTypehintTest.php | 43 ++++--- tests/FunctionMapTest.php | 30 ++--- tests/FunctionRaceTest.php | 22 ++-- tests/FunctionReduceTest.php | 60 ++++----- tests/FunctionRejectTest.php | 8 +- tests/FunctionResolveTest.php | 30 ++--- tests/FunctionSomeTest.php | 33 ++--- tests/Internal/CancellationQueueTest.php | 13 +- tests/Internal/QueueTest.php | 13 +- .../PromiseAdapter/CallbackPromiseAdapter.php | 10 +- .../PromiseAdapterInterface.php | 10 +- tests/PromiseTest.php | 11 +- tests/PromiseTest/CancelTestTrait.php | 16 +-- .../PromiseTest/PromiseFulfilledTestTrait.php | 49 ++++---- tests/PromiseTest/PromisePendingTestTrait.php | 17 +-- .../PromiseTest/PromiseRejectedTestTrait.php | 114 +++++++++--------- tests/PromiseTest/PromiseSettledTestTrait.php | 17 +-- tests/PromiseTest/RejectTestTrait.php | 90 +++++++------- tests/PromiseTest/ResolveTestTrait.php | 50 ++++---- tests/RejectedPromiseTest.php | 10 +- tests/TestCase.php | 15 +-- 26 files changed, 395 insertions(+), 340 deletions(-) diff --git a/composer.json b/composer.json index f0b279d2..278b1fef 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "php": ">=7.1.0" }, "require-dev": { - "phpunit/phpunit": "~6.4" + "phpunit/phpunit": "^7" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ef3b2a5d..3d184322 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,7 +8,6 @@ convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" - syntaxCheck="false" bootstrap="vendor/autoload.php" > diff --git a/tests/FulfilledPromiseTest.php b/tests/FulfilledPromiseTest.php index 9692f1d9..a34867ad 100644 --- a/tests/FulfilledPromiseTest.php +++ b/tests/FulfilledPromiseTest.php @@ -2,6 +2,8 @@ namespace React\Promise; +use InvalidArgumentException; +use LogicException; use React\Promise\PromiseAdapter\CallbackPromiseAdapter; class FulfilledPromiseTest extends TestCase @@ -16,7 +18,7 @@ public function getPromiseTestAdapter(callable $canceller = null) return new CallbackPromiseAdapter([ 'promise' => function () use (&$promise) { if (!$promise) { - throw new \LogicException('FulfilledPromise must be resolved before obtaining the promise'); + throw new LogicException('FulfilledPromise must be resolved before obtaining the promise'); } return $promise; @@ -27,7 +29,7 @@ public function getPromiseTestAdapter(callable $canceller = null) } }, 'reject' => function () { - throw new \LogicException('You cannot call reject() for React\Promise\FulfilledPromise'); + throw new LogicException('You cannot call reject() for React\Promise\FulfilledPromise'); }, 'settle' => function ($value = null) use (&$promise) { if (!$promise) { @@ -39,10 +41,10 @@ public function getPromiseTestAdapter(callable $canceller = null) /** * @test - * @expectedException InvalidArgumentException */ public function shouldThrowExceptionIfConstructedWithAPromise() { + $this->expectException(InvalidArgumentException::class); return new FulfilledPromise(new FulfilledPromise()); } } diff --git a/tests/FunctionAllTest.php b/tests/FunctionAllTest.php index 10efdea7..a268c21a 100644 --- a/tests/FunctionAllTest.php +++ b/tests/FunctionAllTest.php @@ -2,6 +2,8 @@ namespace React\Promise; +use Exception; + class FunctionAllTest extends TestCase { /** @test */ @@ -9,9 +11,9 @@ public function shouldResolveEmptyInput() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo([])); + ->with(self::identicalTo([])); all([]) ->then($mock); @@ -22,9 +24,9 @@ public function shouldResolveValuesArray() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo([1, 2, 3])); + ->with(self::identicalTo([1, 2, 3])); all([1, 2, 3]) ->then($mock); @@ -35,9 +37,9 @@ public function shouldResolvePromisesArray() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo([1, 2, 3])); + ->with(self::identicalTo([1, 2, 3])); all([resolve(1), resolve(2), resolve(3)]) ->then($mock); @@ -48,9 +50,9 @@ public function shouldResolveSparseArrayInput() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo([null, 1, null, 1, 1])); + ->with(self::identicalTo([null, 1, null, 1, 1])); all([null, 1, null, 1, 1]) ->then($mock); @@ -59,14 +61,14 @@ public function shouldResolveSparseArrayInput() /** @test */ public function shouldRejectIfAnyInputPromiseRejects() { - $exception2 = new \Exception(); - $exception3 = new \Exception(); + $exception2 = new Exception(); + $exception3 = new Exception(); $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo($exception2)); + ->with(self::identicalTo($exception2)); all([resolve(1), reject($exception2), resolve($exception3)]) ->then($this->expectCallableNever(), $mock); @@ -77,9 +79,9 @@ public function shouldPreserveTheOrderOfArrayWhenResolvingAsyncPromises() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo([1, 2, 3])); + ->with(self::identicalTo([1, 2, 3])); $deferred = new Deferred(); diff --git a/tests/FunctionAnyTest.php b/tests/FunctionAnyTest.php index 14f56ef6..38ffb28e 100644 --- a/tests/FunctionAnyTest.php +++ b/tests/FunctionAnyTest.php @@ -2,6 +2,7 @@ namespace React\Promise; +use Exception; use React\Promise\Exception\CompositeException; use React\Promise\Exception\LengthException; @@ -12,10 +13,10 @@ public function shouldRejectWithLengthExceptionWithEmptyInputArray() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') ->with( - $this->callback(function ($exception) { + self::callback(function ($exception) { return $exception instanceof LengthException && 'Input array must contain at least 1 item but contains only 0 items.' === $exception->getMessage(); }) @@ -30,9 +31,9 @@ public function shouldResolveWithAnInputValue() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(1)); + ->with(self::identicalTo(1)); any([1, 2, 3]) ->then($mock); @@ -43,9 +44,9 @@ public function shouldResolveWithAPromisedInputValue() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(1)); + ->with(self::identicalTo(1)); any([resolve(1), resolve(2), resolve(3)]) ->then($mock); @@ -54,9 +55,9 @@ public function shouldResolveWithAPromisedInputValue() /** @test */ public function shouldRejectWithAllRejectedInputValuesIfAllInputsAreRejected() { - $exception1 = new \Exception(); - $exception2 = new \Exception(); - $exception3 = new \Exception(); + $exception1 = new Exception(); + $exception2 = new Exception(); + $exception3 = new Exception(); $compositeException = new CompositeException( [0 => $exception1, 1 => $exception2, 2 => $exception3], @@ -65,7 +66,7 @@ public function shouldRejectWithAllRejectedInputValuesIfAllInputsAreRejected() $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') ->with($compositeException); @@ -76,14 +77,14 @@ public function shouldRejectWithAllRejectedInputValuesIfAllInputsAreRejected() /** @test */ public function shouldResolveWhenFirstInputPromiseResolves() { - $exception2 = new \Exception(); - $exception3 = new \Exception(); + $exception2 = new Exception(); + $exception3 = new Exception(); $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(1)); + ->with(self::identicalTo(1)); any([resolve(1), reject($exception2), reject($exception3)]) ->then($mock); @@ -94,9 +95,9 @@ public function shouldNotRelyOnArryIndexesWhenUnwrappingToASingleResolutionValue { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(2)); + ->with(self::identicalTo(2)); $d1 = new Deferred(); $d2 = new Deferred(); diff --git a/tests/FunctionCheckTypehintTest.php b/tests/FunctionCheckTypehintTest.php index 60ea8cf5..bebda2c7 100644 --- a/tests/FunctionCheckTypehintTest.php +++ b/tests/FunctionCheckTypehintTest.php @@ -2,76 +2,79 @@ namespace React\Promise; +use Exception; +use InvalidArgumentException; + class FunctionCheckTypehintTest extends TestCase { /** @test */ public function shouldAcceptClosureCallbackWithTypehint() { - $this->assertTrue(_checkTypehint(function (\InvalidArgumentException $e) {}, new \InvalidArgumentException())); - $this->assertFalse(_checkTypehint(function (\InvalidArgumentException $e) {}, new \Exception())); + self::assertTrue(_checkTypehint(function (InvalidArgumentException $e) {}, new InvalidArgumentException())); + self::assertFalse(_checkTypehint(function (InvalidArgumentException $e) {}, new Exception())); } /** @test */ public function shouldAcceptFunctionStringCallbackWithTypehint() { - $this->assertTrue(_checkTypehint('React\Promise\testCallbackWithTypehint', new \InvalidArgumentException())); - $this->assertFalse(_checkTypehint('React\Promise\testCallbackWithTypehint', new \Exception())); + self::assertTrue(_checkTypehint(new TestCallbackWithTypehintClass(), new InvalidArgumentException())); + self::assertFalse(_checkTypehint(new TestCallbackWithTypehintClass(), new Exception())); } /** @test */ public function shouldAcceptInvokableObjectCallbackWithTypehint() { - $this->assertTrue(_checkTypehint(new TestCallbackWithTypehintClass(), new \InvalidArgumentException())); - $this->assertFalse(_checkTypehint(new TestCallbackWithTypehintClass(), new \Exception())); + self::assertTrue(_checkTypehint(new TestCallbackWithTypehintClass(), new InvalidArgumentException())); + self::assertFalse(_checkTypehint(new TestCallbackWithTypehintClass(), new Exception())); } /** @test */ public function shouldAcceptObjectMethodCallbackWithTypehint() { - $this->assertTrue(_checkTypehint([new TestCallbackWithTypehintClass(), 'testCallback'], new \InvalidArgumentException())); - $this->assertFalse(_checkTypehint([new TestCallbackWithTypehintClass(), 'testCallback'], new \Exception())); + self::assertTrue(_checkTypehint([new TestCallbackWithTypehintClass(), 'testCallback'], new InvalidArgumentException())); + self::assertFalse(_checkTypehint([new TestCallbackWithTypehintClass(), 'testCallback'], new Exception())); } /** @test */ public function shouldAcceptStaticClassCallbackWithTypehint() { - $this->assertTrue(_checkTypehint(['React\Promise\TestCallbackWithTypehintClass', 'testCallbackStatic'], new \InvalidArgumentException())); - $this->assertFalse(_checkTypehint(['React\Promise\TestCallbackWithTypehintClass', 'testCallbackStatic'], new \Exception())); + self::assertTrue(_checkTypehint([TestCallbackWithTypehintClass::class, 'testCallbackStatic'], new InvalidArgumentException())); + self::assertFalse(_checkTypehint([TestCallbackWithTypehintClass::class, 'testCallbackStatic'], new Exception())); } /** @test */ public function shouldAcceptClosureCallbackWithoutTypehint() { - $this->assertTrue(_checkTypehint(function (\InvalidArgumentException $e) { - }, new \InvalidArgumentException())); + self::assertTrue(_checkTypehint(function (InvalidArgumentException $e) { + }, new InvalidArgumentException())); } /** @test */ public function shouldAcceptFunctionStringCallbackWithoutTypehint() { - $this->assertTrue(_checkTypehint('React\Promise\testCallbackWithoutTypehint', new \InvalidArgumentException())); + self::assertTrue(_checkTypehint(new TestCallbackWithTypehintClass(), new InvalidArgumentException())); } /** @test */ public function shouldAcceptInvokableObjectCallbackWithoutTypehint() { - $this->assertTrue(_checkTypehint(new TestCallbackWithoutTypehintClass(), new \InvalidArgumentException())); + self::assertTrue(_checkTypehint(new TestCallbackWithoutTypehintClass(), new InvalidArgumentException())); } /** @test */ public function shouldAcceptObjectMethodCallbackWithoutTypehint() { - $this->assertTrue(_checkTypehint([new TestCallbackWithoutTypehintClass(), 'testCallback'], new \InvalidArgumentException())); + self::assertTrue(_checkTypehint([new TestCallbackWithoutTypehintClass(), 'testCallback'], new InvalidArgumentException())); } /** @test */ public function shouldAcceptStaticClassCallbackWithoutTypehint() { - $this->assertTrue(_checkTypehint(['React\Promise\TestCallbackWithoutTypehintClass', 'testCallbackStatic'], new \InvalidArgumentException())); + self::assertTrue(_checkTypehint([TestCallbackWithoutTypehintClass::class, 'testCallbackStatic'], new InvalidArgumentException())); } } -function testCallbackWithTypehint(\InvalidArgumentException $e) +function testCallbackWithTypehint(InvalidArgumentException $e) { } @@ -81,15 +84,15 @@ function testCallbackWithoutTypehint() class TestCallbackWithTypehintClass { - public function __invoke(\InvalidArgumentException $e) + public function __invoke(InvalidArgumentException $e) { } - public function testCallback(\InvalidArgumentException $e) + public function testCallback(InvalidArgumentException $e) { } - public static function testCallbackStatic(\InvalidArgumentException $e) + public static function testCallbackStatic(InvalidArgumentException $e) { } } diff --git a/tests/FunctionMapTest.php b/tests/FunctionMapTest.php index 751f95b6..46441d37 100644 --- a/tests/FunctionMapTest.php +++ b/tests/FunctionMapTest.php @@ -2,6 +2,8 @@ namespace React\Promise; +use Exception; + class FunctionMapTest extends TestCase { protected function mapper() @@ -23,9 +25,9 @@ public function shouldMapInputValuesArray() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo([2, 4, 6])); + ->with(self::identicalTo([2, 4, 6])); map( [1, 2, 3], @@ -38,9 +40,9 @@ public function shouldMapInputPromisesArray() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo([2, 4, 6])); + ->with(self::identicalTo([2, 4, 6])); map( [resolve(1), resolve(2), resolve(3)], @@ -53,9 +55,9 @@ public function shouldMapMixedInputArray() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo([2, 4, 6])); + ->with(self::identicalTo([2, 4, 6])); map( [1, resolve(2), 3], @@ -68,9 +70,9 @@ public function shouldMapInputWhenMapperReturnsAPromise() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo([2, 4, 6])); + ->with(self::identicalTo([2, 4, 6])); map( [1, 2, 3], @@ -83,9 +85,9 @@ public function shouldPreserveTheOrderOfArrayWhenResolvingAsyncPromises() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo([2, 4, 6])); + ->with(self::identicalTo([2, 4, 6])); $deferred = new Deferred(); @@ -100,14 +102,14 @@ public function shouldPreserveTheOrderOfArrayWhenResolvingAsyncPromises() /** @test */ public function shouldRejectWhenInputContainsRejection() { - $exception2 = new \Exception(); - $exception3 = new \Exception(); + $exception2 = new Exception(); + $exception3 = new Exception(); $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo($exception2)); + ->with(self::identicalTo($exception2)); map( [resolve(1), reject($exception2), resolve($exception3)], diff --git a/tests/FunctionRaceTest.php b/tests/FunctionRaceTest.php index 289f7454..83649173 100644 --- a/tests/FunctionRaceTest.php +++ b/tests/FunctionRaceTest.php @@ -2,6 +2,8 @@ namespace React\Promise; +use Exception; + class FunctionRaceTest extends TestCase { /** @test */ @@ -17,9 +19,9 @@ public function shouldResolveValuesArray() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(1)); + ->with(self::identicalTo(1)); race( [1, 2, 3] @@ -31,9 +33,9 @@ public function shouldResolvePromisesArray() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(2)); + ->with(self::identicalTo(2)); $d1 = new Deferred(); $d2 = new Deferred(); @@ -54,9 +56,9 @@ public function shouldResolveSparseArrayInput() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(null)); + ->with(self::identicalTo(null)); race( [null, 1, null, 2, 3] @@ -66,13 +68,13 @@ public function shouldResolveSparseArrayInput() /** @test */ public function shouldRejectIfFirstSettledPromiseRejects() { - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo($exception)); + ->with(self::identicalTo($exception)); $d1 = new Deferred(); $d2 = new Deferred(); @@ -112,7 +114,7 @@ public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfill public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseRejects() { $deferred = new Deferred($this->expectCallableNever()); - $deferred->reject(new \Exception()); + $deferred->reject(new Exception()); $promise2 = new Promise(function () {}, $this->expectCallableNever()); diff --git a/tests/FunctionReduceTest.php b/tests/FunctionReduceTest.php index ecffe8bd..3f6db624 100644 --- a/tests/FunctionReduceTest.php +++ b/tests/FunctionReduceTest.php @@ -2,6 +2,8 @@ namespace React\Promise; +use Exception; + class FunctionReduceTest extends TestCase { protected function plus() @@ -23,9 +25,9 @@ public function shouldReduceValuesWithoutInitialValue() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(6)); + ->with(self::identicalTo(6)); reduce( [1, 2, 3], @@ -38,9 +40,9 @@ public function shouldReduceValuesWithInitialValue() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(7)); + ->with(self::identicalTo(7)); reduce( [1, 2, 3], @@ -54,9 +56,9 @@ public function shouldReduceValuesWithInitialPromise() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(7)); + ->with(self::identicalTo(7)); reduce( [1, 2, 3], @@ -70,9 +72,9 @@ public function shouldReducePromisedValuesWithoutInitialValue() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(6)); + ->with(self::identicalTo(6)); reduce( [resolve(1), resolve(2), resolve(3)], @@ -85,9 +87,9 @@ public function shouldReducePromisedValuesWithInitialValue() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(7)); + ->with(self::identicalTo(7)); reduce( [resolve(1), resolve(2), resolve(3)], @@ -101,9 +103,9 @@ public function shouldReducePromisedValuesWithInitialPromise() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(7)); + ->with(self::identicalTo(7)); reduce( [resolve(1), resolve(2), resolve(3)], @@ -117,9 +119,9 @@ public function shouldReduceEmptyInputWithInitialValue() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(1)); + ->with(self::identicalTo(1)); reduce( [], @@ -133,9 +135,9 @@ public function shouldReduceEmptyInputWithInitialPromise() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(1)); + ->with(self::identicalTo(1)); reduce( [], @@ -147,13 +149,13 @@ public function shouldReduceEmptyInputWithInitialPromise() /** @test */ public function shouldRejectWhenInputContainsRejection() { - $exception2 = new \Exception(); + $exception2 = new Exception(); $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo($exception2)); + ->with(self::identicalTo($exception2)); reduce( [resolve(1), reject($exception2), resolve(3)], @@ -171,9 +173,9 @@ public function shouldResolveWithNullWhenInputIsEmptyAndNoInitialValueOrPromiseP // We're following PHP's array_reduce behavior and resolve with NULL. $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(null)); + ->with(self::identicalTo(null)); reduce( [], @@ -186,9 +188,9 @@ public function shouldAllowSparseArrayInputWithoutInitialValue() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(3)); + ->with(self::identicalTo(3)); reduce( [null, null, 1, null, 1, 1], @@ -201,9 +203,9 @@ public function shouldAllowSparseArrayInputWithInitialValue() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(4)); + ->with(self::identicalTo(4)); reduce( [null, null, 1, null, 1, 1], @@ -217,9 +219,9 @@ public function shouldReduceInInputOrder() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo('123')); + ->with(self::identicalTo('123')); reduce( [1, 2, 3], @@ -243,9 +245,9 @@ public function shouldProvideCorrectBasisValue() $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo([1, 2, 3])); + ->with(self::identicalTo([1, 2, 3])); reduce( [$d1->promise(), $d2->promise(), $d3->promise()], diff --git a/tests/FunctionRejectTest.php b/tests/FunctionRejectTest.php index 887d3c34..d867cab8 100644 --- a/tests/FunctionRejectTest.php +++ b/tests/FunctionRejectTest.php @@ -2,18 +2,20 @@ namespace React\Promise; +use Exception; + class FunctionRejectTest extends TestCase { /** @test */ public function shouldRejectAnException() { - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo($exception)); + ->with(self::identicalTo($exception)); reject($exception) ->then($this->expectCallableNever(), $mock); diff --git a/tests/FunctionResolveTest.php b/tests/FunctionResolveTest.php index c7802c02..53d5d334 100644 --- a/tests/FunctionResolveTest.php +++ b/tests/FunctionResolveTest.php @@ -2,6 +2,8 @@ namespace React\Promise; +use Exception; + class FunctionResolveTest extends TestCase { /** @test */ @@ -11,9 +13,9 @@ public function shouldResolveAnImmediateValue() $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo($expected)); + ->with(self::identicalTo($expected)); resolve($expected) ->then( @@ -31,9 +33,9 @@ public function shouldResolveAFulfilledPromise() $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo($expected)); + ->with(self::identicalTo($expected)); resolve($resolved) ->then( @@ -49,9 +51,9 @@ public function shouldResolveAThenable() $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo('foo')); + ->with(self::identicalTo('foo')); resolve($thenable) ->then( @@ -68,21 +70,21 @@ public function shouldResolveACancellableThenable() $promise = resolve($thenable); $promise->cancel(); - $this->assertTrue($thenable->cancelCalled); + self::assertTrue($thenable->cancelCalled); } /** @test */ public function shouldRejectARejectedPromise() { - $exception = new \Exception(); + $exception = new Exception(); $resolved = new RejectedPromise($exception); $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo($exception)); + ->with(self::identicalTo($exception)); resolve($resolved) ->then( @@ -114,9 +116,9 @@ function ($val) { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(true)); + ->with(self::identicalTo(true)); $result->then($mock); } @@ -151,9 +153,9 @@ public function shouldSupportVeryDeepNestedPromises() $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo(true)); + ->with(self::identicalTo(true)); $deferreds[0]->promise()->then($mock); } diff --git a/tests/FunctionSomeTest.php b/tests/FunctionSomeTest.php index 341948f6..9af1c1e0 100644 --- a/tests/FunctionSomeTest.php +++ b/tests/FunctionSomeTest.php @@ -2,6 +2,7 @@ namespace React\Promise; +use Exception; use React\Promise\Exception\CompositeException; use React\Promise\Exception\LengthException; @@ -12,10 +13,10 @@ public function shouldRejectWithLengthExceptionWithEmptyInputArray() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') ->with( - $this->callback(function ($exception) { + self::callback(function ($exception) { return $exception instanceof LengthException && 'Input array must contain at least 1 item but contains only 0 items.' === $exception->getMessage(); }) @@ -32,10 +33,10 @@ public function shouldRejectWithLengthExceptionWithInputArrayContainingNotEnough { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') ->with( - $this->callback(function ($exception) { + self::callback(function ($exception) { return $exception instanceof LengthException && 'Input array must contain at least 4 items but contains only 3 items.' === $exception->getMessage(); }) @@ -52,9 +53,9 @@ public function shouldResolveValuesArray() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo([1, 2])); + ->with(self::identicalTo([1, 2])); some( [1, 2, 3], @@ -67,9 +68,9 @@ public function shouldResolvePromisesArray() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo([1, 2])); + ->with(self::identicalTo([1, 2])); some( [resolve(1), resolve(2), resolve(3)], @@ -82,9 +83,9 @@ public function shouldResolveSparseArrayInput() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo([null, 1])); + ->with(self::identicalTo([null, 1])); some( [null, 1, null, 2, 3], @@ -98,8 +99,8 @@ public function shouldResolveSparseArrayInput() */ public function shouldRejectIfAnyInputPromiseRejectsBeforeDesiredNumberOfInputsAreResolved() { - $exception2 = new \Exception(); - $exception3 = new \Exception(); + $exception2 = new Exception(); + $exception3 = new Exception(); $compositeException = new CompositeException( [1 => $exception2, 2 => $exception3], @@ -108,7 +109,7 @@ public function shouldRejectIfAnyInputPromiseRejectsBeforeDesiredNumberOfInputsA $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') ->with($compositeException); @@ -124,9 +125,9 @@ public function shouldResolveWithEmptyArrayIfHowManyIsLessThanOne() { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo([])); + ->with(self::identicalTo([])); some( [1], @@ -158,7 +159,7 @@ public function shouldCancelOtherPendingInputArrayPromisesIfEnoughPromisesFulfil public function shouldNotCancelOtherPendingInputArrayPromisesIfEnoughPromisesReject() { $deferred = new Deferred($this->expectCallableNever()); - $deferred->reject(new \Exception()); + $deferred->reject(new Exception()); $promise2 = new Promise(function () {}, $this->expectCallableNever()); diff --git a/tests/Internal/CancellationQueueTest.php b/tests/Internal/CancellationQueueTest.php index 5ea20fdd..f168cb34 100644 --- a/tests/Internal/CancellationQueueTest.php +++ b/tests/Internal/CancellationQueueTest.php @@ -2,6 +2,7 @@ namespace React\Promise\Internal; +use Exception; use React\Promise\Deferred; use React\Promise\SimpleTestCancellable; use React\Promise\SimpleTestCancellableThenable; @@ -19,7 +20,7 @@ public function acceptsSimpleCancellableThenable() $cancellationQueue(); - $this->assertTrue($p->cancelCalled); + self::assertTrue($p->cancelCalled); } /** @test */ @@ -32,7 +33,7 @@ public function ignoresSimpleCancellable() $cancellationQueue(); - $this->assertFalse($p->cancelCalled); + self::assertFalse($p->cancelCalled); } /** @test */ @@ -76,16 +77,16 @@ public function doesNotCallCancelTwiceWhenStartedTwice() /** * @test - * @expectedException Exception - * @expectedExceptionMessage test */ public function rethrowsExceptionsThrownFromCancel() { + $this->expectException(Exception::class); + $this->expectExceptionMessage('test'); $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->will($this->throwException(new \Exception('test'))); + ->will(self::throwException(new Exception('test'))); $promise = new SimpleTestCancellableThenable($mock); diff --git a/tests/Internal/QueueTest.php b/tests/Internal/QueueTest.php index 3789a408..ee26a003 100644 --- a/tests/Internal/QueueTest.php +++ b/tests/Internal/QueueTest.php @@ -2,12 +2,13 @@ namespace React\Promise\Internal; +use Exception; use React\Promise\TestCase; class QueueTest extends TestCase { /** @test */ - public function excutesTasks() + public function executesTasks() { $queue = new Queue(); @@ -16,7 +17,7 @@ public function excutesTasks() } /** @test */ - public function excutesNestedEnqueuedTasks() + public function executesNestedEnqueuedTasks() { $queue = new Queue(); @@ -31,16 +32,16 @@ public function excutesNestedEnqueuedTasks() /** * @test - * @expectedException Exception - * @expectedException test */ public function rethrowsExceptionsThrownFromTasks() { + $this->expectException(Exception::class); + $this->expectExceptionMessage('test'); $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->will($this->throwException(new \Exception('test'))); + ->will(self::throwException(new Exception('test'))); $queue = new Queue(); $queue->enqueue($mock); diff --git a/tests/PromiseAdapter/CallbackPromiseAdapter.php b/tests/PromiseAdapter/CallbackPromiseAdapter.php index f9b13303..aa10436c 100644 --- a/tests/PromiseAdapter/CallbackPromiseAdapter.php +++ b/tests/PromiseAdapter/CallbackPromiseAdapter.php @@ -2,6 +2,8 @@ namespace React\Promise\PromiseAdapter; +use React\Promise\PromiseInterface; + class CallbackPromiseAdapter implements PromiseAdapterInterface { private $callbacks; @@ -11,22 +13,22 @@ public function __construct(array $callbacks) $this->callbacks = $callbacks; } - public function promise() + public function promise(): ?PromiseInterface { return call_user_func_array($this->callbacks['promise'], func_get_args()); } - public function resolve() + public function resolve(): ?PromiseInterface { return call_user_func_array($this->callbacks['resolve'], func_get_args()); } - public function reject() + public function reject(): ?PromiseInterface { return call_user_func_array($this->callbacks['reject'], func_get_args()); } - public function settle() + public function settle(): ?PromiseInterface { return call_user_func_array($this->callbacks['settle'], func_get_args()); } diff --git a/tests/PromiseAdapter/PromiseAdapterInterface.php b/tests/PromiseAdapter/PromiseAdapterInterface.php index 2520a515..8581f303 100644 --- a/tests/PromiseAdapter/PromiseAdapterInterface.php +++ b/tests/PromiseAdapter/PromiseAdapterInterface.php @@ -2,10 +2,12 @@ namespace React\Promise\PromiseAdapter; +use React\Promise\PromiseInterface; + interface PromiseAdapterInterface { - public function promise(); - public function resolve(); - public function reject(); - public function settle(); + public function promise(): ?PromiseInterface; + public function resolve(): ?PromiseInterface; + public function reject(): ?PromiseInterface; + public function settle(): ?PromiseInterface; } diff --git a/tests/PromiseTest.php b/tests/PromiseTest.php index df8ae724..26ebdf9b 100644 --- a/tests/PromiseTest.php +++ b/tests/PromiseTest.php @@ -2,6 +2,7 @@ namespace React\Promise; +use Exception; use React\Promise\PromiseAdapter\CallbackPromiseAdapter; class PromiseTest extends TestCase @@ -30,7 +31,7 @@ public function getPromiseTestAdapter(callable $canceller = null) /** @test */ public function shouldRejectIfResolverThrowsException() { - $exception = new \Exception('foo'); + $exception = new Exception('foo'); $promise = new Promise(function () use ($exception) { throw $exception; @@ -38,9 +39,9 @@ public function shouldRejectIfResolverThrowsException() $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke') - ->with($this->identicalTo($exception)); + ->with(self::identicalTo($exception)); $promise ->then($this->expectCallableNever(), $mock); @@ -51,10 +52,10 @@ public function shouldRejectWithoutCreatingGarbageCyclesIfResolverThrowsExceptio { gc_collect_cycles(); $promise = new Promise(function () { - throw new \Exception('foo'); + throw new Exception('foo'); }); unset($promise); - $this->assertSame(0, gc_collect_cycles()); + self::assertSame(0, gc_collect_cycles()); } } diff --git a/tests/PromiseTest/CancelTestTrait.php b/tests/PromiseTest/CancelTestTrait.php index 7b2c6053..d6626655 100644 --- a/tests/PromiseTest/CancelTestTrait.php +++ b/tests/PromiseTest/CancelTestTrait.php @@ -2,12 +2,14 @@ namespace React\Promise\PromiseTest; +use Exception; use React\Promise; +use React\Promise\PromiseAdapter\PromiseAdapterInterface; trait CancelTestTrait { /** - * @return \React\Promise\PromiseAdapter\PromiseAdapterInterface + * @return PromiseAdapterInterface */ abstract public function getPromiseTestAdapter(callable $canceller = null); @@ -21,9 +23,9 @@ public function cancelShouldCallCancellerWithResolverArguments() $adapter->promise()->cancel(); - $this->assertCount(2, $args); - $this->assertTrue(is_callable($args[0])); - $this->assertTrue(is_callable($args[1])); + self::assertCount(2, $args); + self::assertTrue(is_callable($args[0])); + self::assertTrue(is_callable($args[1])); } /** @test */ @@ -36,7 +38,7 @@ public function cancelShouldCallCancellerWithoutArgumentsIfNotAccessed() $adapter->promise()->cancel(); - $this->assertSame(0, $args); + self::assertSame(0, $args); } /** @test */ @@ -61,7 +63,7 @@ public function cancelShouldFulfillPromiseIfCancellerFulfills() /** @test */ public function cancelShouldRejectPromiseIfCancellerRejects() { - $exception = new \Exception(); + $exception = new Exception(); $adapter = $this->getPromiseTestAdapter(function ($resolve, $reject) use ($exception) { $reject($exception); @@ -82,7 +84,7 @@ public function cancelShouldRejectPromiseIfCancellerRejects() /** @test */ public function cancelShouldRejectPromiseWithExceptionIfCancellerThrows() { - $e = new \Exception(); + $e = new Exception(); $adapter = $this->getPromiseTestAdapter(function () use ($e) { throw $e; diff --git a/tests/PromiseTest/PromiseFulfilledTestTrait.php b/tests/PromiseTest/PromiseFulfilledTestTrait.php index f1c18025..9213375c 100644 --- a/tests/PromiseTest/PromiseFulfilledTestTrait.php +++ b/tests/PromiseTest/PromiseFulfilledTestTrait.php @@ -2,12 +2,17 @@ namespace React\Promise\PromiseTest; +use Exception; use React\Promise\ErrorCollector; +use React\Promise\PromiseAdapter\PromiseAdapterInterface; +use stdClass; +use function React\Promise\reject; +use function React\Promise\resolve; trait PromiseFulfilledTestTrait { /** - * @return \React\Promise\PromiseAdapter\PromiseAdapterInterface + * @return PromiseAdapterInterface */ abstract public function getPromiseTestAdapter(callable $canceller = null); @@ -112,7 +117,7 @@ public function thenShouldForwardPromisedCallbackResultValueToNextCallback() $adapter->promise() ->then( function ($val) { - return \React\Promise\resolve($val + 1); + return resolve($val + 1); }, $this->expectCallableNever() ) @@ -127,7 +132,7 @@ public function thenShouldSwitchFromCallbacksToErrbacksWhenCallbackReturnsARejec { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -139,7 +144,7 @@ public function thenShouldSwitchFromCallbacksToErrbacksWhenCallbackReturnsARejec $adapter->promise() ->then( function () use ($exception) { - return \React\Promise\reject($exception); + return reject($exception); }, $this->expectCallableNever() ) @@ -154,7 +159,7 @@ public function thenShouldSwitchFromCallbacksToErrbacksWhenCallbackThrows() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -187,7 +192,7 @@ public function cancelShouldReturnNullForFulfilledPromise() $adapter->resolve(); - $this->assertNull($adapter->promise()->cancel()); + self::assertNull($adapter->promise()->cancel()); } /** @test */ @@ -212,7 +217,7 @@ public function doneShouldInvokeFulfillmentHandlerForFulfilledPromise() ->with($this->identicalTo(1)); $adapter->resolve(1); - $this->assertNull($adapter->promise()->done($mock)); + self::assertNull($adapter->promise()->done($mock)); } /** @test */ @@ -225,14 +230,14 @@ public function doneShouldTriggerFatalErrorThrownFulfillmentHandlerForFulfilledP $errorCollector = new ErrorCollector(); $errorCollector->start(); - $this->assertNull($adapter->promise()->done(function () { - throw new \Exception('Unhandled Rejection'); + self::assertNull($adapter->promise()->done(function () { + throw new Exception('Unhandled Rejection'); })); $errors = $errorCollector->stop(); - $this->assertEquals(E_USER_ERROR, $errors[0]['errno']); - $this->assertContains('Unhandled Rejection', $errors[0]['errstr']); + self::assertEquals(E_USER_ERROR, $errors[0]['errno']); + self::assertContains('Unhandled Rejection', $errors[0]['errstr']); } /** @test */ @@ -245,14 +250,14 @@ public function doneShouldTriggerFatalErrorUnhandledRejectionExceptionWhenFulfil $errorCollector = new ErrorCollector(); $errorCollector->start(); - $this->assertNull($adapter->promise()->done(function () { - return \React\Promise\reject(new \Exception('Unhandled Rejection')); + self::assertNull($adapter->promise()->done(function () { + return reject(new Exception('Unhandled Rejection')); })); $errors = $errorCollector->stop(); - $this->assertEquals(E_USER_ERROR, $errors[0]['errno']); - $this->assertContains('Unhandled Rejection', $errors[0]['errstr']); + self::assertEquals(E_USER_ERROR, $errors[0]['errno']); + self::assertContains('Unhandled Rejection', $errors[0]['errstr']); } /** @test */ @@ -269,7 +274,7 @@ public function alwaysShouldNotSuppressValueForFulfilledPromise() { $adapter = $this->getPromiseTestAdapter(); - $value = new \stdClass(); + $value = new stdClass(); $mock = $this->createCallableMock(); $mock @@ -288,7 +293,7 @@ public function alwaysShouldNotSuppressValueWhenHandlerReturnsANonPromiseForFulf { $adapter = $this->getPromiseTestAdapter(); - $value = new \stdClass(); + $value = new stdClass(); $mock = $this->createCallableMock(); $mock @@ -309,7 +314,7 @@ public function alwaysShouldNotSuppressValueWhenHandlerReturnsAPromiseForFulfill { $adapter = $this->getPromiseTestAdapter(); - $value = new \stdClass(); + $value = new stdClass(); $mock = $this->createCallableMock(); $mock @@ -320,7 +325,7 @@ public function alwaysShouldNotSuppressValueWhenHandlerReturnsAPromiseForFulfill $adapter->resolve($value); $adapter->promise() ->always(function () { - return \React\Promise\resolve(1); + return resolve(1); }) ->then($mock); } @@ -330,7 +335,7 @@ public function alwaysShouldRejectWhenHandlerThrowsForFulfilledPromise() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -351,7 +356,7 @@ public function alwaysShouldRejectWhenHandlerRejectsForFulfilledPromise() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -362,7 +367,7 @@ public function alwaysShouldRejectWhenHandlerRejectsForFulfilledPromise() $adapter->resolve(1); $adapter->promise() ->always(function () use ($exception) { - return \React\Promise\reject($exception); + return reject($exception); }) ->then(null, $mock); } diff --git a/tests/PromiseTest/PromisePendingTestTrait.php b/tests/PromiseTest/PromisePendingTestTrait.php index a4f48ee2..8916b466 100644 --- a/tests/PromiseTest/PromisePendingTestTrait.php +++ b/tests/PromiseTest/PromisePendingTestTrait.php @@ -2,10 +2,13 @@ namespace React\Promise\PromiseTest; +use React\Promise\PromiseAdapter\PromiseAdapterInterface; +use React\Promise\PromiseInterface; + trait PromisePendingTestTrait { /** - * @return \React\Promise\PromiseAdapter\PromiseAdapterInterface + * @return PromiseAdapterInterface */ abstract public function getPromiseTestAdapter(callable $canceller = null); @@ -14,7 +17,7 @@ public function thenShouldReturnAPromiseForPendingPromise() { $adapter = $this->getPromiseTestAdapter(); - $this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->then()); + self::assertInstanceOf(PromiseInterface::class, $adapter->promise()->then()); } /** @test */ @@ -22,7 +25,7 @@ public function thenShouldReturnAllowNullForPendingPromise() { $adapter = $this->getPromiseTestAdapter(); - $this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->then(null, null, null)); + self::assertInstanceOf(PromiseInterface::class, $adapter->promise()->then(null, null, null)); } /** @test */ @@ -30,7 +33,7 @@ public function cancelShouldReturnNullForPendingPromise() { $adapter = $this->getPromiseTestAdapter(); - $this->assertNull($adapter->promise()->cancel()); + self::assertNull($adapter->promise()->cancel()); } /** @test */ @@ -38,7 +41,7 @@ public function doneShouldReturnNullForPendingPromise() { $adapter = $this->getPromiseTestAdapter(); - $this->assertNull($adapter->promise()->done()); + self::assertNull($adapter->promise()->done()); } /** @test */ @@ -46,7 +49,7 @@ public function doneShouldReturnAllowNullForPendingPromise() { $adapter = $this->getPromiseTestAdapter(); - $this->assertNull($adapter->promise()->done(null, null, null)); + self::assertNull($adapter->promise()->done(null, null, null)); } /** @test */ @@ -63,6 +66,6 @@ public function alwaysShouldReturnAPromiseForPendingPromise() { $adapter = $this->getPromiseTestAdapter(); - $this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->always(function () {})); + self::assertInstanceOf(PromiseInterface::class, $adapter->promise()->always(function () {})); } } diff --git a/tests/PromiseTest/PromiseRejectedTestTrait.php b/tests/PromiseTest/PromiseRejectedTestTrait.php index 5a456b13..d2fb8ac0 100644 --- a/tests/PromiseTest/PromiseRejectedTestTrait.php +++ b/tests/PromiseTest/PromiseRejectedTestTrait.php @@ -2,14 +2,18 @@ namespace React\Promise\PromiseTest; +use Exception; +use InvalidArgumentException; use React\Promise\Deferred; use React\Promise\ErrorCollector; -use React\Promise\UnhandledRejectionException; +use React\Promise\PromiseAdapter\PromiseAdapterInterface; +use function React\Promise\reject; +use function React\Promise\resolve; trait PromiseRejectedTestTrait { /** - * @return \React\Promise\PromiseAdapter\PromiseAdapterInterface + * @return PromiseAdapterInterface */ abstract public function getPromiseTestAdapter(callable $canceller = null); @@ -18,8 +22,8 @@ public function rejectedPromiseShouldBeImmutable() { $adapter = $this->getPromiseTestAdapter(); - $exception1 = new \Exception(); - $exception2 = new \Exception(); + $exception1 = new Exception(); + $exception2 = new Exception(); $mock = $this->createCallableMock(); $mock @@ -42,7 +46,7 @@ public function rejectedPromiseShouldInvokeNewlyAddedCallback() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $adapter->reject($exception); @@ -67,7 +71,7 @@ public function shouldForwardUndefinedRejectionValue() ->method('__invoke') ->with(null); - $adapter->reject(new \Exception()); + $adapter->reject(new Exception()); $adapter->promise() ->then( $this->expectCallableNever(), @@ -95,7 +99,7 @@ public function shouldSwitchFromErrbacksToCallbacksWhenErrbackDoesNotExplicitlyP ->method('__invoke') ->with($this->identicalTo(2)); - $adapter->reject(new \Exception()); + $adapter->reject(new Exception()); $adapter->promise() ->then( $this->expectCallableNever(), @@ -120,12 +124,12 @@ public function shouldSwitchFromErrbacksToCallbacksWhenErrbackReturnsAResolution ->method('__invoke') ->with($this->identicalTo(2)); - $adapter->reject(new \Exception()); + $adapter->reject(new Exception()); $adapter->promise() ->then( $this->expectCallableNever(), function () { - return \React\Promise\resolve(2); + return resolve(2); } ) ->then( @@ -139,7 +143,7 @@ public function shouldPropagateRejectionsWhenErrbackThrows() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -153,7 +157,7 @@ public function shouldPropagateRejectionsWhenErrbackThrows() ->method('__invoke') ->with($this->identicalTo($exception)); - $adapter->reject(new \Exception()); + $adapter->reject(new Exception()); $adapter->promise() ->then( $this->expectCallableNever(), @@ -170,7 +174,7 @@ public function shouldPropagateRejectionsWhenErrbackReturnsARejection() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -178,12 +182,12 @@ public function shouldPropagateRejectionsWhenErrbackReturnsARejection() ->method('__invoke') ->with($this->identicalTo($exception)); - $adapter->reject(new \Exception()); + $adapter->reject(new Exception()); $adapter->promise() ->then( $this->expectCallableNever(), function () use ($exception) { - return \React\Promise\reject($exception); + return reject($exception); } ) ->then( @@ -197,7 +201,7 @@ public function doneShouldInvokeRejectionHandlerForRejectedPromise() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -206,7 +210,7 @@ public function doneShouldInvokeRejectionHandlerForRejectedPromise() ->with($this->identicalTo($exception)); $adapter->reject($exception); - $this->assertNull($adapter->promise()->done(null, $mock)); + self::assertNull($adapter->promise()->done(null, $mock)); } /** @test */ @@ -217,15 +221,15 @@ public function doneShouldTriggerFatalErrorExceptionThrownByRejectionHandlerForR $adapter = $this->getPromiseTestAdapter(); - $adapter->reject(new \Exception()); - $this->assertNull($adapter->promise()->done(null, function () { - throw new \Exception('Unhandled Rejection'); + $adapter->reject(new Exception()); + self::assertNull($adapter->promise()->done(null, function () { + throw new Exception('Unhandled Rejection'); })); $errors = $errorCollector->stop(); - $this->assertEquals(E_USER_ERROR, $errors[0]['errno']); - $this->assertContains('Unhandled Rejection', $errors[0]['errstr']); + self::assertEquals(E_USER_ERROR, $errors[0]['errno']); + self::assertContains('Unhandled Rejection', $errors[0]['errstr']); } /** @test */ @@ -236,15 +240,15 @@ public function doneShouldTriggerFatalErrorRejectionExceptionWhenRejectionHandle $adapter = $this->getPromiseTestAdapter(); - $adapter->reject(new \Exception()); - $this->assertNull($adapter->promise()->done(null, function () { - return \React\Promise\reject(new \Exception('Unhandled Rejection')); + $adapter->reject(new Exception()); + self::assertNull($adapter->promise()->done(null, function () { + return reject(new Exception('Unhandled Rejection')); })); $errors = $errorCollector->stop(); - $this->assertEquals(E_USER_ERROR, $errors[0]['errno']); - $this->assertContains('Unhandled Rejection', $errors[0]['errstr']); + self::assertEquals(E_USER_ERROR, $errors[0]['errno']); + self::assertContains('Unhandled Rejection', $errors[0]['errstr']); } /** @test */ @@ -255,15 +259,15 @@ public function doneShouldTriggerFatalErrorExceptionProvidedAsRejectionValueForR $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception('Unhandled Rejection'); + $exception = new Exception('Unhandled Rejection'); $adapter->reject($exception); - $this->assertNull($adapter->promise()->done()); + self::assertNull($adapter->promise()->done()); $errors = $errorCollector->stop(); - $this->assertEquals(E_USER_ERROR, $errors[0]['errno']); - $this->assertEquals((string) $exception, $errors[0]['errstr']); + self::assertEquals(E_USER_ERROR, $errors[0]['errno']); + self::assertEquals((string) $exception, $errors[0]['errstr']); } /** @test */ @@ -272,16 +276,16 @@ public function doneShouldTriggerFatalErrorWithDeepNestingPromiseChainsForReject $errorCollector = new ErrorCollector(); $errorCollector->start(); - $exception = new \Exception('UnhandledRejectionException'); + $exception = new Exception('UnhandledRejectionException'); $d = new Deferred(); $d->resolve(); - $result = \React\Promise\resolve(\React\Promise\resolve($d->promise()->then(function () use ($exception) { + $result = resolve(resolve($d->promise()->then(function () use ($exception) { $d = new Deferred(); $d->resolve(); - return \React\Promise\resolve($d->promise()->then(function () {}))->then( + return resolve($d->promise()->then(function () {}))->then( function () use ($exception) { throw $exception; } @@ -292,8 +296,8 @@ function () use ($exception) { $errors = $errorCollector->stop(); - $this->assertEquals(E_USER_ERROR, $errors[0]['errno']); - $this->assertEquals((string) $exception, $errors[0]['errstr']); + self::assertEquals(E_USER_ERROR, $errors[0]['errno']); + self::assertEquals((string) $exception, $errors[0]['errstr']); } /** @test */ @@ -301,8 +305,8 @@ public function doneShouldRecoverWhenRejectionHandlerCatchesExceptionForRejected { $adapter = $this->getPromiseTestAdapter(); - $adapter->reject(new \Exception('UnhandledRejectionException')); - $this->assertNull($adapter->promise()->done(null, function (\Exception $e) { + $adapter->reject(new Exception('UnhandledRejectionException')); + self::assertNull($adapter->promise()->done(null, function (Exception $e) { })); } @@ -312,7 +316,7 @@ public function otherwiseShouldInvokeRejectionHandlerForRejectedPromise() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -329,7 +333,7 @@ public function otherwiseShouldInvokeNonTypeHintedRejectionHandlerIfReasonIsAnEx { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -349,7 +353,7 @@ public function otherwiseShouldInvokeRejectionHandlerIfReasonMatchesTypehintForR { $adapter = $this->getPromiseTestAdapter(); - $exception = new \InvalidArgumentException(); + $exception = new InvalidArgumentException(); $mock = $this->createCallableMock(); $mock @@ -359,7 +363,7 @@ public function otherwiseShouldInvokeRejectionHandlerIfReasonMatchesTypehintForR $adapter->reject($exception); $adapter->promise() - ->otherwise(function (\InvalidArgumentException $reason) use ($mock) { + ->otherwise(function (InvalidArgumentException $reason) use ($mock) { $mock($reason); }); } @@ -369,13 +373,13 @@ public function otherwiseShouldNotInvokeRejectionHandlerIfReaonsDoesNotMatchType { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->expectCallableNever(); $adapter->reject($exception); $adapter->promise() - ->otherwise(function (\InvalidArgumentException $reason) use ($mock) { + ->otherwise(function (InvalidArgumentException $reason) use ($mock) { $mock($reason); }); } @@ -385,7 +389,7 @@ public function alwaysShouldNotSuppressRejectionForRejectedPromise() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -404,7 +408,7 @@ public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsANonPromiseFor { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -425,7 +429,7 @@ public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsAPromiseForRej { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -436,7 +440,7 @@ public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsAPromiseForRej $adapter->reject($exception); $adapter->promise() ->always(function () { - return \React\Promise\resolve(1); + return resolve(1); }) ->then(null, $mock); } @@ -446,8 +450,8 @@ public function alwaysShouldRejectWhenHandlerThrowsForRejectedPromise() { $adapter = $this->getPromiseTestAdapter(); - $exception1 = new \Exception(); - $exception2 = new \Exception(); + $exception1 = new Exception(); + $exception2 = new Exception(); $mock = $this->createCallableMock(); $mock @@ -468,8 +472,8 @@ public function alwaysShouldRejectWhenHandlerRejectsForRejectedPromise() { $adapter = $this->getPromiseTestAdapter(); - $exception1 = new \Exception(); - $exception2 = new \Exception(); + $exception1 = new Exception(); + $exception2 = new Exception(); $mock = $this->createCallableMock(); $mock @@ -480,7 +484,7 @@ public function alwaysShouldRejectWhenHandlerRejectsForRejectedPromise() $adapter->reject($exception1); $adapter->promise() ->always(function () use ($exception2) { - return \React\Promise\reject($exception2); + return reject($exception2); }) ->then(null, $mock); } @@ -490,9 +494,9 @@ public function cancelShouldReturnNullForRejectedPromise() { $adapter = $this->getPromiseTestAdapter(); - $adapter->reject(new \Exception()); + $adapter->reject(new Exception()); - $this->assertNull($adapter->promise()->cancel()); + self::assertNull($adapter->promise()->cancel()); } /** @test */ @@ -500,7 +504,7 @@ public function cancelShouldHaveNoEffectForRejectedPromise() { $adapter = $this->getPromiseTestAdapter($this->expectCallableNever()); - $adapter->reject(new \Exception()); + $adapter->reject(new Exception()); $adapter->promise()->cancel(); } diff --git a/tests/PromiseTest/PromiseSettledTestTrait.php b/tests/PromiseTest/PromiseSettledTestTrait.php index 0f01b609..ccca99ab 100644 --- a/tests/PromiseTest/PromiseSettledTestTrait.php +++ b/tests/PromiseTest/PromiseSettledTestTrait.php @@ -2,10 +2,13 @@ namespace React\Promise\PromiseTest; +use React\Promise\PromiseAdapter\PromiseAdapterInterface; +use React\Promise\PromiseInterface; + trait PromiseSettledTestTrait { /** - * @return \React\Promise\PromiseAdapter\PromiseAdapterInterface + * @return PromiseAdapterInterface */ abstract public function getPromiseTestAdapter(callable $canceller = null); @@ -15,7 +18,7 @@ public function thenShouldReturnAPromiseForSettledPromise() $adapter = $this->getPromiseTestAdapter(); $adapter->settle(); - $this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->then()); + self::assertInstanceOf(PromiseInterface::class, $adapter->promise()->then()); } /** @test */ @@ -24,7 +27,7 @@ public function thenShouldReturnAllowNullForSettledPromise() $adapter = $this->getPromiseTestAdapter(); $adapter->settle(); - $this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->then(null, null, null)); + self::assertInstanceOf(PromiseInterface::class, $adapter->promise()->then(null, null, null)); } /** @test */ @@ -34,7 +37,7 @@ public function cancelShouldReturnNullForSettledPromise() $adapter->settle(); - $this->assertNull($adapter->promise()->cancel()); + self::assertNull($adapter->promise()->cancel()); } /** @test */ @@ -53,7 +56,7 @@ public function doneShouldReturnNullForSettledPromise() $adapter = $this->getPromiseTestAdapter(); $adapter->settle(); - $this->assertNull($adapter->promise()->done(null, function () {})); + self::assertNull($adapter->promise()->done(null, function () {})); } /** @test */ @@ -62,7 +65,7 @@ public function doneShouldReturnAllowNullForSettledPromise() $adapter = $this->getPromiseTestAdapter(); $adapter->settle(); - $this->assertNull($adapter->promise()->done(null, function () {}, null)); + self::assertNull($adapter->promise()->done(null, function () {}, null)); } /** @test */ @@ -71,6 +74,6 @@ public function alwaysShouldReturnAPromiseForSettledPromise() $adapter = $this->getPromiseTestAdapter(); $adapter->settle(); - $this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->always(function () {})); + self::assertInstanceOf(PromiseInterface::class, $adapter->promise()->always(function () {})); } } diff --git a/tests/PromiseTest/RejectTestTrait.php b/tests/PromiseTest/RejectTestTrait.php index 9afd2be9..949426cd 100644 --- a/tests/PromiseTest/RejectTestTrait.php +++ b/tests/PromiseTest/RejectTestTrait.php @@ -2,13 +2,17 @@ namespace React\Promise\PromiseTest; +use Exception; use React\Promise; use React\Promise\Deferred; +use React\Promise\PromiseAdapter\PromiseAdapterInterface; +use function React\Promise\reject; +use function React\Promise\resolve; trait RejectTestTrait { /** - * @return \React\Promise\PromiseAdapter\PromiseAdapterInterface + * @return PromiseAdapterInterface */ abstract public function getPromiseTestAdapter(callable $canceller = null); @@ -17,7 +21,7 @@ public function rejectShouldRejectWithAnException() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -36,7 +40,7 @@ public function rejectShouldForwardReasonWhenCallbackIsNull() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -61,9 +65,9 @@ public function rejectShouldMakePromiseImmutable() { $adapter = $this->getPromiseTestAdapter(); - $exception1 = new \Exception(); - $exception2 = new \Exception(); - $exception3 = new \Exception(); + $exception1 = new Exception(); + $exception2 = new Exception(); + $exception3 = new Exception(); $mock = $this->createCallableMock(); $mock @@ -75,7 +79,7 @@ public function rejectShouldMakePromiseImmutable() ->then(null, function ($value) use ($exception3, $adapter) { $adapter->reject($exception3); - return Promise\reject($value); + return reject($value); }) ->then( $this->expectCallableNever(), @@ -91,7 +95,7 @@ public function rejectShouldInvokeOtherwiseHandler() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -110,7 +114,7 @@ public function doneShouldInvokeRejectionHandler() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -118,7 +122,7 @@ public function doneShouldInvokeRejectionHandler() ->method('__invoke') ->with($this->identicalTo($exception)); - $this->assertNull($adapter->promise()->done(null, $mock)); + self::assertNull($adapter->promise()->done(null, $mock)); $adapter->reject($exception); } @@ -130,15 +134,15 @@ public function doneShouldTriggerFatalErrorExceptionThrownByRejectionHandler() $adapter = $this->getPromiseTestAdapter(); - $this->assertNull($adapter->promise()->done(null, function () { - throw new \Exception('Unhandled Rejection'); + self::assertNull($adapter->promise()->done(null, function () { + throw new Exception('Unhandled Rejection'); })); - $adapter->reject(new \Exception()); + $adapter->reject(new Exception()); $errors = $errorCollector->stop(); - $this->assertEquals(E_USER_ERROR, $errors[0]['errno']); - $this->assertContains('Unhandled Rejection', $errors[0]['errstr']); + self::assertEquals(E_USER_ERROR, $errors[0]['errno']); + self::assertContains('Unhandled Rejection', $errors[0]['errstr']); } /** @test */ @@ -149,15 +153,15 @@ public function doneShouldTriggerFatalErrorRejectionExceptionWhenRejectionHandle $adapter = $this->getPromiseTestAdapter(); - $this->assertNull($adapter->promise()->done(null, function () { - return \React\Promise\reject(new \Exception('Unhandled Rejection')); + self::assertNull($adapter->promise()->done(null, function () { + return reject(new Exception('Unhandled Rejection')); })); - $adapter->reject(new \Exception()); + $adapter->reject(new Exception()); $errors = $errorCollector->stop(); - $this->assertEquals(E_USER_ERROR, $errors[0]['errno']); - $this->assertContains('Unhandled Rejection', $errors[0]['errstr']); + self::assertEquals(E_USER_ERROR, $errors[0]['errno']); + self::assertContains('Unhandled Rejection', $errors[0]['errstr']); } /** @test */ @@ -171,16 +175,16 @@ public function doneShouldTriggerFatalErrorUnhandledRejectionExceptionWhenReject $d = new Deferred(); $promise = $d->promise(); - $this->assertNull($adapter->promise()->done(null, function () use ($promise) { + self::assertNull($adapter->promise()->done(null, function () use ($promise) { return $promise; })); - $adapter->reject(new \Exception()); - $d->reject(new \Exception('Unhandled Rejection')); + $adapter->reject(new Exception()); + $d->reject(new Exception('Unhandled Rejection')); $errors = $errorCollector->stop(); - $this->assertEquals(E_USER_ERROR, $errors[0]['errno']); - $this->assertContains('Unhandled Rejection', $errors[0]['errstr']); + self::assertEquals(E_USER_ERROR, $errors[0]['errno']); + self::assertContains('Unhandled Rejection', $errors[0]['errstr']); } /** @test */ @@ -191,13 +195,13 @@ public function doneShouldTriggerFatalErrorExceptionProvidedAsRejectionValue() $adapter = $this->getPromiseTestAdapter(); - $this->assertNull($adapter->promise()->done()); - $adapter->reject(new \Exception('Unhandled Rejection')); + self::assertNull($adapter->promise()->done()); + $adapter->reject(new Exception('Unhandled Rejection')); $errors = $errorCollector->stop(); - $this->assertEquals(E_USER_ERROR, $errors[0]['errno']); - $this->assertContains('Unhandled Rejection', $errors[0]['errstr']); + self::assertEquals(E_USER_ERROR, $errors[0]['errno']); + self::assertContains('Unhandled Rejection', $errors[0]['errstr']); } /** @test */ @@ -206,15 +210,15 @@ public function doneShouldTriggerFatalErrorWithDeepNestingPromiseChains() $errorCollector = new Promise\ErrorCollector(); $errorCollector->start(); - $exception = new \Exception('Unhandled Rejection'); + $exception = new Exception('Unhandled Rejection'); $d = new Deferred(); - $result = \React\Promise\resolve(\React\Promise\resolve($d->promise()->then(function () use ($exception) { + $result = resolve(resolve($d->promise()->then(function () use ($exception) { $d = new Deferred(); $d->resolve(); - return \React\Promise\resolve($d->promise()->then(function () {}))->then( + return resolve($d->promise()->then(function () {}))->then( function () use ($exception) { throw $exception; } @@ -227,8 +231,8 @@ function () use ($exception) { $errors = $errorCollector->stop(); - $this->assertEquals(E_USER_ERROR, $errors[0]['errno']); - $this->assertEquals((string) $exception, $errors[0]['errstr']); + self::assertEquals(E_USER_ERROR, $errors[0]['errno']); + self::assertEquals((string) $exception, $errors[0]['errstr']); } /** @test */ @@ -236,10 +240,10 @@ public function doneShouldRecoverWhenRejectionHandlerCatchesException() { $adapter = $this->getPromiseTestAdapter(); - $this->assertNull($adapter->promise()->done(null, function (\Exception $e) { + self::assertNull($adapter->promise()->done(null, function (Exception $e) { })); - $adapter->reject(new \Exception('UnhandledRejectionException')); + $adapter->reject(new Exception('UnhandledRejectionException')); } /** @test */ @@ -247,7 +251,7 @@ public function alwaysShouldNotSuppressRejection() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -267,7 +271,7 @@ public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsANonPromise() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -289,7 +293,7 @@ public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsAPromise() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -299,7 +303,7 @@ public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsAPromise() $adapter->promise() ->always(function () { - return \React\Promise\resolve(1); + return resolve(1); }) ->then(null, $mock); @@ -311,7 +315,7 @@ public function alwaysShouldRejectWhenHandlerThrowsForRejection() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -333,7 +337,7 @@ public function alwaysShouldRejectWhenHandlerRejectsForRejection() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -343,7 +347,7 @@ public function alwaysShouldRejectWhenHandlerRejectsForRejection() $adapter->promise() ->always(function () use ($exception) { - return \React\Promise\reject($exception); + return reject($exception); }) ->then(null, $mock); diff --git a/tests/PromiseTest/ResolveTestTrait.php b/tests/PromiseTest/ResolveTestTrait.php index bfe03738..d4694344 100644 --- a/tests/PromiseTest/ResolveTestTrait.php +++ b/tests/PromiseTest/ResolveTestTrait.php @@ -2,12 +2,18 @@ namespace React\Promise\PromiseTest; +use Exception; +use LogicException; use React\Promise; +use React\Promise\PromiseAdapter\PromiseAdapterInterface; +use stdClass; +use function React\Promise\reject; +use function React\Promise\resolve; trait ResolveTestTrait { /** - * @return \React\Promise\PromiseAdapter\PromiseAdapterInterface + * @return PromiseAdapterInterface */ abstract public function getPromiseTestAdapter(callable $canceller = null); @@ -42,7 +48,7 @@ public function resolveShouldResolveWithPromisedValue() $adapter->promise() ->then($mock); - $adapter->resolve(Promise\resolve(1)); + $adapter->resolve(resolve(1)); } /** @test */ @@ -50,7 +56,7 @@ public function resolveShouldRejectWhenResolvedWithRejectedPromise() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -61,7 +67,7 @@ public function resolveShouldRejectWhenResolvedWithRejectedPromise() $adapter->promise() ->then($this->expectCallableNever(), $mock); - $adapter->resolve(Promise\reject($exception)); + $adapter->resolve(reject($exception)); } /** @test */ @@ -125,7 +131,7 @@ public function resolveShouldRejectWhenResolvedWithItself() $mock ->expects($this->once()) ->method('__invoke') - ->with(new \LogicException('Cannot resolve a promise with itself.')); + ->with(new LogicException('Cannot resolve a promise with itself.')); $adapter->promise() ->then( @@ -148,7 +154,7 @@ public function resolveShouldRejectWhenResolvedWithAPromiseWhichFollowsItself() $mock ->expects($this->once()) ->method('__invoke') - ->with(new \LogicException('Cannot resolve a promise with itself.')); + ->with(new LogicException('Cannot resolve a promise with itself.')); $promise1 = $adapter1->promise(); @@ -174,7 +180,7 @@ public function doneShouldInvokeFulfillmentHandler() ->method('__invoke') ->with($this->identicalTo(1)); - $this->assertNull($adapter->promise()->done($mock)); + self::assertNull($adapter->promise()->done($mock)); $adapter->resolve(1); } @@ -186,15 +192,15 @@ public function doneShouldTriggerFatalErrorExceptionThrownFulfillmentHandler() $adapter = $this->getPromiseTestAdapter(); - $this->assertNull($adapter->promise()->done(function () { - throw new \Exception('Unhandled Rejection'); + self::assertNull($adapter->promise()->done(function () { + throw new Exception('Unhandled Rejection'); })); $adapter->resolve(1); $errors = $errorCollector->stop(); - $this->assertEquals(E_USER_ERROR, $errors[0]['errno']); - $this->assertContains('Unhandled Rejection', $errors[0]['errstr']); + self::assertEquals(E_USER_ERROR, $errors[0]['errno']); + self::assertContains('Unhandled Rejection', $errors[0]['errstr']); } /** @test */ @@ -205,15 +211,15 @@ public function doneShouldTriggerFatalErrorUnhandledRejectionExceptionWhenFulfil $adapter = $this->getPromiseTestAdapter(); - $this->assertNull($adapter->promise()->done(function () { - return \React\Promise\reject(new \Exception('Unhandled Rejection')); + self::assertNull($adapter->promise()->done(function () { + return reject(new Exception('Unhandled Rejection')); })); $adapter->resolve(1); $errors = $errorCollector->stop(); - $this->assertEquals(E_USER_ERROR, $errors[0]['errno']); - $this->assertContains('Unhandled Rejection', $errors[0]['errstr']); + self::assertEquals(E_USER_ERROR, $errors[0]['errno']); + self::assertContains('Unhandled Rejection', $errors[0]['errstr']); } /** @test */ @@ -221,7 +227,7 @@ public function alwaysShouldNotSuppressValue() { $adapter = $this->getPromiseTestAdapter(); - $value = new \stdClass(); + $value = new stdClass(); $mock = $this->createCallableMock(); $mock @@ -241,7 +247,7 @@ public function alwaysShouldNotSuppressValueWhenHandlerReturnsANonPromise() { $adapter = $this->getPromiseTestAdapter(); - $value = new \stdClass(); + $value = new stdClass(); $mock = $this->createCallableMock(); $mock @@ -263,7 +269,7 @@ public function alwaysShouldNotSuppressValueWhenHandlerReturnsAPromise() { $adapter = $this->getPromiseTestAdapter(); - $value = new \stdClass(); + $value = new stdClass(); $mock = $this->createCallableMock(); $mock @@ -273,7 +279,7 @@ public function alwaysShouldNotSuppressValueWhenHandlerReturnsAPromise() $adapter->promise() ->always(function () { - return \React\Promise\resolve(1); + return resolve(1); }) ->then($mock); @@ -285,7 +291,7 @@ public function alwaysShouldRejectWhenHandlerThrowsForFulfillment() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -307,7 +313,7 @@ public function alwaysShouldRejectWhenHandlerRejectsForFulfillment() { $adapter = $this->getPromiseTestAdapter(); - $exception = new \Exception(); + $exception = new Exception(); $mock = $this->createCallableMock(); $mock @@ -317,7 +323,7 @@ public function alwaysShouldRejectWhenHandlerRejectsForFulfillment() $adapter->promise() ->always(function () use ($exception) { - return \React\Promise\reject($exception); + return reject($exception); }) ->then(null, $mock); diff --git a/tests/RejectedPromiseTest.php b/tests/RejectedPromiseTest.php index 8cffe12f..e214cb0a 100644 --- a/tests/RejectedPromiseTest.php +++ b/tests/RejectedPromiseTest.php @@ -2,6 +2,8 @@ namespace React\Promise; +use Exception; +use LogicException; use React\Promise\PromiseAdapter\CallbackPromiseAdapter; class RejectedPromiseTest extends TestCase @@ -16,13 +18,13 @@ public function getPromiseTestAdapter(callable $canceller = null) return new CallbackPromiseAdapter([ 'promise' => function () use (&$promise) { if (!$promise) { - throw new \LogicException('RejectedPromise must be rejected before obtaining the promise'); + throw new LogicException('RejectedPromise must be rejected before obtaining the promise'); } return $promise; }, 'resolve' => function () { - throw new \LogicException('You cannot call resolve() for React\Promise\RejectedPromise'); + throw new LogicException('You cannot call resolve() for React\Promise\RejectedPromise'); }, 'reject' => function ($reason = null) use (&$promise) { if (!$promise) { @@ -31,8 +33,8 @@ public function getPromiseTestAdapter(callable $canceller = null) }, 'settle' => function ($reason = null) use (&$promise) { if (!$promise) { - if (!$reason instanceof \Exception) { - $reason = new \Exception($reason); + if (!$reason instanceof Exception) { + $reason = new Exception($reason); } $promise = new RejectedPromise($reason); diff --git a/tests/TestCase.php b/tests/TestCase.php index b774c86c..50f701a0 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,34 +3,35 @@ namespace React\Promise; use PHPUnit\Framework\TestCase as BaseTestCase; +use React\Promise\Stub\CallableStub; class TestCase extends BaseTestCase { - public function expectCallableExactly($amount) + public function expectCallableExactly($amount): callable { $mock = $this->createCallableMock(); $mock - ->expects($this->exactly($amount)) + ->expects(self::exactly($amount)) ->method('__invoke'); return $mock; } - public function expectCallableOnce() + public function expectCallableOnce(): callable { $mock = $this->createCallableMock(); $mock - ->expects($this->once()) + ->expects(self::once()) ->method('__invoke'); return $mock; } - public function expectCallableNever() + public function expectCallableNever(): callable { $mock = $this->createCallableMock(); $mock - ->expects($this->never()) + ->expects(self::never()) ->method('__invoke'); return $mock; @@ -39,7 +40,7 @@ public function expectCallableNever() public function createCallableMock() { return $this - ->getMockBuilder('React\Promise\Stub\CallableStub') + ->getMockBuilder(CallableStub::class) ->getMock(); } }