cs fixes, bug fixes, code coverage

This commit is contained in:
Dennis Eichhorn 2021-11-16 00:05:43 +01:00
parent 1f064cd1cb
commit 8e5669a77d
17 changed files with 1012 additions and 174 deletions

View File

@ -16,13 +16,13 @@ use phpOMS\Uri\UriFactory;
?>
<header>
<nav>
<ul>
<li><a href="<?= UriFactory::build('{/app}'); ?>">Website</a>
<li><a href="<?= UriFactory::build('{/app}/components'); ?>">Profile</a>
</ul>
</nav>
<div id="search">
<input type="text">
</div>
<nav>
<ul>
<li><a href="<?= UriFactory::build('{/app}'); ?>">Website</a>
<li><a href="<?= UriFactory::build('{/app}/components'); ?>">Profile</a>
</ul>
</nav>
<div id="search">
<input type="text">
</div>
</header>

View File

@ -12,9 +12,7 @@ If you have a good idea for improvement feel free to create a new issue with all
### Issues
Feel free to grab any open issue implement it and create a new pull request. Most issues can be found in the `Project.md` file in the `Docs` repository.
The issue information can be used to provide additional information such as priority, difficulty and type. For your first issue try to find a issue marked `[d:first]` or `[d:beginner]`.
Feel free to grab any open issue implement it and create a new pull request. Most issues can be found in the code marked with `@todo` or in the [PROJECT.md](https://github.com/Orange-Management/Docs/blob/master/Project/PROJECT.md) file.
### Code Style

View File

@ -271,7 +271,8 @@ final class ApiController extends Controller
if (!empty($uploadedFiles = $request->getFiles() ?? [])) {
$uploaded = $this->app->moduleManager->get('Media')->uploadFiles(
[''],
[],
[],
$uploadedFiles,
$request->header->account,
__DIR__ . '/../../../Modules/Media/Files/Modules/QA',

View File

@ -132,58 +132,6 @@ class QAAnswer implements \JsonSerializable
return $this->id;
}
/**
* Get the answer
*
* @return string
*
* @since 1.0.0
*/
public function getAnswer() : string
{
return $this->answer;
}
/**
* Set the answer
*
* @param string $answer Answer
*
* @return void
*
* @since 1.0.0
*/
public function setAnswer(string $answer) : void
{
$this->answer = $answer;
}
/**
* Get the question
*
* @return QAQuestion
*
* @since 1.0.0
*/
public function getQuestion() : QAQuestion
{
return $this->question;
}
/**
* Set the question
*
* @param QAQuestion $question Question
*
* @return void
*
* @since 1.0.0
*/
public function setQuestion(QAQuestion $question) : void
{
$this->question = $question;
}
/**
* Get the status
*
@ -210,20 +158,6 @@ class QAAnswer implements \JsonSerializable
$this->status = $status;
}
/**
* Set the answer as accepted
*
* @param bool $accepted Accepted
*
* @return void
*
* @since 1.0.0
*/
public function setAccepted(bool $accepted) : void
{
$this->isAccepted = $accepted;
}
/**
* Get the total vote score
*
@ -261,6 +195,32 @@ class QAAnswer implements \JsonSerializable
return 0;
}
/**
* Get all votes
*
* @return QAVnswerVote[]
*
* @since 1.0.0
*/
public function getVotes() : array
{
return $this->votes;
}
/**
* Add vote
*
* @param QAAnswerVote $vote Vote
*
* @return void
*
* @since 1.0.0
*/
public function addVote(QAAnswerVote $vote) : void
{
$this->votes[] = $vote;
}
/**
* Get all media
*
@ -290,8 +250,27 @@ class QAAnswer implements \JsonSerializable
/**
* {@inheritdoc}
*/
public function jsonSerialize() : array
public function toArray() : array
{
return [];
return [
'id' => $this->id,
'status' => $this->status,
'answer' => $this->answer,
'answerRaw' => $this->answerRaw,
'question' => $this->question,
'isAccepted' => $this->isAccepted,
'createdBy' => $this->createdBy,
'createdAt' => $this->createdAt,
'votes' => $this->votes,
'media' => $this->media,
];
}
/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return $this->toArray();
}
}

View File

