-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathOfficeTest.php
More file actions
103 lines (80 loc) · 3.44 KB
/
OfficeTest.php
File metadata and controls
103 lines (80 loc) · 3.44 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Tests\Richdocuments\Preview;
use OCA\Richdocuments\AppConfig;
use OCA\Richdocuments\Capabilities;
use OCA\Richdocuments\Preview\Office;
use OCA\Richdocuments\Service\RemoteService;
use OCP\Files\File;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
class OfficeTest extends TestCase {
private RemoteService&MockObject $remoteService;
private LoggerInterface&MockObject $logger;
private AppConfig&MockObject $appConfig;
private Capabilities&MockObject $capabilities;
private Office $provider;
protected function setUp(): void {
parent::setUp();
$this->remoteService = $this->createMock(RemoteService::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->appConfig = $this->createMock(AppConfig::class);
$this->capabilities = $this->createMock(Capabilities::class);
$this->capabilities->method('getCapabilities')->willReturn(['richdocuments' => []]);
$this->provider = new class($this->remoteService, $this->logger, $this->appConfig, $this->capabilities) extends Office {
#[\Override]
public function getMimeType(): string {
return '/application\\/test/';
}
};
}
public function testGetThumbnailSkipsConversionWhenFileIsTooLarge(): void {
$file = $this->createMock(File::class);
$file->expects($this->once())->method('getSize')->willReturn(101 * 1024 * 1024);
$this->appConfig->expects($this->once())
->method('getPreviewConversionMaxFileSize')
->willReturn(100 * 1024 * 1024);
$this->remoteService->expects($this->never())->method('convertFileTo');
$result = $this->provider->getThumbnail($file, 64, 64);
$this->assertNull($result);
}
public function testGetThumbnailReturnsNullForEmptyFile(): void {
$file = $this->createMock(File::class);
$file->expects($this->once())->method('getSize')->willReturn(0);
$this->appConfig->expects($this->never())->method('getPreviewConversionMaxFileSize');
$this->remoteService->expects($this->never())->method('convertFileTo');
$result = $this->provider->getThumbnail($file, 64, 64);
$this->assertNull($result);
}
public function testGetThumbnailAttemptsConversionWhenFileSizeIsExactlyAtLimit(): void {
$file = $this->createMock(File::class);
$file->expects($this->once())->method('getSize')->willReturn(100 * 1024 * 1024);
$this->appConfig->expects($this->once())
->method('getPreviewConversionMaxFileSize')
->willReturn(100 * 1024 * 1024);
// Conversion is attempted; throw to keep the test simple (image loading is not unit-tested here)
$this->remoteService->expects($this->once())
->method('convertFileTo')
->with($file, 'png')
->willThrowException(new \Exception('conversion failed'));
$result = $this->provider->getThumbnail($file, 64, 64);
$this->assertNull($result);
}
public function testGetThumbnailReturnsNullWhenConversionFails(): void {
$file = $this->createMock(File::class);
$file->expects($this->once())->method('getSize')->willReturn(1024);
$this->appConfig->expects($this->once())
->method('getPreviewConversionMaxFileSize')
->willReturn(100 * 1024 * 1024);
$this->remoteService->expects($this->once())
->method('convertFileTo')
->with($file, 'png')
->willThrowException(new \Exception('conversion failed'));
$result = $this->provider->getThumbnail($file, 64, 64);
$this->assertNull($result);
}
}