diff --git a/composer.json b/composer.json index 91dd475..666de43 100644 --- a/composer.json +++ b/composer.json @@ -19,22 +19,23 @@ }, "require": { - "php": "^7.1", + "php": "^7.3", "beberlei/assert": "^3.2", "symfony/property-access": "^3.1 || ^4.0 || ^5.0", + "psr/http-client": "^1.0", + "psr/http-client-implementation": "^1.0", + "psr/http-message": "^1.0", "psr/http-message-implementation": "^1.0", - "php-http/httplug": "^1.0", - "php-http/discovery": "^1.0", - "php-http/client-common": "^1.4", - "php-http/message-factory": "^1.0", + "psr/http-factory": "^1.0", + "psr/http-factory-implementation": "^1.0", - "php-http/client-implementation": "^1.0", - "php-http/message-factory-implementation": "^1.0" + "php-http/discovery": "^1.0", + "php-http/client-common": "^2.0" }, "suggest": { @@ -57,8 +58,8 @@ "coduo/php-matcher": "^2.3 || ^3.0", "php-http/message": "^1.0", - "php-http/mock-client": "^0.3", + "symfony/http-client": "^4.3 || ^5.0", "symfony/var-dumper": "^2.8 || ^3.3 || ^4.0", "phpstan/phpstan": "^0.10", diff --git a/src/Container.php b/src/Container.php index a5077e5..8efcc0d 100644 --- a/src/Container.php +++ b/src/Container.php @@ -5,17 +5,6 @@ use Behat\Behat\HelperContainer\Exception\ServiceNotFoundException; -use Symfony\Component\EventDispatcher\EventDispatcher; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; - -use Http\Message\StreamFactory; -use Http\Message\MessageFactory; - -use Http\Discovery\UriFactoryDiscovery; -use Http\Discovery\StreamFactoryDiscovery; -use Http\Discovery\MessageFactoryDiscovery; - -use Http\Client\Common\PluginClient; use Http\Client\Common\Plugin\BaseUriPlugin; use Http\Client\Common\Plugin\HistoryPlugin; use Http\Client\Common\Plugin\ContentLengthPlugin; @@ -23,6 +12,11 @@ use Behapi\Http\PluginClientBuilder; use Behapi\HttpHistory\History as HttpHistory; +use Http\Discovery\Psr17FactoryDiscovery; + +use Psr\Http\Message\RequestFactoryInterface; +use Psr\Http\Message\StreamFactoryInterface; + use function bin2hex; use function in_array; use function random_bytes; @@ -42,20 +36,18 @@ public function __construct(HttpHistory $history, string $baseUrl) $this->services[HttpHistory::class] = $history; } - /** {@inheritDoc} */ public function has($id) { static $services = [ HttpHistory::class, - StreamFactory::class, - MessageFactory::class, PluginClientBuilder::class, + StreamFactoryInterface::class, + RequestFactoryInterface::class, ]; return in_array($id, $services, true); } - /** {@inheritDoc} */ public function get($id) { if (array_key_exists($id, $this->services)) { @@ -66,11 +58,11 @@ public function get($id) case PluginClientBuilder::class: return $this->services[$id] = $this->getPluginClientBuilder(); - case MessageFactory::class: - return $this->services[$id] = MessageFactoryDiscovery::find(); + case RequestFactoryInterface::class: + return $this->services[$id] = Psr17FactoryDiscovery::findRequestFactory(); - case StreamFactory::class: - return $this->services[$id] = StreamFactoryDiscovery::find(); + case StreamFactoryInterface::class: + return $this->services[$id] = Psr17FactoryDiscovery::findStreamFactory(); } throw new ServiceNotFoundException("Service {$id} is not available", $id); @@ -79,7 +71,8 @@ public function get($id) private function getPluginClientBuilder(): PluginClientBuilder { $builder = new PluginClientBuilder; - $uriFactory = UriFactoryDiscovery::find(); + $uriFactory = Psr17FactoryDiscovery::findUrlFactory(); + $baseUri = $uriFactory->createUri($this->baseUrl); assert($this->services[HttpHistory::class] instanceof HttpHistory); diff --git a/src/Http/Builder.php b/src/Http/Builder.php deleted file mode 100644 index c13a25c..0000000 --- a/src/Http/Builder.php +++ /dev/null @@ -1,17 +0,0 @@ - Query args to add */ + private $query = []; - /** @var HttpClient|HttpAsyncClient */ + /** @var ClientInterface */ private $client; - public function __construct(PluginClientBuilder $builder, StreamFactory $streamFactory, MessageFactory $messageFactory) + /** @var PluginClientBuilder */ + private $builder; + + /** @var StreamFactoryInterface */ + private $streamFactory; + + /** @var RequestFactoryInterface */ + private $requestFactory; + + public function __construct(PluginClientBuilder $builder, StreamFactoryInterface $streamFactory, RequestFactoryInterface $requestFactory) { $this->builder = $builder; $this->streamFactory = $streamFactory; - $this->messageFactory = $messageFactory; + $this->requestFactory = $requestFactory; $this->client = HttpClientDiscovery::find(); } @@ -48,7 +52,7 @@ final public function create_a_request(string $method, string $url): void $url = trim($url); $this->query = []; - $this->request = $this->messageFactory->createRequest(strtoupper($method), $url); + $this->request = $this->requestFactory->createRequest(strtoupper($method), $url); // let's set a default content-type $this->set_the_content_type($this->getDefaultContentType()); @@ -61,14 +65,17 @@ final public function create_a_request(string $method, string $url): void * * Shortcut for `When I create a X request to Then send the request` */ - final public function send_a_request($method, $url): void + final public function send_a_request(string $method, string $url): void { $this->create_a_request($method, $url); $this->send_request(); } - /** @When I add/set the value :value to the parameter :parameter */ - final public function add_a_parameter(string $parameter, string $value): void + /** + * @param mixed $value + * @When I add/set the value :value to the parameter :parameter + */ + final public function add_a_parameter(string $parameter, $value): void { if (!isset($this->query[$parameter])) { $this->query[$parameter] = $value; diff --git a/src/HttpHistory/History.php b/src/HttpHistory/History.php index 3926f38..5a2640d 100644 --- a/src/HttpHistory/History.php +++ b/src/HttpHistory/History.php @@ -5,10 +5,10 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Client\ClientExceptionInterface; use Http\Client\Common\Plugin\Journal; -use Http\Client\Exception; use Http\Client\Exception\HttpException; use function end; @@ -17,21 +17,20 @@ final class History implements Journal, IteratorAggregate { - /** @var Tuple[] */ + /** @var list */ private $tuples = []; - /** {@inheritDoc} */ - public function addSuccess(RequestInterface $request, ResponseInterface $response) + public function addSuccess(RequestInterface $request, ResponseInterface $response): void { $this->tuples[] = new Tuple($request, $response); } - /** {@inheritDoc} */ - public function addFailure(RequestInterface $request, Exception $exception) + public function addFailure(RequestInterface $request, ClientExceptionInterface $exception): void { $response = $exception instanceof HttpException ? $exception->getResponse() - : null; + : null + ; $this->tuples[] = new Tuple($request, $response); } @@ -42,7 +41,6 @@ public function getLastResponse(): ResponseInterface throw new NoResponse; } - /** @var Tuple $tuple */ $tuple = end($this->tuples); reset($this->tuples); @@ -55,8 +53,7 @@ public function getLastResponse(): ResponseInterface return $response; } - /** @return iterable */ - public function getIterator(): iterable + public function getIterator() { yield from $this->tuples; diff --git a/src/Json/Context.php b/src/Json/Context.php index 09528ba..a08e9e4 100644 --- a/src/Json/Context.php +++ b/src/Json/Context.php @@ -1,7 +1,6 @@