@ -18,6 +18,7 @@ use Modules\Media\Models\Media;
use Modules\Profile\Models\NullProfile;
use Modules\Profile\Models\Profile;
use Modules\Tag\Models\Tag;
use phpOMS\Localization\ISO639x1Enum;
/**
* QA question class.
@ -75,7 +76,7 @@ class QAQuestion implements \JsonSerializable
* @var string
* @since 1.0.0
*/
private string $language = '';
private string $language = ISO639x1Enum::_EN;
/**
* Created by.
@ -156,7 +157,7 @@ class QAQuestion implements \JsonSerializable
}
/**
* Finds all accounts in Question
* Finds all accounts in the question
* e.g. asked by and all accoounts who answered
*
* @return array
@ -217,24 +218,6 @@ class QAQuestion implements \JsonSerializable
$this->language = $language;
}
/**
* Is the question answered?
*
* @return bool
*
* @since 1.0.0
*/
public function isAnswered() : bool
{
foreach ($this->answers as $answer) {
if ($answer->isAccepted()) {
return true;
}
}
return false;
}
/**
* Get the status
*
@ -262,11 +245,49 @@ class QAQuestion implements \JsonSerializable
}
/**
* Get tags
* Adding new tag.
*
* @return array
* @param Tag $tag Tag
*
* @return int
*
* @since 1.0.0
*/
public function addTag(Tag $tag) : int
{
$this->tags[] = $tag;
\end($this->tags);
$key = (int) \key($this->tags);
\reset($this->tags);
return $key;
}
/**
* Remove Tag from list.
*
* @param int $id Tag
*
* @return bool
*
* @since 1.0.0
*/
public function removeTag($id) : bool
{
if (isset($this->tags[$id])) {
unset($this->tags[$id]);
return true;
}
return false;
}
/**
* Get task elements.
*
* @return Tag[]
*
* @since 1.0.0
*/
@ -276,27 +297,17 @@ class QAQuestion implements \JsonSerializable
}
/**
* Add tag to question
* Get task elements.
*
* @param int|Tag $tag Tag
* @param int $id Element id
*
* @return Tag
*
* @since 1.0.0
*/
public function addTag(int | Tag $tag) : void
public function getTag(int $id) : Tag
{
$this->tags[] = $tag;
}
/**
* Set tags to question
*
* @param array<int, int|Tag> $tags Tags
*
* @since 1.0.0
*/
public function setTags(array $tags) : void
{
$this->tags = $tags;
return $this->tags[$id] ?? new NullTag();
}
/**
@ -328,6 +339,32 @@ class QAQuestion implements \JsonSerializable
return $score;
}
/**
* Get all votes
*
* @return QAVnswerVote[]
*
* @since 1.0.0
*/
public function getVotes() : array
{
return $this->votes;
}
/**
* Add vote
*
* @param QAQuestionVote $vote Vote
*
* @return void
*
* @since 1.0.0
*/
public function addVote(QAQuestionVote $vote) : void
{
$this->votes[] = $vote;
}
/**
* Get the vote score from an account
*
@ -431,8 +468,30 @@ class QAQuestion implements \JsonSerializable
/**
* {@inheritdoc}
*/
public function jsonSerialize() : array
public function toArray() : array
{
return [];
return [
'id' => $this->id,
'name' => $this->name,
'status' => $this->status,
'question' => $this->question,
'questionRaw' => $this->questionRaw,
'language' => $this->language,
'createdBy' => $this->createdBy,
'createdAt' => $this->createdAt,
'app' => $this->app,
'tags' => $this->tags,
'answers' => $this->votes,
'votes' => $this->votes,
'media' => $this->media,
];
}
/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return $this->toArray();
}
}

View File

