Fix test mocking

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2020-12-30 12:54:19 +01:00
parent 7291c46d0d
commit 74814ad614
14 changed files with 32 additions and 55 deletions

View File

@@ -13,6 +13,7 @@ $config
->notPath('build') ->notPath('build')
->notPath('l10n') ->notPath('l10n')
->notPath('src') ->notPath('src')
->notPath('node_modules')
->notPath('vendor') ->notPath('vendor')
->in(__DIR__); ->in(__DIR__);
return $config; return $config;

View File

@@ -26,7 +26,6 @@ namespace OCA\Deck\AppInfo;
use Closure; use Closure;
use Exception; use Exception;
use OC\EventDispatcher\SymfonyAdapter; use OC\EventDispatcher\SymfonyAdapter;
use OC\Share20\ProviderFactory;
use OCA\Deck\Activity\CommentEventHandler; use OCA\Deck\Activity\CommentEventHandler;
use OCA\Deck\Capabilities; use OCA\Deck\Capabilities;
use OCA\Deck\Collaboration\Resources\ResourceProvider; use OCA\Deck\Collaboration\Resources\ResourceProvider;
@@ -66,7 +65,6 @@ use OCP\IUser;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\Notification\IManager as NotificationManager; use OCP\Notification\IManager as NotificationManager;
use OCP\Share\IManager; use OCP\Share\IManager;
use OCP\Share\IProviderFactory;
use OCP\Util; use OCP\Util;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;

View File

