mirror of
https://github.com/Neur0toxine/pock.git
synced 2024-11-28 15:56:09 +03:00
port matcher & php 8.1 support
This commit is contained in:
parent
aec5a4c4a0
commit
f38395734a
2
.github/workflows/tests.yml
vendored
2
.github/workflows/tests.yml
vendored
@ -14,7 +14,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
php-version: ['7.2', '7.3', '7.4', '8.0']
|
||||
php-version: ['7.2', '7.3', '7.4', '8.0', '8.1']
|
||||
steps:
|
||||
- name: Check out code into the workspace
|
||||
uses: actions/checkout@v2
|
||||
|
@ -41,15 +41,16 @@
|
||||
},
|
||||
"require-dev": {
|
||||
"squizlabs/php_codesniffer": "^3.6",
|
||||
"phpmd/phpmd": "^2.10",
|
||||
"phpmd/phpmd": "^2.12",
|
||||
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.1",
|
||||
"phpcompatibility/php-compatibility": "^9.3",
|
||||
"phpstan/phpstan": "^0.12.87",
|
||||
"jms/serializer": "^2 | ^3.12",
|
||||
"phpstan/phpstan": "^1.5",
|
||||
"jms/serializer": "^2 | ^3.17",
|
||||
"symfony/phpunit-bridge": "^5.2",
|
||||
"symfony/serializer": "^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": {
|
||||
"psr/http-client-implementation": "1.0",
|
||||
|
56
src/Matchers/PortMatcher.php
Normal file
56
src/Matchers/PortMatcher.php
Normal 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;
|
||||
}
|
||||
}
|
@ -33,6 +33,7 @@ use Pock\Matchers\MethodMatcher;
|
||||
use Pock\Matchers\MultipartFormDataMatcher;
|
||||
use Pock\Matchers\MultipleMatcher;
|
||||
use Pock\Matchers\PathMatcher;
|
||||
use Pock\Matchers\PortMatcher;
|
||||
use Pock\Matchers\QueryMatcher;
|
||||
use Pock\Matchers\RegExpBodyMatcher;
|
||||
use Pock\Matchers\RegExpPathMatcher;
|
||||
@ -49,6 +50,8 @@ use Psr\Http\Client\ClientInterface;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\HttpClient\Psr18Client;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
@ -60,6 +63,7 @@ use Throwable;
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
|
||||
* @SuppressWarnings(PHPMD.TooManyMethods)
|
||||
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
|
||||
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
|
||||
*/
|
||||
class PockBuilder
|
||||
@ -136,6 +140,18 @@ class PockBuilder
|
||||
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.
|
||||
*
|
||||
@ -160,6 +176,10 @@ class PockBuilder
|
||||
$this->matchHost($parsed['host']);
|
||||
}
|
||||
|
||||
if (array_key_exists('port', $parsed) && is_int($parsed['port']) && $parsed['port'] > 0) {
|
||||
$this->matchPort($parsed['port']);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
33
tests/src/Matchers/PortMatcherTest.php
Normal file
33
tests/src/Matchers/PortMatcherTest.php
Normal 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()));
|
||||
}
|
||||
}
|
@ -174,7 +174,7 @@ class PockBuilderTest extends PockTestCase
|
||||
|
||||
public function testMatchOrigin(): void
|
||||
{
|
||||
$origin = RequestScheme::HTTPS . '://' . self::TEST_HOST;
|
||||
$origin = RequestScheme::HTTPS . '://' . self::TEST_HOST . ':443';
|
||||
$builder = new PockBuilder();
|
||||
|
||||
$builder->matchMethod(RequestMethod::GET)
|
||||
|
Loading…
Reference in New Issue
Block a user