diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..20b110f --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,32 @@ +name: Tests with PHPUnit + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Validate composer.json and composer.lock + run: composer validate --strict + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v2 + with: + path: vendor + key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-php- + - name: Install dependencies + run: composer install --prefer-dist --no-progress + + - name: Run test suite + run: composer run-script test \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 1c5e371..8ba996e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,13 @@ language: php php: - - 7.1 - - 7.2 - 7.3 + - 7.4 + - 8.0 sudo: false -dist: trusty +dist: bionic before_script: - composer self-update @@ -18,4 +18,4 @@ script: - vendor/bin/phpunit --coverage-clover build/logs/clover.xml after_script: - - vendor/bin/php-coveralls -v + - vendor/bin/php-coveralls -v \ No newline at end of file diff --git a/composer.json b/composer.json index d1c56b9..d8bc7b4 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,9 @@ { "name": "jenssegers/proxy", "description": "Proxy library that forwards requests to the desired url and returns the response.", - "keywords": ["proxy"], + "keywords": [ + "proxy" + ], "homepage": "https://github.com/jenssegers/php-proxy", "license": "MIT", "authors": [ @@ -16,17 +18,16 @@ } ], "require": { - "php": "^5.6 || ^7.0", - "psr/http-message": "^1.0", - "guzzlehttp/guzzle": "^6.0", + "php": "^7.3|^8.0", + "guzzlehttp/guzzle": "^7.0.1", "laminas/laminas-diactoros": "^2.0", - "relay/relay": "^1.0", + "relay/relay": "~2.0", "laminas/laminas-httphandlerrunner": "^1.1" }, "require-dev": { - "phpunit/phpunit": "^5.0|^6.0|^7.0", + "phpunit/phpunit": "^9.3", "php-coveralls/php-coveralls": "^2.0", - "mockery/mockery": "^1.1" + "mockery/mockery": "^1.4.2" }, "autoload": { "psr-4": { @@ -37,5 +38,8 @@ "psr-4": { "Proxy\\": "tests" } + }, + "scripts": { + "test": "vendor/bin/phpunit" } -} +} \ No newline at end of file diff --git a/src/Filter/FilterInterface.php b/src/Filter/FilterInterface.php index 33e1a53..1a8cf44 100644 --- a/src/Filter/FilterInterface.php +++ b/src/Filter/FilterInterface.php @@ -14,5 +14,5 @@ interface FilterInterface * @param callable $next * @return ResponseInterface */ - public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next); + public function __invoke(RequestInterface $request, callable $next): ResponseInterface; } diff --git a/src/Filter/RemoveEncodingFilter.php b/src/Filter/RemoveEncodingFilter.php index e4ee059..9e3a9c6 100644 --- a/src/Filter/RemoveEncodingFilter.php +++ b/src/Filter/RemoveEncodingFilter.php @@ -13,9 +13,9 @@ class RemoveEncodingFilter implements FilterInterface /** * @inheritdoc */ - public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) + public function __invoke(RequestInterface $request, callable $next): ResponseInterface { - $response = $next($request, $response); + $response = $next($request); return $response ->withoutHeader(self::TRANSFER_ENCODING) diff --git a/src/Filter/RemoveLocationFilter.php b/src/Filter/RemoveLocationFilter.php index 2b4bd2b..e1307e9 100644 --- a/src/Filter/RemoveLocationFilter.php +++ b/src/Filter/RemoveLocationFilter.php @@ -12,9 +12,9 @@ class RemoveLocationFilter implements FilterInterface /** * @inheritdoc */ - public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) + public function __invoke(RequestInterface $request, callable $next): ResponseInterface { - $response = $next($request, $response); + $response = $next($request); if ($response->hasHeader(self::LOCATION)) { $response = $response diff --git a/src/Filter/RewriteLocationFilter.php b/src/Filter/RewriteLocationFilter.php index 2d1fec8..ccebdea 100644 --- a/src/Filter/RewriteLocationFilter.php +++ b/src/Filter/RewriteLocationFilter.php @@ -12,9 +12,9 @@ class RewriteLocationFilter implements FilterInterface /** * @inheritdoc */ - public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) + public function __invoke(RequestInterface $request, callable $next): ResponseInterface { - $response = $next($request, $response); + $response = $next($request); if ($response->hasHeader(self::LOCATION)) { $location = $response->getHeader(self::LOCATION)[0]; diff --git a/src/Proxy.php b/src/Proxy.php index 42d4bfb..042e78d 100644 --- a/src/Proxy.php +++ b/src/Proxy.php @@ -2,14 +2,15 @@ namespace Proxy; -use GuzzleHttp\Exception\ClientException; +use Relay\RelayBuilder; +use Laminas\Diactoros\Uri; +use Laminas\Diactoros\Response; use Proxy\Adapter\AdapterInterface; -use Proxy\Exception\UnexpectedValueException; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; -use Relay\RelayBuilder; -use Laminas\Diactoros\Response; -use Laminas\Diactoros\Uri; +use Psr\Http\Server\MiddlewareInterface; +use GuzzleHttp\Exception\ClientException; +use Proxy\Exception\UnexpectedValueException; class Proxy { @@ -79,19 +80,19 @@ public function to($target) $stack = $this->filters; - $stack[] = function (RequestInterface $request, ResponseInterface $response, callable $next) { + $stack[] = function (RequestInterface $request, callable $next) { try { $response = $this->adapter->send($request); } catch (ClientException $ex) { $response = $ex->getResponse(); } - return $next($request, $response); + return $response; }; $relay = (new RelayBuilder)->newInstance($stack); - return $relay($request, new Response); + return $relay->handle($request); } /** diff --git a/tests/Proxy/Adapter/Dummy/DummyAdapterTest.php b/tests/Proxy/Adapter/Dummy/DummyAdapterTest.php index 62a869b..46a3df0 100644 --- a/tests/Proxy/Adapter/Dummy/DummyAdapterTest.php +++ b/tests/Proxy/Adapter/Dummy/DummyAdapterTest.php @@ -13,7 +13,7 @@ class DummyAdapterTest extends TestCase */ private $adapter; - public function setUp() + public function setUp(): Void { $this->adapter = new DummyAdapter(); } diff --git a/tests/Proxy/Adapter/Guzzle/GuzzleAdapterTest.php b/tests/Proxy/Adapter/Guzzle/GuzzleAdapterTest.php index 9f9940b..f175dd3 100644 --- a/tests/Proxy/Adapter/Guzzle/GuzzleAdapterTest.php +++ b/tests/Proxy/Adapter/Guzzle/GuzzleAdapterTest.php @@ -32,7 +32,7 @@ class GuzzleAdapterTest extends TestCase */ private $body = 'Totally awesome response body'; - public function setUp() + public function setUp(): Void { $mock = new MockHandler([ $this->createResponse(), diff --git a/tests/Proxy/Filter/RemoveEncodingFilterTest.php b/tests/Proxy/Filter/RemoveEncodingFilterTest.php index c407695..60c81e1 100644 --- a/tests/Proxy/Filter/RemoveEncodingFilterTest.php +++ b/tests/Proxy/Filter/RemoveEncodingFilterTest.php @@ -13,7 +13,7 @@ class RemoveEncodingFilterTest extends TestCase */ private $filter; - public function setUp() + public function setUp(): Void { $this->filter = new RemoveEncodingFilter(); } @@ -29,7 +29,7 @@ public function filter_removes_transfer_encoding() return $response; }; - $response = call_user_func($this->filter, $request, $response, $next); + $response = call_user_func($this->filter, $request, $next); $this->assertFalse($response->hasHeader(RemoveEncodingFilter::TRANSFER_ENCODING)); } @@ -41,11 +41,11 @@ public function filter_removes_content_encoding() { $request = new Request(); $response = new Response('php://memory', 200, [RemoveEncodingFilter::TRANSFER_ENCODING => 'foo']); - $next = function ($request, $response) { + $next = function () use ($response) { return $response; }; - $response = call_user_func($this->filter, $request, $response, $next); + $response = call_user_func($this->filter, $request, $next); $this->assertFalse($response->hasHeader(RemoveEncodingFilter::CONTENT_ENCODING)); } diff --git a/tests/Proxy/Filter/RemoveLocationFilterTest.php b/tests/Proxy/Filter/RemoveLocationFilterTest.php index ea96bde..916ed02 100644 --- a/tests/Proxy/Filter/RemoveLocationFilterTest.php +++ b/tests/Proxy/Filter/RemoveLocationFilterTest.php @@ -13,7 +13,7 @@ class RemoveLocationFilterTest extends TestCase */ private $filter; - public function setUp() + public function setUp(): Void { $this->filter = new RemoveLocationFilter(); } @@ -29,7 +29,7 @@ public function filter_removes_location() return $response; }; - $response = call_user_func($this->filter, $request, $response, $next); + $response = call_user_func($this->filter, $request, $next); $this->assertFalse($response->hasHeader(RemoveLocationFilter::LOCATION)); } @@ -45,7 +45,7 @@ public function filter_adds_location_as_xheader() return $response; }; - $response = call_user_func($this->filter, $request, $response, $next); + $response = call_user_func($this->filter, $request, $next); $this->assertEquals('http://www.example.com', $response->getHeader('X-Proxy-Location')[0]); } diff --git a/tests/Proxy/Filter/RewriteLocationFilterTest.php b/tests/Proxy/Filter/RewriteLocationFilterTest.php index 3bcc5f4..522acc0 100644 --- a/tests/Proxy/Filter/RewriteLocationFilterTest.php +++ b/tests/Proxy/Filter/RewriteLocationFilterTest.php @@ -1,4 +1,6 @@ -filter = new RewriteLocationFilter(); } @@ -30,7 +32,7 @@ public function filter_rewrites_location() return $response; }; - $response = call_user_func($this->filter, $request, $response, $next); + $response = call_user_func($this->filter, $request, $next); $this->assertTrue($response->hasHeader('X-Proxy-Location')); $this->assertTrue($response->hasHeader(RewriteLocationFilter::LOCATION)); diff --git a/tests/Proxy/ProxyTest.php b/tests/Proxy/ProxyTest.php index b46017e..b5e078b 100644 --- a/tests/Proxy/ProxyTest.php +++ b/tests/Proxy/ProxyTest.php @@ -2,13 +2,14 @@ namespace Proxy; +use Laminas\Diactoros\Request; +use Laminas\Diactoros\Response; use PHPUnit\Framework\TestCase; use Proxy\Adapter\Dummy\DummyAdapter; -use Proxy\Exception\UnexpectedValueException; use Psr\Http\Message\RequestInterface; -use Laminas\Diactoros\Request; -use Laminas\Diactoros\Response; use Laminas\Diactoros\ServerRequestFactory; +use Proxy\Exception\UnexpectedValueException; +use PHPUnit\Framework\InvalidArgumentException; class ProxyTest extends TestCase { @@ -17,7 +18,7 @@ class ProxyTest extends TestCase */ private $proxy; - public function setUp() + public function setUp(): void { $this->proxy = new Proxy(new DummyAdapter()); } @@ -28,6 +29,7 @@ public function setUp() */ public function to_throws_exception_if_no_request_is_given() { + $this->expectException(UnexpectedValueException::class); $this->proxy->to('http://www.example.com'); } @@ -48,9 +50,12 @@ public function to_applies_filters() { $applied = false; - $this->proxy->forward(ServerRequestFactory::fromGlobals())->filter(function ($request, $response) use (&$applied + $this->proxy->forward(ServerRequestFactory::fromGlobals())->filter(function ($request, $next) use ( + &$applied ) { $applied = true; + + return new Response('php://memory', 200); })->to('http://www.example.com'); $this->assertTrue($applied); @@ -61,7 +66,7 @@ public function to_applies_filters() */ public function to_sends_request() { - $request = new Request('http://localhost/path?query=yes', 'GET'); + $request = (new ServerRequestFactory)->createServerRequest('GET', 'http://localhost/path?query=yes'); $url = 'https://www.example.com'; $adapter = $this->getMockBuilder(DummyAdapter::class) @@ -85,7 +90,7 @@ public function to_sends_request() */ public function to_sends_request_with_port() { - $request = new Request('http://localhost/path?query=yes', 'GET'); + $request = (new ServerRequestFactory)->createServerRequest('GET', 'http://localhost/path?query=yes'); $url = 'https://www.example.com:3000'; $adapter = $this->getMockBuilder(DummyAdapter::class) @@ -109,7 +114,7 @@ public function to_sends_request_with_port() */ public function to_sends_request_with_subdirectory() { - $request = new Request('http://localhost/path?query=yes', 'GET'); + $request = (new ServerRequestFactory)->createServerRequest('GET', 'http://localhost/path?query=yes'); $url = 'https://www.example.com/proxy/'; $adapter = $this->getMockBuilder(DummyAdapter::class)