
PHPixie Illusion - a simple mock HTTP server for your tests

Another useful testing tool has been released, PHPixie Illusion .
If your application depends on external APIs, then you probably faced the problem of how to present them in your tests. This is exactly the problem that Illusion solves. It creates a separate HTTP server on the specified port and allows you to easily manage its routes and results, including HTTP headers. And
all this is extremely simple:
//Создаем сервер и клиент
$illusion = \PHPixie\Illusion::start($port = 4747);
//Добавляем роут и получаем его ссылку
$url = $illusion->route('/pixie/fairy', 'Trixie');
//И теперь
echo file_get_contents($url); // 'Trixie'
//Также можно задавать HTTP метод роута
//и заголовки ответа
$headers = array('Content-Type: text/plain');
$url = $illusion->route('/pixie', 'PHPixie', 'GET', $headers);
//Для остановки сервера
$client->stopServer();
//но сервер останавливается автоматически
//в деструкторе клиента
How it works ?
Pretty simple. In a separate process, a slightly modified StupidHTTP server is launched, to which two sockets are added for sending notifications with the client. Having received the appropriate command, the server adds the route to its configuration.
It should be noted that since the server receives a separate process, a possible option is when the main thread ends and the server remains hanging (for example, with an exception). To avoid this behavior, you can put a call to stopServer () in the exception handler in your test.