From 602358286b3466077c87a7cd016020ab6137f272 Mon Sep 17 00:00:00 2001 From: Aleksei Khudiakov Date: Wed, 6 Dec 2023 14:33:25 +1000 Subject: [PATCH] Fix test mocks return type Signed-off-by: Aleksei Khudiakov --- .../Listener/DocsBuildActionListenerTest.php | 3 +- ...GitHubReleaseWebsiteUpdateListenerTest.php | 3 +- .../Listener/GitHubStatusListenerTest.php | 7 +++-- .../Listener/RegisterWebhookListenerTest.php | 3 +- test/Psr7Helper.php | 28 +++++++++++++++++++ test/Slack/SlackClientTest.php | 17 +++++------ 6 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 test/Psr7Helper.php diff --git a/test/GitHub/Listener/DocsBuildActionListenerTest.php b/test/GitHub/Listener/DocsBuildActionListenerTest.php index c2f278d4..7b0e4070 100644 --- a/test/GitHub/Listener/DocsBuildActionListenerTest.php +++ b/test/GitHub/Listener/DocsBuildActionListenerTest.php @@ -10,6 +10,7 @@ use App\Slack\Domain\SlashResponseMessage; use App\Slack\Response\SlackResponse; use App\Slack\SlackClientInterface; +use AppTest\Psr7Helper; use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; @@ -81,7 +82,7 @@ public function testLogsErrorAndReportsViaSlackIfGitHubRequestFails(int $httpSta $response = $this->prophesize(ResponseInterface::class); $response->getStatusCode()->willReturn($httpStatus)->shouldBeCalled(); - $response->getBody()->willReturn('error message from github')->shouldBeCalled(); + $response->getBody()->willReturn(Psr7Helper::stream('error message from github'))->shouldBeCalled(); $this->githubClient->send($request->reveal())->will([$response, 'reveal'])->shouldBeCalled(); diff --git a/test/GitHub/Listener/GitHubReleaseWebsiteUpdateListenerTest.php b/test/GitHub/Listener/GitHubReleaseWebsiteUpdateListenerTest.php index 6c8ee230..16418814 100644 --- a/test/GitHub/Listener/GitHubReleaseWebsiteUpdateListenerTest.php +++ b/test/GitHub/Listener/GitHubReleaseWebsiteUpdateListenerTest.php @@ -7,6 +7,7 @@ use App\GitHub\Event\GitHubRelease; use App\GitHub\Listener\GitHubReleaseWebsiteUpdateListener; use App\HttpClientInterface; +use AppTest\Psr7Helper; use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; @@ -128,7 +129,7 @@ public function testLogsErrorUpdatingWebsite(): void $response = $this->prophesize(ResponseInterface::class); $response->getStatusCode()->willReturn(400); - $response->getBody()->willReturn(''); + $response->getBody()->willReturn(Psr7Helper::stream('')); $this->httpClient->send($request->reveal())->will([$response, 'reveal'])->shouldBeCalled(); diff --git a/test/GitHub/Listener/GitHubStatusListenerTest.php b/test/GitHub/Listener/GitHubStatusListenerTest.php index cf00cc65..1deb4d76 100644 --- a/test/GitHub/Listener/GitHubStatusListenerTest.php +++ b/test/GitHub/Listener/GitHubStatusListenerTest.php @@ -13,6 +13,7 @@ use App\Slack\Domain\WebAPIMessage; use App\Slack\Response\SlackResponseInterface; use App\Slack\SlackClientInterface; +use AppTest\Psr7Helper; use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; @@ -103,7 +104,7 @@ public function testLogsErrorAndNotifiesSlackWithGenericMessageForPullRequestWhe $ghRequest = $this->prophesize(RequestInterface::class); $ghResponse = $this->prophesize(ResponseInterface::class); $ghResponse->getStatusCode()->willReturn(400)->shouldBeCalled(); - $ghResponse->getBody()->willReturn('')->shouldBeCalled(); + $ghResponse->getBody()->willReturn(Psr7Helper::stream(''))->shouldBeCalled(); $this->githubClient ->createRequest( @@ -246,7 +247,7 @@ public function testNotifesSlackWithPullRequestBuildStatusWhenSearchHasAtLeastOn $ghRequest = $this->prophesize(RequestInterface::class); - $ghResponsePayload = json_encode([ + $ghResponsePayload = Psr7Helper::stream(json_encode([ 'incomplete_results' => false, 'items' => [ [ @@ -255,7 +256,7 @@ public function testNotifesSlackWithPullRequestBuildStatusWhenSearchHasAtLeastOn 'html_url' => 'pull-request-url', ], ], - ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); $ghResponse = $this->prophesize(ResponseInterface::class); $ghResponse->getStatusCode()->willReturn(200)->shouldBeCalled(); diff --git a/test/GitHub/Listener/RegisterWebhookListenerTest.php b/test/GitHub/Listener/RegisterWebhookListenerTest.php index 9dc52901..c01a2ad4 100644 --- a/test/GitHub/Listener/RegisterWebhookListenerTest.php +++ b/test/GitHub/Listener/RegisterWebhookListenerTest.php @@ -11,6 +11,7 @@ use App\Slack\Response\SlackResponseInterface; use App\Slack\SlackClientInterface; use App\UrlHelper; +use AppTest\Psr7Helper; use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; @@ -153,7 +154,7 @@ public function testLogsErrorAndNotifiesSlackWhenWebhookRegistrationFails(): voi $response = $this->prophesize(ResponseInterface::class); $response->getStatusCode()->willReturn(400)->shouldBeCalled(); - $response->getBody()->willReturn(''); + $response->getBody()->willReturn(Psr7Helper::stream('')); $this->github ->createRequest( diff --git a/test/Psr7Helper.php b/test/Psr7Helper.php new file mode 100644 index 00000000..505d7be5 --- /dev/null +++ b/test/Psr7Helper.php @@ -0,0 +1,28 @@ +createStream($content); + } +} diff --git a/test/Slack/SlackClientTest.php b/test/Slack/SlackClientTest.php index 5fed1ade..a40b4516 100644 --- a/test/Slack/SlackClientTest.php +++ b/test/Slack/SlackClientTest.php @@ -9,6 +9,7 @@ use App\Slack\Domain\WebAPIMessage; use App\Slack\Response\SlackResponse; use App\Slack\SlackClient; +use AppTest\Psr7Helper; use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; @@ -59,9 +60,9 @@ public function testSendReturnsReceivedResponseOnSuccess(): void $response = $this->prophesize(ResponseInterface::class); $response ->getBody() - ->willReturn(json_encode([ + ->willReturn(Psr7Helper::stream(json_encode([ 'ok' => true, - ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) + ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))) ->shouldBeCalled(); $this->httpClient @@ -89,10 +90,10 @@ public function testSendLogsResponseOnFailureBeforeReturning(): void $response->getStatusCode()->willReturn(400)->shouldBeCalled(); $response ->getBody() - ->willReturn(json_encode([ + ->willReturn(Psr7Helper::stream(json_encode([ 'ok' => false, 'error' => 'the error message', - ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) + ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))) ->shouldBeCalled(); $this->httpClient @@ -180,9 +181,9 @@ public function testSendWebhookMessageMarshalsRequestFromMessageAndSendsIt(): vo $response = $this->prophesize(ResponseInterface::class); $response ->getBody() - ->willReturn(json_encode([ + ->willReturn(Psr7Helper::stream(json_encode([ 'ok' => true, - ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) + ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))) ->shouldBeCalled(); $this->httpClient @@ -257,9 +258,9 @@ public function testSendWebAPIMessageMarshalsRequestFromMessageAndSendsIt(): voi $response = $this->prophesize(ResponseInterface::class); $response ->getBody() - ->willReturn(json_encode([ + ->willReturn(Psr7Helper::stream(json_encode([ 'ok' => true, - ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) + ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))) ->shouldBeCalled(); $this->httpClient