-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathFastCGITest.php
119 lines (93 loc) · 3.97 KB
/
FastCGITest.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace CacheTool\Adapter;
use CacheTool\Code;
use CacheTool\PhpFpmRunner;
use CacheTool\Adapter\FastCGI;
use \hollodotme\FastCGI\SocketConnections\NetworkSocket;
use \hollodotme\FastCGI\Requests\PostRequest;
use \hollodotme\FastCGI\Responses\Response;
use \Monolog\Logger;
class FastCGITest extends \PHPUnit\Framework\TestCase
{
public function testRun()
{
$fpm = new PhpFpmRunner();
$fcgi = new FastCGI($fpm->socket);
$fcgi->setTempDir(sys_get_temp_dir());
$fcgi->setLogger($this->getMockBuilder(Logger::class)->disableOriginalConstructor()->getMock());
$code = Code::fromString('return true;');
$result = $fcgi->run($code);
$this->assertTrue($result);
}
public function testGetScriptFileNameWithChroot()
{
$tmpdir = sys_get_temp_dir();
$fcgi = new FastCGI(null, $tmpdir);
$class = new \ReflectionClass($fcgi);
$method = $class->getMethod('getScriptFileName');
$method->setAccessible(true);
$this->assertSame('/test.php', $method->invoke($fcgi, "{$tmpdir}/test.php"));
}
public function testGetScriptFileNameWithoutChroot()
{
$fcgi = new FastCGI(null);
$class = new \ReflectionClass($fcgi);
$method = $class->getMethod('getScriptFileName');
$method->setAccessible(true);
$this->assertSame('/tmp/test.php', $method->invoke($fcgi, '/tmp/test.php'));
}
public function testRunWithChroot()
{
$fcgi = $this->getMockBuilder(FastCGI::class)
->setMethods(['getScriptFileName'])
->setConstructorArgs(['127.0.0.1:9000', sys_get_temp_dir()])
->getMock();
$reflection = new \ReflectionClass($fcgi);
$reflectionClient = $reflection->getProperty('client');
$reflectionClient->setAccessible(true);
$clientMock = $this->getMockBuilder(\hollodotme\FastCGI\Client::class)
->disableOriginalConstructor()
->getMock();
$reflectionClient->setValue($fcgi, $clientMock);
$fileName = '/tmp/testRunWithChroot/test.php';
$fcgi->expects(self::once())
->method('getScriptFileName')
->willReturn($fileName);
$connectionMock = new NetworkSocket('127.0.0.1', '9000', 5000, 120000);
$request = new PostRequest($fileName, '');
$response = new Response("Content-type: text/html; charset=UTF-8\r\n\r\na:2:{s:6:\"result\";b:1;s:6:\"errors\";a:0:{}}", '', 0);
$clientMock->expects(self::once())
->method('sendRequest')
->with($connectionMock, $request)
->willReturn($response);
$fcgi->setTempDir(sys_get_temp_dir());
$fcgi->setLogger($this->getMockBuilder(Logger::class)->disableOriginalConstructor()->getMock());
$code = Code::fromString('return true;');
$result = $fcgi->run($code);
$this->assertTrue($result);
}
public function testRunWithIPv4()
{
$fcgi = new FastCGI('127.0.0.1:9000');
$reflection = new \ReflectionClass($fcgi);
$reflectionConn = $reflection->getProperty('connection');
$reflectionConn->setAccessible(true);
$connection = $reflectionConn->getValue($fcgi);
$reflection = new \ReflectionClass($connection);
$reflectionHost = $reflection->getProperty('host');
$reflectionHost->setAccessible(true);
$this->assertSame('127.0.0.1', $reflectionHost->getValue($connection));
}
public function testRunWithIPv6()
{
$fcgi = new FastCGI(':::9000');
$reflection = new \ReflectionClass($fcgi);
$reflectionConn = $reflection->getProperty('connection');
$reflectionConn->setAccessible(true);
$connection = $reflectionConn->getValue($fcgi);
$reflection = new \ReflectionClass($connection);
$reflectionHost = $reflection->getProperty('host');
$reflectionHost->setAccessible(true);
$this->assertSame('[::]', $reflectionHost->getValue($connection));
}
}