From a84a11358b9cdc4dce1a178426bba6d4f5718add Mon Sep 17 00:00:00 2001 From: Flavio Heleno Date: Wed, 24 Apr 2024 10:10:03 -0300 Subject: [PATCH] Replace "php-http/mock-client" with "psr-mock/http-client-implementation" --- composer.json | 6 +- tests/ClientTest.php | 22 ++++-- tests/Resource/Package/ReleaseTest.php | 25 ++++-- tests/Resource/PackageTest.php | 103 +++++++++++++++++-------- tests/Traits/HttpRequestTest.php | 23 ++++-- 5 files changed, 121 insertions(+), 58 deletions(-) diff --git a/composer.json b/composer.json index 8e005cc..d9f6ca8 100644 --- a/composer.json +++ b/composer.json @@ -25,8 +25,7 @@ "sort-packages": true, "preferred-install": "dist", "allow-plugins": { - "infection/extension-installer": true, - "php-http/discovery": true + "infection/extension-installer": true } }, "minimum-stability": "dev", @@ -51,11 +50,10 @@ "require-dev": { "infection/infection": "^0.28", "nyholm/psr7": "^1.3", - "php-http/mock-client": "^1.4", - "php-http/socket-client": "^2.1", "php-parallel-lint/php-parallel-lint": "^1.3", "phpstan/phpstan": "^1.10", "phpunit/phpunit": "^11.1", + "psr-mock/http-client-implementation": "^1.0", "psy/psysh": "^0.12", "roave/security-advisories": "dev-latest", "squizlabs/php_codesniffer": "^3.6" diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 2191525..f79325f 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -3,7 +3,6 @@ namespace Pickling\Test; -use Http\Mock\Client as MockClient; use Nyholm\Psr7\Factory\Psr17Factory; use PHPUnit\Framework\TestCase; use Pickling\Channel\PeclChannel; @@ -12,6 +11,7 @@ use Pickling\Resource\Package; use Pickling\Resource\PackageList; use Psr\Http\Message\ResponseInterface; +use PsrMock\Psr18\Client as MockClient; final class ClientTest extends TestCase { private MockClient $httpClient; @@ -26,18 +26,26 @@ protected function setUp(): void { public function testGetCategoryList(): void { $response = $this->createMock(ResponseInterface::class); - $response->method('getStatusCode')->willReturn(200); - $response->method('getBody')->willReturn($this->psr17Factory->createStreamFromFile(__DIR__ . '/Fixtures/categories.xml')); - $this->httpClient->addResponse($response); + $response + ->method('getStatusCode') + ->willReturn(200); + $response + ->method('getBody') + ->willReturn($this->psr17Factory->createStreamFromFile(__DIR__ . '/Fixtures/categories.xml')); + $this->httpClient->addResponse('GET', 'https://pecl.php.net/rest/c/categories.xml', $response); $this->assertInstanceOf(CategoryList::class, $this->peclClient->getCategoryList()); } public function testGetPackageList(): void { $response = $this->createMock(ResponseInterface::class); - $response->method('getStatusCode')->willReturn(200); - $response->method('getBody')->willReturn($this->psr17Factory->createStreamFromFile(__DIR__ . '/Fixtures/packages.xml')); - $this->httpClient->addResponse($response); + $response + ->method('getStatusCode') + ->willReturn(200); + $response + ->method('getBody') + ->willReturn($this->psr17Factory->createStreamFromFile(__DIR__ . '/Fixtures/packages.xml')); + $this->httpClient->addResponse('GET', 'https://pecl.php.net/rest/p/packages.xml', $response); $this->assertInstanceOf(PackageList::class, $this->peclClient->getPackageList()); } diff --git a/tests/Resource/Package/ReleaseTest.php b/tests/Resource/Package/ReleaseTest.php index 3a7eba2..9a5dd50 100644 --- a/tests/Resource/Package/ReleaseTest.php +++ b/tests/Resource/Package/ReleaseTest.php @@ -3,7 +3,6 @@ namespace Pickling\Test\Resource\Package; -use Http\Mock\Client as MockClient; use Nyholm\Psr7\Factory\Psr17Factory; use PHPUnit\Framework\TestCase; use Pickling\Channel\PeclChannel; @@ -11,7 +10,7 @@ use Pickling\Resource\Package\Release\Info; use Pickling\Resource\Package\Release\Manifest; use Psr\Http\Message\ResponseInterface; -use RuntimeException; +use PsrMock\Psr18\Client as MockClient; final class ReleaseTest extends TestCase { private MockClient $httpClient; @@ -38,18 +37,28 @@ public function testPropertyGetters(): void { public function testGetManifest(): void { $response = $this->createMock(ResponseInterface::class); - $response->method('getStatusCode')->willReturn(200); - $response->method('getBody')->willReturn($this->psr17Factory->createStreamFromFile(__DIR__ . '/../../Fixtures/mongo/package.1.6.16.xml')); - $this->httpClient->addResponse($response); + $response + ->method('getStatusCode') + ->willReturn(200); + $response + ->method('getBody') + ->willReturn($this->psr17Factory->createStreamFromFile(__DIR__ . '/../../Fixtures/mongo/package.1.6.16.xml')); + + $this->httpClient->addResponse('GET', 'https://pecl.php.net/rest/r/mongo/package.0.0.0.xml', $response); $this->assertInstanceOf(Manifest::class, $this->release->getManifest()); } public function testGetInfo(): void { $response = $this->createMock(ResponseInterface::class); - $response->method('getStatusCode')->willReturn(200); - $response->method('getBody')->willReturn($this->psr17Factory->createStreamFromFile(__DIR__ . '/../../Fixtures/mongo/1.6.16.xml')); - $this->httpClient->addResponse($response); + $response + ->method('getStatusCode') + ->willReturn(200); + $response + ->method('getBody') + ->willReturn($this->psr17Factory->createStreamFromFile(__DIR__ . '/../../Fixtures/mongo/1.6.16.xml')); + + $this->httpClient->addResponse('GET', 'https://pecl.php.net/rest/r/mongo/0.0.0.xml', $response); $this->assertInstanceOf(Info::class, $this->release->getInfo()); } diff --git a/tests/Resource/PackageTest.php b/tests/Resource/PackageTest.php index 047070a..ce5ed18 100644 --- a/tests/Resource/PackageTest.php +++ b/tests/Resource/PackageTest.php @@ -3,7 +3,6 @@ namespace Pickling\Test\Resource; -use Http\Mock\Client as MockClient; use Nyholm\Psr7\Factory\Psr17Factory; use PHPUnit\Framework\TestCase; use Pickling\Channel\PeclChannel; @@ -12,7 +11,7 @@ use Pickling\Resource\Package\Release; use Pickling\Resource\Package\ReleaseList; use Psr\Http\Message\ResponseInterface; -use RuntimeException; +use PsrMock\Psr18\Client as MockClient; final class PackageTest extends TestCase { private MockClient $httpClient; @@ -37,62 +36,86 @@ public function testPropertyGetters(): void { public function testGetInfo(): void { $response = $this->createMock(ResponseInterface::class); - $response->method('getStatusCode')->willReturn(200); - $response->method('getBody')->willReturn($this->psr17Factory->createStreamFromFile(__DIR__ . '/../Fixtures/mongo/info.xml')); - $this->httpClient->addResponse($response); + $response + ->method('getStatusCode') + ->willReturn(200); + $response + ->method('getBody') + ->willReturn($this->psr17Factory->createStreamFromFile(__DIR__ . '/../Fixtures/mongo/info.xml')); + $this->httpClient->addResponse('GET', 'https://pecl.php.net/rest/p/mongo/info.xml', $response); $this->assertInstanceOf(Info::class, $this->package->getInfo()); } public function testGetReleaseList(): void { $response = $this->createMock(ResponseInterface::class); - $response->method('getStatusCode')->willReturn(200); - $response->method('getBody')->willReturn($this->psr17Factory->createStreamFromFile(__DIR__ . '/../Fixtures/mongo/allreleases.xml')); - $this->httpClient->addResponse($response); + $response + ->method('getStatusCode') + ->willReturn(200); + $response + ->method('getBody') + ->willReturn($this->psr17Factory->createStreamFromFile(__DIR__ . '/../Fixtures/mongo/allreleases.xml')); + $this->httpClient->addResponse('GET', 'https://pecl.php.net/rest/r/mongo/allreleases.xml', $response); $this->assertInstanceOf(ReleaseList::class, $this->package->getReleaseList()); } public function testGetLatestVersion(): void { $response = $this->createMock(ResponseInterface::class); - $response->method('getStatusCode')->willReturn(200); + $response + ->method('getStatusCode') + ->willReturn(200); $stream = $this->psr17Factory->createStream('1.0.1'); $stream->rewind(); // https://github.com/Nyholm/psr7/issues/99 - $response->method('getBody')->willReturn($stream); - $this->httpClient->addResponse($response); + $response + ->method('getBody') + ->willReturn($stream); + $this->httpClient->addResponse('GET', 'https://pecl.php.net/rest/r/mongo/latest.txt', $response); $this->assertSame('1.0.1', $this->package->getLatestVersion()); } public function testGetStableVersion(): void { $response = $this->createMock(ResponseInterface::class); - $response->method('getStatusCode')->willReturn(200); + $response + ->method('getStatusCode') + ->willReturn(200); $stream = $this->psr17Factory->createStream('1.0.0'); $stream->rewind(); // https://github.com/Nyholm/psr7/issues/99 - $response->method('getBody')->willReturn($stream); - $this->httpClient->addResponse($response); + $response + ->method('getBody') + ->willReturn($stream); + $this->httpClient->addResponse('GET', 'https://pecl.php.net/rest/r/mongo/stable.txt', $response); $this->assertSame('1.0.0', $this->package->getStableVersion()); } public function testGetBetaVersion(): void { $response = $this->createMock(ResponseInterface::class); - $response->method('getStatusCode')->willReturn(200); + $response + ->method('getStatusCode') + ->willReturn(200); $stream = $this->psr17Factory->createStream('0.1.0'); $stream->rewind(); // https://github.com/Nyholm/psr7/issues/99 - $response->method('getBody')->willReturn($stream); - $this->httpClient->addResponse($response); + $response + ->method('getBody') + ->willReturn($stream); + $this->httpClient->addResponse('GET', 'https://pecl.php.net/rest/r/mongo/beta.txt', $response); $this->assertSame('0.1.0', $this->package->getBetaVersion()); } public function testGetAlphaVersion(): void { $response = $this->createMock(ResponseInterface::class); - $response->method('getStatusCode')->willReturn(200); + $response + ->method('getStatusCode') + ->willReturn(200); $stream = $this->psr17Factory->createStream('0.0.1'); $stream->rewind(); // https://github.com/Nyholm/psr7/issues/99 - $response->method('getBody')->willReturn($stream); - $this->httpClient->addResponse($response); + $response + ->method('getBody') + ->willReturn($stream); + $this->httpClient->addResponse('GET', 'https://pecl.php.net/rest/r/mongo/alpha.txt', $response); $this->assertSame('0.0.1', $this->package->getAlphaVersion()); } @@ -105,11 +128,15 @@ public function testAtRelease(): void { public function testAtLatestRelease(): void { $response = $this->createMock(ResponseInterface::class); - $response->method('getStatusCode')->willReturn(200); + $response + ->method('getStatusCode') + ->willReturn(200); $stream = $this->psr17Factory->createStream('1.0.1'); $stream->rewind(); // https://github.com/Nyholm/psr7/issues/99 - $response->method('getBody')->willReturn($stream); - $this->httpClient->addResponse($response); + $response + ->method('getBody') + ->willReturn($stream); + $this->httpClient->addResponse('GET', 'https://pecl.php.net/rest/r/mongo/latest.txt', $response); $release = $this->package->at('latest'); $this->assertInstanceOf(Release::class, $release); @@ -118,11 +145,15 @@ public function testAtLatestRelease(): void { public function testAtStableRelease(): void { $response = $this->createMock(ResponseInterface::class); - $response->method('getStatusCode')->willReturn(200); + $response + ->method('getStatusCode') + ->willReturn(200); $stream = $this->psr17Factory->createStream('1.0.0'); $stream->rewind(); // https://github.com/Nyholm/psr7/issues/99 - $response->method('getBody')->willReturn($stream); - $this->httpClient->addResponse($response); + $response + ->method('getBody') + ->willReturn($stream); + $this->httpClient->addResponse('GET', 'https://pecl.php.net/rest/r/mongo/stable.txt', $response); $release = $this->package->at('stable'); $this->assertInstanceOf(Release::class, $release); @@ -131,11 +162,15 @@ public function testAtStableRelease(): void { public function testAtBetaRelease(): void { $response = $this->createMock(ResponseInterface::class); - $response->method('getStatusCode')->willReturn(200); + $response + ->method('getStatusCode') + ->willReturn(200); $stream = $this->psr17Factory->createStream('0.1.0'); $stream->rewind(); // https://github.com/Nyholm/psr7/issues/99 - $response->method('getBody')->willReturn($stream); - $this->httpClient->addResponse($response); + $response + ->method('getBody') + ->willReturn($stream); + $this->httpClient->addResponse('GET', 'https://pecl.php.net/rest/r/mongo/beta.txt', $response); $release = $this->package->at('beta'); $this->assertInstanceOf(Release::class, $release); @@ -144,11 +179,15 @@ public function testAtBetaRelease(): void { public function testAtAlphaRelease(): void { $response = $this->createMock(ResponseInterface::class); - $response->method('getStatusCode')->willReturn(200); + $response + ->method('getStatusCode') + ->willReturn(200); $stream = $this->psr17Factory->createStream('0.0.1'); $stream->rewind(); // https://github.com/Nyholm/psr7/issues/99 - $response->method('getBody')->willReturn($stream); - $this->httpClient->addResponse($response); + $response + ->method('getBody') + ->willReturn($stream); + $this->httpClient->addResponse('GET', 'https://pecl.php.net/rest/r/mongo/alpha.txt', $response); $release = $this->package->at('alpha'); $this->assertInstanceOf(Release::class, $release); diff --git a/tests/Traits/HttpRequestTest.php b/tests/Traits/HttpRequestTest.php index 168bc35..8a5e768 100644 --- a/tests/Traits/HttpRequestTest.php +++ b/tests/Traits/HttpRequestTest.php @@ -3,7 +3,6 @@ namespace Pickling\Test\Traits; -use Http\Mock\Client as MockClient; use Nyholm\Psr7\Factory\Psr17Factory; use PHPUnit\Framework\TestCase; use Pickling\Traits\HttpRequest; @@ -11,6 +10,7 @@ use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamFactoryInterface; +use PsrMock\Psr18\Client as MockClient; use RuntimeException; use stdClass; @@ -48,19 +48,28 @@ public function testRequest(): void { public function testSendRequestWithResponseError(): void { $response = $this->createMock(ResponseInterface::class); - $response->method('getStatusCode')->willReturn(500); - $this->httpClient->addResponse($response); + $response + ->method('getStatusCode') + ->willReturn(500); + $response + ->method('getReasonPhrase') + ->willReturn('Internal Server Error'); + $this->httpClient->addResponse('GET', 'http://localhost/', $response); $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('Server Response Status Code: 500'); + $this->expectExceptionMessage('Internal Server Error'); $this->trait->testRequest(); } public function testSendRequestWithEmptyResponseBody(): void { $response = $this->createMock(ResponseInterface::class); - $response->method('getStatusCode')->willReturn(200); - $response->method('getBody')->willReturn($this->psr17Factory->createStream('')); - $this->httpClient->addResponse($response); + $response + ->method('getStatusCode') + ->willReturn(200); + $response + ->method('getBody') + ->willReturn($this->psr17Factory->createStream('')); + $this->httpClient->addResponse('GET', 'http://localhost/', $response); $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Response body is empty');