Skip to content

Commit 7535b5f

Browse files
committed
Add units tests (95% coverage)
1 parent bb8f0f9 commit 7535b5f

8 files changed

+308
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
4+
use ConnectionManager\Extra\ConnectionManagerDelay;
5+
6+
class ConnectionManagerDelayTest extends TestCase
7+
{
8+
public function setUp()
9+
{
10+
$this->loop = React\EventLoop\Factory::create();
11+
}
12+
13+
public function testDelayTenth()
14+
{
15+
$will = $this->createConnectionManagerMock(true);
16+
$cm = new ConnectionManagerDelay($will, $this->loop, 0.1);
17+
18+
$promise = $cm->create('www.google.com', 80);
19+
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
20+
21+
$this->loop->run();
22+
$promise->then($this->expectCallableOnce(), $this->expectCallableNever());
23+
}
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use ConnectionManager\Extra\ConnectionManagerReject;
4+
5+
class ConnectionManagerRejectTest extends TestCase
6+
{
7+
public function testReject()
8+
{
9+
$cm = new ConnectionManagerReject();
10+
$promise = $cm->create('www.google.com', 80);
11+
12+
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
13+
14+
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
15+
}
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use ConnectionManager\Extra\ConnectionManagerRepeat;
4+
use ConnectionManager\Extra\ConnectionManagerReject;
5+
6+
class ConnectionManagerRepeatTest extends TestCase
7+
{
8+
public function testRepeatRejected()
9+
{
10+
$wont = new ConnectionManagerReject();
11+
$cm = new ConnectionManagerRepeat($wont, 3);
12+
$promise = $cm->create('www.google.com', 80);
13+
14+
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
15+
16+
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
17+
}
18+
19+
/**
20+
* @expectedException InvalidArgumentException
21+
*/
22+
public function testInvalidRepetitions()
23+
{
24+
$wont = new ConnectionManagerReject();
25+
$cm = new ConnectionManagerRepeat($wont, -3);
26+
}
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use ConnectionManager\Extra\ConnectionManagerSwappable;
4+
use ConnectionManager\Extra\ConnectionManagerReject;
5+
6+
class ConnectionManagerSwappableTest extends TestCase
7+
{
8+
public function testSwap()
9+
{
10+
$wont = new ConnectionManagerReject();
11+
$cm = new ConnectionManagerSwappable($wont);
12+
13+
$promise = $cm->create('www.google.com', 80);
14+
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
15+
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
16+
17+
$will = $this->createConnectionManagerMock(true);
18+
$cm->setConnectionManager($will);
19+
20+
$promise = $cm->create('www.google.com', 80);
21+
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
22+
$promise->then($this->expectCallableOnce(), $this->expectCallableNever());
23+
}
24+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
use ConnectionManager\Extra\ConnectionManagerReject;
4+
5+
use React\Stream\Stream;
6+
7+
use ConnectionManager\Extra\ConnectionManagerDelay;
8+
9+
use ConnectionManager\Extra\ConnectionManagerTimeout;
10+
11+
class ConnectionManagerTimeoutTest extends TestCase
12+
{
13+
public function setUp()
14+
{
15+
$this->loop = React\EventLoop\Factory::create();
16+
}
17+
18+
public function testTimeoutOkay()
19+
{
20+
$will = $this->createConnectionManagerMock(true);
21+
$cm = new ConnectionManagerTimeout($will, $this->loop, 0.1);
22+
23+
$promise = $cm->create('www.google.com', 80);
24+
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
25+
26+
$this->loop->run();
27+
$promise->then($this->expectCallableOnce(), $this->expectCallableNever());
28+
}
29+
30+
public function testTimeoutExpire()
31+
{
32+
$will = $this->createConnectionManagerMock(new Stream(fopen('php://temp', 'r'), $this->loop));
33+
$wont = new ConnectionManagerDelay($will, $this->loop, 0.2);
34+
35+
$cm = new ConnectionManagerTimeout($wont, $this->loop, 0.1);
36+
37+
$promise = $cm->create('www.google.com', 80);
38+
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
39+
40+
$this->loop->run();
41+
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
42+
}
43+
44+
public function testTimeoutAbort()
45+
{
46+
$wont = new ConnectionManagerReject();
47+
48+
$cm = new ConnectionManagerTimeout($wont, $this->loop, 0.1);
49+
50+
$promise = $cm->create('www.google.com', 80);
51+
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
52+
53+
$this->loop->run();
54+
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
55+
}
56+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use ConnectionManager\Extra\Multiple\ConnectionManagerConsecutive;
4+
use ConnectionManager\Extra\ConnectionManagerReject;
5+
6+
class ConnectionManagerConsecutiveTest extends TestCase
7+
{
8+
public function testEmpty()
9+
{
10+
$cm = new ConnectionManagerConsecutive();
11+
12+
$promise = $cm->create('www.google.com', 80);
13+
14+
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
15+
16+
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
17+
}
18+
19+
public function testReject()
20+
{
21+
$wont = new ConnectionManagerReject();
22+
23+
$cm = new ConnectionManagerConsecutive();
24+
$cm->addConnectionManager($wont);
25+
26+
$promise = $cm->create('www.google.com', 80);
27+
28+
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
29+
30+
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use ConnectionManager\Extra\Multiple\ConnectionManagerRandom;
4+
use ConnectionManager\Extra\ConnectionManagerReject;
5+
6+
class ConnectionManagerRandomTest extends TestCase
7+
{
8+
public function testEmpty()
9+
{
10+
$cm = new ConnectionManagerRandom();
11+
12+
$promise = $cm->create('www.google.com', 80);
13+
14+
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
15+
16+
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
17+
}
18+
19+
public function testReject()
20+
{
21+
$wont = new ConnectionManagerReject();
22+
23+
$cm = new ConnectionManagerRandom();
24+
$cm->addConnectionManager($wont);
25+
26+
$promise = $cm->create('www.google.com', 80);
27+
28+
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
29+
30+
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
31+
}
32+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
use React\Stream\Stream;
4+
5+
use ConnectionManager\Extra\Multiple\ConnectionManagerSelective;
6+
use ConnectionManager\Extra\ConnectionManagerReject;
7+
8+
class ConnectionManagerSelectiveTest extends TestCase
9+
{
10+
public function testEmptyWillAlwaysReject()
11+
{
12+
$cm = new ConnectionManagerSelective();
13+
14+
$promise = $cm->create('www.google.com', 80);
15+
$this->assertPromiseReject($promise);
16+
}
17+
18+
public function testReject()
19+
{
20+
$wont = new ConnectionManagerReject();
21+
$will = $this->createConnectionManagerMock(new Stream(fopen('php://temp', 'r'), $this->createLoopMock()));
22+
23+
$cm = new ConnectionManagerSelective();
24+
25+
$cm->addConnectionManagerFor($will, 'www.google.com', 443);
26+
$cm->addConnectionManagerFor($will, 'www.youtube.com');
27+
28+
$this->assertPromiseResolve($cm->create('www.google.com', 443));
29+
30+
$this->assertPromiseReject($cm->create('www.google.com', 80));
31+
32+
$this->assertPromiseResolve($cm->create('www.youtube.com', 80));
33+
}
34+
35+
public function testRemove()
36+
{
37+
$wont = new ConnectionManagerReject();
38+
39+
$cm = new ConnectionManagerSelective();
40+
$this->assertCount(0, $cm->getConnectionManagerEntries());
41+
42+
$id = $cm->addConnectionManagerFor($wont);
43+
$this->assertCount(1, $cm->getConnectionManagerEntries());
44+
45+
$cm->removeConnectionManagerEntry($id);
46+
$this->assertCount(0, $cm->getConnectionManagerEntries());
47+
48+
// removing a non-existant ID is a NO-OP
49+
$cm->removeConnectionManagerEntry(12345);
50+
$this->assertCount(0, $cm->getConnectionManagerEntries());
51+
}
52+
53+
public function testSamePriorityFirstWins()
54+
{
55+
$wont = new ConnectionManagerReject();
56+
$will = $this->createConnectionManagerMock(new Stream(fopen('php://temp', 'r'), $this->createLoopMock()));
57+
58+
$cm = new ConnectionManagerSelective();
59+
60+
$cm->addConnectionManagerFor($will, 'www.google.com', 443, 100);
61+
$cm->addConnectionManagerFor($wont, ConnectionManagerSelective::MATCH_ALL, ConnectionManagerSelective::MATCH_ALL, 100);
62+
63+
$this->assertPromiseResolve($cm->create('www.google.com', 443));
64+
$this->assertPromiseReject($cm->create('www.google.com', 80));
65+
}
66+
67+
public function testWildcardsMatch()
68+
{
69+
$wont = new ConnectionManagerReject();
70+
$will = $this->createConnectionManagerMock(new Stream(fopen('php://temp', 'r'), $this->createLoopMock()));
71+
72+
$cm = new ConnectionManagerSelective();
73+
74+
$cm->addConnectionManagerFor($will, '*.com');
75+
$cm->addConnectionManagerFor($will, '*', '443-444,8080');
76+
$cm->addConnectionManagerFor($will, '*.youtube.*,youtube.*', '*');
77+
78+
$this->assertPromiseResolve($cm->create('www.google.com', 80));
79+
$this->assertPromiseReject($cm->create('www.google.de', 80));
80+
81+
$this->assertPromiseResolve($cm->create('www.google.de', 443));
82+
$this->assertPromiseResolve($cm->create('www.google.de', 444));
83+
$this->assertPromiseResolve($cm->create('www.google.de', 8080));
84+
$this->assertPromiseReject($cm->create('www.google.de', 445));
85+
86+
$this->assertPromiseResolve($cm->create('www.youtube.de', 80));
87+
$this->assertPromiseResolve($cm->create('download.youtube.de', 80));
88+
$this->assertPromiseResolve($cm->create('youtube.de', 80));
89+
}
90+
91+
private function createLoopMock()
92+
{
93+
return $this->getMockBuilder('React\EventLoop\StreamSelectLoop')
94+
->disableOriginalConstructor()
95+
->getMock();
96+
}
97+
}

0 commit comments

Comments
 (0)