| 1 |
<?php |
| 2 |
namespace GuzzleHttp\Promise\Tests; |
| 3 |
|
| 4 |
use GuzzleHttp\Promise\Coroutine; |
| 5 |
use GuzzleHttp\Promise\Promise; |
| 6 |
use GuzzleHttp\Promise\PromiseInterface; |
| 7 |
use PHPUnit\Framework\TestCase; |
| 8 |
use ReflectionClass; |
| 9 |
|
| 10 |
class CoroutineTest extends TestCase |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @dataProvider promiseInterfaceMethodProvider |
| 14 |
* |
| 15 |
* @param string $method |
| 16 |
* @param array $args |
| 17 |
*/ |
| 18 |
public function testShouldProxyPromiseMethodsToResultPromise($method, $args = []) |
| 19 |
{ |
| 20 |
$coroutine = new Coroutine(function () { yield 0; }); |
| 21 |
$mockPromise = $this->getMockForAbstractClass(PromiseInterface::class); |
| 22 |
call_user_func_array([$mockPromise->expects($this->once())->method($method), 'with'], $args); |
| 23 |
|
| 24 |
$resultPromiseProp = (new ReflectionClass(Coroutine::class))->getProperty('result'); |
| 25 |
$resultPromiseProp->setAccessible(true); |
| 26 |
$resultPromiseProp->setValue($coroutine, $mockPromise); |
| 27 |
|
| 28 |
call_user_func_array([$coroutine, $method], $args); |
| 29 |
} |
| 30 |
|
| 31 |
public function promiseInterfaceMethodProvider() |
| 32 |
{ |
| 33 |
return [ |
| 34 |
['then', [null, null]], |
| 35 |
['otherwise', [function () {}]], |
| 36 |
['wait', [true]], |
| 37 |
['getState', []], |
| 38 |
['resolve', [null]], |
| 39 |
['reject', [null]], |
| 40 |
]; |
| 41 |
} |
| 42 |
|
| 43 |
public function testShouldCancelResultPromiseAndOutsideCurrentPromise() |
| 44 |
{ |
| 45 |
$coroutine = new Coroutine(function () { yield 0; }); |
| 46 |
|
| 47 |
$mockPromises = [ |
| 48 |
'result' => $this->getMockForAbstractClass(PromiseInterface::class), |
| 49 |
'currentPromise' => $this->getMockForAbstractClass(PromiseInterface::class), |
| 50 |
]; |
| 51 |
foreach ($mockPromises as $propName => $mockPromise) { |
| 52 |
/** |
| 53 |
* @var $mockPromise \PHPUnit_Framework_MockObject_MockObject |
| 54 |
*/ |
| 55 |
$mockPromise->expects($this->once()) |
| 56 |
->method('cancel') |
| 57 |
->with(); |
| 58 |
|
| 59 |
$promiseProp = (new ReflectionClass(Coroutine::class))->getProperty($propName); |
| 60 |
$promiseProp->setAccessible(true); |
| 61 |
$promiseProp->setValue($coroutine, $mockPromise); |
| 62 |
} |
| 63 |
|
| 64 |
$coroutine->cancel(); |
| 65 |
} |
| 66 |
|
| 67 |
public function testWaitShouldResolveChainedCoroutines() |
| 68 |
{ |
| 69 |
$promisor = function () { |
| 70 |
return \GuzzleHttp\Promise\coroutine(function () { |
| 71 |
yield $promise = new Promise(function () use (&$promise) { |
| 72 |
$promise->resolve(1); |
| 73 |
}); |
| 74 |
}); |
| 75 |
}; |
| 76 |
|
| 77 |
$promise = $promisor()->then($promisor)->then($promisor); |
| 78 |
|
| 79 |
$this->assertSame(1, $promise->wait()); |
| 80 |
} |
| 81 |
|
| 82 |
public function testWaitShouldHandleIntermediateErrors() |
| 83 |
{ |
| 84 |
$promise = \GuzzleHttp\Promise\coroutine(function () { |
| 85 |
yield $promise = new Promise(function () use (&$promise) { |
| 86 |
$promise->resolve(1); |
| 87 |
}); |
| 88 |
}) |
| 89 |
->then(function () { |
| 90 |
return \GuzzleHttp\Promise\coroutine(function () { |
| 91 |
yield $promise = new Promise(function () use (&$promise) { |
| 92 |
$promise->reject(new \Exception); |
| 93 |
}); |
| 94 |
}); |
| 95 |
}) |
| 96 |
->otherwise(function (\Exception $error = null) { |
| 97 |
if (!$error) { |
| 98 |
self::fail('Error did not propagate.'); |
| 99 |
} |
| 100 |
return 3; |
| 101 |
}); |
| 102 |
|
| 103 |
$this->assertSame(3, $promise->wait()); |
| 104 |
} |
| 105 |
} |
| 106 |
|