-
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
352 additions
and
1 deletion.
There are no files selected for viewing
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,39 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Amp\Test; | ||
|
||
class CallableMaker { | ||
use \Amp\CallableMaker { | ||
callableFromInstanceMethod as public; | ||
callableFromStaticMethod as public; | ||
} | ||
|
||
public function instanceMethod() { | ||
return __METHOD__; | ||
} | ||
|
||
public static function staticMethod() { | ||
return __METHOD__; | ||
} | ||
} | ||
|
||
class CallableMakerTest extends \PHPUnit_Framework_TestCase { | ||
/** @var \Amp\Test\CallableMaker */ | ||
private $maker; | ||
|
||
public function setUp() { | ||
$this->maker = new CallableMaker; | ||
} | ||
|
||
public function testCallableFromInstanceMethod() { | ||
$callable = $this->maker->callableFromInstanceMethod("instanceMethod"); | ||
$this->assertInternalType("callable", $callable); | ||
$this->assertSame(\sprintf("%s::%s", CallableMaker::class, "instanceMethod"), $callable()); | ||
} | ||
|
||
public function testCallableFromStaticMethod() { | ||
$callable = $this->maker->callableFromInstanceMethod("staticMethod"); | ||
$this->assertInternalType("callable", $callable); | ||
$this->assertSame(\sprintf("%s::%s", CallableMaker::class, "staticMethod"), $callable()); | ||
} | ||
} |
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,247 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Amp\Test; | ||
|
||
use Amp\{ Deferred, Failure, Success }; | ||
use Interop\Async\{ Loop, Promise }; | ||
|
||
class Producer { | ||
use \Amp\Internal\Producer { | ||
emit as public; | ||
resolve as public; | ||
fail as public; | ||
} | ||
} | ||
|
||
class ProducerTest extends \PHPUnit_Framework_TestCase { | ||
/** @var \Amp\Test\Producer */ | ||
private $producer; | ||
|
||
public function setUp() { | ||
$this->producer = new Producer; | ||
} | ||
|
||
public function testEmit() { | ||
$invoked = false; | ||
$value = 1; | ||
|
||
$callback = function ($emitted) use (&$invoked, $value) { | ||
$invoked = true; | ||
$this->assertSame($emitted, $value); | ||
}; | ||
|
||
$this->producer->subscribe($callback); | ||
$promise = $this->producer->emit($value); | ||
|
||
$this->assertInstanceOf(Promise::class, $promise); | ||
$this->assertTrue($invoked); | ||
} | ||
|
||
/** | ||
* @depends testEmit | ||
*/ | ||
public function testEmitSuccessfulPromise() { | ||
$invoked = false; | ||
$value = 1; | ||
$promise = new Success($value); | ||
|
||
$callback = function ($emitted) use (&$invoked, $value) { | ||
$invoked = true; | ||
$this->assertSame($emitted, $value); | ||
}; | ||
|
||
$this->producer->subscribe($callback); | ||
$this->producer->emit($promise); | ||
|
||
$this->assertTrue($invoked); | ||
} | ||
|
||
/** | ||
* @depends testEmit | ||
*/ | ||
public function testEmitFailedPromise() { | ||
$invoked = false; | ||
$exception = new \Exception; | ||
$promise = new Failure($exception); | ||
|
||
$callback = function ($emitted) use (&$invoked) { | ||
$invoked = true; | ||
}; | ||
|
||
$this->producer->subscribe($callback); | ||
$this->producer->emit($promise); | ||
|
||
$this->assertFalse($invoked); | ||
|
||
$this->producer->when(function ($exception) use (&$invoked, &$reason) { | ||
$invoked = true; | ||
$reason = $exception; | ||
}); | ||
|
||
$this->assertTrue($invoked); | ||
$this->assertSame($exception, $reason); | ||
} | ||
|
||
/** | ||
* @depends testEmit | ||
*/ | ||
public function testEmitPendingPromise() { | ||
$invoked = false; | ||
$value = 1; | ||
$deferred = new Deferred; | ||
|
||
$callback = function ($emitted) use (&$invoked) { | ||
$invoked = true; | ||
}; | ||
|
||
$callback = function ($emitted) use (&$invoked, $value) { | ||
$invoked = true; | ||
$this->assertSame($emitted, $value); | ||
}; | ||
|
||
$this->producer->subscribe($callback); | ||
$this->producer->emit($deferred->promise()); | ||
|
||
$this->assertFalse($invoked); | ||
|
||
$deferred->resolve($value); | ||
|
||
$this->assertTrue($invoked); | ||
} | ||
|
||
/** | ||
* @depends testEmit | ||
*/ | ||
public function testEmitPendingPromiseThenNonPromise() { | ||
$invoked = false; | ||
$deferred = new Deferred; | ||
|
||
$callback = function ($emitted) use (&$invoked, &$result) { | ||
$invoked = true; | ||
$result = $emitted; | ||
}; | ||
|
||
$this->producer->subscribe($callback); | ||
$this->producer->emit($deferred->promise()); | ||
|
||
$this->assertFalse($invoked); | ||
|
||
$this->producer->emit(2); | ||
$this->assertTrue($invoked); | ||
$this->assertSame(2, $result); | ||
|
||
$deferred->resolve(1); | ||
$this->assertSame(1, $result); | ||
} | ||
|
||
/** | ||
* @depends testEmit | ||
* @expectedException \Error | ||
* @expectedExceptionMessage The observable has been resolved; cannot emit more values | ||
*/ | ||
public function testEmitAfterResolve() { | ||
$this->producer->resolve(); | ||
$this->producer->emit(1); | ||
} | ||
|
||
/** | ||
* @depends testEmit | ||
* @expectedException \Error | ||
* @expectedExceptionMessage The observable was resolved before the promise result could be emitted | ||
*/ | ||
public function testEmitPendingPromiseThenResolve() { | ||
$invoked = false; | ||
$deferred = new Deferred; | ||
|
||
$promise = $this->producer->emit($deferred->promise()); | ||
|
||
$this->producer->resolve(); | ||
$deferred->resolve(); | ||
|
||
$promise->when(function ($exception) use (&$invoked, &$reason) { | ||
$invoked = true; | ||
$reason = $exception; | ||
}); | ||
|
||
$this->assertTrue($invoked); | ||
throw $reason; | ||
} | ||
|
||
/** | ||
* @depends testEmit | ||
* @expectedException \Error | ||
* @expectedExceptionMessage The observable was resolved before the promise result could be emitted | ||
*/ | ||
public function testEmitPendingPromiseThenFail() { | ||
$invoked = false; | ||
$deferred = new Deferred; | ||
|
||
$promise = $this->producer->emit($deferred->promise()); | ||
|
||
$this->producer->resolve(); | ||
$deferred->fail(new \Exception); | ||
|
||
$promise->when(function ($exception) use (&$invoked, &$reason) { | ||
$invoked = true; | ||
$reason = $exception; | ||
}); | ||
|
||
$this->assertTrue($invoked); | ||
throw $reason; | ||
} | ||
|
||
public function testSubscriberThrows() { | ||
$exception = new \Exception; | ||
|
||
try { | ||
Loop::execute(function () use ($exception) { | ||
$this->producer->subscribe(function () use ($exception) { | ||
throw $exception; | ||
}); | ||
|
||
$this->producer->emit(1); | ||
}); | ||
} catch (\Exception $caught) { | ||
$this->assertSame($exception, $caught); | ||
} | ||
} | ||
|
||
public function testSubscriberReturnsSuccessfulPromise() { | ||
$invoked = true; | ||
$value = 1; | ||
$promise = new Success($value); | ||
|
||
$this->producer->subscribe(function () use ($promise) { | ||
return $promise; | ||
}); | ||
|
||
$promise = $this->producer->emit(1); | ||
$promise->when(function () use (&$invoked) { | ||
$invoked = true; | ||
}); | ||
|
||
$this->assertTrue($invoked); | ||
} | ||
|
||
public function testSubscriberReturnsFailedPromise() { | ||
$exception = new \Exception; | ||
$promise = new Failure($exception); | ||
|
||
try { | ||
Loop::execute(function () use ($exception, $promise) { | ||
$this->producer->subscribe(function () use ($promise) { | ||
return $promise; | ||
}); | ||
|
||
$promise = $this->producer->emit(1); | ||
$promise->when(function () use (&$invoked) { | ||
$invoked = true; | ||
}); | ||
|
||
$this->assertTrue($invoked); | ||
}); | ||
} catch (\Exception $caught) { | ||
$this->assertSame($exception, $caught); | ||
} | ||
} | ||
} |