Skip to content

Commit

Permalink
test: added some more tests for helper classes
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed May 17, 2024
1 parent 8aa04bc commit 6f64efa
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 2 deletions.
4 changes: 2 additions & 2 deletions phpmyfaq/src/phpMyFAQ/Helper/UserHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
*
* @package phpMyFAQ\Helper
*/
class UserHelper
readonly class UserHelper
{
/**
* UserHelper constructor.
*/
public function __construct(private readonly User $user)
public function __construct(private User $user)
{
}

Expand Down
49 changes: 49 additions & 0 deletions tests/phpMyFAQ/Helper/AttachmentHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace phpMyFAQ\Helper;

use phpMyFAQ\Attachment\AttachmentAbstract;
use phpMyFAQ\Translation;
use PHPUnit\Framework\TestCase;

class AttachmentHelperTest extends TestCase
{
private AttachmentHelper $attachmentHelper;

protected function setUp(): void
{
Translation::create()
->setLanguagesDir(PMF_TRANSLATION_DIR)
->setDefaultLanguage('en')
->setCurrentLanguage('en')
->setMultiByteLanguage();


$this->attachmentHelper = new AttachmentHelper();
}

public function testRenderAttachmentListEmpty()
{
$attachmentList = [];
$result = $this->attachmentHelper->renderAttachmentList($attachmentList);
$this->assertEquals('', $result);
}

public function testRenderAttachmentListWithAttachments()
{
$attachmentMock = $this->createMock(AttachmentAbstract::class);
$attachmentMock->method('getMimeType')->willReturn('application/pdf');
$attachmentMock->method('buildUrl')->willReturn('http://example.com/file.pdf');
$attachmentMock->method('getFilename')->willReturn('file.pdf');

$attachmentList = [$attachmentMock];

$result = $this->attachmentHelper->renderAttachmentList($attachmentList);

$expectedHtml = '<p>Attached files:</p><ul>';
$expectedHtml .= '<li><i class="bi bi-file-pdf-o" aria-hidden="true"></i> <a href="http://example.com/file.pdf">file&period;pdf</a></li>';
$expectedHtml .= '</ul>';

$this->assertEquals($expectedHtml, $result);
}
}
73 changes: 73 additions & 0 deletions tests/phpMyFAQ/Helper/TagsHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace phpMyFAQ\Helper;

use PHPUnit\Framework\TestCase;

class TagsHelperTest extends TestCase
{
private $tagsHelper;

protected function setUp(): void
{
$this->tagsHelper = new TagsHelper();
}

public function testRenderTagList()
{
$tags = [
1 => 'tag1',
2 => 'tag2',
];

$this->tagsHelper->setTaggingIds([1, 2]);

$expectedOutput = '<a class="btn btn-primary m-1" href="?action=search&amp;tagging_id=2">tag1 ' .
'<i aria-hidden="true" class="bi bi-minus-square"></i></a> ' .
'<a class="btn btn-primary m-1" href="?action=search&amp;tagging_id=1">tag2 ' .
'<i aria-hidden="true" class="bi bi-minus-square"></i></a> ';

$result = $this->tagsHelper->renderTagList($tags);
$this->assertEquals($expectedOutput, $result);
}

public function testRenderSearchTag()
{
$tagId = 1;
$tagName = 'tag1';

$this->tagsHelper->setTaggingIds([1, 2]);

$expectedOutput = '<a class="btn btn-primary m-1" href="?action=search&amp;tagging_id=2">tag1 ' .
'<i aria-hidden="true" class="bi bi-minus-square"></i></a> ';

$result = $this->tagsHelper->renderSearchTag($tagId, $tagName);
$this->assertEquals($expectedOutput, $result);
}

public function testGetTaggingIds()
{
$taggingIds = [1, 2, 3];
$this->tagsHelper->setTaggingIds($taggingIds);

$result = $this->tagsHelper->getTaggingIds();
$this->assertEquals($taggingIds, $result);
}

public function testRenderRelatedTag()
{
$tagId = 1;
$tagName = 'tag1';
$relevance = 10;

$this->tagsHelper->setTaggingIds([2, 3]);

$expectedOutput = '<a class="btn btn-primary m-1" href="?action=search&amp;tagging_id=2,3,1">' .
'<i aria-hidden="true" class="bi bi-plus-square"></i> tag1 ' .
'<span class="badge bg-secondary">10</span></a>';

$result = $this->tagsHelper->renderRelatedTag($tagId, $tagName, $relevance);
$this->assertEquals($expectedOutput, $result);
}
}

0 comments on commit 6f64efa

Please sign in to comment.