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
|
<?php
use JsonApi\Routes\Users\Rel\Contacts;
use JsonApi\Schemas\User as UserSchema;
class UsersRelationshipContactsReplaceTest 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 testShouldReplaceContacts()
{
$credentials = $this->tester->getCredentialsForTestAutor();
$credentials2 = $this->tester->getCredentialsForTestDozent();
$credentials3 = $this->tester->getCredentialsForRoot();
$this->assertNotEmpty(
\Contact::create(
[
'owner_id' => $credentials['id'],
'user_id' => $credentials2['id'],
]
)
);
$response = $this->replaceContactsOfUser(
$credentials,
[$credentials3['id']]
);
$this->tester->assertSame(204, $response->getStatusCode());
$this->tester->assertCount(1, \User::find($credentials['id'])->contacts);
}
// **** helper functions ****
private function replaceContactsOfUser($credentials, array $contactIds)
{
return $this->tester->sendMockRequest(
$this->tester->createApp($credentials, 'patch', '/users/{id}/relationships/contacts', Contacts::class),
$this->tester->createRequestBuilder($credentials)
->setUri('/users/'.$credentials['id'].'/relationships/contacts')
->setJsonApiBody($this->prepareValidBody($contactIds))
->update()
->getRequest()
);
}
private function prepareValidBody(array $contactIds)
{
return [
'data' => array_map(
function ($contactId) {
return [
'type' => UserSchema::TYPE,
'id' => $contactId,
];
},
$contactIds
),
];
}
}
|