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

165 lines
5.0 KiB
PHP
Raw Permalink Normal View History

2020-12-06 00:16:15 +01:00
<?php
declare(strict_types=1);
2020-12-06 00:16:15 +01:00
namespace Engelsystem\Test\Unit\Controllers\Admin;
use Engelsystem\Controllers\Admin\FaqController;
2023-02-02 22:53:51 +01:00
use Engelsystem\Controllers\NotificationType;
2020-12-06 00:16:15 +01:00
use Engelsystem\Http\Exceptions\ValidationException;
use Engelsystem\Http\Validation\Validator;
use Engelsystem\Models\Faq;
2020-12-15 13:39:00 +01:00
use Engelsystem\Test\Unit\Controllers\ControllerTest;
2020-12-06 00:16:15 +01:00
2020-12-15 13:39:00 +01:00
class FaqControllerTest extends ControllerTest
2020-12-06 00:16:15 +01:00
{
protected array $data = [
2020-12-06 00:16:15 +01:00
'question' => 'Foo?',
'text' => 'Bar!',
];
/**
* @covers \Engelsystem\Controllers\Admin\FaqController::__construct
* @covers \Engelsystem\Controllers\Admin\FaqController::edit
* @covers \Engelsystem\Controllers\Admin\FaqController::showEdit
*/
public function testEdit(): void
2020-12-06 00:16:15 +01:00
{
$this->request->attributes->set('faq_id', 1);
2020-12-06 00:16:15 +01:00
$this->response->expects($this->once())
->method('withView')
->willReturnCallback(function ($view, $data) {
$this->assertEquals('pages/faq/edit.twig', $view);
$this->assertNotEmpty($data['faq']);
return $this->response;
});
/** @var FaqController $controller */
$controller = $this->app->make(FaqController::class);
$controller->edit($this->request);
2023-02-02 22:53:51 +01:00
$this->assertHasNoNotifications(NotificationType::WARNING);
2020-12-06 00:16:15 +01:00
}
/**
* @covers \Engelsystem\Controllers\Admin\FaqController::save
*/
public function testSaveCreateInvalid(): void
2020-12-06 00:16:15 +01:00
{
/** @var FaqController $controller */
$this->expectException(ValidationException::class);
$controller = $this->app->make(FaqController::class);
$controller->setValidator(new Validator());
$controller->save($this->request);
}
/**
* @covers \Engelsystem\Controllers\Admin\FaqController::save
*/
public function testSaveCreateEdit(): void
2020-12-06 00:16:15 +01:00
{
$this->request->attributes->set('faq_id', 2);
2020-12-06 00:16:15 +01:00
$body = $this->data;
$this->request = $this->request->withParsedBody($body);
$this->response->expects($this->once())
->method('redirectTo')
->with('http://localhost/faq#faq-2')
->willReturn($this->response);
/** @var FaqController $controller */
$controller = $this->app->make(FaqController::class);
$controller->setValidator(new Validator());
$controller->save($this->request);
$this->assertTrue($this->log->hasInfoThatContains('Updated'));
$faq = (new Faq())->find(2);
$this->assertEquals('Foo?', $faq->question);
$this->assertEquals('Bar!', $faq->text);
2023-02-02 22:53:51 +01:00
$this->assertHasNotification('faq.edit.success');
2020-12-06 00:16:15 +01:00
}
/**
* @covers \Engelsystem\Controllers\Admin\FaqController::save
*/
public function testSavePreview(): void
2020-12-06 00:16:15 +01:00
{
$this->request->attributes->set('faq_id', 1);
2020-12-06 00:16:15 +01:00
$this->request = $this->request->withParsedBody([
'question' => 'New question',
'text' => 'New text',
'preview' => '1',
]);
$this->response->expects($this->once())
->method('withView')
->willReturnCallback(function ($view, $data) {
$this->assertEquals('pages/faq/edit.twig', $view);
/** @var Faq $faq */
$faq = $data['faq'];
// Contains new text
$this->assertEquals('New question', $faq->question);
$this->assertEquals('New text', $faq->text);
return $this->response;
});
/** @var FaqController $controller */
$controller = $this->app->make(FaqController::class);
$controller->setValidator(new Validator());
$controller->save($this->request);
// Assert no changes
$faq = Faq::find(1);
$this->assertEquals('Lorem', $faq->question);
$this->assertEquals('Ipsum!', $faq->text);
}
/**
* @covers \Engelsystem\Controllers\Admin\FaqController::save
* @covers \Engelsystem\Controllers\Admin\FaqController::delete
2020-12-06 00:16:15 +01:00
*/
public function testSaveDelete(): void
2020-12-06 00:16:15 +01:00
{
$this->request->attributes->set('faq_id', 1);
2020-12-06 00:16:15 +01:00
$this->request = $this->request->withParsedBody([
'question' => '.',
'text' => '.',
'delete' => '1',
]);
$this->response->expects($this->once())
->method('redirectTo')
->with('http://localhost/faq')
->willReturn($this->response);
/** @var FaqController $controller */
$controller = $this->app->make(FaqController::class);
$controller->setValidator(new Validator());
$controller->save($this->request);
$this->assertTrue($this->log->hasInfoThatContains('Deleted'));
2023-02-02 22:53:51 +01:00
$this->assertHasNotification('faq.delete.success');
2020-12-06 00:16:15 +01:00
}
/**
* Setup environment
*/
public function setUp(): void
{
parent::setUp();
(new Faq([
'question' => 'Lorem',
'text' => 'Ipsum!',
]))->save();
}
}