|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Tests\Richdocuments\Service; |
| 11 | + |
| 12 | +use OCA\Richdocuments\AppConfig; |
| 13 | +use OCA\Richdocuments\Service\CapabilitiesService; |
| 14 | +use OCA\Richdocuments\Service\RemoteService; |
| 15 | +use OCP\Http\Client\IClient; |
| 16 | +use OCP\Http\Client\IClientService; |
| 17 | +use OCP\Http\Client\IResponse; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use Psr\Log\LoggerInterface; |
| 21 | + |
| 22 | +class RemoteServiceTest extends TestCase { |
| 23 | + private AppConfig&MockObject $appConfig; |
| 24 | + private IClientService&MockObject $clientService; |
| 25 | + private IClient&MockObject $client; |
| 26 | + private CapabilitiesService&MockObject $capabilitiesService; |
| 27 | + private LoggerInterface&MockObject $logger; |
| 28 | + private RemoteService $service; |
| 29 | + |
| 30 | + protected function setUp(): void { |
| 31 | + parent::setUp(); |
| 32 | + |
| 33 | + $this->appConfig = $this->createMock(AppConfig::class); |
| 34 | + $this->clientService = $this->createMock(IClientService::class); |
| 35 | + $this->client = $this->createMock(IClient::class); |
| 36 | + $this->capabilitiesService = $this->createMock(CapabilitiesService::class); |
| 37 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 38 | + |
| 39 | + $this->clientService->method('newClient') |
| 40 | + ->willReturn($this->client); |
| 41 | + |
| 42 | + $this->service = new RemoteService( |
| 43 | + $this->appConfig, |
| 44 | + $this->clientService, |
| 45 | + $this->capabilitiesService, |
| 46 | + $this->logger, |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + public function testConvertToSendsConversionOptionsAsMultipartFields(): void { |
| 51 | + $stream = fopen('php://memory', 'r+'); |
| 52 | + $response = $this->createMock(IResponse::class); |
| 53 | + |
| 54 | + $this->appConfig->method('getCollaboraUrlInternal') |
| 55 | + ->willReturn('http://cool.example'); |
| 56 | + $this->appConfig->method('getDisableCertificateValidation') |
| 57 | + ->willReturn(false); |
| 58 | + $response->method('getBody') |
| 59 | + ->willReturn('converted-content'); |
| 60 | + |
| 61 | + $this->client->expects($this->once()) |
| 62 | + ->method('post') |
| 63 | + ->with( |
| 64 | + 'http://cool.example/cool/convert-to/pdf', |
| 65 | + $this->callback(function (array $options) use ($stream): bool { |
| 66 | + $this->assertSame([ |
| 67 | + [ |
| 68 | + 'name' => 'document.xlsx', |
| 69 | + 'filename' => 'document.xlsx', |
| 70 | + 'contents' => $stream, |
| 71 | + ], |
| 72 | + [ |
| 73 | + 'name' => 'lang', |
| 74 | + 'contents' => 'de-DE', |
| 75 | + ], |
| 76 | + ], $options['multipart']); |
| 77 | + |
| 78 | + return true; |
| 79 | + }) |
| 80 | + ) |
| 81 | + ->willReturn($response); |
| 82 | + |
| 83 | + $result = $this->service->convertTo('document.xlsx', $stream, 'pdf', ['lang' => 'de-DE']); |
| 84 | + |
| 85 | + $this->assertSame('converted-content', $result); |
| 86 | + } |
| 87 | +} |
0 commit comments