2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-12-16 00:28:22 +01:00
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
|
|
|
use Amp;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Deferred;
|
|
|
|
use Amp\Failure;
|
|
|
|
use Amp\Loop;
|
|
|
|
use Amp\Pause;
|
|
|
|
use Amp\Success;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2017-03-11 14:57:03 +01:00
|
|
|
class MapTest extends \PHPUnit\Framework\TestCase {
|
2016-12-16 00:28:22 +01:00
|
|
|
public function testEmptyArray() {
|
|
|
|
$values = [];
|
|
|
|
$invoked = false;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$result = Amp\map(function () use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
}, $values);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$this->assertSame($result, $values);
|
|
|
|
$this->assertFalse($invoked);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
public function testSuccessfulPromisesArray() {
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () {
|
2016-12-16 00:28:22 +01:00
|
|
|
$promises = [new Success(1), new Success(2), new Success(3)];;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$count = 0;
|
|
|
|
$callback = function ($value) use (&$count) {
|
|
|
|
++$count;
|
|
|
|
return $value - 1;
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$result = Amp\map($callback, $promises);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$this->assertTrue(\is_array($result));
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
foreach ($result as $key => $promise) {
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
|
|
|
$this->assertSame($key, Amp\wait($promise));
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$this->assertSame(\count($promises), $count);
|
2017-03-10 21:31:57 +01:00
|
|
|
});
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
public function testPendingPromisesArray() {
|
|
|
|
$deferreds = [
|
|
|
|
new Deferred,
|
|
|
|
new Deferred,
|
|
|
|
new Deferred,
|
|
|
|
];
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$promises = \array_map(function (Deferred $deferred) {
|
|
|
|
return $deferred->promise();
|
|
|
|
}, $deferreds);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$count = 0;
|
|
|
|
$callback = function ($value) use (&$count) {
|
|
|
|
++$count;
|
|
|
|
return $value - 1;
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$result = Amp\map($callback, $promises);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$this->assertTrue(\is_array($result));
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
foreach ($deferreds as $key => $deferred) {
|
|
|
|
$deferred->resolve($key + 1);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
foreach ($result as $key => $promise) {
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
|
|
|
$this->assertSame($key, Amp\wait($promise));
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$this->assertSame(\count($promises), $count);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
public function testFailedPromisesArray() {
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () {
|
2016-12-16 00:28:22 +01:00
|
|
|
$exception = new \Exception;
|
|
|
|
$promises = [new Failure($exception), new Failure($exception), new Failure($exception)];;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$count = 0;
|
|
|
|
$callback = function ($value) use (&$count) {
|
|
|
|
++$count;
|
|
|
|
return $value - 1;
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$result = Amp\map($callback, $promises);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$this->assertTrue(\is_array($result));
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
foreach ($result as $key => $promise) {
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$this->assertSame(0, $count);
|
2017-03-10 21:31:57 +01:00
|
|
|
});
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
/**
|
|
|
|
* @depends testFailedPromisesArray
|
|
|
|
*/
|
|
|
|
public function testCallbackThrowingExceptionRejectsPromises()
|
|
|
|
{
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () {
|
2016-12-16 00:28:22 +01:00
|
|
|
$promises = [new Success(1), new Success(2), new Success(3)];;
|
|
|
|
$exception = new \Exception;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$callback = function () use ($exception) {
|
|
|
|
throw $exception;
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$result = Amp\map($callback, $promises);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
foreach ($result as $key => $promise) {
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
foreach ($result as $key => $promise) {
|
|
|
|
try {
|
|
|
|
Amp\wait($promise);
|
|
|
|
} catch (\Exception $reason) {
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 21:31:57 +01:00
|
|
|
});
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
/**
|
|
|
|
* @depends testPendingPromisesArray
|
|
|
|
*/
|
|
|
|
public function testMultipleArrays() {
|
|
|
|
$promises1 = [new Success(1), new Success(2), new Success(3)];;
|
|
|
|
$promises2 = [new Success(3), new Success(2), new Success(1)];;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$count = 0;
|
|
|
|
$callback = function ($value1, $value2) use (&$count) {
|
|
|
|
++$count;
|
|
|
|
return $value1 + $value2;
|
|
|
|
};
|
|
|
|
|
|
|
|
$result = Amp\map($callback, $promises1, $promises2);
|
|
|
|
|
|
|
|
foreach ($result as $key => $promise) {
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($result as $promise) {
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
|
|
|
$this->assertSame(4, Amp\wait($promise));
|
|
|
|
}
|
2017-02-10 16:41:43 +01:00
|
|
|
|
|
|
|
$this->assertSame(3, $count);
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
2017-02-10 05:34:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testMultipleArrays
|
|
|
|
*/
|
|
|
|
public function testMultipleArrayArgumentOrder() {
|
|
|
|
$promises1 = [new Pause(10, 1), new Pause(20, 1), new Pause(30, 1)];;
|
|
|
|
$promises2 = [new Pause(30, 3), new Pause(20, 3), new Pause(10, 3)];;
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
$callback = function ($value1, $value2) use (&$count) {
|
|
|
|
++$count;
|
|
|
|
$this->assertSame(1, $value1);
|
|
|
|
$this->assertSame(3, $value2);
|
|
|
|
return $value1 + $value2;
|
|
|
|
};
|
|
|
|
|
|
|
|
$result = Amp\map($callback, $promises1, $promises2);
|
|
|
|
|
|
|
|
foreach ($result as $promise) {
|
|
|
|
$this->assertSame(4, Amp\wait($promise));
|
|
|
|
}
|
2017-02-10 16:41:43 +01:00
|
|
|
|
|
|
|
$this->assertSame(3, $count);
|
2017-02-10 05:34:24 +01:00
|
|
|
}
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|