1
0
mirror of https://github.com/danog/amp.git synced 2024-12-04 18:38:17 +01:00
amp/test/PipeTest.php

84 lines
2.0 KiB
PHP
Raw Normal View History

2016-08-18 05:25:54 +02:00
<?php declare(strict_types = 1);
2016-07-12 18:20:06 +02:00
namespace Amp\Test;
use Amp;
2016-11-14 20:59:21 +01:00
use Amp\{ Failure, Success };
use Interop\Async\Promise;
2016-07-12 18:20:06 +02:00
class PipeTest extends \PHPUnit_Framework_TestCase {
2016-11-14 20:59:21 +01:00
public function testSuccessfulPromise() {
2016-07-12 18:20:06 +02:00
$invoked = false;
$callback = function ($value) use (&$invoked) {
$invoked = true;
return $value + 1;
};
$value = 1;
2016-11-14 20:59:21 +01:00
$promise = new Success($value);
2016-07-12 18:20:06 +02:00
2016-11-14 20:59:21 +01:00
$promise = Amp\pipe($promise, $callback);
$this->assertInstanceOf(Promise::class, $promise);
2016-07-12 18:20:06 +02:00
$callback = function ($exception, $value) use (&$result) {
$result = $value;
};
2016-11-14 20:59:21 +01:00
$promise->when($callback);
2016-07-12 18:20:06 +02:00
$this->assertTrue($invoked);
$this->assertSame($value + 1, $result);
}
2016-11-14 20:59:21 +01:00
public function testFailedPromise() {
2016-07-12 18:20:06 +02:00
$invoked = false;
$callback = function ($value) use (&$invoked) {
$invoked = true;
return $value + 1;
};
$exception = new \Exception;
2016-11-14 20:59:21 +01:00
$promise = new Failure($exception);
2016-07-12 18:20:06 +02:00
2016-11-14 20:59:21 +01:00
$promise = Amp\pipe($promise, $callback);
$this->assertInstanceOf(Promise::class, $promise);
2016-07-12 18:20:06 +02:00
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
2016-11-14 20:59:21 +01:00
$promise->when($callback);
2016-07-12 18:20:06 +02:00
$this->assertFalse($invoked);
$this->assertSame($exception, $reason);
}
/**
2016-11-14 20:59:21 +01:00
* @depends testSuccessfulPromise
2016-07-12 18:20:06 +02:00
*/
public function testCallbackThrowing() {
$exception = new \Exception;
$callback = function ($value) use (&$invoked, $exception) {
$invoked = true;
throw $exception;
};
$value = 1;
2016-11-14 20:59:21 +01:00
$promise = new Success($value);
2016-07-12 18:20:06 +02:00
2016-11-14 20:59:21 +01:00
$promise = Amp\pipe($promise, $callback);
2016-07-12 18:20:06 +02:00
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
2016-11-14 20:59:21 +01:00
$promise->when($callback);
2016-07-12 18:20:06 +02:00
$this->assertTrue($invoked);
$this->assertSame($exception, $reason);
}
}