aboutsummaryrefslogtreecommitdiff
path: root/tests/jsonapi/UserEventsIndexTest.php
blob: 0941f09b664b45decb9b11032feb16716f395e34 (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
<?php

use JsonApi\Routes\Events\UserEventsIndex;

class UserEventsIndexTest 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 testIndexUserEvents()
    {
        $credentials = $this->tester->getCredentialsForTestAutor();
        $response = $this->getEvents($credentials);
        $this->assertSuccessfulResponse($response);

        $response = $this->getEvents($credentials);
        $this->assertSuccessfulResponse($response);
        $numEvents = count($response->document()->primaryResources());

        $this->createEvent($credentials);

        $response = $this->getEvents($credentials);
        $this->assertSuccessfulResponse($response);
        $this->tester->assertCount($numEvents + 1, $response->document()->primaryResources());
    }

    private function getEvents($credentials)
    {
        $app = $this->tester->createApp($credentials, 'get', '/users/{id}/events', UserEventsIndex::class);

        $requestBuilder = $this->tester->createRequestBuilder($credentials);
        $requestBuilder->setUri('/users/' . $credentials['id'] . '/events')->fetch();

        return $this->tester->sendMockRequest($app, $requestBuilder->getRequest());
    }

    private function assertSuccessfulResponse($response)
    {
        $this->tester->assertTrue($response->isSuccessfulDocument([200]));
        $document = $response->document();
        $this->tester->assertTrue($document->isResourceCollectionDocument());
    }

    private function createEvent($credentials)
    {
        $calendar = new \SingleCalendar($credentials['id']);
        $event = $calendar->getNewEvent();

        $oldUser = $GLOBALS['user'];
        $GLOBALS['user'] = \User::find($credentials['id']);

        $calendar->storeEvent($event, [$credentials['id']]);

        $GLOBALS['user'] = $oldUser;
    }
}