@ -0,0 +1,333 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\QA\tests\Controller;
use Model\CoreSettings;
use Modules\Admin\Models\AccountPermission;
use Modules\QA\Models\QAAnswerStatus;
use Modules\QA\Models\QAQuestionStatus;
use phpOMS\Account\Account;
use phpOMS\Account\AccountManager;
use phpOMS\Account\PermissionType;
use phpOMS\Application\ApplicationAbstract;
use phpOMS\DataStorage\Session\HttpSession;
use phpOMS\Dispatcher\Dispatcher;
use phpOMS\Event\EventManager;
use phpOMS\Localization\ISO639x1Enum;
use phpOMS\Message\Http\HttpRequest;
use phpOMS\Message\Http\HttpResponse;
use phpOMS\Message\Http\RequestStatusCode;
use phpOMS\Module\ModuleAbstract;
use phpOMS\Module\ModuleManager;
use phpOMS\Router\WebRouter;
use phpOMS\System\MimeType;
use phpOMS\Uri\HttpUri;
use phpOMS\Utils\TestUtils;
/**
* @testdox Modules\QA\tests\Controller\ApiControllerTest: QA api controller
*
* @internal
*/
final class ApiControllerTest extends \PHPUnit\Framework\TestCase
{
protected ApplicationAbstract $app;
/**
* @var \Modules\QA\Controller\ApiController
*/
protected ModuleAbstract $module;
/**
* {@inheritdoc}
*/
protected function setUp() : void
{
$this->app = new class() extends ApplicationAbstract
{
protected string $appName = 'Api';
};
$this->app->dbPool = $GLOBALS['dbpool'];
$this->app->orgId = 1;
$this->app->accountManager = new AccountManager($GLOBALS['session']);
$this->app->appSettings = new CoreSettings();
$this->app->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../../Modules/');
$this->app->dispatcher = new Dispatcher($this->app);
$this->app->eventManager = new EventManager($this->app->dispatcher);
$this->app->eventManager->importFromFile(__DIR__ . '/../../../../Web/Api/Hooks.php');
$this->app->sessionManager = new HttpSession(36000);
$account = new Account();
TestUtils::setMember($account, 'id', 1);
$permission = new AccountPermission();
$permission->setUnit(1);
$permission->setApp('backend');
$permission->setPermission(
PermissionType::READ
| PermissionType::CREATE
| PermissionType::MODIFY
| PermissionType::DELETE
| PermissionType::PERMISSION
);
$account->addPermission($permission);
$this->app->accountManager->add($account);
$this->app->router = new WebRouter();
$this->module = $this->app->moduleManager->get('QA');
TestUtils::setMember($this->module, 'app', $this->app);
}
/**
* @covers Modules\QA\Controller\ApiController
* @group module
*/
public function testApiQAAppCreate() : void
{
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->header->account = 1;
$request->setData('name', 'TestQAApp');
$this->module->apiQAAppCreate($request, $response);
self::assertGreaterThan(0, $response->get('')['response']->getId());
}
/**
* @covers Modules\QA\Controller\ApiController
* @group module
*/
public function testApiQAAppCreateInvalidData() : void
{
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->header->account = 1;
$request->setData('invalid', '1');
$this->module->apiQAAppCreate($request, $response);
self::assertEquals(RequestStatusCode::R_400, $response->header->status);
}
/**
* @covers Modules\QA\Controller\ApiController
* @group module
*/
public function testApiQAQuestionCreate() : void
{
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->header->account = 1;
$request->setData('title', 'Test Question');
$request->setData('plain', 'Question content');
$request->setData('language', ISO639x1Enum::_EN);
$request->setData('status', QAQuestionStatus::ACTIVE);
$request->setData('tags', '[{"title": "TestTitle", "color": "#f0f", "language": "en"}, {"id": 1}]');
if (!\is_file(__DIR__ . '/test_tmp.md')) {
\copy(__DIR__ . '/test.md', __DIR__ . '/test_tmp.md');
}
TestUtils::setMember($request, 'files', [
'file1' => [
'name' => 'test.md',
'type' => MimeType::M_TXT,
'tmp_name' => __DIR__ . '/test_tmp.md',
'error' => \UPLOAD_ERR_OK,
'size' => \filesize(__DIR__ . '/test_tmp.md'),
],
]);
$request->setData('media', \json_encode([1]));
$this->module->apiQAQuestionCreate($request, $response);
self::assertGreaterThan(0, $response->get('')['response']->getId());
}
/**
* @covers Modules\QA\Controller\ApiController
* @group module
*/
public function testApiQAQuestionCreateInvalidData() : void
{
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->header->account = 1;
$request->setData('invalid', '1');
$this->module->apiQAQuestionCreate($request, $response);
self::assertEquals(RequestStatusCode::R_400, $response->header->status);
}
/**
* @covers Modules\QA\Controller\ApiController
* @group module
*/
public function testApiQAAnswerCreate() : void
{
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->header->account = 1;
$request->setData('question', '1');
$request->setData('plain', 'Answer content');
$request->setData('status', QAAnswerStatus::ACTIVE);
if (!\is_file(__DIR__ . '/test_tmp.md')) {
\copy(__DIR__ . '/test.md', __DIR__ . '/test_tmp.md');
}
TestUtils::setMember($request, 'files', [
'file1' => [
'name' => 'test.md',
'type' => MimeType::M_TXT,
'tmp_name' => __DIR__ . '/test_tmp.md',
'error' => \UPLOAD_ERR_OK,
'size' => \filesize(__DIR__ . '/test_tmp.md'),
],
]);
$request->setData('media', \json_encode([1]));
$this->module->apiQAAnswerCreate($request, $response);
self::assertGreaterThan(0, $response->get('')['response']->getId());
}
/**
* @covers Modules\QA\Controller\ApiController
* @group module
*/
public function testApiChangeAnsweredStatus() : void
{
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->header->account = 1;
$request->setData('id', '1');
$request->setData('accepted', '1');
$this->module->apiChangeAnsweredStatus($request, $response);
self::assertGreaterThan(0, $response->get('')['response']->getId());
}
/**
* @covers Modules\QA\Controller\ApiController
* @group module
*/
public function testApiQAAnswerCreateInvalidData() : void
{
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->header->account = 1;
$request->setData('invalid', '1');
$this->module->apiQAAnswerCreate($request, $response);
self::assertEquals(RequestStatusCode::R_400, $response->header->status);
}
/**
* @covers Modules\QA\Controller\ApiController
* @group module
*/
public function testApiChangeQAQuestionVote() : void
{
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->header->account = 1;
$request->setData('id', '1');
$request->setData('type', '-1');
$this->module->apiChangeQAQuestionVote($request, $response);
self::assertGreaterThan(0, $response->get('')['response']->getId());
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->header->account = 1;
$request->setData('id', '1');
$request->setData('type', '1');
$this->module->apiChangeQAQuestionVote($request, $response);
self::assertGreaterThan(0, $response->get('')['response']->getId());
}
/**
* @covers Modules\QA\Controller\ApiController
* @group module
*/
public function testApiChangeQAQuestionVoteInvalidData() : void
{
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->header->account = 1;
$request->setData('invalid', '1');
$this->module->apiChangeQAQuestionVote($request, $response);
self::assertEquals(RequestStatusCode::R_400, $response->header->status);
}
/**
* @covers Modules\QA\Controller\ApiController
* @group module
*/
public function testApiChangeQAAnswerVote() : void
{
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->header->account = 1;
$request->setData('id', '1');
$request->setData('type', '-1');
$this->module->apiChangeQAAnswerVote($request, $response);
self::assertGreaterThan(0, $response->get('')['response']->getId());
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->header->account = 1;
$request->setData('id', '1');
$request->setData('type', '1');
$this->module->apiChangeQAAnswerVote($request, $response);
self::assertGreaterThan(0, $response->get('')['response']->getId());
}
/**
* @covers Modules\QA\Controller\ApiController
* @group module
*/
public function testApiChangeQAAnswerVoteInvalidData() : void
{
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->header->account = 1;
$request->setData('invalid', '1');
$this->module->apiChangeQAAnswerVote($request, $response);
self::assertEquals(RequestStatusCode::R_400, $response->header->status);
}
}

