Unit tests for missing service methods

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2018-06-18 13:23:53 +02:00
parent 7955a19149
commit 63ad39dd2d
3 changed files with 119 additions and 1 deletions

View File

@@ -27,6 +27,7 @@ use OCA\Deck\Db\Attachment;
use OCA\Deck\Db\AttachmentMapper;
use OCA\Deck\Db\Board;
use OCA\Deck\Db\BoardMapper;
use OCA\Deck\InvalidAttachmentType;
use OCA\Deck\Service\AttachmentService;
use OCA\Deck\Service\IAttachmentService;
@@ -97,4 +98,24 @@ class DeleteCronTest extends \Test\TestCase {
->with($attachment);
$this->invokePrivate($this->deleteCron, 'run', [null]);
}
public function testDeleteCronInvalidAttachment() {
$boards = [];
$this->boardMapper->expects($this->once())
->method('findToDelete')
->willReturn($boards);
$attachment = new Attachment();
$attachment->setType('deck_file_invalid');
$this->attachmentMapper->expects($this->once())
->method('findToDelete')
->willReturn([$attachment]);
$this->attachmentService->expects($this->once())
->method('getService')
->will($this->throwException(new InvalidAttachmentType('deck_file_invalid')));
$this->attachmentMapper->expects($this->once())
->method('delete')
->with($attachment);
$this->invokePrivate($this->deleteCron, 'run', [null]);
}
}

View File

@@ -160,6 +160,36 @@ class AttachmentServiceTest extends TestCase {
$this->assertEquals($attachments, $this->attachmentService->findAll(123, false));
}
public function testFindAllWithDeleted() {
$this->mockPermission(Acl::PERMISSION_READ);
$attachments = [
$this->createAttachment('deck_file','file1'),
$this->createAttachment('deck_file','file2'),
$this->createAttachment('deck_file_invalid','file3'),
];
$attachmentsDeleted = [
$this->createAttachment('deck_file','file4'),
$this->createAttachment('deck_file','file5'),
$this->createAttachment('deck_file_invalid','file6'),
];
$this->attachmentMapper->expects($this->once())
->method('findAll')
->with(123)
->willReturn($attachments);
$this->attachmentMapper->expects($this->once())
->method('findToDelete')
->with(123, false)
->willReturn($attachmentsDeleted);
$this->attachmentServiceImpl->expects($this->at(0))
->method('extendData')
->with($attachments[0]);
$this->attachmentServiceImpl->expects($this->at(1))
->method('extendData')
->with($attachments[1]);
$this->assertEquals(array_merge($attachments, $attachmentsDeleted), $this->attachmentService->findAll(123, true));
}
public function testCount() {
$this->cache->expects($this->once())->method('get')->with('card-123')->willReturn(null);
$this->attachmentMapper->expects($this->once())->method('findAll')->willReturn([1,2,3,4]);

View File

@@ -44,6 +44,7 @@ use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\ICacheFactory;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
@@ -56,6 +57,8 @@ class FileServiceTest extends TestCase {
private $appData;
/** @var IRequest|MockObject */
private $request;
/** @var ILogger|MockObject */
private $logger;
/** @var FileService */
private $fileService;
@@ -64,7 +67,8 @@ class FileServiceTest extends TestCase {
$this->request = $this->createMock(IRequest::class);
$this->appData = $this->createMock(IAppData::class);
$this->l10n = $this->createMock(IL10N::class);
$this->fileService = new FileService($this->l10n, $this->appData, $this->request);
$this->logger = $this->createMock(ILogger::class);
$this->fileService = new FileService($this->l10n, $this->appData, $this->request, $this->logger);
}
public function mockGetFolder($cardId) {
@@ -75,6 +79,18 @@ class FileServiceTest extends TestCase {
->willReturn($folder);
return $folder;
}
public function mockGetFolderFailure($cardId) {
$folder = $this->createMock(ISimpleFolder::class);
$this->appData->expects($this->once())
->method('getFolder')
->with('file-card-' . $cardId)
->will($this->throwException(new \OCP\Files\NotFoundException()));
$this->appData->expects($this->once())
->method('newFolder')
->with('file-card-' . $cardId)
->willReturn($folder);
return $folder;
}
private function getAttachment() {
$attachment = new Attachment();
@@ -102,6 +118,39 @@ class FileServiceTest extends TestCase {
]);
}
public function testExtendDataNotFound() {
$attachment = $this->getAttachment();
$folder = $this->mockGetFolder(123);
$folder->expects($this->once())->method('getFile')->will($this->throwException(new \OCP\Files\NotFoundException()));
$this->assertEquals($attachment, $this->fileService->extendData($attachment));
}
public function testExtendDataNotPermitted() {
$attachment = $this->getAttachment();
$folder = $this->mockGetFolder(123);
$folder->expects($this->once())->method('getFile')->will($this->throwException(new \OCP\Files\NotPermittedException()));
$this->assertEquals($attachment, $this->fileService->extendData($attachment));
}
public function testExtendData() {
$attachment = $this->getAttachment();
$expected = $this->getAttachment();
$expected->setExtendedData([
'filesize' => 100,
'mimetype' => 'image/jpeg',
'info' => pathinfo(__FILE__)
]);
$file = $this->createMock(ISimpleFile::class);
$file->expects($this->once())->method('getSize')->willReturn(100);
$file->expects($this->once())->method('getMimeType')->willReturn('image/jpeg');
$file->expects($this->once())->method('getName')->willReturn(__FILE__);
$folder = $this->mockGetFolder(123);
$folder->expects($this->once())->method('getFile')->willReturn($file);
$this->assertEquals($expected, $this->fileService->extendData($attachment));
}
/**
* @expectedException \Exception
*/
@@ -144,6 +193,24 @@ class FileServiceTest extends TestCase {
$this->fileService->create($attachment);
}
public function testCreateNoFolder() {
$attachment = $this->getAttachment();
$this->mockGetUploadedFile();
$folder = $this->mockGetFolderFailure(123);
$folder->expects($this->once())
->method('fileExists')
->willReturn(false);
$file = $this->createMock(ISimpleFile::class);
$file->expects($this->once())
->method('putContent')
->with(file_get_contents(__FILE__, 'r'));
$folder->expects($this->once())
->method('newFile')
->willReturn($file);
$this->fileService->create($attachment);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage File already exists.