Tests: Added abstract ControllerTest

This commit is contained in:
Igor Scheller 2020-12-15 13:39:00 +01:00 committed by msquare
parent 857ed23548
commit e322867716
3 changed files with 87 additions and 99 deletions

View File

@ -2,44 +2,22 @@
namespace Engelsystem\Test\Unit\Controllers\Admin; namespace Engelsystem\Test\Unit\Controllers\Admin;
use Engelsystem\Config\Config;
use Engelsystem\Controllers\Admin\FaqController; use Engelsystem\Controllers\Admin\FaqController;
use Engelsystem\Helpers\Authenticator;
use Engelsystem\Http\Exceptions\ValidationException; use Engelsystem\Http\Exceptions\ValidationException;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
use Engelsystem\Http\UrlGenerator;
use Engelsystem\Http\UrlGeneratorInterface;
use Engelsystem\Http\Validation\Validator; use Engelsystem\Http\Validation\Validator;
use Engelsystem\Models\Faq; use Engelsystem\Models\Faq;
use Engelsystem\Test\Unit\HasDatabase; use Engelsystem\Test\Unit\Controllers\ControllerTest;
use Engelsystem\Test\Unit\TestCase;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Psr\Log\Test\TestLogger;
use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
class FaqControllerTest extends TestCase class FaqControllerTest extends ControllerTest
{ {
use HasDatabase;
/** @var array */ /** @var array */
protected $data = [ protected $data = [
'question' => 'Foo?', 'question' => 'Foo?',
'text' => 'Bar!', 'text' => 'Bar!',
]; ];
/** @var TestLogger */
protected $log;
/** @var Response|MockObject */
protected $response;
/** @var Request */
protected $request;
/** /**
* @covers \Engelsystem\Controllers\Admin\FaqController::__construct * @covers \Engelsystem\Controllers\Admin\FaqController::__construct
* @covers \Engelsystem\Controllers\Admin\FaqController::edit * @covers \Engelsystem\Controllers\Admin\FaqController::edit
@ -185,22 +163,6 @@ class FaqControllerTest extends TestCase
public function setUp(): void public function setUp(): void
{ {
parent::setUp(); parent::setUp();
$this->initDatabase();
$this->request = Request::create('http://localhost');
$this->app->instance('request', $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->app->instance('session', new Session(new MockArraySessionStorage()));
$this->app->bind(UrlGeneratorInterface::class, UrlGenerator::class);
$this->app->instance('config', new Config());
(new Faq([ (new Faq([
'question' => 'Lorem', 'question' => 'Lorem',

View File

@ -2,31 +2,19 @@
namespace Engelsystem\Test\Unit\Controllers\Admin; namespace Engelsystem\Test\Unit\Controllers\Admin;
use Engelsystem\Config\Config;
use Engelsystem\Controllers\Admin\NewsController; use Engelsystem\Controllers\Admin\NewsController;
use Engelsystem\Helpers\Authenticator; use Engelsystem\Helpers\Authenticator;
use Engelsystem\Http\Exceptions\ValidationException; use Engelsystem\Http\Exceptions\ValidationException;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
use Engelsystem\Http\UrlGenerator;
use Engelsystem\Http\UrlGeneratorInterface;
use Engelsystem\Http\Validation\Validator; use Engelsystem\Http\Validation\Validator;
use Engelsystem\Models\News; use Engelsystem\Models\News;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Engelsystem\Test\Unit\HasDatabase; use Engelsystem\Test\Unit\Controllers\ControllerTest;
use Engelsystem\Test\Unit\TestCase;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use PHPUnit\Framework\MockObject\MockObject; 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\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
class NewsControllerTest extends TestCase class NewsControllerTest extends ControllerTest
{ {
use HasDatabase;
/** @var Authenticator|MockObject */ /** @var Authenticator|MockObject */
protected $auth; protected $auth;
@ -40,15 +28,6 @@ class NewsControllerTest extends TestCase
] ]
]; ];
/** @var TestLogger */
protected $log;
/** @var Response|MockObject */
protected $response;
/** @var Request */
protected $request;
/** /**
* @covers \Engelsystem\Controllers\Admin\NewsController::edit * @covers \Engelsystem\Controllers\Admin\NewsController::edit
* @covers \Engelsystem\Controllers\Admin\NewsController::showEdit * @covers \Engelsystem\Controllers\Admin\NewsController::showEdit
@ -295,42 +274,6 @@ class NewsControllerTest extends TestCase
$this->assertEquals('news.delete.success', $messages[0]); $this->assertEquals('news.delete.success', $messages[0]);
} }
/**
* 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->app->instance('session', new Session(new MockArraySessionStorage()));
$this->auth = $this->createMock(Authenticator::class);
$this->app->instance(Authenticator::class, $this->auth);
$this->app->bind(UrlGeneratorInterface::class, UrlGenerator::class);
$this->app->instance('config', new Config());
(new News([
'title' => 'Foo',
'text' => '<b>foo</b>',
'is_meeting' => false,
'user_id' => 1,
]))->save();
}
/** /**
* Creates a new user * Creates a new user
*/ */
@ -350,4 +293,22 @@ class NewsControllerTest extends TestCase
->method('user') ->method('user')
->willReturn($user); ->willReturn($user);
} }
/**
* Setup environment
*/
public function setUp(): void
{
parent::setUp();
$this->auth = $this->createMock(Authenticator::class);
$this->app->instance(Authenticator::class, $this->auth);
(new News([
'title' => 'Foo',
'text' => '<b>foo</b>',
'is_meeting' => false,
'user_id' => 1,
]))->save();
}
} }

View File

@ -0,0 +1,65 @@
<?php
namespace Engelsystem\Test\Unit\Controllers;
use Engelsystem\Config\Config;
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;
/** @var Config */
protected $config;
/** @var TestLogger */
protected $log;
/** @var Response|MockObject */
protected $response;
/** @var Request */
protected $request;
/** @var Session */
protected $session;
/**
* 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);
$this->app->bind(UrlGeneratorInterface::class, UrlGenerator::class);
$this->config = new Config();
$this->app->instance('config', $this->config);
}
}