This repository was archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathConnectionTest.php
69 lines (54 loc) · 1.89 KB
/
ConnectionTest.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
<?php
namespace BeyondCode\LaravelTinkerServer\Tests;
use BeyondCode\LaravelTinkerServer\Connection;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
use Symfony\Component\Process\PhpProcess;
use Symfony\Component\Process\Process;
class ConnectionTest extends TestCase
{
const TINKER_SERVER_HOST = 'tcp://127.0.0.1:9914';
public function testDump()
{
$connection = new Connection(self::TINKER_SERVER_HOST);
$dumped = null;
$process = $this->getServerProcess();
try {
$process->start(function ($type, $buffer) use ($process, &$dumped, $connection) {
if (Process::ERR === $type) {
$process->stop();
$this->fail();
} elseif ("READY\n" === $buffer) {
usleep(5000);
$result = $connection->write(['i' => 10]);
$this->assertTrue($result);
} else {
$dumped .= $buffer;
}
});
$process->wait();
} catch (ProcessTimedOutException $e) {
//
}
$this->assertSame("\r\e[K
\r\e[K>> \$i
=> 10\n", $dumped);
}
/** @test */
public function it_detects_if_the_tinker_server_is_offline()
{
$connection = new Connection(self::TINKER_SERVER_HOST);
$start = microtime(true);
$this->assertFalse($connection->write([]));
$this->assertLessThan(1, microtime(true) - $start);
}
protected function getServerProcess(): Process
{
$process = new PhpProcess(file_get_contents(__DIR__.'/fixtures/server.php'), null, [
'COMPONENT_ROOT' => __DIR__.'/../',
'TINKER_SERVER_HOST' => self::TINKER_SERVER_HOST,
]);
$process->inheritEnvironmentVariables(true);
return $process->setTimeout(3);
}
}