aboutsummaryrefslogtreecommitdiff
path: root/tests/jsonapi/CommentsShowTest.php
blob: d57c3356a6b23030de05be31521ae9c3d7073a8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php

require_once 'NewsTestHelper.php';
use JsonApi\Routes\News\CommentsIndex;
use JsonApi\Routes\News\CommentsShow;

class CommentsShowTest extends \Codeception\Test\Unit
{
    use NewsTestHelper;

    /**
     * @var \UnitTester
     */
    protected $tester;

    protected function _before()
    {
        \DBManager::getInstance()->setConnection('studip', $this->getModule('\\Helper\\StudipDb')->dbh);
    }

    protected function _after()
    {
    }

    //testszenario:
    public function testShowAllComments()
    {
        $credentials = $this->tester->getCredentialsForRoot();
        $title = 'A testing title';
        $content = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit';
        $news = $this->createNews($credentials, $title, $content);

        $newsId = $news->id;

        $response = $this->getComments($credentials, $newsId);
    }

    public function testShowOneComment()
    {
        $credentials = $this->tester->getCredentialsForRoot();
        $title = 'A testing title';
        $content = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit';
        $news = $this->createNews($credentials, $title, $content);

        $comment = $this->createComment($credentials, $news, $content);

        $response = $this->getComment($credentials, $comment);
        $this->tester->assertSame(200, $response->getStatusCode());
        $this->tester->assertTrue($response->isSuccessfulDocument([200]));
        $document = $response->document();
    }

    //helpers:
    private function getComments($credentials, $newsId)
    {
        $app = $this->tester->createApp($credentials, 'get', '/news/{id}/comments', CommentsIndex::class);

        return $this->tester->sendMockRequest(
                $app,
                $this->tester->createRequestBuilder($credentials)
                ->setUri('/news/'.$newsId.'/comments')
                ->fetch()
                ->getRequest()
        );
    }

    private function getComment($credentials, $comment)
    {
        $app = $this->tester->createApp($credentials, 'get', '/comments/{id}', CommentsShow::class);

        return $this->tester->sendMockRequest(
                $app,
                $this->tester->createRequestBuilder($credentials)
                ->setUri('/comments/'.$comment->id)
                ->fetch()
                ->getRequest()
        );
    }
}