From f38395734a07205d34cd63f511c250d1cce5ba8c Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Mon, 4 Apr 2022 16:16:24 +0300 Subject: [PATCH] port matcher & php 8.1 support --- .github/workflows/tests.yml | 2 +- composer.json | 9 +++-- src/Matchers/PortMatcher.php | 56 ++++++++++++++++++++++++++ src/PockBuilder.php | 20 +++++++++ tests/src/Matchers/PortMatcherTest.php | 33 +++++++++++++++ tests/src/PockBuilderTest.php | 2 +- 6 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 src/Matchers/PortMatcher.php create mode 100644 tests/src/Matchers/PortMatcherTest.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 96e88b0..b65bee6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 diff --git a/composer.json b/composer.json index c7e8445..d779b56 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Matchers/PortMatcher.php b/src/Matchers/PortMatcher.php new file mode 100644 index 0000000..2cea485 --- /dev/null +++ b/src/Matchers/PortMatcher.php @@ -0,0 +1,56 @@ +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; + } +} diff --git a/src/PockBuilder.php b/src/PockBuilder.php index e521702..bd49539 100644 --- a/src/PockBuilder.php +++ b/src/PockBuilder.php @@ -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; } diff --git a/tests/src/Matchers/PortMatcherTest.php b/tests/src/Matchers/PortMatcherTest.php new file mode 100644 index 0000000..0f6c813 --- /dev/null +++ b/tests/src/Matchers/PortMatcherTest.php @@ -0,0 +1,33 @@ +matches(static::getTestRequest())); + } + + public function testMatches(): void + { + self::assertTrue((new PortMatcher(443))->matches(static::getTestRequest())); + } +} diff --git a/tests/src/PockBuilderTest.php b/tests/src/PockBuilderTest.php index 70963c5..fde2d04 100644 --- a/tests/src/PockBuilderTest.php +++ b/tests/src/PockBuilderTest.php @@ -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)