|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OCA\Text\Tests; |
| 4 | + |
| 5 | +use OCA\Text\Db\Document; |
| 6 | +use OCA\Text\Service\ApiService; |
| 7 | +use OCA\Text\Service\ConfigService; |
| 8 | +use OCA\Text\Service\DocumentService; |
| 9 | +use OCA\Text\Service\EncodingService; |
| 10 | +use OCA\Text\Service\SessionService; |
| 11 | +use OCP\IL10N; |
| 12 | +use OCP\IRequest; |
| 13 | +use Psr\Log\LoggerInterface; |
| 14 | + |
| 15 | +class ApiServiceTest extends \PHPUnit\Framework\TestCase { |
| 16 | + private ApiService $apiService; |
| 17 | + |
| 18 | + private IRequest $request; |
| 19 | + private ConfigService $configService; |
| 20 | + private SessionService $sessionService; |
| 21 | + private DocumentService $documentService; |
| 22 | + private EncodingService $encodingService; |
| 23 | + private LoggerInterface $loggerInterface; |
| 24 | + private IL10N $l10n; |
| 25 | + private string $userId; |
| 26 | + |
| 27 | + public function setUp(): void { |
| 28 | + $this->request = $this->createMock(IRequest::class); |
| 29 | + $this->configService = $this->createMock(ConfigService::class); |
| 30 | + $this->sessionService = $this->createMock(SessionService::class); |
| 31 | + $this->documentService = $this->createMock(DocumentService::class); |
| 32 | + $this->encodingService = $this->createMock(EncodingService::class); |
| 33 | + $this->loggerInterface = $this->createMock(LoggerInterface::class); |
| 34 | + $this->l10n = $this->createMock(IL10N::class); |
| 35 | + $this->userId = 'admin'; |
| 36 | + |
| 37 | + $document = new Document(); |
| 38 | + $document->setId(123); |
| 39 | + $this->documentService->method('getDocument')->willReturn($document); |
| 40 | + $this->documentService->method('isReadOnly')->willReturn(false); |
| 41 | + |
| 42 | + $this->apiService = new ApiService( |
| 43 | + $this->request, |
| 44 | + $this->configService, |
| 45 | + $this->sessionService, |
| 46 | + $this->documentService, |
| 47 | + $this->encodingService, |
| 48 | + $this->loggerInterface, |
| 49 | + $this->l10n, |
| 50 | + $this->userId, |
| 51 | + null, |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + public function testCreateNewSession() { |
| 56 | + $file = $this->mockFile(1234, 'admin'); |
| 57 | + $this->documentService->method('getFileById')->willReturn($file); |
| 58 | + $actual = $this->apiService->create(1234); |
| 59 | + self::assertTrue($actual->getData()['hasOwner']); |
| 60 | + } |
| 61 | + |
| 62 | + public function testCreateNewSessionWithoutOwner() { |
| 63 | + $file = $this->mockFile(1234, null); |
| 64 | + $this->documentService->method('getFileById')->willReturn($file); |
| 65 | + $actual = $this->apiService->create(1234); |
| 66 | + self::assertFalse($actual->getData()['hasOwner']); |
| 67 | + } |
| 68 | + |
| 69 | + private function mockFile(int $id, ?string $owner) { |
| 70 | + $file = $this->createMock(\OCP\Files\File::class); |
| 71 | + $storage = $this->createMock(\OCP\Files\Storage\IStorage::class); |
| 72 | + $file->method('getStorage')->willReturn($storage); |
| 73 | + $file->method('getId')->willReturn($id); |
| 74 | + $file->method('getOwner')->willReturn($owner); |
| 75 | + return $file; |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments