-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathIntegTestCase.php
More file actions
155 lines (131 loc) · 5.35 KB
/
Copy pathIntegTestCase.php
File metadata and controls
155 lines (131 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace SlackPhp\Framework\Tests\Integration;
use SlackPhp\Framework\Context;
use Nyholm\Psr7\Factory\Psr17Factory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use SlackPhp\BlockKit\Surfaces\Message;
use SlackPhp\Framework\Clients\ApiClient;
use SlackPhp\Framework\Clients\RespondClient;
use SlackPhp\Framework\Contexts\DataBag;
use SlackPhp\Framework\Http\HttpServer;
use SlackPhp\Framework\Interceptor;
use SlackPhp\Framework\Interceptors\Tap;
use SlackPhp\Framework\Tests\Fakes\FakeResponseEmitter;
class IntegTestCase extends TestCase
{
private const SIGNING_KEY = 'abc123';
private const BOT_TOKEN = 'xoxb-abc123';
private const HEADER_SIGNATURE = 'X-Slack-Signature';
private const HEADER_TIMESTAMP = 'X-Slack-Request-Timestamp';
protected Psr17Factory $httpFactory;
/** @var LoggerInterface|MockObject */
protected $logger;
private FakeResponseEmitter $responseEmitter;
public function setUp(): void
{
putenv('SLACK_SIGNING_KEY=' . self::SIGNING_KEY);
putenv('SLACK_BOT_TOKEN=' . self::BOT_TOKEN);
parent::setUp();
$this->httpFactory = new Psr17Factory();
$this->logger = $this->createMock(LoggerInterface::class);
$this->responseEmitter = new FakeResponseEmitter();
}
protected function parseResponse(?ResponseInterface $response = null): DataBag
{
$response = $response ?? $this->responseEmitter->getLastResponse();
$content = (string) $response->getBody();
if ($content === '') {
return new DataBag(['ack' => true]);
}
try {
return new DataBag(\json_decode($content, true, 512, \JSON_THROW_ON_ERROR));
} catch (\JsonException $exception) {
$this->fail('Could not parse response JSON: ' . $exception->getMessage());
}
}
protected function createCommandRequest(array $data, ?int $timestamp = null): ServerRequestInterface
{
return $this->createRequest(http_build_query($data), 'application/x-www-form-urlencoded', $timestamp);
}
protected function createInteractiveRequest(array $data, ?int $timestamp = null): ServerRequestInterface
{
return $this->createRequest(
http_build_query(['payload' => json_encode($data)]),
'application/x-www-form-urlencoded',
$timestamp
);
}
protected function createEventRequest(array $data, ?int $timestamp = null): ServerRequestInterface
{
return $this->createRequest(json_encode($data), 'application/json', $timestamp);
}
private function createRequest(string $content, string $contentType, ?int $timestamp = null): ServerRequestInterface
{
// Create signature
$timestamp = $timestamp ?? time();
$stringToSign = sprintf('v0:%d:%s', $timestamp, $content);
$signature = 'v0=' . hash_hmac('sha256', $stringToSign, self::SIGNING_KEY);
return $this->httpFactory->createServerRequest('POST', '/')
->withHeader(self::HEADER_TIMESTAMP, (string) $timestamp)
->withHeader(self::HEADER_SIGNATURE, $signature)
->withHeader('Content-Type', $contentType)
->withHeader('Content-Length', (string) strlen($content))
->withBody($this->httpFactory->createStream($content));
}
protected function createHttpServer(ServerRequestInterface $request): HttpServer
{
return HttpServer::new()
->withLogger($this->logger)
->withRequest($request)
->withResponseEmitter($this->responseEmitter);
}
protected function failOnLoggedErrors(): void
{
$this->logger->method('error')->willReturnCallback(function ($message, array $context = []) {
$message = "Logged an error: {$message}\nContext:\n";
foreach ($context as $key => $value) {
$message .= "- {$key}: {$value}\n";
}
$this->fail($message);
});
}
/**
* @param mixed $result
*/
protected function assertIsAck($result): void
{
if (!$result instanceof DataBag) {
$this->fail('Tried to assertIsAck on invalid value');
}
if ($result->get('ack') !== true) {
$this->fail('Result was not an "ack"');
}
}
protected function interceptApiCall(string $api, callable $handler): Interceptor
{
$apiClient = $this->createMock(ApiClient::class);
$apiClient->expects($this->once())
->method('call')
->with($api, $this->anything())
->willReturnCallback(function (string $api, array $params) use ($handler) {
return $handler($params);
});
return new Tap(function (Context $context) use ($apiClient) {
$context->withApiClient($apiClient);
});
}
protected function interceptRespond(string $responseUrl): Interceptor
{
$respondClient = $this->createMock(RespondClient::class);
$respondClient->expects($this->once())
->method('respond')
->with($responseUrl, $this->isInstanceOf(Message::class));
return new Tap(function (Context $context) use ($respondClient) {
$context->withRespondClient($respondClient);
});
}
}