|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the PromiseFactory package. |
| 5 | + * |
| 6 | + * (c) McGWeb <http://github.com/mcg-web> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace McGWeb\PromiseFactory\Tests; |
| 13 | + |
| 14 | +use McGWeb\PromiseFactory\Factory\GuzzleHttpPromiseFactory; |
| 15 | +use McGWeb\PromiseFactory\Factory\ReactPromiseFactory; |
| 16 | +use McGWeb\PromiseFactory\PromiseFactoryInterface; |
| 17 | + |
| 18 | +class FactoryTest extends \PHPUnit_Framework_TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @dataProvider factoryDataProvider |
| 22 | + * @param string $promiseClass |
| 23 | + * @param PromiseFactoryInterface $factory |
| 24 | + * @param string $context |
| 25 | + */ |
| 26 | + public function testCreate(PromiseFactoryInterface $factory, $context, $promiseClass) |
| 27 | + { |
| 28 | + $promise = $factory->create($resolve, $reject); |
| 29 | + |
| 30 | + $this->assertInstanceOf($promiseClass, $promise, $context); |
| 31 | + $this->assertTrue(is_callable($resolve), $context); |
| 32 | + $this->assertTrue(is_callable($reject), $context); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @dataProvider factoryDataProvider |
| 37 | + * @param PromiseFactoryInterface $factory |
| 38 | + * @param $message |
| 39 | + */ |
| 40 | + public function testResolveCreatedPromise(PromiseFactoryInterface $factory, $message) |
| 41 | + { |
| 42 | + $promise = $factory->create($resolve, $reject); |
| 43 | + $expectResolvedValue = 'Resolve value'; |
| 44 | + $resolve($expectResolvedValue); |
| 45 | + $resolvedValue = $factory->await($promise); |
| 46 | + |
| 47 | + $this->assertEquals($expectResolvedValue, $resolvedValue, $message); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @dataProvider factoryDataProvider |
| 52 | + * @param PromiseFactoryInterface $factory |
| 53 | + * @param string $context |
| 54 | + */ |
| 55 | + public function testRejectCreatedPromise(PromiseFactoryInterface $factory, $context) |
| 56 | + { |
| 57 | + $promise = $factory->create($resolve, $reject); |
| 58 | + |
| 59 | + $expectRejectionReason = new \Exception('Error!'); |
| 60 | + $reject($expectRejectionReason); |
| 61 | + |
| 62 | + $rejectionReason = $factory->await($promise, false); |
| 63 | + $this->assertEquals($expectRejectionReason, $rejectionReason, $context); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @dataProvider factoryDataProvider |
| 68 | + * @param PromiseFactoryInterface $factory |
| 69 | + * @param string $context |
| 70 | + * @param string $promiseClass |
| 71 | + */ |
| 72 | + public function testCreateAll(PromiseFactoryInterface $factory, $context, $promiseClass) |
| 73 | + { |
| 74 | + $values = ['A', 'B', 'C']; |
| 75 | + $promise = $factory->createAll($values); |
| 76 | + $this->assertInstanceOf($promiseClass, $promise, $context); |
| 77 | + |
| 78 | + $resolvedValue = $factory->await($promise); |
| 79 | + |
| 80 | + $this->assertEquals($values, $resolvedValue, $context); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @dataProvider factoryDataProvider |
| 85 | + * @param PromiseFactoryInterface $factory |
| 86 | + * @param string $context |
| 87 | + * @param string $promiseClass |
| 88 | + */ |
| 89 | + public function testCreateResolve(PromiseFactoryInterface $factory, $context, $promiseClass) |
| 90 | + { |
| 91 | + $value = 'resolved!'; |
| 92 | + $promise = $factory->createResolve($value); |
| 93 | + $this->assertInstanceOf($promiseClass, $promise, $context); |
| 94 | + |
| 95 | + $resolvedValue = $factory->await($promise); |
| 96 | + $this->assertEquals($value, $resolvedValue, $context); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @dataProvider factoryDataProvider |
| 101 | + * @param PromiseFactoryInterface $factory |
| 102 | + * @param string $context |
| 103 | + * @param string $promiseClass |
| 104 | + */ |
| 105 | + public function testCreatedReject(PromiseFactoryInterface $factory, $context, $promiseClass) |
| 106 | + { |
| 107 | + $expectRejectionReason = new \Exception('Error!'); |
| 108 | + $promise = $factory->createReject($expectRejectionReason); |
| 109 | + $this->assertInstanceOf($promiseClass, $promise, $context); |
| 110 | + |
| 111 | + $rejectionReason = $factory->await($promise, false); |
| 112 | + $this->assertEquals($expectRejectionReason, $rejectionReason, $context); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @dataProvider factoryDataProvider |
| 117 | + * @param PromiseFactoryInterface $factory |
| 118 | + * @param string $context |
| 119 | + */ |
| 120 | + public function testIsPromise(PromiseFactoryInterface $factory, $context) |
| 121 | + { |
| 122 | + $promise = $factory->create(); |
| 123 | + |
| 124 | + $this->assertTrue($factory->isPromise($promise, true), $context); |
| 125 | + $this->assertFalse($factory->isPromise([]), $context); |
| 126 | + $this->assertFalse($factory->isPromise(new \stdClass()), $context); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @dataProvider factoryDataProvider |
| 131 | + * @param PromiseFactoryInterface $factory |
| 132 | + * @param string $context |
| 133 | + */ |
| 134 | + public function testAwaitWithoutPromise(PromiseFactoryInterface $factory, $context) |
| 135 | + { |
| 136 | + $expected = 'expected value'; |
| 137 | + $promise = $factory->createResolve($expected); |
| 138 | + $actual = null; |
| 139 | + |
| 140 | + $promise->then(function ($value) use (&$actual) { |
| 141 | + $actual = $value; |
| 142 | + }); |
| 143 | + |
| 144 | + $factory->await(); |
| 145 | + |
| 146 | + $this->assertEquals($expected, $actual, $context); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @dataProvider factoryDataProvider |
| 151 | + * @param PromiseFactoryInterface $factory |
| 152 | + * |
| 153 | + * @expectedException \Exception |
| 154 | + * @expectedExceptionMessage error! |
| 155 | + */ |
| 156 | + public function testAwaitWithUnwrap(PromiseFactoryInterface $factory) |
| 157 | + { |
| 158 | + $expected = new \Exception('error!'); |
| 159 | + $promise = $factory->createReject($expected); |
| 160 | + |
| 161 | + $factory->await($promise, true); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @dataProvider factoryDataProvider |
| 166 | + * @param PromiseFactoryInterface $factory |
| 167 | + * |
| 168 | + * @expectedException \InvalidArgumentException |
| 169 | + * @expectedExceptionMessage ::await" method must be called with a Promise ("then" method). |
| 170 | + */ |
| 171 | + public function testAwaitWithInvalidPromise(PromiseFactoryInterface $factory) |
| 172 | + { |
| 173 | + $factory->await(new \stdClass(), true); |
| 174 | + } |
| 175 | + |
| 176 | + public function factoryDataProvider() |
| 177 | + { |
| 178 | + return [ |
| 179 | + [new GuzzleHttpPromiseFactory(), 'guzzle', '\\GuzzleHttp\\Promise\\PromiseInterface'], |
| 180 | + [new ReactPromiseFactory(), 'react', '\\React\\Promise\\PromiseInterface'], |
| 181 | + ]; |
| 182 | + } |
| 183 | +} |
0 commit comments