Skip to content

Commit

Permalink
Merge pull request #51 from simPod/cleaner-tests
Browse files Browse the repository at this point in the history
Cleanup tests
  • Loading branch information
mcg-web authored Mar 13, 2021
2 parents 91d6fc4 + 8683332 commit b842d26
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 43 deletions.
46 changes: 26 additions & 20 deletions tests/AbuseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@

namespace Overblog\DataLoader\Test;

use InvalidArgumentException;
use Overblog\DataLoader\DataLoader;
use React\Promise\Promise;
use RuntimeException;

class AbuseTest extends TestCase
{
/**
* @group provides-descriptive-error-messages-for-api-abuse
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The "Overblog\DataLoader\DataLoader::load" method must be called with a value, but got: NULL.
*/
public function testLoadFunctionRequiresAKeyNotNull()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The "Overblog\DataLoader\DataLoader::load" method must be called with a value, but got: NULL.');

self::idLoader()->load(null);
}

Expand All @@ -31,17 +34,17 @@ public function testLoadFunctionRequiresAKeyNotNull()
*/
public function testLoadFunctionRequiresAKeyWith0()
{
self::idLoader()->load(0);
self::assertInstanceOf(Promise::class, self::idLoader()->load(0));
}

/**
* @group provides-descriptive-error-messages-for-api-abuse
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The "Overblog\DataLoader\DataLoader::loadMany" method must be called with Array<key> but got: integer.
*/
public function testLoadManyFunctionRequiresAListOfKey()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The "Overblog\DataLoader\DataLoader::loadMany" method must be called with Array<key> but got: integer.');

self::idLoader()->loadMany(1, 2, 3);
}

Expand All @@ -50,67 +53,70 @@ public function testLoadManyFunctionRequiresAListOfKey()
*/
public function testLoadManyFunctionRequiresAListEmptyArrayAccepted()
{
self::idLoader()->loadMany([]);
self::assertInstanceOf(Promise::class, self::idLoader()->loadMany([]));
}

/**
* @group provides-descriptive-error-messages-for-api-abuse
*
* @expectedException \RuntimeException
* @expectedExceptionMessage DataLoader must be constructed with a function which accepts Array<key> and returns Promise<Array<value>>, but the function did not return a Promise: array.
*/
public function testBatchFunctionMustReturnAPromiseNotAValue()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('DataLoader must be constructed with a function which accepts Array<key> and returns Promise<Array<value>>, but the function did not return a Promise: array.');

DataLoader::await(self::idLoader(function ($keys) {
return $keys;
})->load(1));
}

/**
* @group provides-descriptive-error-messages-for-api-abuse
*
* @expectedException \RuntimeException
* @expectedExceptionMessage DataLoader must be constructed with a function which accepts Array<key> and returns Promise<Array<value>>, but the function did not return a Promise of an Array: NULL.
*/
public function testBatchFunctionMustReturnAPromiseOfAnArrayNotNull()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('DataLoader must be constructed with a function which accepts Array<key> and returns Promise<Array<value>>, but the function did not return a Promise of an Array: NULL.');

DataLoader::await(self::idLoader(function () {
return self::$promiseAdapter->createFulfilled(null);
})->load(1));
}

/**
* @group provides-descriptive-error-messages-for-api-abuse
* @expectedException \RuntimeException
* @expectedExceptionMessage DataLoader must be constructed with a function which accepts Array<key> and returns Promise<Array<value>>, but the function did not return a Promise of an Array of the same length as the Array of keys.
*/
public function testBatchFunctionMustPromiseAnArrayOfCorrectLength()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('DataLoader must be constructed with a function which accepts Array<key> and returns Promise<Array<value>>, but the function did not return a Promise of an Array of the same length as the Array of keys.');

DataLoader::await(self::idLoader(function () {
return self::$promiseAdapter->createFulfilled([]);
})->load(1));
}

/**
* @group provides-descriptive-error-messages-for-api-abuse
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage ::await" method must be called with a Promise ("then" method).
* @runInSeparateProcess
*/
public function testAwaitPromiseMustHaveAThenMethod()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('::await" method must be called with a Promise ("then" method).');

