function it_should_create_a_multipart_option_for_file_attachment(Message $message) { $message->hasFileToUpload()->willReturn(true); $message->getFileStream()->willReturn('stream'); $message->jsonSerialize()->willReturn(['data' => 'value']); $this::createForMessage('rid', $message, 'notif')->shouldReturn([RequestOptions::MULTIPART => [['name' => 'recipient', 'contents' => json_encode(['id' => 'rid'])], ['name' => 'message', 'contents' => json_encode(['data' => 'value'])], ['name' => 'notification_type', 'contents' => 'notif'], ['name' => 'filedata', 'contents' => 'stream']], 'timeout' => Client::DEFAULT_FILE_UPLOAD_TIMEOUT]); }
/** * @param string $recipientOrPhone * @param Message $message * @param string $notificationType * * @return array */ public static function createForMessage($recipientOrPhone, Message $message, $notificationType = NotificationType::REGULAR) { $options = []; $data = ['recipient' => self::createRecipientField($recipientOrPhone), 'message' => $message, 'notification_type' => $notificationType]; if ($message->hasFileToUpload()) { // Create a multipart request $options[RequestOptions::MULTIPART] = [['name' => 'recipient', 'contents' => json_encode($data['recipient'])], ['name' => 'message', 'contents' => json_encode($data['message'])], ['name' => 'notification_type', 'contents' => $data['notification_type']], ['name' => 'filedata', 'contents' => $message->getFileStream()]]; // Update timeout if we upload a file $options['timeout'] = Client::DEFAULT_FILE_UPLOAD_TIMEOUT; return $options; } $options[RequestOptions::JSON] = $data; return $options; }