engelsystem/tests/Unit/Controllers/Admin/LogsControllerTest.php

113 lines
3.8 KiB
PHP
Raw Permalink Normal View History

2020-05-01 16:29:28 +02:00
<?php
declare(strict_types=1);
2020-05-01 16:29:28 +02:00
namespace Engelsystem\Test\Unit\Controllers\Admin;
use Engelsystem\Config\Config;
2020-05-01 16:29:28 +02:00
use Engelsystem\Controllers\Admin\LogsController;
use Engelsystem\Helpers\Authenticator;
2020-05-01 16:29:28 +02:00
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
use Engelsystem\Models\LogEntry;
use Engelsystem\Models\User\User;
2020-05-01 16:29:28 +02:00
use Engelsystem\Test\Unit\HasDatabase;
use Engelsystem\Test\Unit\TestCase;
use Illuminate\Database\Eloquent\Collection;
use Psr\Log\LogLevel;
class LogsControllerTest extends TestCase
{
use HasDatabase;
/**
* @covers \Engelsystem\Controllers\Admin\LogsController::index
* @covers \Engelsystem\Controllers\Admin\LogsController::__construct
*/
public function testIndex(): void
2020-05-01 16:29:28 +02:00
{
$log = new LogEntry();
$alert = $log->create(['level' => LogLevel::ALERT, 'message' => 'Alert test']);
$alert = $log->with('user')->find($alert)->first();
2020-05-01 16:29:28 +02:00
$error = $log->create(['level' => LogLevel::ERROR, 'message' => 'Error test']);
$error = $log->with('user')->find($error)->first();
$auth = $this->createMock(Authenticator::class);
$this->setExpects($auth, 'user', null, null, 2);
$this->setExpects($auth, 'can', ['logs.all'], true, 2);
2020-05-01 16:29:28 +02:00
$response = $this->createMock(Response::class);
$response->expects($this->exactly(2))
->method('withView')
->withConsecutive(
['admin/log.twig', [
'entries' => new Collection([$error, $alert]),
'search' => null,
'users' => new Collection(),
'search_user_id' => null,
]],
['admin/log.twig', [
'entries' => new Collection([$error]),
'search' => 'error',
'users' => new Collection(),
'search_user_id' => null,
]]
2020-05-01 16:29:28 +02:00
)
->willReturn($response);
$request = Request::create('/');
$controller = new LogsController($log, $response, $auth);
2020-05-01 16:29:28 +02:00
$controller->index($request);
$request->request->set('search', 'error');
$controller->index($request);
}
/**
* @covers \Engelsystem\Controllers\Admin\LogsController::index
*/
public function testIndexUser(): void
{
User::factory()->create();
$user = User::with(['personalData', 'state'])->first();
$log = new LogEntry();
$alert = $log->create(['level' => LogLevel::ALERT, 'message' => 'Users message', 'user_id' => $user->id]);
/** @var LogEntry $alert */
$alert = $log->with('user')->find($alert)->first();
$log->create(['level' => LogLevel::ERROR, 'message' => 'Error test']);
$auth = $this->createMock(Authenticator::class);
$this->setExpects($auth, 'user', null, $user);
$this->setExpects($auth, 'can', ['logs.all'], false);
$response = $this->createMock(Response::class);
$response->expects($this->once())
->method('withView')
->willReturnCallback(function (string $view, array $data) use ($alert, $response) {
$this->assertEquals('admin/log.twig', $view);
$this->assertArrayHasKey('entries', $data);
$this->assertCount(1, $data['entries']);
$this->assertEquals($alert->message, $data['entries'][0]['message']);
return $response;
});
$request = Request::create('/');
$controller = new LogsController($log, $response, $auth);
$controller->index($request);
}
/**
* Set up the DB
2020-05-01 16:29:28 +02:00
*/
public function setUp(): void
{
parent::setUp();
$this->initDatabase();
$this->app->instance('config', new Config([]));
2020-05-01 16:29:28 +02:00
}
}