engelsystem/tests/Unit/Controllers/ControllerTest.php

89 lines
2.9 KiB
PHP
Raw Normal View History

2020-12-15 13:39:00 +01:00
<?php
declare(strict_types=1);
2020-12-15 13:39:00 +01:00
namespace Engelsystem\Test\Unit\Controllers;
use Engelsystem\Config\Config;
2023-02-02 22:53:51 +01:00
use Engelsystem\Controllers\NotificationType;
2020-12-15 13:39:00 +01:00
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
use Engelsystem\Http\UrlGenerator;
use Engelsystem\Http\UrlGeneratorInterface;
use Engelsystem\Test\Unit\HasDatabase;
use Engelsystem\Test\Unit\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\Test\TestLogger;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
abstract class ControllerTest extends TestCase
{
use HasDatabase;
protected Config $config;
2020-12-15 13:39:00 +01:00
protected TestLogger $log;
2020-12-15 13:39:00 +01:00
protected Response|MockObject $response;
2020-12-15 13:39:00 +01:00
protected Request $request;
2020-12-15 13:39:00 +01:00
protected Session $session;
2020-12-15 13:39:00 +01:00
2020-12-18 18:27:10 +01:00
/**
2023-02-02 22:53:51 +01:00
* @param string|string[] $value
2020-12-18 18:27:10 +01:00
*/
2023-02-02 22:53:51 +01:00
protected function setNotification(string|array $value, NotificationType $type = NotificationType::MESSAGE): void
2020-12-18 18:27:10 +01:00
{
2023-02-02 22:53:51 +01:00
$this->session->set(
'messages.' . $type->value,
array_merge($this->session->get('messages.' . $type->value, []), (array) $value)
);
}
protected function assertHasNotification(string $value, NotificationType $type = NotificationType::MESSAGE): void
{
$messages = $this->session->get('messages.' . $type->value, []);
$this->assertTrue(in_array($value, $messages), 'Has ' . $type->value . ' notification: ' . $value);
}
protected function assertHasNoNotifications(NotificationType $type = null): void
{
$messages = $this->session->get('messages' . ($type ? '.' . $type->value : ''), []);
$this->assertEmpty($messages, 'Has no' . ($type ? ' ' . $type->value : '') . ' notification.');
2020-12-18 18:27:10 +01:00
}
2020-12-15 13:39:00 +01:00
/**
* Setup environment
*/
public function setUp(): void
{
parent::setUp();
$this->initDatabase();
$this->request = Request::create('http://localhost');
$this->app->instance('request', $this->request);
$this->app->instance(Request::class, $this->request);
$this->app->instance(ServerRequestInterface::class, $this->request);
$this->response = $this->createMock(Response::class);
$this->app->instance(Response::class, $this->response);
$this->log = new TestLogger();
$this->app->instance(LoggerInterface::class, $this->log);
$this->session = new Session(new MockArraySessionStorage());
$this->app->instance('session', $this->session);
2020-12-18 18:27:10 +01:00
$this->app->instance(Session::class, $this->session);
2020-12-15 13:39:00 +01:00
$this->app->bind(UrlGeneratorInterface::class, UrlGenerator::class);
$this->config = new Config();
$this->app->instance('config', $this->config);
2021-07-14 01:56:03 +02:00
$this->app->instance(Config::class, $this->config);
2020-12-15 13:39:00 +01:00
}
}