Skip to content

Commit 47adce7

Browse files
author
Juuso Leinonen
committed
More exception tests
1 parent 51ed51c commit 47adce7

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

tests/Unit/DataLoaderAbuseTest.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ public function load_many_requires_actual_keys()
5858
/**
5959
* @test
6060
* @expectedException \RuntimeException
61-
* @expectedExceptionMessage leinonen\DataLoader\DataLoader must be constructed with a function which accepts
62-
* an array of keys and returns a Promise which resolves to an array of values not return a Promise: null.
61+
* @expectedExceptionMessage leinonen\DataLoader\DataLoader must be constructed with a function which accepts an array of keys and returns a Promise which resolves to an array of values not return a Promise: NULL.
6362
*/
6463
public function batch_function_must_return_a_promise_not_null()
6564
{
@@ -73,14 +72,43 @@ public function batch_function_must_return_a_promise_not_null()
7372
/**
7473
* @test
7574
* @expectedException \RuntimeException
76-
* @expectedExceptionMessage leinonen\DataLoader\DataLoader must be constructed with a function which accepts
77-
* an array of keys and returns a Promise which resolves to an array of values not return a Promise: 1.
75+
* @expectedExceptionMessage leinonen\DataLoader\DataLoader must be constructed with a function which accepts an array of keys and returns a Promise which resolves to an array of values not return a Promise: array.
76+
*/
77+
public function batch_function_must_return_a_promise_not_a_array()
78+
{
79+
$badLoader = $this->createDataLoader(function ($keys) {
80+
return $keys;
81+
});
82+
83+
$badLoader->load(1);
84+
$this->eventLoop->run();
85+
}
86+
87+
/**
88+
* @test
89+
* @expectedException \RuntimeException
90+
* @expectedExceptionMessage leinonen\DataLoader\DataLoader must be constructed with a function which accepts an array of keys and returns a Promise which resolves to an array of values not return a Promise: integer.
7891
*/
7992
public function batch_function_must_return_a_promise_not_a_value()
93+
{
94+
$badLoader = $this->createDataLoader(function ($keys) {
95+
return 1;
96+
});
97+
98+
$badLoader->load(1);
99+
$this->eventLoop->run();
100+
}
101+
102+
/**
103+
* @test
104+
* @expectedException \RuntimeException
105+
* @expectedExceptionMessage leinonen\DataLoader\DataLoader must be constructed with a function which accepts an array of keys and returns a Promise which resolves to an array of values not return a Promise: object.
106+
*/
107+
public function batch_function_must_return_a_promise_not_an_object()
80108
{
81109
$badLoader = new DataLoader(
82110
function ($keys) {
83-
return $keys;
111+
return new \stdClass();
84112
}, $this->eventLoop, new CacheMap()
85113
);
86114

tests/Unit/DataLoaderTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,31 @@ public function it_batches_multiple_requests_with_max_batch_sizes()
130130
$this->assertEquals([[1, 2], [3]], $this->loadCalls);
131131
}
132132

133+
/** @test */
134+
public function if_max_batch_size_is_set_to_0_it_still_batches_calls_normally()
135+
{
136+
$options = new DataLoaderOptions(0);
137+
$identityLoader = $this->createIdentityLoader($options);
138+
139+
$promise1 = $identityLoader->load(1);
140+
$promise2 = $identityLoader->load(2);
141+
$promise3 = $identityLoader->load(3);
142+
143+
$values = [];
144+
145+
\React\Promise\all([$promise1, $promise2, $promise3])->then(function ($returnedValues) use (&$values) {
146+
$values = $returnedValues;
147+
});
148+
149+
$this->eventLoop->run();
150+
151+
$this->assertEquals(1, $values[0]);
152+
$this->assertEquals(2, $values[1]);
153+
$this->assertEquals(3, $values[2]);
154+
155+
$this->assertEquals([[1, 2, 3]], $this->loadCalls);
156+
}
157+
133158
/** @test */
134159
public function it_coalesces_identical_requests()
135160
{

0 commit comments

Comments
 (0)