Skip to content

Commit

Permalink
Replace "php-http/mock-client" with "psr-mock/http-client-implementat…
Browse files Browse the repository at this point in the history
…ion"
  • Loading branch information
flavioheleno committed Apr 24, 2024
1 parent 3e2a6d6 commit a84a113
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 58 deletions.
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
22 changes: 15 additions & 7 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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());
}
Expand Down
25 changes: 17 additions & 8 deletions tests/Resource/Package/ReleaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@

namespace Pickling\Test\Resource\Package;

use Http\Mock\Client as MockClient;
use Nyholm\Psr7\Factory\Psr17Factory;
use PHPUnit\Framework\TestCase;
use Pickling\Channel\PeclChannel;
use Pickling\Resource\Package\Release;
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;
Expand All @@ -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());
}
Expand Down
103 changes: 71 additions & 32 deletions tests/Resource/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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());
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
23 changes: 16 additions & 7 deletions tests/Traits/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

namespace Pickling\Test\Traits;

use Http\Mock\Client as MockClient;
use Nyholm\Psr7\Factory\Psr17Factory;
use PHPUnit\Framework\TestCase;
use Pickling\Traits\HttpRequest;
use Psr\Http\Client\ClientInterface;
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;

Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit a84a113

Please sign in to comment.