Verified Commit 0044f8e6 authored by Elias Häußler's avatar Elias Häußler 🐛
Browse files

[BUGFIX] Use Symfony Mock Client for to mock requests in Unit tests

parent a5300295
Pipeline #770 failed with stages
in 1 minute and 32 seconds
......@@ -32,7 +32,7 @@
"require-dev": {
"composer/composer": "^1.0 || ^2.0",
"friendsofphp/php-cs-fixer": "^2.17",
"guzzlehttp/guzzle": "^6.5 || ^7.0",
"php-http/mock-client": "^1.4",
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0",
"rpkamp/mailhog-client": "^0.3 || ^0.4 || ^0.5"
},
......
......@@ -24,13 +24,10 @@ declare(strict_types=1);
namespace EliasHaeussler\ComposerUpdateReporter\Tests\Unit;
use EliasHaeussler\ComposerUpdateReporter\Util;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\HttpClient\Response\MockResponse;
/**
* ClientMockTrait
......@@ -41,23 +38,16 @@ use Psr\Http\Message\ResponseInterface;
trait ClientMockTrait
{
/**
* @var MockHandler
* @var MockResponse
*/
private $mockHandler;
/**
* @var array{request: RequestInterface, response: ResponseInterface|null, error: string|mixed, options: array}
*/
private $requestContainer = [];
private $mockedResponse;
private function getClient(): ClientInterface
{
$this->mockHandler = $this->mockHandler ?? new MockHandler();
$this->mockHandler->reset();
$history = Middleware::history($this->requestContainer);
$handlerStack = HandlerStack::create($this->mockHandler);
$handlerStack->push($history);
return new Client(['handler' => $handlerStack]);
$callback = function () {
return $this->mockedResponse;
};
return new Psr18Client(new MockHttpClient($callback));
}
protected function assertPayloadOfLastRequestContainsSubset(array $expectedPayloadSubset): void
......@@ -68,11 +58,10 @@ trait ClientMockTrait
protected function getPayloadOfLastRequest(): array
{
self::assertNotEmpty($this->requestContainer, 'Unable to find last request');
/** @var RequestInterface $request */
$request = end($this->requestContainer)['request'];
self::assertInstanceOf(RequestInterface::class, $request);
$request->getBody()->rewind();
return json_decode($request->getBody()->getContents(), true);
$lastResponse = $this->mockedResponse;
self::assertInstanceOf(MockResponse::class, $lastResponse, 'Unable to find last request');
return json_decode($lastResponse->getRequestOptions()['body'], true);
}
}
......@@ -28,9 +28,9 @@ use EliasHaeussler\ComposerUpdateReporter\Service\GitLab;
use EliasHaeussler\ComposerUpdateReporter\Tests\Unit\AbstractTestCase;
use EliasHaeussler\ComposerUpdateReporter\Tests\Unit\ClientMockTrait;
use EliasHaeussler\ComposerUpdateReporter\Tests\Unit\TestEnvironmentTrait;
use Nyholm\Psr7\Response;
use Nyholm\Psr7\Uri;
use Psr\Http\Client\ClientExceptionInterface;
use Symfony\Component\HttpClient\Response\MockResponse;
/**
* GitLabTest
......@@ -199,7 +199,7 @@ class GitLabTest extends AbstractTestCase
$io = new BufferIO();
$this->subject->setClient($this->getClient());
$this->mockHandler->append(new Response());
$this->mockedResponse = new MockResponse();
static::assertTrue($this->subject->report($result, $io));
static::assertStringContainsString('GitLab report was successful.', $io->getOutput());
......
......@@ -28,9 +28,9 @@ use EliasHaeussler\ComposerUpdateReporter\Service\Mattermost;
use EliasHaeussler\ComposerUpdateReporter\Tests\Unit\AbstractTestCase;
use EliasHaeussler\ComposerUpdateReporter\Tests\Unit\ClientMockTrait;
use EliasHaeussler\ComposerUpdateReporter\Tests\Unit\TestEnvironmentTrait;
use Nyholm\Psr7\Response;
use Nyholm\Psr7\Uri;
use Psr\Http\Client\ClientExceptionInterface;
use Symfony\Component\HttpClient\Response\MockResponse;
/**
* MattermostTest
......@@ -204,7 +204,7 @@ class MattermostTest extends AbstractTestCase
$io = new BufferIO();
$this->subject->setClient($this->getClient());
$this->mockHandler->append(new Response());
$this->mockedResponse = new MockResponse();
static::assertTrue($this->subject->report($result, $io));
static::assertStringContainsString('Mattermost report was successful.', $io->getOutput());
......
......@@ -28,9 +28,9 @@ use EliasHaeussler\ComposerUpdateReporter\Service\Slack;
use EliasHaeussler\ComposerUpdateReporter\Tests\Unit\AbstractTestCase;
use EliasHaeussler\ComposerUpdateReporter\Tests\Unit\ClientMockTrait;
use EliasHaeussler\ComposerUpdateReporter\Tests\Unit\TestEnvironmentTrait;
use Nyholm\Psr7\Response;
use Nyholm\Psr7\Uri;
use Psr\Http\Client\ClientExceptionInterface;
use Symfony\Component\HttpClient\Response\MockResponse;
/**
* SlackTest
......@@ -194,7 +194,7 @@ class SlackTest extends AbstractTestCase
$expectedFields = array_merge($expectedFields, $expectedSecurityPayload);
}
$this->subject->setClient($this->getClient());
$this->mockHandler->append(new Response());
$this->mockedResponse = new MockResponse();
static::assertTrue($this->subject->report($result, $io));
static::assertStringContainsString('Slack report was successful.', $io->getOutput());
......
......@@ -28,10 +28,10 @@ use EliasHaeussler\ComposerUpdateReporter\Service\Teams;
use EliasHaeussler\ComposerUpdateReporter\Tests\Unit\AbstractTestCase;
use EliasHaeussler\ComposerUpdateReporter\Tests\Unit\ClientMockTrait;
use EliasHaeussler\ComposerUpdateReporter\Tests\Unit\TestEnvironmentTrait;
use Nyholm\Psr7\Response;
use Nyholm\Psr7\Uri;
use Psr\Http\Client\ClientExceptionInterface;
use Spatie\Emoji\Emoji;
use Symfony\Component\HttpClient\Response\MockResponse;
/**
* TeamsTest
......@@ -165,7 +165,7 @@ class TeamsTest extends AbstractTestCase
$io = new BufferIO();
$this->subject->setClient($this->getClient());
$this->mockHandler->append(new Response());
$this->mockedResponse = new MockResponse();
static::assertTrue($this->subject->report($result, $io));
static::assertStringContainsString('MS Teams report was successful.', $io->getOutput());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment