Skip to content

separate http client from trait #224

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 1 commit into from
Jun 25, 2015
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
60 changes: 60 additions & 0 deletions src/Test/HttpCaller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the FOSHttpCache package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\HttpCache\Test;

use FOS\HttpCache\Test\HttpClient;
use Psr\Http\Message\ResponseInterface;

/**
* Provides a method for getting responses from your application
*/
trait HttpCaller
{
/**
* HTTP adapter for requests to the application
*
* @var HttpClient
*/
protected $client;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it need to be protected ?


/**
* Call a HTTP resource from your test
*
* @param string $uri HTTP URI, domain and port are added from the embedding class if not specified
* @param array $headers HTTP headers
* @param string $method HTTP method
*
* @return ResponseInterface
*/
public function getResponse($uri, array $headers = [], $method = 'GET')
{
if (!$this->client) {
$this->client = new HttpClient($this->getHostName(), $this->getCachingProxyPort());
}

return $this->client->getResponse($uri, $headers, $method);
}

/**
* Get the default host name to use.
*
* @return string
*/
abstract protected function getHostName();

/**
* Get the default port to use.
*
* @return string
*/
abstract protected function getCachingProxyPort();
}
34 changes: 26 additions & 8 deletions src/Test/AppClient.php → src/Test/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,36 @@
use Psr\Http\Message\UriInterface;

/**
* Provides a HTTP client for getting responses from your application
* Provides a very simple way to fetch HTTP responses, auto-creating a client.
*/
trait AppClient
class HttpClient
{
/**
* HTTP adapter for requests to the application
*
* @var HttpAdapter
*/
protected $httpAdapter;
private $httpAdapter;

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

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

/**
* @param string $hostname Default hostname if not specified in the URL
* @param string $port Default port if not specified in the URL
*/
public function __construct($hostname, $port)
{
$this->hostname = $hostname;
$this->port = $port;
}

/**
* Get HTTP response from your application
Expand All @@ -40,7 +60,7 @@ trait AppClient
*
* @return ResponseInterface
*/
public function getResponse($uri, array $headers = [], $method = 'GET')
public function getResponse($uri, array $headers, $method)
{
// Close connections to make sure invalidation (PURGE/BAN) requests will
// not interfere with content (GET) requests.
Expand Down Expand Up @@ -79,11 +99,11 @@ protected function createRequest($method, $uri, $headers)
$uri = $this->createUri($uri);
if ($uri->getHost() === '') {
// Add base URI host
$uri = $uri->withHost($this->getHostName());
$uri = $uri->withHost($this->hostname);
}

if (!$uri->getPort()) {
$uri = $uri->withPort($this->getCachingProxyPort());
$uri = $uri->withPort($this->port);
}

if ($uri->getScheme() === '') {
Expand All @@ -109,6 +129,4 @@ protected function createUri($uriString)
{
return UriFactoryDiscovery::find()->createUri($uriString);
}

abstract protected function getHostName();
}
2 changes: 1 addition & 1 deletion src/Test/ProxyTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
*/
abstract class ProxyTestCase extends \PHPUnit_Framework_TestCase
{
use AppClient;
use HttpCaller;
use CacheAssertions;
}