Skip to content

Fix poll wait and update dependences #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"email": "joshua.eichorn@pagely.com"
}
],
"require": {
"require": {
"php": "~7.2",
"ext-json": "*",
"symfony/process": "~3|~4|~5",
"jeremeamia/superclosure": "~2",
"lstrojny/hmmmath": ">=0.5.0",
Expand All @@ -27,14 +28,18 @@
"sensiolabs/security-checker": "^6.0"
},
"require-dev": {
"internations/testing-component": "1.0.1",
"internations/testing-component": "1.3.0",
"phpunit/phpunit": "^7"
},
"autoload": {
"psr-4": {"InterNations\\Component\\HttpMock\\": "src/"}
"psr-4": {
"InterNations\\Component\\HttpMock\\": "src/"
}
},
"autoload-dev": {
"psr-4": {"InterNations\\Component\\HttpMock\\Tests\\": "tests/"}
"psr-4": {
"InterNations\\Component\\HttpMock\\Tests\\": "tests/"
}
},
"scripts": {
"auto-scripts": [
Expand Down
7 changes: 1 addition & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.4/phpunit.xsd"
backupGlobals="false"
convertErrorsToExceptions="true"
convertWarningsToExceptions="true"
convertNoticesToExceptions="true"
bootstrap="vendor/autoload.php"
verbose="true"
colors="true"
Expand All @@ -17,13 +13,12 @@
</testsuites>

<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="junit" target="build/logs/junit.xml" />
</logging>

<php>
<ini name="error_reporting" value="-1"/>
<ini name="date.timezone" value="Europe/Berlin"/>
<ini name="date.timezone" value="America/Mexico_City"/>
<const name="HTTP_MOCK_PORT" value="38080"/>
<const name="HTTP_MOCK_HOST" value="localhost"/>
</php>
Expand Down
48 changes: 10 additions & 38 deletions src/RequestCollectionFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Countable;
use GuzzleHttp\Client;
use function GuzzleHttp\Psr7\parse_request;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use UnexpectedValueException;
Expand All @@ -17,52 +19,32 @@ public function __construct(Client $client)
$this->client = $client;
}

/**
* @return UnifiedRequest
*/
public function latest()
public function latest() : RequestInterface
{
return $this->getRecordedRequest('/_request/last');
}

/**
* @return UnifiedRequest
*/
public function last()
public function last() : RequestInterface
{
return $this->getRecordedRequest('/_request/last');
}

/**
* @return UnifiedRequest
*/
public function first()
public function first() : RequestInterface
{
return $this->getRecordedRequest('/_request/first');
}

/**
* @param int $position
*
* @return UnifiedRequest
*/
public function at($position)
public function at($position) : RequestInterface
{
return $this->getRecordedRequest('/_request/' . $position);
}

/**
* @return UnifiedRequest
*/
public function pop()
public function pop() : RequestInterface
{
return $this->deleteRecordedRequest('/_request/last');
}

/**
* @return UnifiedRequest
*/
public function shift()
public function shift() : RequestInterface
{
return $this->deleteRecordedRequest('/_request/first');
}
Expand All @@ -75,15 +57,7 @@ public function count()
return (int) $response->getBody()->getContents();
}

/**
* @param Response $response
* @param string $path
*
* @throws UnexpectedValueException
*
* @return RequestInterface
*/
private function parseRequestFromResponse(ResponseInterface $response, $path)
private function parseRequestFromResponse(ResponseInterface $response, $path) : Request
{
try {
$contents = $response->getBody()->getContents();
Expand All @@ -92,9 +66,7 @@ private function parseRequestFromResponse(ResponseInterface $response, $path)
throw new UnexpectedValueException(sprintf('Cannot deserialize response from "%s": "%s"', $path, $contents), null, $e);
}

$request = \GuzzleHttp\Psr7\parse_request($requestInfo['request']);

return $request;
return parse_request($requestInfo['request']);
}

private function getRecordedRequest($path)
Expand Down
16 changes: 10 additions & 6 deletions src/ResponseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use Closure;
use GuzzleHttp\Psr7\Response;
use function GuzzleHttp\Psr7\stream_for;
use Psr\Http\Message\ResponseInterface;
use SuperClosure\SerializableClosure;
use SuperClosure\SerializerInterface;

class ResponseBuilder
{
Expand All @@ -14,6 +17,7 @@ class ResponseBuilder
/** @var Response */
private $response;

/** @var SerializerInterface|null */
private $responseCallback;

public function __construct(MockBuilder $mockBuilder)
Expand All @@ -22,7 +26,7 @@ public function __construct(MockBuilder $mockBuilder)
$this->response = new Response();
}

public function statusCode($statusCode)
public function statusCode($statusCode) : self
{
$this->response = $this->response->withStatus($statusCode);

Expand All @@ -31,31 +35,31 @@ public function statusCode($statusCode)

public function body($body)
{
$this->response = $this->response->withBody(\GuzzleHttp\Psr7\stream_for($body));
$this->response = $this->response->withBody(stream_for($body));

return $this;
}

public function callback(Closure $callback)
public function callback(Closure $callback) : self
{
$this->responseCallback = new SerializableClosure($callback);

return $this;
}

public function header($header, $value)
public function header($header, $value) : self
{
$this->response = $this->response->withHeader($header, $value);

return $this;
}

public function end()
public function end() : MockBuilder
{
return $this->mockBuilder;
}

public function getResponse()
public function getResponse() : ResponseInterface
{
return $this->response;
}
Expand Down
24 changes: 8 additions & 16 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@

class Server extends Process
{
/** @var int */
private $port;

/** @var string */
private $host;

/** @var Client */
private $client;

public function __construct($port, $host)
Expand Down Expand Up @@ -40,7 +43,7 @@ public function start(callable $callback = null, array $env = [])
$this->pollWait();
}

public function stop($timeout = 10, $signal = null)
public function stop($timeout = 10, int $signal = null)
{
return parent::stop($timeout, $signal);
}
Expand All @@ -52,9 +55,7 @@ public function getClient()

private function createClient()
{
$client = new Client(['base_uri' => $this->getBaseUrl(), 'http_errors' => false]);

return $client;
return new Client(['base_uri' => $this->getBaseUrl(), 'http_errors' => false]);
}

public function getBaseUrl()
Expand Down Expand Up @@ -103,23 +104,14 @@ public function clean()

private function pollWait()
{
$success = false;
foreach (FibonacciFactory::sequence(50000, 10000, 8) as $sleepTime) {
foreach (FibonacciFactory::sequence(50000, 10000) as $sleepTime) {
try {
usleep($sleepTime);
$r = $this->getClient()->head('/_me');
if ($r->getStatusCode() != 418) {
continue;
}
$success = true;
$this->getClient()->head('/_me');
break;
} catch (ServerException $e) {
} catch (\Exception $e) {
continue;
}
}

if (!$success) {
throw $e;
}
}
}
6 changes: 2 additions & 4 deletions tests/AppIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace InterNations\Component\HttpMock\Tests;

use Guzzle\Http\Message\EntityEnclosingRequest;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use InterNations\Component\HttpMock\Server;
use InterNations\Component\Testing\AbstractTestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use SuperClosure\SerializableClosure;

/**
Expand Down Expand Up @@ -76,7 +75,6 @@ static function ($request) {

$response = $this->client->get('/_request/latest');

/** @var EntityEnclosingRequest $request */
$request = $this->parseRequestFromResponse($response);
$this->assertSame('1', (string) $request->getHeaderLine('X-Special'));
$this->assertSame('post=data', (string) $request->getBody());
Expand Down Expand Up @@ -237,7 +235,7 @@ static function ($request) {
$this->assertSame('second', $this->client->get('/')->getBody()->getContents());
}

private function parseRequestFromResponse(Response $response)
private function parseRequestFromResponse(ResponseInterface $response)
{
$body = unserialize($response->getBody());

Expand Down