Skip to content

Instantly share code, notes, and snippets.

@abdes-zakari
Created January 26, 2026 09:12
Show Gist options
  • Select an option

  • Save abdes-zakari/bf98631baf8e6bb92fee5e889a74873c to your computer and use it in GitHub Desktop.

Select an option

Save abdes-zakari/bf98631baf8e6bb92fee5e889a74873c to your computer and use it in GitHub Desktop.
Schönes loging output in der console beim test
<?php
namespace Test;
use App\Database\Connections\KDB;
use App\Database\Repositories\Kundenportal\DriverchatChatroomUserLinkRepository;
use App\Database\Repositories\Kundenportal\DriverchatChatroomUsersRepository;
use App\Database\Repositories\Kundenportal\DriverchatMessagesRepository;
use App\Database\Repositories\Kundenportal\DriverchatMessageStatusRepository;
use App\Http\Middleware\AuthGlobalMiddleware;
use GuzzleHttp\Psr7\ServerRequest;
class ChatDriverappControllerTest extends \Test\AppTestCase
{
protected \App\Services\DriverappChatService $driverappChatService;
protected \PDO $connection;
protected string $token;
protected string $roomId;
protected string $uid;
protected string $amsidnr;
public function setUp(): void
{
fwrite(STDERR, "\n========== START Setup ==========\n");
fwrite(STDERR, "\033[42;32mSTART Setup\033[0m\n");
fwrite(STDERR, "\033[41mSTART Setup\033[0m\n");
fwrite(STDERR, "\033[41;32mERROR\033[0m\n");
fwrite(STDERR, "\033[31mERROR\033[0m\n");
fwrite(STDERR, "\033[33mWARNING\033[0m\n");
fwrite(STDERR, "\033[34mINFO\033[0m\n");
$this->driverappChatService = new \App\Services\DriverappChatService(
new DriverchatChatroomUserLinkRepository,
new DriverchatChatroomUsersRepository,
new DriverchatMessagesRepository,
new DriverchatMessageStatusRepository,
);
$this->connection = KDB::getInstance();
$this->connection->beginTransaction();
$this->token = '2fsdfsdfsdkCkzcPPjpvsGUL5GtigUwz';
$this->roomId = '0851d7f28ac7b216';
$this->uid = '53erewrewrwe 229e87a97ad9';
$this->amsidnr = '044980001085486';
$this->app->group('/fahrerapp', function ($app) {
$app->group('/chat', function ($group) {
$group->post('/room',[\App\Http\Controllers\Fahrerapp\ChatDriverappController::class, 'createOrGetRoom']);
$group->post('/unread/count',[\App\Http\Controllers\Fahrerapp\ChatDriverappController::class, 'getCountUnreadMessages']);
$group->post('/room/{roomid}/send',[\App\Http\Controllers\Fahrerapp\ChatDriverappController::class, 'sendMessageToChatroom']);
$group->post('/room/messages/{roomid}',[\App\Http\Controllers\Fahrerapp\ChatDriverappController::class, 'getAllMessagesOfChatroom']);
});
})
->add(new AuthGlobalMiddleware());
}
public function _testGetCountUnreadMessages()
{
$request = new ServerRequest('POST', '/fahrerapp/chat/unread/count');
$request = $request->withQueryParams([
'token' => $this->token,
]);
$request = $request->withParsedBody([
'room_id' => $this->roomId,
]);
$response = $this->app->handle($request);
$body = json_decode($response->getBody());
$this->looog($request);
$this->looog($response);
$this->assertEquals(200, $response->getStatusCode());
$this->assertIsObject($body);
$this->assertObjectHasProperty('count', $body);
}
public function testGetAllMessagesOfChatroom()
{
$request = new ServerRequest('POST', '/fahrerapp/chat/room/messages/'.$this->roomId);
$request = $request->withQueryParams([
'token' => $this->token,
]);
$request = $request->withParsedBody([
'uid' => $this->uid,
'amsidnr' => $this->amsidnr
]);
$response = $this->app->handle($request);
// $body = json_decode($response->getBody());
//
// $this->assertEquals(200, $response->getStatusCode());
// $this->assertIsArray($body);
// $this->assertIsObject($body[0]);
// $this->assertObjectHasProperty('message', $body[0]);
$this->looog($request);
$this->looog($response);
$this->assertEquals(200, $response->getStatusCode());
$body = json_decode((string)$response->getBody(), true);
$this->assertIsArray($body);
$this->assertNotEmpty($body);
$this->assertIsArray($body[0]);
$this->assertArrayHasKey('message', $body[0]);
}
protected function tearDown(): void
{
fwrite(STDERR, "\n========== END tearDown ==========\n");
if ($this->connection->inTransaction()) {
$this->connection->rollBack();
}
}
private function looog(mixed $item):void
{
if($item instanceof \GuzzleHttp\Psr7\ServerRequest){ // $request = GuzzleHttp\Psr7\ServerRequest
fwrite(STDERR, "\nQueryParams:\n \033[33m".print_r(json_encode($item->getQueryParams()), true)."\033[0m \n");
fwrite(STDERR, "\nBodyParams:\n \033[33m".print_r(json_encode($item->getParsedBody()), true)."\033[0m \n");
}
if($item instanceof \Slim\Http\Response || $item instanceof \GuzzleHttp\Psr7\Response){ // $response = Slim\Http\Response or GuzzleHttp\Psr7\Response^
if ($item->getStatusCode() === 200 || $item->getStatusCode() === 201){
fwrite(STDERR, "\nStatus: \n \033[42m".print_r(json_encode($item->getStatusCode()), true)."\033[0m \n");
}else{
fwrite(STDERR, "\nStatus: \n \033[41m".print_r(json_encode($item->getStatusCode()), true)."\033[0m \n");
}
fwrite(STDERR, "\nResponse:\n \033[32m".print_r(json_encode(json_decode($item->getBody())), true)."\033[0m \n");
}
if(!$item instanceof \GuzzleHttp\Psr7\ServerRequest && !$item instanceof \Slim\Http\Response && !$item instanceof \GuzzleHttp\Psr7\Responsee){// $data
fwrite(STDERR, "\n".print_r(json_encode($item), true)."\n");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment