-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add missing typehint * Add missing typehint * Add missing typehint * Update dependencies * Improve tests for FulfilledPromise * Complete testing of FulfilledPromise class * Test Waitable
- Loading branch information
Showing
9 changed files
with
186 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
namespace Gt\Promise; | ||
|
||
class FulfilledValueNotConcreteException extends PromiseException {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
namespace Gt\Promise; | ||
|
||
class PromiseWaitTaskNotSetException extends PromiseException {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
namespace Gt\Promise\Test; | ||
|
||
use Closure; | ||
use Exception; | ||
use Gt\Promise\FulfilledPromise; | ||
use Gt\Promise\FulfilledValueNotConcreteException; | ||
use Gt\Promise\Promise; | ||
use Http\Promise\Promise as HttpPromiseInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use TypeError; | ||
|
||
class FulfilledPromiseTest extends TestCase { | ||
public function testCanNotResolveWithPromise() { | ||
$callback = fn()=>true; | ||
$promise = new Promise($callback); | ||
self::expectException(FulfilledValueNotConcreteException::class); | ||
new FulfilledPromise($promise); | ||
} | ||
|
||
public function testDoNothingWithNullComplete() { | ||
$message = "Test message"; | ||
$sut = new FulfilledPromise($message); | ||
$exception = null; | ||
try { | ||
$sut->complete(); | ||
} | ||
catch(Exception $exception) {} | ||
|
||
self::assertNull($exception); | ||
} | ||
|
||
public function testCompleteWithPromise() { | ||
$message = "Test message"; | ||
|
||
$promise = self::createMock(Promise::class); | ||
$promise->expects(self::once()) | ||
->method("complete"); | ||
$callback = fn() => $promise; | ||
|
||
$sut = new FulfilledPromise($message); | ||
$sut->complete($callback); | ||
} | ||
|
||
public function testCatch() { | ||
// Catch does nothing because a FulfilledPromise is already resolved. | ||
$callCount = 0; | ||
$callback = function() use(&$callCount) { | ||
$callCount++; | ||
}; | ||
$sut = new FulfilledPromise(true); | ||
self::assertSame($sut, $sut->catch($callback)); | ||
self::assertEquals(0, $callCount); | ||
} | ||
|
||
public function testFinally() { | ||
$callCount = 0; | ||
$callback = function() use(&$callCount) { | ||
$callCount++; | ||
}; | ||
|
||
$message = "Test message"; | ||
$sut = new FulfilledPromise($message); | ||
$sut->finally($callback); | ||
self::assertEquals(1, $callCount); | ||
} | ||
|
||
public function testCompleteWithInvalidCallback() { | ||
$callback = function(string $requiredStringParameter) {}; | ||
|
||
$sut = new FulfilledPromise("Callback should not have a required string parameter!"); | ||
$reasonArray = []; | ||
$sut->finally($callback)->catch(function(\Throwable $reason) use (&$reasonArray) { | ||
array_push($reasonArray, $reason); | ||
}); | ||
self::assertCount(1, $reasonArray); | ||
self::assertInstanceOf(TypeError::class, $reasonArray[0]); | ||
} | ||
|
||
public function testGetState() { | ||
$sut = new FulfilledPromise(); | ||
self::assertEquals( | ||
HttpPromiseInterface::FULFILLED, | ||
$sut->getState() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
namespace Gt\Promise\Test; | ||
|
||
use Gt\Promise\Promise; | ||
use Gt\Promise\PromiseWaitTaskNotSetException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class WaitableTest extends TestCase { | ||
public function testWait() { | ||
$callCount = 0; | ||
$resolveCallback = null; | ||
$executor = function(callable $resolve, callable $reject) use(&$resolveCallback):void { | ||
$resolveCallback = $resolve; | ||
}; | ||
$resolvedValue = "Done!"; | ||
$sut = new Promise($executor); | ||
|
||
$waitTask = function() use(&$callCount, $resolveCallback, $resolvedValue) { | ||
if($callCount >= 10) { | ||
call_user_func($resolveCallback, $resolvedValue); | ||
} | ||
else { | ||
$callCount++; | ||
} | ||
}; | ||
|
||
$sut->setWaitTask($waitTask); | ||
self::assertEquals($resolvedValue, $sut->wait()); | ||
self::assertEquals(10, $callCount); | ||
} | ||
|
||
public function testWaitNotUnwrapped() { | ||
$callCount = 0; | ||
$resolveCallback = null; | ||
$executor = function(callable $resolve, callable $reject) use(&$resolveCallback):void { | ||
$resolveCallback = $resolve; | ||
}; | ||
$resolvedValue = "Done!"; | ||
$sut = new Promise($executor); | ||
|
||
$waitTask = function() use(&$callCount, $resolveCallback, $resolvedValue) { | ||
if($callCount >= 10) { | ||
call_user_func($resolveCallback, $resolvedValue); | ||
} | ||
else { | ||
$callCount++; | ||
} | ||
}; | ||
|
||
$sut->setWaitTask($waitTask); | ||
self::assertNull($sut->wait(false)); | ||
self::assertEquals(10, $callCount); | ||
} | ||
|
||
public function testWaitWithNoWaitTask() { | ||
$executor = function(callable $resolve, callable $reject) use(&$resolveCallback):void { | ||
$resolveCallback = $resolve; | ||
}; | ||
$sut = new Promise($executor);; | ||
self::expectException(PromiseWaitTaskNotSetException::class); | ||
$sut->wait(); | ||
} | ||
} |