matrix-api/tests/Fakes/HttpFake.php

56 lines
1.4 KiB
PHP

<?php
namespace Zoomyboy\MatrixApi\Tests\Fakes;
use Illuminate\Support\Facades\Http;
class HttpFake
{
public string $roomId;
public string $baseUrl;
public function __construct()
{
$this->roomId = config('matrix.room_id');
$this->baseUrl = config('matrix.base_url');
}
public function sendsMessage(): void
{
Http::fake(function ($request) {
if ($request->method() !== 'PUT') {
return;
}
if (!str($request->url())->startsWith("{$this->baseUrl}/_matrix/client/v3/rooms/{$this->roomIdParam()}/send/m.room.message/")) {
return;
}
return Http::response(json_encode([
'event_id' => str()->random(32),
]));
});
}
public function assertMessageSent(string $message): void
{
Http::assertSent(function ($request) use ($message) {
if ($request['body'] !== $message) {
dump($message, $request['body']);
}
return $request->method() === 'PUT'
&& $request['body'] === $message
&& $request['msgtype'] === 'm.text'
&& str($request->url())->startsWith("{$this->baseUrl}/_matrix/client/v3/rooms/{$this->roomIdParam()}/send/m.room.message/");
});
}
protected function roomIdParam(): string
{
return rawurlencode($this->roomId);
}
}