1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCache package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\HttpCache\Test; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This fake trait is used to have the same code and behavior between WebServerListener and its legacy version. |
16
|
|
|
*/ |
17
|
|
|
class WebServerListenerTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* PHP web server PID. |
21
|
|
|
* |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
protected $pid; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Make sure the PHP built-in web server is running for tests with group |
28
|
|
|
* 'webserver'. |
29
|
|
|
*/ |
30
|
|
|
public function startTestSuite($suite) |
31
|
|
|
{ |
32
|
|
|
// Only run on PHP >= 5.4 as PHP below that and HHVM don't have a |
33
|
|
|
// built-in web server |
34
|
|
|
if (defined('HHVM_VERSION')) { |
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (!in_array('webserver', $suite->getGroups()) || null !== $this->pid) { |
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->pid = $pid = $this->startPhpWebServer(); |
43
|
|
|
|
44
|
|
|
register_shutdown_function(function () use ($pid) { |
45
|
|
|
exec('kill '.$pid); |
46
|
|
|
}); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get web server hostname. |
51
|
|
|
* |
52
|
|
|
* @throws \Exception |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getHostName() |
57
|
|
|
{ |
58
|
|
|
if (!defined('WEB_SERVER_HOSTNAME')) { |
59
|
|
|
throw new \Exception('Set WEB_SERVER_HOSTNAME in your phpunit.xml'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return WEB_SERVER_HOSTNAME; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get web server port. |
67
|
|
|
* |
68
|
|
|
* @throws \Exception |
69
|
|
|
* |
70
|
|
|
* @return int |
71
|
|
|
*/ |
72
|
|
|
public function getPort() |
73
|
|
|
{ |
74
|
|
|
if (!defined('WEB_SERVER_PORT')) { |
75
|
|
|
throw new \Exception('Set WEB_SERVER_PORT in your phpunit.xml'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return WEB_SERVER_PORT; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get web server port. |
83
|
|
|
* |
84
|
|
|
* @throws \Exception |
85
|
|
|
* |
86
|
|
|
* @return int |
87
|
|
|
*/ |
88
|
|
|
public function getDocRoot() |
89
|
|
|
{ |
90
|
|
|
if (!defined('WEB_SERVER_DOCROOT')) { |
91
|
|
|
throw new \Exception('Set WEB_SERVER_DOCROOT in your phpunit.xml'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return WEB_SERVER_DOCROOT; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Start PHP built-in web server. |
99
|
|
|
* |
100
|
|
|
* @return int PID |
101
|
|
|
*/ |
102
|
|
|
public function startPhpWebServer() |
103
|
|
|
{ |
104
|
|
|
$command = sprintf( |
105
|
|
|
'php -S %s:%d -t %s >/dev/null 2>&1 & echo $!', |
106
|
|
|
'127.0.0.1', // on travis, localhost is not 127.0.0.1 but IPv6 ::1 |
107
|
|
|
$this->getPort(), |
108
|
|
|
$this->getDocRoot() |
109
|
|
|
); |
110
|
|
|
exec($command, $output); |
111
|
|
|
|
112
|
|
|
$this->waitFor($this->getHostName(), $this->getPort(), 2000); |
113
|
|
|
|
114
|
|
|
return $output[0]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Wait for caching proxy to be started up and reachable. |
119
|
|
|
* |
120
|
|
|
* @param string $ip |
121
|
|
|
* @param int $port |
122
|
|
|
* @param int $timeout Timeout in milliseconds |
123
|
|
|
* |
124
|
|
|
* @throws \RuntimeException If proxy is not reachable within timeout |
125
|
|
|
*/ |
126
|
|
|
public function waitFor($ip, $port, $timeout) |
127
|
|
|
{ |
128
|
|
|
for ($i = 0; $i < $timeout; ++$i) { |
129
|
|
|
if (@fsockopen($ip, $port)) { |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
usleep(1000); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
throw new \RuntimeException( |
137
|
|
|
sprintf( |
138
|
|
|
'Webserver cannot be reached at %s:%s', |
139
|
|
|
$ip, |
140
|
|
|
$port |
141
|
|
|
) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|