155 lines
4.7 KiB
PHP
155 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace KupShop\CommentsBundle\Tests;
|
|
|
|
use KupShop\CommentsBundle\Comment;
|
|
use KupShop\CommentsBundle\Exception\CommentException;
|
|
use KupShop\CommentsBundle\Util\CommentList;
|
|
use KupShop\CommentsBundle\Util\CommentsUtil;
|
|
use KupShop\DevelopmentBundle\MailArchive;
|
|
use KupShop\KupShopBundle\Context\ContextManager;
|
|
use KupShop\KupShopBundle\Context\UserContext;
|
|
|
|
class CommentsTest extends \DatabaseTestCase
|
|
{
|
|
/** @var CommentsUtil */
|
|
private $commentsUtil;
|
|
|
|
/** @var CommentList */
|
|
private $commentList;
|
|
|
|
/** @var MailArchive */
|
|
private $mailArchive;
|
|
|
|
/** @var ContextManager */
|
|
private $contextManager;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->commentsUtil = $this->get(CommentsUtil::class);
|
|
$this->commentList = $this->get(CommentList::class);
|
|
$this->mailArchive = $this->get(MailArchive::class);
|
|
$this->contextManager = $this->get(ContextManager::class);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider data_GetCommentById
|
|
*/
|
|
public function testGetCommentById(int $id): void
|
|
{
|
|
$comment = $this->commentsUtil->getCommentById($id);
|
|
|
|
$this->assertInstanceOf(Comment::class, $comment);
|
|
$this->assertEquals($id, $comment->getId());
|
|
}
|
|
|
|
public function data_GetCommentById(): array
|
|
{
|
|
return [
|
|
[1],
|
|
[2],
|
|
];
|
|
}
|
|
|
|
public function testCommentList(): void
|
|
{
|
|
$comments = $this->commentList->getComments(1, Comment::TYPE_PRODUCT);
|
|
|
|
$this->assertNotEmpty($comments, 'Check that comments are loaded');
|
|
$this->assertCount(1, $comments);
|
|
|
|
$comment = reset($comments);
|
|
|
|
$this->assertInstanceOf(Comment::class, $comment);
|
|
$this->assertNotEmpty($comment->getChildren(), 'Check that children are loaded to comment');
|
|
}
|
|
|
|
public function testCommentListDifferentLanguage(): void
|
|
{
|
|
$comments = $this->commentList->getComments(1, Comment::TYPE_PRODUCT, 'sk');
|
|
|
|
$this->assertNotEmpty($comments, 'Check that SK comments are loaded');
|
|
}
|
|
|
|
public function testAddCommentWithoutUser(): void
|
|
{
|
|
$this->expectException(CommentException::class);
|
|
|
|
$this->commentsUtil->addComment(1, Comment::TYPE_PRODUCT, 'Test comment');
|
|
}
|
|
|
|
/**
|
|
* @dataProvider data_AddCommentWithLoggedUser
|
|
*/
|
|
public function testAddCommentWithLoggedUser(int $objectId, string $type): void
|
|
{
|
|
$dbcfg = \Settings::getDefault();
|
|
$dbcfg->saveValue('comments', ['shopkeeper_email' => 'comments@wpj.cz']);
|
|
\Settings::clearCache(true);
|
|
|
|
$result = $this->asLoggedUser(function () use ($objectId, $type) {
|
|
return $this->commentsUtil->addComment($objectId, $type, 'Test comment');
|
|
});
|
|
|
|
$lastMail = $this->mailArchive->getLast();
|
|
|
|
$this->assertInstanceOf(Comment::class, $result, 'Check that u are able to add comment as logged user');
|
|
$this->assertEquals('comments@wpj.cz', $lastMail['to'], 'Check that email notification to admin was send');
|
|
}
|
|
|
|
public function data_AddCommentWithLoggedUser(): array
|
|
{
|
|
return [
|
|
[1, Comment::TYPE_PRODUCT],
|
|
[2, Comment::TYPE_ARTICLE],
|
|
];
|
|
}
|
|
|
|
public function testAddCommentEmptyContent(): void
|
|
{
|
|
$this->expectException(CommentException::class);
|
|
|
|
$this->asLoggedUser(function () {
|
|
return $this->commentsUtil->addComment(1, Comment::TYPE_PRODUCT, '');
|
|
});
|
|
}
|
|
|
|
public function testAddCommentWithSpecifiedAdmin(): void
|
|
{
|
|
$this->assertInstanceOf(
|
|
Comment::class,
|
|
$this->commentsUtil->addComment(1, Comment::TYPE_PRODUCT, 'Test comment', null, null, null, 1),
|
|
'Check that u are able to add comment with specified adminId argument only'
|
|
);
|
|
}
|
|
|
|
public function testAddCommentReply(): void
|
|
{
|
|
$result = $this->asLoggedUser(function () {
|
|
return $this->commentsUtil->addComment(1, Comment::TYPE_PRODUCT, 'Test comment', 1);
|
|
});
|
|
|
|
$lastMail = $this->mailArchive->getLast();
|
|
|
|
$this->assertInstanceOf(Comment::class, $result, 'Check that u are able to add comment reply');
|
|
$this->assertNotEmpty($result->getParent(), 'Check that parent is set');
|
|
$this->assertEquals('petr@wpj.cz', $lastMail['to'], 'Check that email notification to user was send');
|
|
}
|
|
|
|
protected function getDataSet()
|
|
{
|
|
return $this->getJsonDataSetFromFile(__DIR__.'/CommentsTest.json');
|
|
}
|
|
|
|
private function asLoggedUser(callable $callback)
|
|
{
|
|
$user = \User::createFromId(1);
|
|
|
|
return $this->contextManager->activateContexts([UserContext::class => $user], function () use ($callback) {
|
|
return $callback();
|
|
});
|
|
}
|
|
}
|