3
tests/Controller/test.md Normal file
View File

@ -0,0 +1,3 @@
# Test Title
Some **test** text.

View File

@ -20,7 +20,6 @@ use Modules\QA\Models\NullQAQuestion;
use Modules\QA\Models\QAAnswer;
use Modules\QA\Models\QAAnswerMapper;
use Modules\QA\Models\QAAnswerStatus;
use phpOMS\Utils\RnG\Text;
/**
* @internal
@ -28,6 +27,7 @@ use phpOMS\Utils\RnG\Text;
final class QAAnswerMapperTest extends \PHPUnit\Framework\TestCase
{
/**
* @depends Modules\QA\tests\Models\QAQuestionMapperTest::testCRUD
* @covers Modules\QA\Models\QAAnswerMapper
* @group module
*/
@ -35,19 +35,19 @@ final class QAAnswerMapperTest extends \PHPUnit\Framework\TestCase
{
$answer = new QAAnswer();
$answer->setAnswer('Answer content');
$answer->answer = 'Answer content';
$answer->setStatus(QAAnswerStatus::ACTIVE);
$answer->createdBy = new Profile(new NullAccount(1));
$answer->setQuestion(new NullQAQuestion(1));
$answer->setAccepted(true);
$answer->createdBy = new Profile(new NullAccount(1));
$answer->question = new NullQAQuestion(1);
$answer->isAccepted = true;
$id = QAAnswerMapper::create($answer);
self::assertGreaterThan(0, $answer->getId());
self::assertEquals($id, $answer->getId());
$answerR = QAAnswerMapper::get($answer->getId());
self::assertEquals($answer->getAnswer(), $answerR->getAnswer());
self::assertEquals($answer->getQuestion()->getId(), $answerR->getQuestion()->getId());
self::assertEquals($answer->answer, $answerR->answer);
self::assertEquals($answer->question->getId(), $answerR->question->getId());
self::assertEquals($answer->getStatus(), $answerR->getStatus());
self::assertEquals($answer->isAccepted, $answerR->isAccepted);
self::assertEquals($answer->createdBy->account->getId(), $answerR->createdBy->account->getId());

View File

@ -14,51 +14,108 @@ declare(strict_types=1);
namespace Modules\QA\tests\Models;
use Modules\Profile\Models\NullProfile;
use Modules\QA\Models\NullQAQuestion;
use Modules\Admin\Models\NullAccount;
use Modules\Media\Models\Media;
use Modules\QA\Models\QAAnswer;
use Modules\QA\Models\QAAnswerStatus;
use Modules\QA\Models\QAAnswerVote;
/**
* @internal
*/
final class QAAnswerTest extends \PHPUnit\Framework\TestCase
{
private QAAnswer $answer;
/**
* {@inheritdoc}
*/
protected function setUp() : void
{
$this->answer = new QAAnswer();
}
/**
* @covers Modules\QA\Models\QAAnswer
* @group module
*/
public function testDefault() : void
{
$answer = new QAAnswer();
self::assertEquals(0, $answer->getId());
self::assertEquals('', $answer->getAnswer());
self::assertEquals(0, $answer->getQuestion()->getId());
self::assertFalse($answer->isAccepted);
self::assertEquals(QAAnswerStatus::ACTIVE, $answer->getStatus());
self::assertEquals(0, $answer->createdBy->getId());
self::assertInstanceOf('\DateTimeImmutable', $answer->createdAt);
self::assertEquals(0, $this->answer->getId());
self::assertEquals('', $this->answer->answer);
self::assertEquals(0, $this->answer->question->getId());
self::assertFalse($this->answer->isAccepted);
self::assertEquals(QAAnswerStatus::ACTIVE, $this->answer->getStatus());
self::assertEquals(0, $this->answer->createdBy->getId());
self::assertEquals(0, $this->answer->getVoteScore());
self::assertEquals(0, $this->answer->getAccountVoteScore(0));
self::assertEquals([], $this->answer->getMedia());
self::assertEquals([], $this->answer->getVotes());
self::assertInstanceOf('\DateTimeImmutable', $this->answer->createdAt);
}
/**
* @covers Modules\QA\Models\QAAnswer
* @group module
*/
public function testSetGet() : void
public function testStatusInputOutput() : void
{
$answer = new QAAnswer();
$this->answer->setStatus(QAAnswerStatus::ACTIVE);
self::assertEquals(QAAnswerStatus::ACTIVE, $this->answer->getStatus());
}
$answer->setAnswer('Answer content');
$answer->setStatus(QAAnswerStatus::ACTIVE);
$answer->setQuestion(new NullQAQuestion(3));
$answer->createdBy = new NullProfile(1);
$answer->setAccepted(true);
/**
* @covers Modules\QA\Models\QAAnswer
* @group module
*/
public function testMediaInputOutput() : void
{
$this->answer->addMedia(new Media());
self::assertCount(1, $this->answer->getMedia());
}
self::assertEquals('Answer content', $answer->getAnswer());
self::assertEquals(QAAnswerStatus::ACTIVE, $answer->getStatus());
self::assertEquals(1, $answer->createdBy->getId());
self::assertEquals(3, $answer->getQuestion()->getId());
self::assertTrue($answer->isAccepted);
/**
* @covers Modules\QA\Models\QAAnswer
* @group module
*/
public function testVoteInputOutput() : void
{
$vote = new QAAnswerVote();
$vote->createdBy = new NullAccount(1);
$vote->score = 1;
$this->answer->addVote($vote);
self::assertCount(1, $this->answer->getVotes());
self::assertEquals(1, $this->answer->getVoteScore());
self::assertEquals(1, $this->answer->getAccountVoteScore(1));
}
/**
* @covers Modules\QA\Models\QAAnswer
* @group module
*/
public function testSerialize() : void
{
$this->answer->setStatus(QAAnswerStatus::ACTIVE);
$this->answer->answer = 'Answer';
$this->answer->answerRaw = 'AnswerRaw';
$serialized = $this->answer->jsonSerialize();
unset($serialized['question']);
unset($serialized['createdBy']);
unset($serialized['createdAt']);
self::assertEquals(
[
'id' => 0,
'status' => QAAnswerStatus::ACTIVE,
'answer' => 'Answer',
'answerRaw' => 'AnswerRaw',
'isAccepted' => false,
'votes' => [],
'media' => [],
],
$serialized
);
}
}

View File

@ -0,0 +1,48 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\QA\tests\Models;
use Modules\Admin\Models\NullAccount;
use Modules\QA\Models\QAAnswerVote;
use Modules\QA\Models\QAAnswerVoteMapper;
/**
* @internal
*/
final class QAAnswerVoteMapperTest extends \PHPUnit\Framework\TestCase
{
/**
* @depends Modules\QA\tests\Models\QAAnswerMapperTest::testCRUD
* @covers Modules\QA\Models\QAAnswerVoteMapper
* @group module
*/
public function testCRUD() : void
{
$vote = new QAAnswerVote();
$vote->answer = 1;
$vote->score = 1;
$vote->createdBy = new NullAccount(1);
$id = QAAnswerVoteMapper::create($vote);
self::assertGreaterThan(0, $vote->getId());
self::assertEquals($id, $vote->getId());
$voteR = QAAnswerVoteMapper::get($vote->getId());
self::assertEquals($vote->answer, $voteR->answer);
self::assertEquals($vote->score, $voteR->score);
self::assertEquals(1, QAAnswerVoteMapper::findVote(1, 1)->getId());
}
}

View File

@ -0,0 +1,44 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\QA\tests\Models;
use Modules\QA\Models\QAAnswerVote;
/**
* @internal
*/
final class QAAnswerVoteTest extends \PHPUnit\Framework\TestCase
{
private QAAnswerVote $vote;
/**
* {@inheritdoc}
*/
protected function setUp() : void
{
$this->vote = new QAAnswerVote();
}
/**
* @covers Modules\QA\Models\QAAnswerVote
* @group module
*/
public function testDefault() : void
{
self::assertEquals(0, $this->vote->getId());
self::assertEquals(0, $this->vote->answer);
self::assertEquals(0, $this->vote->score);
}
}

View File

@ -0,0 +1,62 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\QA\tests\Models;
use Modules\QA\Models\QAApp;
/**
* @internal
*/
final class QAAppTest extends \PHPUnit\Framework\TestCase
{
private QAApp $app;
/**
* {@inheritdoc}
*/
protected function setUp() : void
{
$this->app = new QAApp();
}
/**
* @covers Modules\QA\Models\QAApp
* @group module
*/
public function testDefault() : void
{
self::assertEquals(0, $this->app->getId());
self::assertEquals('', $this->app->name);
}
/**
* @covers Modules\QA\Models\QAApp
* @group module
*/
public function testSerialize() : void
{
$this->app->name = 'Test Title';
$serialized = $this->app->jsonSerialize();
self::assertEquals(
[
'id' => 0,
'name' => 'Test Title',
],
$serialized
);
}
}

View File

@ -0,0 +1,33 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\QA\tests\Models;
use Modules\QA\Models\QAHelperMapper;
/**
* @internal
*/
final class QAHelperMapperTest extends \PHPUnit\Framework\TestCase
{
/**
* @depends Modules\QA\tests\Models\QAAnswerVoteMapperTest::testCRUD
* @covers Modules\QA\Models\QAHelperMapper
* @group module
*/
public function testAccountScore() : void
{
self::assertGreaterThan(0, QAHelperMapper::getAccountScore([1])[1]);
}
}

View File

@ -19,7 +19,6 @@ use Modules\Profile\Models\Profile;
use Modules\QA\Models\QAQuestion;
use Modules\QA\Models\QAQuestionMapper;
use Modules\QA\Models\QAQuestionStatus;
use phpOMS\Utils\RnG\Text;
/**
* @internal

View File

@ -14,51 +14,181 @@ declare(strict_types=1);
namespace Modules\QA\tests\Models;
use Modules\Admin\Models\NullAccount;
use Modules\Media\Models\Media;
use Modules\Profile\Models\NullProfile;
use Modules\QA\Models\QAAnswer;
use Modules\QA\Models\QAQuestion;
use Modules\QA\Models\QAQuestionStatus;
use Modules\QA\Models\QAQuestionVote;
use Modules\Tag\Models\Tag;
use phpOMS\Localization\ISO639x1Enum;
/**
* @internal
*/
final class QAQuestionTest extends \PHPUnit\Framework\TestCase
{
private QAQuestion $question;
/**
* {@inheritdoc}
*/
protected function setUp() : void
{
$this->question = new QAQuestion();
}
/**
* @covers Modules\QA\Models\QAQuestion
* @group module
*/
public function testDefault() : void
{
$question = new QAQuestion();
self::assertEquals(0, $question->getId());
self::assertEquals('', $question->name);
self::assertEquals('', $question->question);
self::assertEquals(QAQuestionStatus::ACTIVE, $question->getStatus());
self::assertEquals('', $question->getLanguage());
self::assertEquals(0, $question->createdBy->getId());
self::assertInstanceOf('\DateTimeImmutable', $question->createdAt);
self::assertEquals([], $question->getTags());
self::assertEquals(0, $this->question->getId());
self::assertEquals('', $this->question->name);
self::assertEquals('', $this->question->question);
self::assertEquals('', $this->question->questionRaw);
self::assertEquals(QAQuestionStatus::ACTIVE, $this->question->getStatus());
self::assertEquals(ISO639x1Enum::_EN, $this->question->getLanguage());
self::assertEquals(0, $this->question->createdBy->getId());
self::assertInstanceOf('\DateTimeImmutable', $this->question->createdAt);
self::assertFalse($this->question->hasAccepted());
self::assertEquals([0 => 0], $this->question->getAccounts()); // includes createdBy
self::assertEquals([], $this->question->getTags());
self::assertEquals([], $this->question->getMedia());
self::assertEquals([], $this->question->getAnswers());
self::assertEquals([], $this->question->getAnswersByScore());
self::assertEquals(0, $this->question->getVoteScore());
self::assertEquals(0, $this->question->getAccountVoteScore(0));
self::assertEquals(0, $this->question->getAnswerCount());
}
/**
* @covers Modules\QA\Models\QAQuestion
* @group module
*/
public function testSetGet() : void
public function testStatusInputOutput() : void
{
$question = new QAQuestion();
$this->question->setStatus(QAQuestionStatus::ACTIVE);
self::assertEquals(QAQuestionStatus::ACTIVE, $this->question->getStatus());
}
$question->name = 'Question Name';
$question->question = 'Question content';
$question->setStatus(QAQuestionStatus::ACTIVE);
$question->createdBy = new NullProfile(1);
$question->setLanguage('en');
/**
* @covers Modules\QA\Models\QAQuestion
* @group module
*/
public function testLanguageInputOutput() : void
{
$this->question->setLanguage(ISO639x1Enum::_DE);
self::assertEquals(ISO639x1Enum::_DE, $this->question->getLanguage());
}
self::assertEquals('Question Name', $question->name);
self::assertEquals('Question content', $question->question);
self::assertEquals(QAQuestionStatus::ACTIVE, $question->getStatus());
self::assertEquals('en', $question->getLanguage());
self::assertEquals(1, $question->createdBy->getId());
/**
* @covers Modules\QA\Models\QAQuestion
* @group module
*/
public function testMediaInputOutput() : void
{
$this->question->addMedia(new Media());
self::assertCount(1, $this->question->getMedia());
}
/**
* @covers Modules\QA\Models\QAQuestion
* @group module
*/
public function testTagInputOutput() : void
{
$tag = new Tag();
$tag->setL11n('Tag');
$this->question->addTag($tag);
self::assertEquals($tag, $this->question->getTag(0));
self::assertCount(1, $this->question->getTags());
}
/**
* @covers Modules\QA\Models\QAQuestion
* @group module
*/
public function testTagRemove() : void
{
$tag = new Tag();
$tag->setL11n('Tag');
$this->question->addTag($tag);
self::assertTrue($this->question->removeTag(0));
self::assertCount(0, $this->question->getTags());
self::assertFalse($this->question->removeTag(0));
}
/**
* @covers Modules\QA\Models\QAQuestion
* @group module
*/
public function testAnswerInputOutput() : void
{
$answer = new QAAnswer();
$answer->createdBy = new NullProfile(1);
$answer->isAccepted = true;
$this->question->addAnswer($answer);
$this->question->addAnswer(clone $answer);
self::assertTrue($this->question->hasAccepted());
self::assertCount(1, $this->question->getAccounts());
self::assertCount(2, $this->question->getAnswers());
self::assertCount(2, $this->question->getAnswersByScore());
self::assertEquals(2, $this->question->getAnswerCount());
}
/**
* @covers Modules\QA\Models\QAQuestion
* @group module
*/
public function testVoteInputOutput() : void
{
$vote = new QAQuestionVote();
$vote->createdBy = new NullAccount(1);
$vote->score = 1;
$this->question->addVote($vote);
self::assertCount(1, $this->question->getVotes());
self::assertEquals(1, $this->question->getVoteScore());
self::assertEquals(1, $this->question->getAccountVoteScore(1));
}
/**
* @covers Modules\QA\Models\QAQuestion
* @group module
*/
public function testSerialize() : void
{
$this->question->name = 'Test Title';
$this->question->setStatus(QAQuestionStatus::ACTIVE);
$this->question->question = 'Question';
$this->question->questionRaw = 'QuestionRaw';
$this->question->setLanguage(ISO639x1Enum::_DE);
$serialized = $this->question->jsonSerialize();
unset($serialized['app']);
unset($serialized['createdBy']);
unset($serialized['createdAt']);
self::assertEquals(
[
'id' => 0,
'name' => 'Test Title',
'status' => QAQuestionStatus::ACTIVE,
'question' => 'Question',
'questionRaw' => 'QuestionRaw',
'language' => ISO639x1Enum::_DE,
'tags' => [],
'answers' => [],
'votes' => [],
'media' => [],
],
$serialized
);
}
}

View File

@ -0,0 +1,48 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\QA\tests\Models;
use Modules\Admin\Models\NullAccount;
use Modules\QA\Models\QAQuestionVote;
use Modules\QA\Models\QAQuestionVoteMapper;
/**
* @internal
*/
final class QAQuestionVoteMapperTest extends \PHPUnit\Framework\TestCase
{
/**
* @depends Modules\QA\tests\Models\QAQuestionMapperTest::testCRUD
* @covers Modules\QA\Models\QAQuestionVoteMapper
* @group module
*/
public function testCRUD() : void
{
$vote = new QAQuestionVote();
$vote->question = 1;
$vote->score = 1;
$vote->createdBy = new NullAccount(1);
$id = QAQuestionVoteMapper::create($vote);
self::assertGreaterThan(0, $vote->getId());
self::assertEquals($id, $vote->getId());
$voteR = QAQuestionVoteMapper::get($vote->getId());
self::assertEquals($vote->question, $voteR->question);
self::assertEquals($vote->score, $voteR->score);
self::assertEquals(1, QAQuestionVoteMapper::findVote(1, 1)->getId());
}
}

View File

@ -0,0 +1,44 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\QA\tests\Models;
use Modules\QA\Models\QAQuestionVote;
/**
* @internal
*/
final class QAQuestionVoteTest extends \PHPUnit\Framework\TestCase
{
private QAQuestionVote $vote;
/**
* {@inheritdoc}
*/
protected function setUp() : void
{
$this->vote = new QAQuestionVote();
}
/**
* @covers Modules\QA\Models\QAQuestionVote
* @group module
*/
public function testDefault() : void
{
self::assertEquals(0, $this->vote->getId());
self::assertEquals(0, $this->vote->question);
self::assertEquals(0, $this->vote->score);
}
}