Skip to content

Commit 5bdcf62

Browse files
committed
Split up test cases into individual files
1 parent 29f1c00 commit 5bdcf62

File tree

7 files changed

+196
-182
lines changed

7 files changed

+196
-182
lines changed

tests/BlockerTest.php

Lines changed: 0 additions & 182 deletions
This file was deleted.

tests/FunctionAwaitAllTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
class FunctionAwaitAllTest extends TestCase
4+
{
5+
public function testAwaitAllEmpty()
6+
{
7+
$this->assertEquals(array(), $this->block->awaitAll(array()));
8+
}
9+
10+
public function testAwaitAllAllResolved()
11+
{
12+
$all = array(
13+
'first' => $this->createPromiseResolved(1),
14+
'second' => $this->createPromiseResolved(2)
15+
);
16+
17+
$this->assertEquals(array('first' => 1, 'second' => 2), $this->block->awaitAll($all));
18+
}
19+
20+
public function testAwaitAllRejected()
21+
{
22+
$all = array(
23+
$this->createPromiseResolved(1),
24+
$this->createPromiseRejected(new Exception('test'))
25+
);
26+
27+
$this->setExpectedException('Exception', 'test');
28+
$this->block->awaitAll($all);
29+
}
30+
31+
public function testAwaitAllOnlyRejected()
32+
{
33+
$all = array(
34+
$this->createPromiseRejected(new Exception('first')),
35+
$this->createPromiseRejected(new Exception('second'))
36+
);
37+
38+
$this->setExpectedException('Exception', 'first');
39+
$this->block->awaitAll($all);
40+
}
41+
42+
public function testAwaitAllInterrupted()
43+
{
44+
$promise = $this->createPromiseResolved(2, 0.02);
45+
$this->createTimerInterrupt(0.01);
46+
47+
$this->assertEquals(array(2), $this->block->awaitAll(array($promise)));
48+
}
49+
}

tests/FunctionAwaitAnyTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
use React\Promise\Deferred;
4+
5+
class FunctionAwaitAnyTest extends TestCase
6+
{
7+
/**
8+
* @expectedException UnderflowException
9+
*/
10+
public function testAwaitAnyEmpty()
11+
{
12+
$this->block->awaitAny(array());
13+
}
14+
15+
public function testAwaitAnyFirstResolved()
16+
{
17+
$all = array(
18+
$this->createPromiseRejected(1),
19+
$this->createPromiseResolved(2, 0.01),
20+
$this->createPromiseResolved(3, 0.02)
21+
);
22+
23+
$this->assertEquals(2, $this->block->awaitAny($all));
24+
}
25+
26+
public function testAwaitAnyFirstResolvedConcurrently()
27+
{
28+
$d1 = new Deferred();
29+
$d2 = new Deferred();
30+
$d3 = new Deferred();
31+
32+
$this->loop->addTimer(0.01, function() use ($d1, $d2, $d3) {
33+
$d1->reject(1);
34+
$d2->resolve(2);
35+
$d3->resolve(3);
36+
});
37+
38+
$all = array(
39+
$d1->promise(),
40+
$d2->promise(),
41+
$d3->promise()
42+
);
43+
44+
$this->assertEquals(2, $this->block->awaitAny($all));
45+
}
46+
47+
public function testAwaitAnyAllRejected()
48+
{
49+
$all = array(
50+
$this->createPromiseRejected(1),
51+
$this->createPromiseRejected(2)
52+
);
53+
54+
$this->setExpectedException('UnderflowException');
55+
$this->block->awaitAny($all);
56+
}
57+
58+
public function testAwaitAnyInterrupted()
59+
{
60+
$promise = $this->createPromiseResolved(2, 0.02);
61+
$this->createTimerInterrupt(0.01);
62+
63+
$this->assertEquals(2, $this->block->awaitAny(array($promise)));
64+
}
65+
}

tests/FunctionAwaitRaceTest_.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

tests/FunctionAwaitTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
class FunctionAwaitTest extends TestCase
4+
{
5+
public function testAwaitOneRejected()
6+
{
7+
$promise = $this->createPromiseRejected(new Exception('test'));
8+
9+
$this->setExpectedException('Exception', 'test');
10+
$this->block->await($promise);
11+
}
12+
13+
public function testAwaitOneResolved()
14+
{
15+
$promise = $this->createPromiseResolved(2);
16+
17+
$this->assertEquals(2, $this->block->await($promise));
18+
}
19+
20+
public function testAwaitOneInterrupted()
21+
{
22+
$promise = $this->createPromiseResolved(2, 0.02);
23+
$this->createTimerInterrupt(0.01);
24+
25+
$this->assertEquals(2, $this->block->await($promise));
26+
}
27+
}

tests/FunctionSleepTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
class FunctionSleepTest extends TestCase
4+
{
5+
public function testSleep()
6+
{
7+
$time = microtime(true);
8+
$this->block->sleep(0.2);
9+
$time = microtime(true) - $time;
10+
11+
$this->assertEquals(0.2, $time, '', 0.1);
12+
}
13+
}

tests/bootstrap.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,50 @@
11
<?php
22

3+
use Clue\React\Block\Blocker;
4+
use React\Promise\Deferred;
5+
36
require_once __DIR__ . '/../vendor/autoload.php';
47

58
error_reporting(-1);
69

710
class TestCase extends PHPUnit_Framework_TestCase
811
{
12+
protected $loop;
13+
protected $block;
14+
15+
public function setUp()
16+
{
17+
$this->loop = React\EventLoop\Factory::create();
18+
$this->block = new Blocker($this->loop);
19+
}
20+
21+
protected function createPromiseResolved($value = null, $delay = 0.01)
22+
{
23+
$deferred = new Deferred();
24+
25+
$this->loop->addTimer($delay, function () use ($deferred, $value) {
26+
$deferred->resolve($value);
27+
});
28+
29+
return $deferred->promise();
30+
}
31+
32+
protected function createPromiseRejected($value = null, $delay = 0.01)
33+
{
34+
$deferred = new Deferred();
35+
36+
$this->loop->addTimer($delay, function () use ($deferred, $value) {
37+
$deferred->reject($value);
38+
});
39+
40+
return $deferred->promise();
41+
}
42+
43+
protected function createTimerInterrupt($delay = 0.01)
44+
{
45+
$loop = $this->loop;
46+
$loop->addTimer($delay, function () use ($loop) {
47+
$loop->stop();
48+
});
49+
}
950
}

0 commit comments

Comments
 (0)