Skip to content

Commit

Permalink
test: Fix all types
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <kate@provokateurin.de>
  • Loading branch information
provokateurin committed Sep 19, 2024
1 parent 5f7ae0e commit f282302
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 57 deletions.
9 changes: 4 additions & 5 deletions tests/ACL/ACLCacheWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@
use OCA\GroupFolders\ACL\ACLManager;
use OCP\Constants;
use OCP\Files\Cache\ICache;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

/**
* @group DB
*/
class ACLCacheWrapperTest extends TestCase {
/** @var ACLManager|\PHPUnit_Framework_MockObject_MockObject */
private $aclManager;
/** @var ICache|\PHPUnit_Framework_MockObject_MockObject */
private $source;
private ?ACLCacheWrapper $cache = null;
private ACLManager&MockObject $aclManager;
private ICache&MockObject $source;
private ACLCacheWrapper $cache;
private array $aclPermissions = [];

protected function setUp(): void {
Expand Down
17 changes: 9 additions & 8 deletions tests/ACL/ACLManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountPoint;
use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;

class ACLManagerTest extends TestCase {
private RuleManager $ruleManager;
private TrashManager $trashManager;
private LoggerInterface $logger;
private IUser $user;
private RuleManager&MockObject $ruleManager;
private TrashManager&MockObject $trashManager;
private LoggerInterface&MockObject $logger;
private IUser&MockObject $user;
private ACLManager $aclManager;
private IUserMapping $dummyMapping;
/** @var Rule[] */
private IUserMapping&MockObject $dummyMapping;
/** @var array<string, list<Rule>> */
private array $rules = [];

protected function setUp(): void {
Expand All @@ -50,7 +51,7 @@ protected function setUp(): void {
});
}

private function createMapping(string $id): IUserMapping {
private function createMapping(string $id): IUserMapping&MockObject {
$mapping = $this->createMock(IUserMapping::class);
$mapping->method('getType')->willReturn('dummy');
$mapping->method('getId')->willReturn($id);
Expand All @@ -67,7 +68,7 @@ private function getAclManager(bool $perUserMerge = false): ACLManager {
$rootFolder->method('getMountPoint')
->willReturn($rootMountPoint);

return new ACLManager($this->ruleManager, $this->trashManager, $this->logger, $this->user, fn () => $rootFolder, null, $perUserMerge);
return new ACLManager($this->ruleManager, $this->trashManager, $this->logger, $this->user, fn (): IRootFolder&MockObject => $rootFolder, null, $perUserMerge);
}

public function testGetACLPermissionsForPathNoRules(): void {
Expand Down
5 changes: 3 additions & 2 deletions tests/ACL/ACLScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
use OCA\GroupFolders\ACL\ACLManager;
use OCA\GroupFolders\ACL\ACLStorageWrapper;
use OCP\Constants;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

/**
* @group DB
*/
class ACLScannerTest extends TestCase {
private function getAclManager(array $rules): ACLManager {
private function getAclManager(array $rules): ACLManager&MockObject {
$manager = $this->getMockBuilder(ACLManager::class)
->disableOriginalConstructor()
->getMock();
$manager->method('getACLPermissionsForPath')
->willReturnCallback(fn ($path) => $rules[$path] ?? Constants::PERMISSION_ALL);
->willReturnCallback(fn (string $path) => $rules[$path] ?? Constants::PERMISSION_ALL);

return $manager;
}
Expand Down
9 changes: 5 additions & 4 deletions tests/ACL/ACLStorageWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
use OCA\GroupFolders\ACL\ACLManager;
use OCA\GroupFolders\ACL\ACLStorageWrapper;
use OCP\Constants;
use OCP\Files\Storage\IStorage;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class ACLStorageWrapperTest extends TestCase {
/** @var ACLManager|\PHPUnit_Framework_MockObject_MockObject */
private $aclManager;
private ?Temporary $source = null;
private ?ACLStorageWrapper $storage = null;
private ACLManager&MockObject $aclManager;
private IStorage $source;
private ACLStorageWrapper $storage;
private array $aclPermissions = [];

protected function setUp(): void {
Expand Down
39 changes: 15 additions & 24 deletions tests/ACL/RuleManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,17 @@
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IUser;
use OCP\Log\Audit\CriticalActionPerformedEvent;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

/**
* @group DB
*/
class RuleManagerTest extends TestCase {
/** @var \PHPUnit_Framework_MockObject_MockObject | IUserMappingManager */
private $userMappingManager;
private ?RuleManager $ruleManager = null;
/** @var \PHPUnit_Framework_MockObject_MockObject | IUser */
private $user;

/** @var \PHPUnit_Framework_MockObject_MockObject | IEventDispatcher */
private $eventDispatcher;
private IUserMappingManager&MockObject $userMappingManager;
private RuleManager $ruleManager;
private IUser&MockObject $user;
private IEventDispatcher&MockObject $eventDispatcher;

protected function setUp(): void {
parent::setUp();
Expand All @@ -41,20 +38,14 @@ protected function setUp(): void {
$this->userMappingManager = $this->createMock(IUserMappingManager::class);
$this->userMappingManager->expects($this->any())
->method('mappingFromId')
->willReturnCallback(function ($type, $id): UserMapping {
if ($type === 'user') {
return new UserMapping($type, $id, 'The User');
}

return new UserMapping($type, $id);
});
->willReturnCallback(fn (string $type, string $id): UserMapping => new UserMapping($type, $id));

$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->ruleManager = new RuleManager(\OC::$server->getDatabaseConnection(), $this->userMappingManager, $this->eventDispatcher);
}

public function testGetSetRule(): void {
$mapping = new UserMapping('user', '1', 'The User');
$mapping = new UserMapping('user', '1');
$this->userMappingManager->expects($this->any())
->method('getMappingsForUser')
->with($this->user)
Expand All @@ -67,17 +58,17 @@ public function testGetSetRule(): void {
'permissions' => 0b00001001,
'mask' => 0b00001111,
'fileId' => 10,
'user' => 'The User (1)',
'user' => '1 (1)',
])],
[$this->callback(fn (CriticalActionPerformedEvent $event): bool => $event->getParameters() === [
'permissions' => 0b00001000,
'mask' => 0b00001111,
'fileId' => 10,
'user' => 'The User (1)',
'user' => '1 (1)',
])],
[$this->callback(fn (CriticalActionPerformedEvent $event): bool => $event->getParameters() === [
'fileId' => 10,
'user' => 'The User (1)',
'user' => '1 (1)',
])],
);

Expand All @@ -98,8 +89,8 @@ public function testGetSetRule(): void {
}

public function testGetMultiple(): void {
$mapping1 = new UserMapping('test', '1');
$mapping2 = new UserMapping('test', '2');
$mapping1 = new UserMapping('user', '1');
$mapping2 = new UserMapping('user', '2');
$this->userMappingManager->expects($this->any())
->method('getMappingsForUser')
->with($this->user)
Expand Down Expand Up @@ -134,7 +125,7 @@ public function testGetByPath(): void {
$id2 = (int)$cache->getId('foo/bar');
$storageId = $cache->getNumericStorageId();

$mapping = new UserMapping('test', '1');
$mapping = new UserMapping('user', '1');
$this->userMappingManager->expects($this->any())
->method('getMappingsForUser')
->with($this->user)
Expand Down Expand Up @@ -174,7 +165,7 @@ public function testGetByPathMore(): void {
$id1 = (int)$cache->getId('foo');
$storageId = $cache->getNumericStorageId();

$mapping = new UserMapping('test', '1');
$mapping = new UserMapping('user', '1');
$this->userMappingManager->expects($this->any())
->method('getMappingsForUser')
->with($this->user)
Expand Down Expand Up @@ -211,7 +202,7 @@ public function testGetByParent(): void {
$id3 = (int)$cache->getId('foo/asd');
$storageId = $cache->getNumericStorageId();

$mapping = new UserMapping('test', '1');
$mapping = new UserMapping('user', '1');
$this->userMappingManager->expects($this->any())
->method('getMappingsForUser')
->with($this->user)
Expand Down
8 changes: 4 additions & 4 deletions tests/ACL/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Test\TestCase;

class RuleTest extends TestCase {
public function permissionsProvider() {
public function permissionsProvider(): array {
return [
[0b00000000, 0b00000000, 0b00000000, 0b00000000],
[0b10101010, 0b00000000, 0b11110000, 0b10101010], //empty mask should have no effect
Expand All @@ -26,12 +26,12 @@ public function permissionsProvider() {
/**
* @dataProvider permissionsProvider
*/
public function testApplyPermissions($input, $mask, $permissions, $expected): void {
public function testApplyPermissions(int $input, int $mask, int $permissions, int $expected): void {
$rule = new Rule($this->createMock(IUserMapping::class), 0, $mask, $permissions);
$this->assertEquals($expected, $rule->applyPermissions($input));
}

public function mergeRulesProvider() {
public function mergeRulesProvider(): array {
return [
[[
[0b00001111, 0b00000011],
Expand All @@ -56,7 +56,7 @@ public function mergeRulesProvider() {
/**
* @dataProvider mergeRulesProvider
*/
public function testMergeRules($inputs, $expectedMask, $expectedPermissions): void {
public function testMergeRules(array $inputs, int $expectedMask, int $expectedPermissions): void {
$inputRules = array_map(fn (array $input): Rule => new Rule($this->createMock(IUserMapping::class), 0, $input[0], $input[1]), $inputs);

$result = Rule::mergeRules($inputRules);
Expand Down
17 changes: 7 additions & 10 deletions tests/Folder/FolderManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;

Expand All @@ -22,11 +23,11 @@
*/
class FolderManagerTest extends TestCase {
private FolderManager $manager;
private IGroupManager $groupManager;
private IMimeTypeLoader $mimeLoader;
private LoggerInterface $logger;
private IEventDispatcher $eventDispatcher;
private IConfig $config;
private IGroupManager&MockObject $groupManager;
private IMimeTypeLoader&MockObject $mimeLoader;
private LoggerInterface&MockObject $logger;
private IEventDispatcher&MockObject $eventDispatcher;
private IConfig&MockObject $config;

protected function setUp(): void {
parent::setUp();
Expand Down Expand Up @@ -291,9 +292,8 @@ public function testGetFoldersForGroups(): void {

/**
* @param string[] $groups
* @return \PHPUnit_Framework_MockObject_MockObject|IUser
*/
protected function getUser($groups = []) {
protected function getUser($groups = []): IUser&MockObject {
$id = uniqid();
$user = $this->createMock(IUser::class);
$this->groupManager->expects($this->any())
Expand All @@ -313,7 +313,6 @@ public function testGetFoldersForUserEmpty(): void {

public function testGetFoldersForUserSimple(): void {
$db = $this->createMock(IDBConnection::class);
/** @var FolderManager|\PHPUnit_Framework_MockObject_MockObject $manager */
$manager = $this->getMockBuilder(FolderManager::class)
->setConstructorArgs([$db, $this->groupManager, $this->mimeLoader, $this->logger, $this->eventDispatcher, $this->config])
->setMethods(['getFoldersForGroups'])
Expand All @@ -336,7 +335,6 @@ public function testGetFoldersForUserSimple(): void {

public function testGetFoldersForUserMerge(): void {
$db = $this->createMock(IDBConnection::class);
/** @var FolderManager|\PHPUnit_Framework_MockObject_MockObject $manager */
$manager = $this->getMockBuilder(FolderManager::class)
->setConstructorArgs([$db, $this->groupManager, $this->mimeLoader, $this->logger, $this->eventDispatcher, $this->config])
->setMethods(['getFoldersForGroups'])
Expand Down Expand Up @@ -372,7 +370,6 @@ public function testGetFoldersForUserMerge(): void {

public function testGetFolderPermissionsForUserMerge(): void {
$db = $this->createMock(IDBConnection::class);
/** @var FolderManager|\PHPUnit_Framework_MockObject_MockObject $manager */
$manager = $this->getMockBuilder(FolderManager::class)
->setConstructorArgs([$db, $this->groupManager, $this->mimeLoader, $this->logger, $this->eventDispatcher, $this->config])
->setMethods(['getFoldersForGroups'])
Expand Down

0 comments on commit f282302

Please sign in to comment.