Skip to content

Commit 21fbe9a

Browse files
committed
feat(test): basic tests for ConversionApiController
Signed-off-by: Elizabeth Danzberger <lizzy7128@tutanota.de>
1 parent 403502c commit 21fbe9a

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

apps/testing/lib/Conversion/ConversionProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function getSupportedMimeTypes(): array {
2929

3030
public function convertFile(File $file, string $targetMimeType): mixed {
3131
$image = imagecreatefromstring($file->getContent());
32-
32+
3333
imagepalettetotruecolor($image);
3434

3535
ob_start();
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/**
4+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
namespace Core\Controller;
9+
10+
use OC\Core\Controller\ConversionApiController;
11+
use OCP\AppFramework\Http;
12+
use OCP\AppFramework\Http\DataResponse;
13+
use OCP\AppFramework\OCS\OCSException;
14+
use OCP\AppFramework\OCS\OCSNotFoundException;
15+
use OCP\Conversion\IConversionManager;
16+
use OCP\Files\File;
17+
use OCP\Files\Folder;
18+
use OCP\Files\IRootFolder;
19+
use OCP\IRequest;
20+
use PHPUnit\Framework\MockObject\MockObject;
21+
use Test\TestCase;
22+
23+
class ConversionApiControllerTest extends TestCase {
24+
/** @var ConversionApiController */
25+
private $conversionApiController;
26+
27+
/** @var IRequest|MockObject */
28+
private $request;
29+
30+
/** @var IConversionManager|MockObject */
31+
private $conversionManager;
32+
33+
/** @var IRootFolder|MockObject */
34+
private $rootFolder;
35+
36+
/** @var File|MockObject */
37+
private $file;
38+
39+
/** @var Folder|MockObject */
40+
private $userFolder;
41+
42+
/** @var string */
43+
private $user;
44+
45+
protected function setUp(): void {
46+
parent::setUp();
47+
48+
$this->request = $this->createMock(IRequest::class);
49+
$this->conversionManager = $this->createMock(IConversionManager::class);
50+
$this->file = $this->createMock(File::class);
51+
$this->user = 'userid';
52+
53+
$this->userFolder = $this->createMock(Folder::class);
54+
$this->rootFolder = $this->createMock(IRootFolder::class);
55+
$this->rootFolder->method('getUserFolder')->with($this->user)->willReturn($this->userFolder);
56+
57+
$this->conversionApiController = new ConversionApiController(
58+
'core',
59+
$this->request,
60+
$this->conversionManager,
61+
$this->rootFolder,
62+
$this->user,
63+
);
64+
}
65+
66+
public function testThrowsNotFoundException() {
67+
$this->expectException(OCSNotFoundException::class);
68+
$this->conversionApiController->convert(42, 'image/png');
69+
}
70+
71+
public function testThrowsOcsException() {
72+
$this->userFolder->method('getFirstNodeById')->with(42)->willReturn($this->file);
73+
$this->conversionManager->method('convert')->willThrowException(new \Exception());
74+
75+
$this->expectException(OCSException::class);
76+
$this->conversionApiController->convert(42, 'image/png');
77+
}
78+
79+
public function testConvert() {
80+
$this->userFolder->method('getFirstNodeById')->with(42)->willReturn($this->file);
81+
$this->conversionManager->method('convert')->with($this->file, 'image/png')->willReturn('files/test.png');
82+
83+
$actual = $this->conversionApiController->convert(42, 'image/png');
84+
$expected = new DataResponse([
85+
'path' => 'files/test.png',
86+
], Http::STATUS_CREATED);
87+
88+
$this->assertEquals($expected, $actual);
89+
}
90+
}

0 commit comments

Comments
 (0)