aboutsummaryrefslogtreecommitdiff
path: root/tests/jsonapi/MessagesOutboxTest.php
blob: 21b59cd3d13e343fdb673c7518bdafd636bf44dd (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
<?php


use JsonApi\Routes\Messages\OutboxShow;

class MessagesOutboxTest extends \Codeception\Test\Unit
{
    /**
     * @var \UnitTester
     */
    protected $tester;

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

    protected function _after()
    {
    }

    // tests

    public function testShouldShowInitiallyEmptyOutbox()
    {
        $credentials = $this->tester->getCredentialsForTestAutor();
        $response = $this->getOutbox($credentials);

        $this->tester->assertTrue($response->isSuccessfulDocument([200]));
        $this->tester->assertEmpty($response->document()->primaryResources());
    }

    public function testShouldShowNonEmptyOutboxAfterMessage()
    {
        $credentials = $this->tester->getCredentialsForTestAutor();

        $this->sendMessage($credentials);
        $response = $this->getOutbox($credentials);

        $this->tester->assertTrue($response->isSuccessfulDocument([200]));
        $this->tester->assertCount(1, $response->document()->primaryResources());
    }

    // **** helper functions ****
    private function getOutbox($credentials)
    {
        $app = $this->tester->createApp($credentials, 'get', '/users/{id}/outbox', OutboxShow::class);

        return $this->tester->sendMockRequest(
            $app,
            $this->tester->createRequestBuilder($credentials)
            ->setUri('/users/'.($credentials['id']).'/outbox')
            ->fetch()
            ->getRequest()
        );
    }

    private function sendMessage(array $credentials)
    {
        // EVIL HACK
        $oldUser = $GLOBALS['user'];
        $GLOBALS['user'] = \User::find($credentials['id']);

        $message = \Message::send($credentials['id'], [$credentials['username']], 'empty subject', 'empty message');

        // EVIL HACK
        $GLOBALS['user'] = $oldUser;

        return $message;
    }
}