-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathAbuseTest.php
137 lines (116 loc) · 4.48 KB
/
AbuseTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/*
* This file is part of the DataLoaderPhp package.
*
* (c) Overblog <http://github.com/overblog/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
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
*/
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);
}
/**
* @group provides-descriptive-error-messages-for-api-abuse
*/
public function testLoadFunctionRequiresAKeyWith0()
{
self::assertInstanceOf(Promise::class, self::idLoader()->load(0));
}
/**
* @group provides-descriptive-error-messages-for-api-abuse
*/
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);
}
/**
* @group provides-descriptive-error-messages-for-api-abuse
*/
public function testLoadManyFunctionRequiresAListEmptyArrayAccepted()
{
self::assertInstanceOf(Promise::class, self::idLoader()->loadMany([]));
}
/**
* @group provides-descriptive-error-messages-for-api-abuse
*/
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
*/
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
*/
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
* @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
* @runInSeparateProcess
*/
public function testAwaitWithoutNoInstance()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage("Found no active DataLoader instance.");
DataLoader::await(self::$promiseAdapter->create());
}
/**
* @param callable $batchLoadFn
* @return DataLoader
*/
private static function idLoader(?callable $batchLoadFn = null)
{
if (null === $batchLoadFn) {
$batchLoadFn = function ($keys) {
return self::$promiseAdapter->createAll($keys);
};
}
return new DataLoader($batchLoadFn, self::$promiseAdapter);
}
}