diff --git a/tests/AbuseTest.php b/tests/AbuseTestCase.php similarity index 94% rename from tests/AbuseTest.php rename to tests/AbuseTestCase.php index c163fa8..dadd0a0 100644 --- a/tests/AbuseTest.php +++ b/tests/AbuseTestCase.php @@ -13,10 +13,9 @@ use InvalidArgumentException; use Overblog\DataLoader\DataLoader; -use React\Promise\Promise; use RuntimeException; -class AbuseTest extends TestCase +abstract class AbuseTestCase extends TestCase { /** * @group provides-descriptive-error-messages-for-api-abuse @@ -34,7 +33,7 @@ public function testLoadFunctionRequiresAKeyNotNull() */ public function testLoadFunctionRequiresAKeyWith0() { - self::assertInstanceOf(Promise::class, self::idLoader()->load(0)); + self::assertTrue(self::$promiseAdapter->isPromise(self::idLoader()->load(0), true)); } /** @@ -53,7 +52,7 @@ public function testLoadManyFunctionRequiresAListOfKey() */ public function testLoadManyFunctionRequiresAListEmptyArrayAccepted() { - self::assertInstanceOf(Promise::class, self::idLoader()->loadMany([])); + self::assertTrue(self::$promiseAdapter->isPromise(self::idLoader()->loadMany([]), true)); } /** diff --git a/tests/DataLoadTest.php b/tests/DataLoadTestCase.php similarity index 95% rename from tests/DataLoadTest.php rename to tests/DataLoadTestCase.php index fd17e01..74849f0 100644 --- a/tests/DataLoadTest.php +++ b/tests/DataLoadTestCase.php @@ -15,7 +15,7 @@ use Overblog\DataLoader\DataLoader; use Overblog\DataLoader\Option; -class DataLoadTest extends TestCase +abstract class DataLoadTestCase extends TestCase { /** * @group primary-api @@ -306,7 +306,7 @@ public function testCanRepresentFailuresAndSuccessesSimultaneously() $promise1->then(null, function ($error) use (&$caughtError) { $caughtError = $error; }); - DataLoader::await(); + DataLoader::await($promise1, false); $this->assertInstanceOf(\Exception::class, $caughtError); $this->assertEquals($caughtError->getMessage(), 'Odd: 1'); @@ -815,50 +815,6 @@ public function testAwaitAlsoAwaitsNewlyCreatedDataloaders() $this->assertTrue($secondComplete); } - /** - * @runInSeparateProcess - */ - public function testAwaitShouldReturnTheValueOfFulfilledPromiseWithoutNeedingActiveDataLoaderInstance() - { - $expectedValue = 'Ok!'; - $value = DataLoader::await(self::$promiseAdapter->createFulfilled($expectedValue)); - - $this->assertEquals($expectedValue, $value); - } - - /** - * @runInSeparateProcess - */ - public function testAwaitShouldReturnTheRejectReasonOfRejectedPromiseWithoutNeedingActiveDataLoaderInstance() - { - $expectedException = new \Exception('Rejected!'); - $exception = DataLoader::await(self::$promiseAdapter->createRejected($expectedException), false); - - $this->assertEquals($expectedException, $exception); - } - - /** - * @runInSeparateProcess - */ - public function testAwaitShouldThrowTheRejectReasonOfRejectedPromiseWithoutNeedingActiveDataLoaderInstance() - { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Rejected!'); - - DataLoader::await(self::$promiseAdapter->createRejected(new Exception('Rejected!'))); - } - - /** - * @runInSeparateProcess - */ - public function testAwaitShouldThrowThrowable() - { - $this->expectException(\Error::class); - $this->expectExceptionMessage('Rejected Error!'); - - DataLoader::await(self::$promiseAdapter->createRejected(new \Error('Rejected Error!'))); - } - public function cacheKey($key) { $cacheKey = []; @@ -914,6 +870,7 @@ private static function idLoader(?Option $options = null, ?callable $batchLoadFn private function assertInstanceOfPromise($object) { - $this->assertTrue(self::$promiseAdapter->isPromise($object, true)); + $adapter = self::$promiseAdapter; + $this->assertTrue($adapter->isPromise($object, true)); } } diff --git a/tests/ReactAbuseTest.php b/tests/ReactAbuseTest.php new file mode 100644 index 0000000..1346cd9 --- /dev/null +++ b/tests/ReactAbuseTest.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Overblog\DataLoader\Test; + +use Overblog\PromiseAdapter\Adapter\ReactPromiseAdapter; +use Overblog\PromiseAdapter\PromiseAdapterInterface; + +final class ReactAbuseTest extends AbuseTestCase +{ + protected function createPromiseAdapter(): PromiseAdapterInterface + { + return new ReactPromiseAdapter(); + } +} diff --git a/tests/ReactDataLoadTest.php b/tests/ReactDataLoadTest.php new file mode 100644 index 0000000..727529d --- /dev/null +++ b/tests/ReactDataLoadTest.php @@ -0,0 +1,68 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Overblog\DataLoader\Test; + +use Overblog\DataLoader\DataLoader; +use Overblog\PromiseAdapter\Adapter\ReactPromiseAdapter; +use Overblog\PromiseAdapter\PromiseAdapterInterface; + +final class ReactDataLoadTest extends DataLoadTestCase +{ + protected function createPromiseAdapter(): PromiseAdapterInterface + { + return new ReactPromiseAdapter(); + } + + /** + * @runInSeparateProcess + */ + public function testAwaitShouldReturnTheValueOfFulfilledPromiseWithoutNeedingActiveDataLoaderInstance() + { + $expectedValue = 'Ok!'; + $value = DataLoader::await(self::$promiseAdapter->createFulfilled($expectedValue)); + + $this->assertEquals($expectedValue, $value); + } + + /** + * @runInSeparateProcess + */ + public function testAwaitShouldReturnTheRejectReasonOfRejectedPromiseWithoutNeedingActiveDataLoaderInstance() + { + $expectedException = new \Exception('Rejected!'); + $exception = DataLoader::await(self::$promiseAdapter->createRejected($expectedException), false); + + $this->assertEquals($expectedException, $exception); + } + + /** + * @runInSeparateProcess + */ + public function testAwaitShouldThrowTheRejectReasonOfRejectedPromiseWithoutNeedingActiveDataLoaderInstance() + { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Rejected!'); + + DataLoader::await(self::$promiseAdapter->createRejected(new \Exception('Rejected!'))); + } + + /** + * @runInSeparateProcess + */ + public function testAwaitShouldThrowThrowable() + { + $this->expectException(\Error::class); + $this->expectExceptionMessage('Rejected Error!'); + + DataLoader::await(self::$promiseAdapter->createRejected(new \Error('Rejected Error!'))); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 755ef26..afea948 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -11,7 +11,7 @@ namespace Overblog\DataLoader\Test; -use Overblog\PromiseAdapter\Adapter\ReactPromiseAdapter; +use Overblog\DataLoader\DataLoader; use Overblog\PromiseAdapter\PromiseAdapterInterface; abstract class TestCase extends \PHPUnit\Framework\TestCase @@ -23,6 +23,16 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase public function setUp(): void { - self::$promiseAdapter = new ReactPromiseAdapter(); + self::$promiseAdapter = $this->createPromiseAdapter(); } + + protected function tearDown(): void + { + $instances = new \ReflectionProperty(DataLoader::class, 'instances'); + $instances->setValue([]); + + parent::tearDown(); + } + + abstract protected function createPromiseAdapter(): PromiseAdapterInterface; } diff --git a/tests/WebonyxAbuseTest.php b/tests/WebonyxAbuseTest.php new file mode 100644 index 0000000..e248b4a --- /dev/null +++ b/tests/WebonyxAbuseTest.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Overblog\DataLoader\Test; + +use Overblog\PromiseAdapter\Adapter\WebonyxGraphQLSyncPromiseAdapter; +use Overblog\PromiseAdapter\PromiseAdapterInterface; + +final class WebonyxAbuseTest extends AbuseTestCase +{ + protected function createPromiseAdapter(): PromiseAdapterInterface + { + return new WebonyxGraphQLSyncPromiseAdapter(); + } +} diff --git a/tests/WebonyxDataLoadTest.php b/tests/WebonyxDataLoadTest.php new file mode 100644 index 0000000..98831a1 --- /dev/null +++ b/tests/WebonyxDataLoadTest.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Overblog\DataLoader\Test; + +use Overblog\PromiseAdapter\Adapter\WebonyxGraphQLSyncPromiseAdapter; +use Overblog\PromiseAdapter\PromiseAdapterInterface; + +final class WebonyxDataLoadTest extends DataLoadTestCase +{ + protected function createPromiseAdapter(): PromiseAdapterInterface + { + return new WebonyxGraphQLSyncPromiseAdapter(); + } +}