port matcher & php 8.1 support

This commit is contained in:
Pavel 2022-04-04 16:16:24 +03:00
parent aec5a4c4a0
commit f38395734a
6 changed files with 116 additions and 6 deletions

View File

@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
strategy: strategy:
matrix: matrix:
php-version: ['7.2', '7.3', '7.4', '8.0'] php-version: ['7.2', '7.3', '7.4', '8.0', '8.1']
steps: steps:
- name: Check out code into the workspace - name: Check out code into the workspace
uses: actions/checkout@v2 uses: actions/checkout@v2

View File

@ -41,15 +41,16 @@
}, },
"require-dev": { "require-dev": {
"squizlabs/php_codesniffer": "^3.6", "squizlabs/php_codesniffer": "^3.6",
"phpmd/phpmd": "^2.10", "phpmd/phpmd": "^2.12",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.1", "dealerdirect/phpcodesniffer-composer-installer": "^0.7.1",
"phpcompatibility/php-compatibility": "^9.3", "phpcompatibility/php-compatibility": "^9.3",
"phpstan/phpstan": "^0.12.87", "phpstan/phpstan": "^1.5",
"jms/serializer": "^2 | ^3.12", "jms/serializer": "^2 | ^3.17",
"symfony/phpunit-bridge": "^5.2", "symfony/phpunit-bridge": "^5.2",
"symfony/serializer": "^5.2", "symfony/serializer": "^5.2",
"symfony/property-access": "^5.2", "symfony/property-access": "^5.2",
"php-http/multipart-stream-builder": "^1.2" "php-http/multipart-stream-builder": "^1.2",
"symfony/http-client": "^5.3"
}, },
"provide": { "provide": {
"psr/http-client-implementation": "1.0", "psr/http-client-implementation": "1.0",

View File

@ -0,0 +1,56 @@
<?php
/**
* PHP version 7.3
*
* @category PortMatcher
* @package Pock\Matchers
*/
namespace Pock\Matchers;
use Pock\Enum\RequestScheme;
use Psr\Http\Message\RequestInterface;
/**
* Class PortMatcher
*
* @category PortMatcher
* @package Pock\Matchers
*/
class PortMatcher implements RequestMatcherInterface
{
/** @var int */
protected $port;
/**
* PortMatcher constructor.
*
* @param int $port
*/
public function __construct(int $port)
{
$this->port = $port;
}
/**
* @inheritDoc
*/
public function matches(RequestInterface $request): bool
{
$port = $request->getUri()->getPort();
if (null === $port) {
switch ($request->getUri()->getScheme()) {
case RequestScheme::HTTP:
return 80 === $this->port;
case RequestScheme::HTTPS:
return 443 === $this->port;
default:
return false;
}
}
return $port === $this->port;
}
}

View File

@ -33,6 +33,7 @@ use Pock\Matchers\MethodMatcher;
use Pock\Matchers\MultipartFormDataMatcher; use Pock\Matchers\MultipartFormDataMatcher;
use Pock\Matchers\MultipleMatcher; use Pock\Matchers\MultipleMatcher;
use Pock\Matchers\PathMatcher; use Pock\Matchers\PathMatcher;
use Pock\Matchers\PortMatcher;
use Pock\Matchers\QueryMatcher; use Pock\Matchers\QueryMatcher;
use Pock\Matchers\RegExpBodyMatcher; use Pock\Matchers\RegExpBodyMatcher;
use Pock\Matchers\RegExpPathMatcher; use Pock\Matchers\RegExpPathMatcher;
@ -49,6 +50,8 @@ use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use RuntimeException; use RuntimeException;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Throwable; use Throwable;
/** /**
@ -60,6 +63,7 @@ use Throwable;
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyPublicMethods) * @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.TooManyMethods) * @SuppressWarnings(PHPMD.TooManyMethods)
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity) * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/ */
class PockBuilder class PockBuilder
@ -136,6 +140,18 @@ class PockBuilder
return $this->addMatcher(new HostMatcher($host)); return $this->addMatcher(new HostMatcher($host));
} }
/**
* Matches request by the port.
*
* @param int $port
*
* @return self
*/
public function matchPort(int $port): self
{
return $this->addMatcher(new PortMatcher($port));
}
/** /**
* Matches request by origin. * Matches request by origin.
* *
@ -160,6 +176,10 @@ class PockBuilder
$this->matchHost($parsed['host']); $this->matchHost($parsed['host']);
} }
if (array_key_exists('port', $parsed) && is_int($parsed['port']) && $parsed['port'] > 0) {
$this->matchPort($parsed['port']);
}
return $this; return $this;
} }

View File

@ -0,0 +1,33 @@
<?php
/**
* PHP version 7.3
*
* @category PortMatcherTest
* @package Pock\Tests\Matchers
*/
namespace Pock\Tests\Matchers;
use Pock\Matchers\HostMatcher;
use Pock\Matchers\PortMatcher;
use Pock\TestUtils\PockTestCase;
/**
* Class PortMatcherTest
*
* @category PortMatcherTest
* @package Pock\Tests\Matchers
*/
class PortMatcherTest extends PockTestCase
{
public function testNotMatches(): void
{
self::assertFalse((new PortMatcher(80))->matches(static::getTestRequest()));
}
public function testMatches(): void
{
self::assertTrue((new PortMatcher(443))->matches(static::getTestRequest()));
}
}

View File

@ -174,7 +174,7 @@ class PockBuilderTest extends PockTestCase
public function testMatchOrigin(): void public function testMatchOrigin(): void
{ {
$origin = RequestScheme::HTTPS . '://' . self::TEST_HOST; $origin = RequestScheme::HTTPS . '://' . self::TEST_HOST . ':443';
$builder = new PockBuilder(); $builder = new PockBuilder();
$builder->matchMethod(RequestMethod::GET) $builder->matchMethod(RequestMethod::GET)