Skip to content

Commit

Permalink
added test
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-yulin committed Mar 6, 2019
1 parent 6c8448a commit 3a0610d
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/PromiseA.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ final class PromiseA implements PromiseInterface
*/
public function __construct(callable $promise)
{
// @codeCoverageIgnoreStart
if (PHP_SAPI !== 'cli' || !extension_loaded('swoole')) {
throw new Exception\RuntimeException(
'PromiseCo MUST running only in CLI mode with swoole extension'
);
}
// @codeCoverageIgnoreEnd
$this->sequenceSet = new Channel();
Coroutine::create(function (callable $promise) {
try {
Expand Down
62 changes: 61 additions & 1 deletion test/PromiseATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace Streamcommon\Test\Promise;

use PHPUnit\Framework\TestCase;
use Streamcommon\Promise\PromiseA;
use Streamcommon\Promise\{PromiseA, PromiseInterface};
use Streamcommon\Promise\Exception\RuntimeException;

/**
Expand Down Expand Up @@ -74,4 +74,64 @@ public function testPromiseThrow(): void
$this->assertInstanceOf(RuntimeException::class, $value);
});
}

/**
* Test sub promise
*/
public function testSubPromise(): void
{
$promise = PromiseA::create(function (callable $resolve) {
$promise = PromiseA::create(function (callable $resolve) {
$resolve(41);
});
$promise->then(function ($value) use ($resolve) {
$resolve($value);
});
});
$promise->then(function ($value) {
return PromiseA::create(function (callable $resolve) use ($value) {
$resolve($value + 1);
});
});
$promise->then(function ($value) {
return PromiseA::create(function (callable $resolve) use ($value) {
$resolve($value + 1);
})->then(function ($value) {
return $value + 1;
});
});
$promise->then(function ($value) {
$this->assertEquals(44, $value);
});
}

/**
* Test sub promise instance exception
*/
public function testSubPromiseException(): void
{
$promise = PromiseA::create(function (callable $resolve) {
$resolve(new class implements PromiseInterface {
public function then(?callable $onFulfilled = null, ?callable $onRejected = null): PromiseInterface
{
}

public static function create(callable $promise): PromiseInterface
{
}

public static function resolve($value): PromiseInterface
{
}

public static function reject($value): PromiseInterface
{
}

});
});
$promise->then(null, function ($value) {
$this->assertInstanceOf(RuntimeException::class, $value);
});
}
}
65 changes: 64 additions & 1 deletion test/PromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace Streamcommon\Test\Promise;

use PHPUnit\Framework\TestCase;
use Streamcommon\Promise\Promise;
use Streamcommon\Promise\{Promise, PromiseInterface};
use Streamcommon\Promise\Exception\RuntimeException;

/**
Expand Down Expand Up @@ -78,4 +78,67 @@ public function testPromiseThrow(): void
});
$promise->wait();
}

/**
* Test sub promise
*/
public function testSubPromise(): void
{
$promise = Promise::create(function (callable $resolve) {
$promise = Promise::create(function (callable $resolve) {
$resolve(41);
});
$promise->then(function ($value) use ($resolve) {
$resolve($value);
});
$promise->wait();
});
$promise->then(function ($value) {
return Promise::create(function (callable $resolve) use ($value) {
$resolve($value + 1);
});
});
$promise->then(function ($value) {
return Promise::create(function (callable $resolve) use ($value) {
$resolve($value + 1);
})->then(function ($value) {
return $value + 1;
});
});
$promise->then(function ($value) {
$this->assertEquals(44, $value);
});
$promise->wait();
}

/**
* Test sub promise instance exception
*/
public function testSubPromiseException(): void
{
$promise = Promise::create(function (callable $resolve) {
$resolve(new class implements PromiseInterface {
public function then(?callable $onFulfilled = null, ?callable $onRejected = null): PromiseInterface
{
}

public static function create(callable $promise): PromiseInterface
{
}

public static function resolve($value): PromiseInterface
{
}

public static function reject($value): PromiseInterface
{
}

});
});
$promise->then(null, function ($value) {
$this->assertInstanceOf(RuntimeException::class, $value);
});
$promise->wait();
}
}

0 comments on commit 3a0610d

Please sign in to comment.