Skip to content

Commit f790820

Browse files
committed
Tests of comments presenter and sending of email notifications
1 parent 9d34b3c commit f790820

File tree

1 file changed

+67
-5
lines changed

1 file changed

+67
-5
lines changed

tests/Presenters/CommentsPresenter.phpt

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
$container = require_once __DIR__ . "/../bootstrap.php";
33

4+
use App\Helpers\Notifications\SolutionCommentsEmailsSender;
45
use App\V1Module\Presenters\CommentsPresenter;
56
use Tester\Assert;
67

@@ -36,20 +37,21 @@ class TestCommentsPresenter extends Tester\TestCase
3637
protected function setUp()
3738
{
3839
PresenterTestHelper::fillDatabase($this->container);
39-
4040
$this->presenter = PresenterTestHelper::createPresenter($this->container, CommentsPresenter::class);
4141
}
4242

4343
protected function tearDown()
4444
{
45+
Mockery::close();
46+
4547
if ($this->user->isLoggedIn()) {
4648
$this->user->logout(true);
4749
}
4850
}
4951

5052
public function testGetCommentsInNormalThread()
5153
{
52-
$token = PresenterTestHelper::login($this->container, $this->userLogin, $this->userPassword);
54+
$token = PresenterTestHelper::login($this->container, $this->userLogin);
5355

5456
$request = new Nette\Application\Request('V1:Comments', 'GET', ['action' => 'default', 'id' => 'mainThread']);
5557
$response = $this->presenter->run($request);
@@ -68,7 +70,7 @@ class TestCommentsPresenter extends Tester\TestCase
6870

6971
public function testGetCommentsInEmptyThread()
7072
{
71-
$token = PresenterTestHelper::login($this->container, $this->userLogin, $this->userPassword);
73+
$token = PresenterTestHelper::login($this->container, $this->userLogin);
7274

7375
$request = new Nette\Application\Request('V1:Comments', 'GET', ['action' => 'default', 'id' => 'emptyThread']);
7476
$response = $this->presenter->run($request);
@@ -82,7 +84,7 @@ class TestCommentsPresenter extends Tester\TestCase
8284

8385
public function testAddCommentIntoExistingThread()
8486
{
85-
$token = PresenterTestHelper::login($this->container, $this->userLogin, $this->userPassword);
87+
$token = PresenterTestHelper::login($this->container, $this->userLogin);
8688

8789
$request = new Nette\Application\Request(
8890
'V1:Comments',
@@ -104,9 +106,69 @@ class TestCommentsPresenter extends Tester\TestCase
104106
Assert::same($this->presenter->comments->findOneBy(['id' => $comment->id]), $result['payload']);
105107
}
106108

109+
public function testAddCommentIntoExistingAssignmentSolutionThread()
110+
{
111+
PresenterTestHelper::login($this->container, $this->userLogin);
112+
$assignmentSolution = current($this->presenter->assignmentSolutions->findAll());
113+
114+
// mock emails sender
115+
$mockSolutionCommentsEmailsSender = Mockery::mock(SolutionCommentsEmailsSender::class);
116+
$mockSolutionCommentsEmailsSender->shouldReceive("assignmentSolutionComment")->once();
117+
$this->presenter->solutionCommentsEmailsSender = $mockSolutionCommentsEmailsSender;
118+
119+
$request = new Nette\Application\Request(
120+
'V1:Comments',
121+
'POST',
122+
['action' => 'addComment', 'id' => $assignmentSolution->getId()],
123+
['text' => 'some comment text', 'isPrivate' => 'false']
124+
);
125+
$response = $this->presenter->run($request);
126+
Assert::type(Nette\Application\Responses\JsonResponse::class, $response);
127+
128+
$result = $response->getPayload();
129+
Assert::equal(200, $result['code']);
130+
$comment = $result['payload'];
131+
Assert::false($comment->isPrivate);
132+
Assert::equal("some comment text", $comment->text);
133+
Assert::equal($assignmentSolution->getId(), $comment->commentThread->id);
134+
135+
// Make sure the assignment was persisted
136+
Assert::same($this->presenter->comments->findOneBy(['id' => $comment->id]), $result['payload']);
137+
}
138+
139+
public function testAddCommentIntoExistingReferenceSolutionThread()
140+
{
141+
PresenterTestHelper::login($this->container, $this->userLogin);
142+
$referenceSolution = current($this->presenter->referenceExerciseSolutions->findAll());
143+
144+
// mock emails sender
145+
$mockSolutionCommentsEmailsSender = Mockery::mock(SolutionCommentsEmailsSender::class);
146+
$mockSolutionCommentsEmailsSender->shouldReceive("referenceSolutionComment")->once();
147+
$this->presenter->solutionCommentsEmailsSender = $mockSolutionCommentsEmailsSender;
148+
149+
$request = new Nette\Application\Request(
150+
'V1:Comments',
151+
'POST',
152+
['action' => 'addComment', 'id' => $referenceSolution->getId()],
153+
['text' => 'some comment text', 'isPrivate' => 'false']
154+
);
155+
$response = $this->presenter->run($request);
156+
Assert::type(Nette\Application\Responses\JsonResponse::class, $response);
157+
158+
$result = $response->getPayload();
159+
Assert::equal(200, $result['code']);
160+
$comment = $result['payload'];
161+
Assert::false($comment->isPrivate);
162+
Assert::equal("some comment text", $comment->text);
163+
Assert::equal($referenceSolution->getId(), $comment->commentThread->id);
164+
165+
// Make sure the assignment was persisted
166+
Assert::same($this->presenter->comments->findOneBy(['id' => $comment->id]), $result['payload']);
167+
}
168+
107169
public function testAddCommentIntoNonexistingThread()
108170
{
109-
$token = PresenterTestHelper::login($this->container, $this->userLogin, $this->userPassword);
171+
$token = PresenterTestHelper::login($this->container, $this->userLogin);
110172

111173
$request = new Nette\Application\Request(
112174
'V1:Comments',

0 commit comments

Comments
 (0)