Open Journal Systems  3.3.0
Server.php
1 <?php
2 
3 namespace Guzzle\Tests\Http;
4 
12 
27 class Server
28 {
29  const DEFAULT_PORT = 8124;
30  const REQUEST_DELIMITER = "\n----[request]\n";
31 
33  private $port;
34 
36  private $running = false;
37 
39  private $client;
40 
46  public function __construct($port = null)
47  {
48  $this->port = $port ?: self::DEFAULT_PORT;
49  $this->client = new Client($this->getUrl());
50  register_shutdown_function(array($this, 'stop'));
51  }
52 
57  public function flush()
58  {
59  $this->client->delete('guzzle-server/requests')->send();
60  }
61 
71  public function enqueue($responses)
72  {
73  $data = array();
74  foreach ((array) $responses as $response) {
75 
76  // Create the response object from a string
77  if (is_string($response)) {
78  $response = Response::fromMessage($response);
79  } elseif (!($response instanceof Response)) {
80  throw new BadResponseException('Responses must be strings or implement Response');
81  }
82 
83  $data[] = array(
84  'statusCode' => $response->getStatusCode(),
85  'reasonPhrase' => $response->getReasonPhrase(),
86  'headers' => $response->getHeaders()->toArray(),
87  'body' => $response->getBody(true)
88  );
89  }
90 
91  $request = $this->client->put('guzzle-server/responses', null, json_encode($data));
92  $request->send();
93  }
94 
100  public function isRunning()
101  {
102  if ($this->running) {
103  return true;
104  }
105 
106  try {
107  $this->client->get('guzzle-server/perf', array(), array('timeout' => 5))->send();
108  $this->running = true;
109  return true;
110  } catch (\Exception $e) {
111  return false;
112  }
113  }
114 
120  public function getUrl()
121  {
122  return 'http://127.0.0.1:' . $this->getPort() . '/';
123  }
124 
130  public function getPort()
131  {
132  return $this->port;
133  }
134 
145  public function getReceivedRequests($hydrate = false)
146  {
147  $response = $this->client->get('guzzle-server/requests')->send();
148  $data = array_filter(explode(self::REQUEST_DELIMITER, $response->getBody(true)));
149  if ($hydrate) {
150  $data = array_map(function($message) {
151  return RequestFactory::getInstance()->fromMessage($message);
152  }, $data);
153  }
154 
155  return $data;
156  }
157 
161  public function start()
162  {
163  if (!$this->isRunning()) {
164  exec('node ' . __DIR__ . \DIRECTORY_SEPARATOR
165  . 'server.js ' . $this->port
166  . ' >> /tmp/server.log 2>&1 &');
167  // Wait at most 5 seconds for the server the setup before
168  // proceeding.
169  $start = time();
170  while (!$this->isRunning() && time() - $start < 5);
171  if (!$this->running) {
172  throw new RuntimeException(
173  'Unable to contact server.js. Have you installed node.js v0.5.0+? node must be in your path.'
174  );
175  }
176  }
177  }
178 
182  public function stop()
183  {
184  if (!$this->isRunning()) {
185  return false;
186  }
187 
188  $this->running = false;
189  $this->client->delete('guzzle-server')->send();
190  }
191 }
Guzzle\Http\Message\RequestInterface
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestInterface.php:16
Guzzle\Tests\Http\Server\getUrl
getUrl()
Definition: Server.php:129
Guzzle\Tests\Http\Server\flush
flush()
Definition: Server.php:66
Guzzle\Tests\Http\Server\REQUEST_DELIMITER
const REQUEST_DELIMITER
Definition: Server.php:30
Guzzle\Http\Message\Response
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php:17
Guzzle\Tests\Http\Server\__construct
__construct($port=null)
Definition: Server.php:55
Guzzle\Http\Message\Response\fromMessage
static fromMessage($message)
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php:123
Guzzle\Tests\Http\Server\getReceivedRequests
getReceivedRequests($hydrate=false)
Definition: Server.php:154
Guzzle\Tests\Http\Server
Definition: Server.php:27
Guzzle\Http\Message\Request
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Request.php:25
Guzzle\Tests\Http\Server\isRunning
isRunning()
Definition: Server.php:109
Guzzle\Http\Message\RequestFactory
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestFactory.php:14
Guzzle\Tests\Http\Server\start
start()
Definition: Server.php:170
Guzzle\Http\Client
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Client.php:24
Guzzle\Tests\Http\Server\stop
stop()
Definition: Server.php:191
Guzzle\Http\Exception\BadResponseException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Exception/BadResponseException.php:11
Guzzle\Http\Message\RequestFactory\getInstance
static getInstance()
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestFactory.php:42
Guzzle\Tests\Http\Server\DEFAULT_PORT
const DEFAULT_PORT
Definition: Server.php:29
Guzzle\Tests\Http\Server\enqueue
enqueue($responses)
Definition: Server.php:80
Guzzle\Common\Exception\RuntimeException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/RuntimeException.php:5
Guzzle\Tests\Http\Server\getPort
getPort()
Definition: Server.php:139
Guzzle\Tests\Http
Definition: AbstractEntityBodyDecoratorTest.php:3