Skip to content

Commit 47971f6

Browse files
author
ace411
committed
test: add project tests
1 parent e453cdf commit 47971f6

File tree

10 files changed

+255
-0
lines changed

10 files changed

+255
-0
lines changed

tests/001.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
init() creates instance of Mrloop
3+
--FILE--
4+
<?php
5+
6+
use ringphp\Mrloop;
7+
8+
$loop = Mrloop::init();
9+
10+
var_dump($loop instanceof Mrloop);
11+
12+
?>
13+
--EXPECT--
14+
bool(true)

tests/002.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
addReadStream() funnels file descriptor in readable stream into event loop and thence executes a non-blocking read operation
3+
--FILE--
4+
<?php
5+
6+
use ringphp\Mrloop;
7+
8+
$loop = Mrloop::init();
9+
10+
$loop->addReadStream(
11+
\popen('php -m | grep "mrloop"', 'r'),
12+
null,
13+
function (...$args) use ($loop) {
14+
[$data] = $args;
15+
16+
var_dump(\rtrim($data));
17+
18+
$loop->stop();
19+
},
20+
);
21+
22+
$loop->run();
23+
24+
?>
25+
--EXPECT--
26+
string(6) "mrloop"

tests/003.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
addWriteStream() funnels file descriptor in writable stream into event loop and thence executes a non-blocking write operation
3+
--FILE--
4+
<?php
5+
6+
use ringphp\Mrloop;
7+
8+
$loop = Mrloop::init();
9+
10+
$file = \sprintf("%s/file.txt", __DIR__);
11+
12+
$loop->addWriteStream(
13+
\fopen($file, 'w'),
14+
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
15+
function (int $nbytes) use ($file, $loop) {
16+
var_dump($nbytes);
17+
18+
\unlink($file);
19+
20+
$loop->stop();
21+
},
22+
);
23+
24+
$loop->run();
25+
26+
?>
27+
--EXPECT--
28+
int(56)

tests/004.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
parseHttpRequest() parses HTTP request
3+
--FILE--
4+
<?php
5+
6+
use ringphp\Mrloop;
7+
8+
var_dump(
9+
Mrloop::parseHttpRequest(
10+
"GET / HTTP/1.1\r\nhost: localhost:8080\r\ncontent-type: text/plain\r\n\r\n",
11+
),
12+
);
13+
?>
14+
--EXPECT--
15+
array(4) {
16+
["path"]=>
17+
string(1) "/"
18+
["method"]=>
19+
string(3) "GET"
20+
["body"]=>
21+
string(0) ""
22+
["headers"]=>
23+
array(2) {
24+
["host"]=>
25+
string(14) "localhost:8080"
26+
["content-type"]=>
27+
string(10) "text/plain"
28+
}
29+
}

tests/005.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
parseHttpRequest() throws exception when invalid HTTP request syntax is encountered
3+
--FILE--
4+
<?php
5+
6+
use ringphp\Mrloop;
7+
8+
try {
9+
var_dump(
10+
Mrloop::parseHttpRequest("foo\nbar\n"),
11+
);
12+
} catch (Throwable $err) {
13+
var_dump(
14+
$err->getMessage(),
15+
);
16+
}
17+
18+
?>
19+
--EXPECT--
20+
string(44) "There is an error in the HTTP request syntax"

tests/006.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
parseHttpResponse() parses HTTP response
3+
--FILE--
4+
<?php
5+
6+
use ringphp\Mrloop;
7+
8+
var_dump(
9+
Mrloop::parseHttpResponse(
10+
"HTTP/1.1 200 OK\r\nserver: mrloop\r\ncontent-type: text/plain\r\n\r\nHello, user",
11+
),
12+
);
13+
14+
?>
15+
--EXPECT--
16+
array(4) {
17+
["reason"]=>
18+
string(2) "OK"
19+
["status"]=>
20+
int(200)
21+
["body"]=>
22+
string(11) "Hello, user"
23+
["headers"]=>
24+
array(2) {
25+
["server"]=>
26+
string(6) "mrloop"
27+
["content-type"]=>
28+
string(10) "text/plain"
29+
}
30+
}

tests/007.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
parseHttpResponse() throws exception when invalid HTTP response syntax is encountered
3+
--FILE--
4+
<?php
5+
6+
use ringphp\Mrloop;
7+
8+
try {
9+
var_dump(
10+
Mrloop::parseHttpResponse("foo\r\nbar\r\n"),
11+
);
12+
} catch (Throwable $err) {
13+
var_dump(
14+
$err->getMessage(),
15+
);
16+
}
17+
18+
?>
19+
--EXPECT--
20+
string(45) "There is an error in the HTTP response syntax"

tests/008.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
addSignal() performs a specified action in the event that a specified signal is detected
3+
--FILE--
4+
<?php
5+
6+
use ringphp\Mrloop;
7+
8+
$loop = Mrloop::init();
9+
10+
$loop->addReadStream(
11+
\popen('echo "Readable"', 'r'),
12+
null,
13+
function (...$args) use ($loop) {
14+
[$data] = $args;
15+
16+
echo \rtrim($data) . PHP_EOL;
17+
18+
\posix_kill(\posix_getpid(), SIGINT);
19+
},
20+
);
21+
22+
$loop->addSignal(
23+
SIGINT,
24+
function () {
25+
echo "Terminated with SIGINT\n";
26+
},
27+
);
28+
29+
$loop->run();
30+
31+
?>
32+
--EXPECT--
33+
Readable
34+
Terminated with SIGINT

tests/009.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
addTimer() executes a specified action after a specified amount of time
3+
--FILE--
4+
<?php
5+
6+
use ringphp\Mrloop;
7+
8+
$loop = Mrloop::init();
9+
10+
$loop->addTimer(
11+
1.5,
12+
function () use ($loop) {
13+
var_dump('Tick');
14+
15+
$loop->stop();
16+
},
17+
);
18+
19+
$loop->run();
20+
21+
?>
22+
--EXPECT--
23+
string(4) "Tick"

tests/010.phpt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
addPeriodicTimer() executes a specified action in perpetuity with each successive execution occurring after a specified time interval
3+
--FILE--
4+
<?php
5+
6+
use ringphp\Mrloop;
7+
8+
$loop = Mrloop::init();
9+
10+
$tick = 0;
11+
12+
$loop->addPeriodicTimer(
13+
1.2,
14+
function () use ($loop, &$tick) {
15+
var_dump(++$tick);
16+
17+
if ($tick === 5) {
18+
$loop->stop();
19+
}
20+
},
21+
);
22+
23+
$loop->run();
24+
25+
?>
26+
--EXPECT--
27+
int(1)
28+
int(2)
29+
int(3)
30+
int(4)
31+
int(5)

0 commit comments

Comments
 (0)