@@ -34,16 +34,11 @@ use OCA\Deck\Db\ChangeHelper;
use OCA\Deck\InvalidAttachmentType; use OCA\Deck\InvalidAttachmentType;
use OCA\Deck\NoPermissionException; use OCA\Deck\NoPermissionException;
use OCA\Deck\NotFoundException; use OCA\Deck\NotFoundException;
use OCA\Deck\Sharing\DeckShareProvider;
use OCA\Deck\StatusException; use OCA\Deck\StatusException;
use OCP\AppFramework\Http\Response; use OCP\AppFramework\Http\Response;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\ICache; use OCP\ICache;
use OCP\ICacheFactory; use OCP\ICacheFactory;
use OCP\IDBConnection;
use OCP\IL10N; use OCP\IL10N;
use OCP\IPreview;
use OCP\Share\IShare;
class AttachmentService { class AttachmentService {
private $attachmentMapper; private $attachmentMapper;
@@ -63,7 +58,7 @@ class AttachmentService {
/** @var ChangeHelper */ /** @var ChangeHelper */
private $changeHelper; private $changeHelper;
public function __construct(AttachmentMapper $attachmentMapper, CardMapper $cardMapper, ChangeHelper $changeHelper, PermissionService $permissionService, Application $application, ICacheFactory $cacheFactory, $userId, IL10N $l10n, ActivityManager $activityManager, DeckShareProvider $shareProvider) { public function __construct(AttachmentMapper $attachmentMapper, CardMapper $cardMapper, ChangeHelper $changeHelper, PermissionService $permissionService, Application $application, ICacheFactory $cacheFactory, $userId, IL10N $l10n, ActivityManager $activityManager) {
$this->attachmentMapper = $attachmentMapper; $this->attachmentMapper = $attachmentMapper;
$this->cardMapper = $cardMapper; $this->cardMapper = $cardMapper;
$this->permissionService = $permissionService; $this->permissionService = $permissionService;
@@ -156,7 +151,7 @@ class AttachmentService {
foreach (array_keys($this->services) as $attachmentType) { foreach (array_keys($this->services) as $attachmentType) {
$service = $this->getService($attachmentType); $service = $this->getService($attachmentType);
if ($service instanceof ICustomAttachmentService) { if ($service instanceof ICustomAttachmentService) {
$count += $service->getAttachmentCount($cardId); $count += $service->getAttachmentCount($cardId);
} }
} }
@@ -212,7 +207,6 @@ class AttachmentService {
} }
$service->extendData($attachment); $service->extendData($attachment);
} catch (InvalidAttachmentType $e) { } catch (InvalidAttachmentType $e) {
// just store the data // just store the data
} }
@@ -259,7 +253,6 @@ class AttachmentService {
} catch (\Exception $e) { } catch (\Exception $e) {
throw new NotFoundException(); throw new NotFoundException();
} }
} }
/** /**

View File

@@ -324,7 +324,7 @@ class BoardService {
'PERMISSION_MANAGE' => $permissions[Acl::PERMISSION_MANAGE] ?? false, 'PERMISSION_MANAGE' => $permissions[Acl::PERMISSION_MANAGE] ?? false,
'PERMISSION_SHARE' => $permissions[Acl::PERMISSION_SHARE] ?? false 'PERMISSION_SHARE' => $permissions[Acl::PERMISSION_SHARE] ?? false
]); ]);
$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $new_board, ActivityManager::SUBJECT_BOARD_CREATE); $this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $new_board, ActivityManager::SUBJECT_BOARD_CREATE, [], $userId);
$this->changeHelper->boardChanged($new_board->getId()); $this->changeHelper->boardChanged($new_board->getId());
$this->eventDispatcher->dispatch( $this->eventDispatcher->dispatch(

View File

@@ -133,7 +133,7 @@ class CardService {
return $this->cardMapper->searchRaw($boardIds, $term, $limit, $offset); return $this->cardMapper->searchRaw($boardIds, $term, $limit, $offset);
} }
/** /**
* @param $cardId * @param $cardId
* @return \OCA\Deck\Db\RelationalEntity * @return \OCA\Deck\Db\RelationalEntity
* @throws \OCA\Deck\NoPermissionException * @throws \OCA\Deck\NoPermissionException

View File

@@ -40,7 +40,6 @@ use OCP\IConfig;
use OCP\IL10N; use OCP\IL10N;
use OCP\ILogger; use OCP\ILogger;
use OCP\IRequest; use OCP\IRequest;
use OCP\Share\IManager;
class FileService implements IAttachmentService { class FileService implements IAttachmentService {
private $l10n; private $l10n;
@@ -58,8 +57,7 @@ class FileService implements IAttachmentService {
ILogger $logger, ILogger $logger,
IRootFolder $rootFolder, IRootFolder $rootFolder,
IConfig $config, IConfig $config,
AttachmentMapper $attachmentMapper, AttachmentMapper $attachmentMapper
IManager $shareManager
) { ) {
$this->l10n = $l10n; $this->l10n = $l10n;
$this->appData = $appData; $this->appData = $appData;
@@ -68,7 +66,6 @@ class FileService implements IAttachmentService {
$this->rootFolder = $rootFolder; $this->rootFolder = $rootFolder;
$this->config = $config; $this->config = $config;
$this->attachmentMapper = $attachmentMapper; $this->attachmentMapper = $attachmentMapper;
$this->shareManager = $shareManager;
} }
/** /**

View File

@@ -24,25 +24,17 @@
namespace OCA\Deck\Service; namespace OCA\Deck\Service;
use OCA\Deck\Db\Attachment; use OCA\Deck\Db\Attachment;
use OCA\Deck\Db\AttachmentMapper;
use OCA\Deck\Sharing\DeckShareProvider; use OCA\Deck\Sharing\DeckShareProvider;
use OCA\Deck\StatusException; use OCA\Deck\StatusException;
use OCA\Deck\Exceptions\ConflictException; use OCA\Deck\Exceptions\ConflictException;
use OCP\AppFramework\Http\ContentSecurityPolicy; use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\FileDisplayResponse; use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\StreamResponse; use OCP\AppFramework\Http\StreamResponse;
use OCP\Constants; use OCP\Constants;
use OCP\Files\IAppData;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IConfig;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\IL10N; use OCP\IL10N;
use OCP\ILogger;
use OCP\IPreview; use OCP\IPreview;
use OCP\IRequest; use OCP\IRequest;
use OCP\Share; use OCP\Share;
@@ -50,7 +42,6 @@ use OCP\Share\IManager;
use OCP\Share\IShare; use OCP\Share\IShare;
class FilesAppService implements IAttachmentService, ICustomAttachmentService { class FilesAppService implements IAttachmentService, ICustomAttachmentService {
private $request; private $request;
private $rootFolder; private $rootFolder;
private $shareProvider; private $shareProvider;
@@ -100,7 +91,7 @@ class FilesAppService implements IAttachmentService, ICustomAttachmentService {
}, $shares); }, $shares);
} }
function getAttachmentCount(int $cardId): int { public function getAttachmentCount(int $cardId): int {
/** @var IDBConnection $qb */ /** @var IDBConnection $qb */
$db = \OC::$server->getDatabaseConnection(); $db = \OC::$server->getDatabaseConnection();
$qb = $db->getQueryBuilder(); $qb = $db->getQueryBuilder();
@@ -247,7 +238,6 @@ class FilesAppService implements IAttachmentService, ICustomAttachmentService {
return; return;
} }
} }
} }
public function allowUndo() { public function allowUndo() {

View File

@@ -26,7 +26,6 @@ declare(strict_types=1);
namespace OCA\Deck\Service; namespace OCA\Deck\Service;
/** /**
* Interface to implement in case attachments are handled by a different backend than * Interface to implement in case attachments are handled by a different backend than
* then oc_deck_attachments table, e.g. for file sharing. When this interface is used * then oc_deck_attachments table, e.g. for file sharing. When this interface is used
@@ -34,9 +33,7 @@ namespace OCA\Deck\Service;
* table and it is up to the implementation to track attachment to card relation. * table and it is up to the implementation to track attachment to card relation.
*/ */
interface ICustomAttachmentService { interface ICustomAttachmentService {
public function listAttachments(int $cardId): array; public function listAttachments(int $cardId): array;
public function getAttachmentCount(int $cardId): int; public function getAttachmentCount(int $cardId): int;
} }

View File

@@ -26,7 +26,6 @@ declare(strict_types=1);
namespace OCA\Deck\Sharing; namespace OCA\Deck\Sharing;
use OC\Files\Cache\Cache; use OC\Files\Cache\Cache;
use OCA\Deck\Db\Acl; use OCA\Deck\Db\Acl;
use OCA\Deck\Db\Board; use OCA\Deck\Db\Board;
@@ -60,7 +59,6 @@ interface IShareProviderBackend {
} }
class DeckShareProvider implements \OCP\Share\IShareProvider { class DeckShareProvider implements \OCP\Share\IShareProvider {
public const DECK_FOLDER = '/Deck'; public const DECK_FOLDER = '/Deck';
public const DECK_FOLDER_PLACEHOLDER = '/{DECK_PLACEHOLDER}'; public const DECK_FOLDER_PLACEHOLDER = '/{DECK_PLACEHOLDER}';
@@ -117,7 +115,7 @@ class DeckShareProvider implements \OCP\Share\IShareProvider {
try { try {
$board = $this->boardMapper->find($boardId); $board = $this->boardMapper->find($boardId);
$valid &= !$board->getArchived(); $valid &= !$board->getArchived();
} catch (DoesNotExistException|MultipleObjectsReturnedException $e) { } catch (DoesNotExistException | MultipleObjectsReturnedException $e) {
$valid = false; $valid = false;
} }
@@ -901,7 +899,7 @@ class DeckShareProvider implements \OCP\Share\IShareProvider {
$types = [IShare::TYPE_DECK]; $types = [IShare::TYPE_DECK];
if ($currentAccess) { if ($currentAccess) {
$types[] = self::SHARE_TYPE_DECK_USER; $types[] = self::SHARE_TYPE_DECK_USER;
} }
$qb->select('id', 'parent', 'share_type', 'share_with', 'file_source', 'file_target', 'permissions') $qb->select('id', 'parent', 'share_type', 'share_with', 'file_source', 'file_target', 'permissions')

View File

@@ -26,7 +26,6 @@ declare(strict_types=1);
namespace OCA\Deck\Sharing; namespace OCA\Deck\Sharing;
use OC\Files\Filesystem; use OC\Files\Filesystem;
use OCA\Deck\Service\ConfigService; use OCA\Deck\Service\ConfigService;
use OCP\EventDispatcher\IEventDispatcher; use OCP\EventDispatcher\IEventDispatcher;

View File

@@ -26,7 +26,6 @@ declare(strict_types=1);
namespace OCA\Deck\Sharing; namespace OCA\Deck\Sharing;
use OCA\Deck\Db\Acl; use OCA\Deck\Db\Acl;
use OCA\Deck\Db\CardMapper; use OCA\Deck\Db\CardMapper;
use OCA\Deck\NoPermissionException; use OCA\Deck\NoPermissionException;
@@ -37,7 +36,6 @@ use OCP\IURLGenerator;
use OCP\Share\IShare; use OCP\Share\IShare;
class ShareAPIHelper { class ShareAPIHelper {
private $urlGenerator; private $urlGenerator;
private $timeFactory; private $timeFactory;
private $cardMapper; private $cardMapper;

View File

@@ -42,7 +42,7 @@ use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase; use Test\TestCase;
/** @internal Just for testing the service registration */ /** @internal Just for testing the service registration */
class MyAttachmentService { class MyAttachmentService implements IAttachmentService {
public function extendData(Attachment $attachment) { public function extendData(Attachment $attachment) {
} }
public function display(Attachment $attachment) { public function display(Attachment $attachment) {
@@ -84,6 +84,10 @@ class AttachmentServiceTest extends TestCase {
private $l10n; private $l10n;
/** @var ChangeHelper */ /** @var ChangeHelper */
private $changeHelper; private $changeHelper;
/**
* @var IAttachmentService|MockObject
*/
private $filesAppServiceImpl;
/** /**
* @throws \OCP\AppFramework\QueryException * @throws \OCP\AppFramework\QueryException
@@ -92,6 +96,8 @@ class AttachmentServiceTest extends TestCase {
parent::setUp(); parent::setUp();
$this->attachmentServiceImpl = $this->createMock(IAttachmentService::class); $this->attachmentServiceImpl = $this->createMock(IAttachmentService::class);
$this->filesAppServiceImpl = $this->createMock(IAttachmentService::class);
$this->appContainer = $this->createMock(IAppContainer::class); $this->appContainer = $this->createMock(IAppContainer::class);
$this->attachmentMapper = $this->createMock(AttachmentMapper::class); $this->attachmentMapper = $this->createMock(AttachmentMapper::class);
@@ -105,6 +111,8 @@ class AttachmentServiceTest extends TestCase {
$this->cacheFactory->expects($this->any())->method('createDistributed')->willReturn($this->cache); $this->cacheFactory->expects($this->any())->method('createDistributed')->willReturn($this->cache);
$this->appContainer->expects($this->at(0))->method('query')->with(FileService::class)->willReturn($this->attachmentServiceImpl); $this->appContainer->expects($this->at(0))->method('query')->with(FileService::class)->willReturn($this->attachmentServiceImpl);
$this->appContainer->expects($this->at(1))->method('query')->with(FilesAppService::class)->willReturn($this->filesAppServiceImpl);
$this->application->expects($this->any()) $this->application->expects($this->any())
->method('getContainer') ->method('getContainer')
->willReturn($this->appContainer); ->willReturn($this->appContainer);
@@ -119,8 +127,12 @@ class AttachmentServiceTest extends TestCase {
$application = $this->createMock(Application::class); $application = $this->createMock(Application::class);
$appContainer = $this->createMock(IAppContainer::class); $appContainer = $this->createMock(IAppContainer::class);
$fileServiceMock = $this->createMock(FileService::class); $fileServiceMock = $this->createMock(FileService::class);
$appContainer->expects($this->at(1))->method('query')->with(MyAttachmentService::class)->willReturn(new MyAttachmentService()); $fileAppServiceMock = $this->createMock(FilesAppService::class);
$appContainer->expects($this->at(0))->method('query')->with(FileService::class)->willReturn($fileServiceMock); $appContainer->expects($this->at(0))->method('query')->with(FileService::class)->willReturn($fileServiceMock);
$appContainer->expects($this->at(1))->method('query')->with(FilesAppService::class)->willReturn($fileAppServiceMock);
$appContainer->expects($this->at(2))->method('query')->with(MyAttachmentService::class)->willReturn(new MyAttachmentService());
$application->expects($this->any()) $application->expects($this->any())
->method('getContainer') ->method('getContainer')
->willReturn($appContainer); ->willReturn($appContainer);
@@ -135,8 +147,10 @@ class AttachmentServiceTest extends TestCase {
$application = $this->createMock(Application::class); $application = $this->createMock(Application::class);
$appContainer = $this->createMock(IAppContainer::class); $appContainer = $this->createMock(IAppContainer::class);
$fileServiceMock = $this->createMock(FileService::class); $fileServiceMock = $this->createMock(FileService::class);
$fileAppServiceMock = $this->createMock(FilesAppService::class);
$appContainer->expects($this->at(0))->method('query')->with(FileService::class)->willReturn($fileServiceMock); $appContainer->expects($this->at(0))->method('query')->with(FileService::class)->willReturn($fileServiceMock);
$appContainer->expects($this->at(1))->method('query')->with(MyAttachmentService::class)->willReturn(new MyAttachmentService()); $appContainer->expects($this->at(1))->method('query')->with(FilesAppService::class)->willReturn($fileAppServiceMock);
$appContainer->expects($this->at(2))->method('query')->with(MyAttachmentService::class)->willReturn(new MyAttachmentService());
$application->expects($this->any()) $application->expects($this->any())
->method('getContainer') ->method('getContainer')
->willReturn($appContainer); ->willReturn($appContainer);

View File

@@ -123,20 +123,9 @@ class BoardServiceTest extends TestCase {
$b3 = new Board(); $b3 = new Board();
$b3->setId(3); $b3->setId(3);
$this->boardMapper->expects($this->once()) $this->boardMapper->expects($this->once())
->method('findAllByUser') ->method('findAllForUser')
->with('admin') ->with('admin')
->willReturn([$b1, $b2]); ->willReturn([$b1, $b2, $b3]);
$this->stackMapper->expects($this->any())
->method('findAll')
->willReturn([]);
$this->boardMapper->expects($this->once())
->method('findAllByGroups')
->with('admin', ['a', 'b', 'c'])
->willReturn([$b2, $b3]);
$this->boardMapper->expects($this->once())
->method('findAllByCircles')
->with('admin')
->willReturn([]);
$user = $this->createMock(IUser::class); $user = $this->createMock(IUser::class);
$this->groupManager->method('getUserGroupIds') $this->groupManager->method('getUserGroupIds')
->willReturn(['a', 'b', 'c']); ->willReturn(['a', 'b', 'c']);

View File

@@ -26,6 +26,7 @@ namespace OCA\Deck\Controller;
use OCA\Deck\Service\ConfigService; use OCA\Deck\Service\ConfigService;
use OCA\Deck\Service\PermissionService; use OCA\Deck\Service\PermissionService;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IInitialStateService; use OCP\IInitialStateService;
use OCP\IL10N; use OCP\IL10N;
use OCP\IRequest; use OCP\IRequest;
@@ -37,6 +38,7 @@ class PageControllerTest extends \Test\TestCase {
private $permissionService; private $permissionService;
private $initialState; private $initialState;
private $configService; private $configService;
private $eventDispatcher;
public function setUp(): void { public function setUp(): void {
$this->l10n = $this->createMock(IL10N::class); $this->l10n = $this->createMock(IL10N::class);
@@ -44,9 +46,10 @@ class PageControllerTest extends \Test\TestCase {
$this->permissionService = $this->createMock(PermissionService::class); $this->permissionService = $this->createMock(PermissionService::class);
$this->configService = $this->createMock(ConfigService::class); $this->configService = $this->createMock(ConfigService::class);
$this->initialState = $this->createMock(IInitialStateService::class); $this->initialState = $this->createMock(IInitialStateService::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->controller = new PageController( $this->controller = new PageController(
'deck', $this->request, $this->permissionService, $this->initialState, $this->configService 'deck', $this->request, $this->permissionService, $this->initialState, $this->configService, $this->eventDispatcher
); );
} }