-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathApiServiceTest.php
More file actions
84 lines (73 loc) · 2.71 KB
/
ApiServiceTest.php
File metadata and controls
84 lines (73 loc) · 2.71 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
<?php
namespace OCA\Text\Tests;
use OCA\Text\Db\Document;
use OCA\Text\Service\ApiService;
use OCA\Text\Service\ConfigService;
use OCA\Text\Service\DocumentService;
use OCA\Text\Service\EncodingService;
use OCA\Text\Service\SessionService;
use OCP\IL10N;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
class ApiServiceTest extends \PHPUnit\Framework\TestCase {
private ApiService $apiService;
private IRequest $request;
private ConfigService $configService;
private SessionService $sessionService;
private DocumentService $documentService;
private EncodingService $encodingService;
private LoggerInterface $loggerInterface;
private IL10N $l10n;
private string $userId;
public function setUp(): void {
$this->request = $this->createMock(IRequest::class);
$this->configService = $this->createMock(ConfigService::class);
$this->sessionService = $this->createMock(SessionService::class);
$this->documentService = $this->createMock(DocumentService::class);
$this->encodingService = $this->createMock(EncodingService::class);
$this->loggerInterface = $this->createMock(LoggerInterface::class);
$this->l10n = $this->createMock(IL10N::class);
$this->userId = 'admin';
$document = new Document();
$document->setId(123);
$this->documentService->method('getOrCreateDocument')->willReturn($document);
$this->documentService->method('isReadOnly')->willReturn(false);
$this->encodingService->method('encodeToUtf8')->willReturnCallback(function ($str) {
return $str;
});
$this->apiService = new ApiService(
$this->request,
$this->configService,
$this->sessionService,
$this->documentService,
$this->encodingService,
$this->loggerInterface,
$this->l10n,
$this->userId,
null,
);
}
public function testCreateNewSession() {
$file = $this->mockFile(1234, 'admin');
$this->documentService->method('getFileById')->willReturn($file);
$actual = $this->apiService->create(1234);
self::assertTrue($actual->getData()['hasOwner']);
self::assertEquals('file content', $actual->getData()['content']);
}
public function testCreateNewSessionWithoutOwner() {
$file = $this->mockFile(1234, null);
$this->documentService->method('getFileById')->willReturn($file);
$actual = $this->apiService->create(1234);
self::assertFalse($actual->getData()['hasOwner']);
}
private function mockFile(int $id, ?string $owner) {
$file = $this->createMock(\OCP\Files\File::class);
$storage = $this->createMock(\OCP\Files\Storage\IStorage::class);
$file->method('getStorage')->willReturn($storage);
$file->method('getId')->willReturn($id);
$file->method('getOwner')->willReturn($owner);
$file->method('getName')->willReturn('name');
$file->method('getContent')->willReturn('file content');
return $file;
}
}