diff options
| author | Jan-Hendrik Willms <tleilax+github@gmail.com> | 2021-07-22 16:07:19 +0200 |
|---|---|---|
| committer | Jan-Hendrik Willms <tleilax+github@gmail.com> | 2021-07-22 16:19:12 +0200 |
| commit | a3da1483a9e689846179159355badfec8073dbec (patch) | |
| tree | 770dcca6bdf5f6f2a11b0e7fcbbeda6919a3fc52 /tests/jsonapi/UsersRelationshipContactsAddTest.php | |
current code from svn, revision 62608
Diffstat (limited to 'tests/jsonapi/UsersRelationshipContactsAddTest.php')
| -rw-r--r-- | tests/jsonapi/UsersRelationshipContactsAddTest.php | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/tests/jsonapi/UsersRelationshipContactsAddTest.php b/tests/jsonapi/UsersRelationshipContactsAddTest.php new file mode 100644 index 0000000..b14cdf2 --- /dev/null +++ b/tests/jsonapi/UsersRelationshipContactsAddTest.php @@ -0,0 +1,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 + ), + ]; + } +} |
