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


use JsonApi\Routes\Users\Rel\Contacts;
use JsonApi\Schemas\User as UserSchema;

class UsersRelationshipContactsAddTest 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 testShouldBeInitiallyEmpty()
    {
        $credentials = $this->tester->getCredentialsForTestAutor();
        $this->tester->assertCount(0, \User::find($credentials['id'])->contacts);
    }

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

        $response = $this->addContactToUser(
            $credentials,
            [
                $this->tester->getCredentialsForTestDozent()['id'],
                $this->tester->getCredentialsForTestDozent()['id'],
                $this->tester->getCredentialsForTestDozent()['id'],
                $this->tester->getCredentialsForTestDozent()['id'],
                $this->tester->getCredentialsForRoot()['id'],
            ]
        );
        $this->tester->assertSame(204, $response->getStatusCode());
        $this->tester->assertCount(2, \User::find($credentials['id'])->contacts);
    }

    // **** helper functions ****
    private function addContactToUser($credentials, array $contactIds)
    {
        return $this->tester->sendMockRequest(
            $this->tester->createApp($credentials, 'post', '/users/{id}/relationships/contacts', Contacts::class),
            $this->tester->createRequestBuilder($credentials)
            ->setUri('/users/'.$credentials['id'].'/relationships/contacts')
            ->setJsonApiBody($this->prepareValidBody($contactIds))
            ->create()
            ->getRequest()
        );
    }

    private function prepareValidBody(array $contactIds)
    {
        return [
            'data' => array_map(
                function ($contactId) {
                    return [
                        'type' => UserSchema::TYPE,
                        'id' => $contactId,
                    ];
                },
                $contactIds
            ),
        ];
    }
}