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/MessagesCreateTest.php | |
current code from svn, revision 62608
Diffstat (limited to 'tests/jsonapi/MessagesCreateTest.php')
| -rw-r--r-- | tests/jsonapi/MessagesCreateTest.php | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/tests/jsonapi/MessagesCreateTest.php b/tests/jsonapi/MessagesCreateTest.php new file mode 100644 index 0000000..bcc3d07 --- /dev/null +++ b/tests/jsonapi/MessagesCreateTest.php @@ -0,0 +1,106 @@ +<?php + + +use JsonApi\Routes\Messages\MessageCreate; +use JsonApi\Schemas\Message; +use JsonApi\Schemas\User as UserSchema; + +class MessagesCreateTest 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 testShouldCreatePublicPosting() + { + $credentials = $this->tester->getCredentialsForTestDozent(); + $recipients = [ + $this->tester->getCredentialsForTestAutor(), + $this->tester->getCredentialsForRoot(), + ]; + $subject = 'Unterbrechen Sie, was immer Sie gerade tun. Dies mussen Sie sich ansehen.'; + $message = 'Euryalos hat einen Business-Vorschlag fur Sie.'; + $tags = ['foo', 'bar', 'baz']; + + $response = $this->createMessage($credentials, $recipients, $subject, $message, $tags); + + $this->tester->assertTrue($response->isSuccessfulDocument([201])); + + $document = $response->document(); + $this->tester->assertTrue($document->isSingleResourceDocument()); + + $resource = $document->primaryResource(); + $this->tester->assertNotEmpty($resource->id()); + $this->tester->assertSame(Message::TYPE, $resource->type()); + + $this->tester->assertSame($subject, $resource->attribute('subject')); + $this->tester->assertSame($message, $resource->attribute('message')); + + $this->tester->assertTrue($resource->hasRelationship('recipients')); + + $recipientsRel = $resource->relationship('recipients'); + $this->tester->assertTrue($recipientsRel->isToManyRelationship()); + + $this->tester->assertSame(count($recipients), count($recipientsRel->resourceLinks())); + } + + // **** helper functions **** + private function createMessage($sender, $recipients, $subject, $content, array $tags) + { + $requestBuilder = $this->tester->createRequestBuilder($sender); + $requestBuilder + ->setUri('/messages') + ->setJsonApiBody($this->prepareValidBody($recipients, $subject, $content, $tags)) + ->create(); + + $app = $this->tester->createApp($sender, 'post', '/messages', MessageCreate::class); + + return $this->tester->sendMockRequest($app, $requestBuilder->getRequest()); + } + + private function prepareValidBody(array $recipients, $subject, $message, $tags) + { + $recipientsData = array_reduce( + $recipients, + function ($memo, $recipient) { + $memo[] = [ + 'type' => UserSchema::TYPE, + 'id' => $recipient['id'], + ]; + + return $memo; + }, + [] + ); + + $json = [ + 'data' => [ + 'type' => Message::TYPE, + 'attributes' => [ + 'subject' => $subject, + 'message' => $message, + 'tags' => $tags, + ], + 'relationships' => [ + 'recipients' => [ + 'data' => $recipientsData, + ], + ], + ], + ]; + + return $json; + } +} |