self::idLoader();
DataLoader::await([]);
}

/**
* @group provides-descriptive-error-messages-for-api-abuse
* @expectedException \RuntimeException
* @expectedExceptionMessage Found no active DataLoader instance.
* @runInSeparateProcess
*/
public function testAwaitWithoutNoInstance()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage("Found no active DataLoader instance.");

DataLoader::await(self::$promiseAdapter->create());
}

Expand Down
27 changes: 9 additions & 18 deletions tests/DataLoadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ public function testOnDestructionAllPromiseInQueueShouldBeCancelled()

public function testCallingAwaitFunctionWhenNoInstanceOfDataLoaderShouldNotThrowError()
{
DataLoader::await();
self::assertNull(DataLoader::await());
}

public function testAwaitAlsoAwaitsNewlyCreatedDataloaders()
Expand Down Expand Up @@ -837,34 +837,25 @@ public function testAwaitShouldReturnTheRejectReasonOfRejectedPromiseWithoutNeed
}

/**
* @expectedException \Exception
* @expectedExceptionMessage Rejected!
* @runInSeparateProcess
*/
public function testAwaitShouldThrowTheRejectReasonOfRejectedPromiseWithoutNeedingActiveDataLoaderInstance()
{
DataLoader::await(self::$promiseAdapter->createRejected(new \Exception('Rejected!')));
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Rejected!');

DataLoader::await(self::$promiseAdapter->createRejected(new Exception('Rejected!')));
}

/**
* @runInSeparateProcess
*
* We cannot use the standard expectedException annotations here since the class does not exist in PHP5.
*/
public function testAwaitShouldThrowThrowable()
{
if (PHP_MAJOR_VERSION >= 7) {
try {
DataLoader::await(self::$promiseAdapter->createRejected(new \Error('Rejected Error!')));

// If we got here, it means no Error is thrown, so fail the test
throw new \Exception("Expected \Error to be thrown.");
} catch (\Error $error) {
if ($error->getMessage() != 'Rejected Error!') {
throw new \Exception("Expected Rejected Error! as message, but got: " . $error->getMessage());
}
}
}
$this->expectException(\Error::class);
$this->expectExceptionMessage('Rejected Error!');

DataLoader::await(self::$promiseAdapter->createRejected(new \Error('Rejected Error!')));
}

public function cacheKey($key)
Expand Down
18 changes: 13 additions & 5 deletions tests/Functional/Webonyx/GraphQL/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Overblog\DataLoader\Test\Functional\Webonyx\GraphQL;

use GraphQL\Executor\ExecutionResult;
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
use GraphQL\Executor\Promise\Promise;
use GraphQL\Executor\Promise\PromiseAdapter;
use GraphQL\GraphQL;
Expand Down Expand Up @@ -75,17 +77,23 @@ public function testExecute(array $expectedMetrics, $query, array $expectedRespo
];

$graphQLPromiseAdapter = $this->createGraphQLPromiseAdapter();
GraphQL::setPromiseAdapter($graphQLPromiseAdapter);
$dataLoaderPromiseAdapter = $this->createDataLoaderPromiseAdapter($graphQLPromiseAdapter);
$dataLoader = $this->createDataLoader($dataLoaderPromiseAdapter, $metrics['callsIds'], $metrics['calls']);
$schema = Schema::build($dataLoader);

$response = GraphQL::execute($schema, $query);
if ($response instanceof Promise) {
$response = DataLoader::await($response);
$result = GraphQL::promiseToExecute($graphQLPromiseAdapter, $schema, $query);
if ($graphQLPromiseAdapter instanceof SyncPromiseAdapter) {
$result = $graphQLPromiseAdapter->wait($result)->toArray();
} else {
$result = $result->then(static function (ExecutionResult $r) : array {
return $r->toArray();
});
}
if ($result instanceof Promise) {
$result = DataLoader::await($result);
}

$this->assertEquals($expectedResponse, $response);
$this->assertEquals($expectedResponse, $result);
$this->assertEquals($expectedMetrics, $metrics);

$dataLoader->clearAll();
Expand Down

0 comments on commit b842d26

Please sign in to comment.