PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 2.5.5
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v2.5.5
3.7.2 3.7.1 3.7.0 3.6.0 3.6.1 3.5.2 trunk 2.0 2.1 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.6.0 2.6.1 All 73 releases
stockpack / vendor / guzzlehttp / promises / tests / CoroutineTest.php

CoroutineTest.php in StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more 2.5.5, at vendor/guzzlehttp/promises/tests/CoroutineTest.php

106 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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