Skip to content

Commit

Permalink
Merge pull request #3253 from nextcloud/test/fix-types
Browse files Browse the repository at this point in the history
test: Fix all types
  • Loading branch information
come-nc authored Sep 20, 2024
2 parents 1b0d3b1 + bfe1c4a commit 64357f8
Show file tree
Hide file tree
Showing 15 changed files with 963 additions and 125 deletions.
72 changes: 39 additions & 33 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,9 @@
<file name="tests/stubs/doctrine_dbal_schema_column.php" />
<file name="tests/stubs/doctrine_dbal_schema_schema.php" />
<file name="tests/stubs/doctrine_dbal_schema_table.php" />
<file name="tests/stubs/stecman_component_symfony_console_bashcompletion_completion_completionawareinterface.php" />
<file name="tests/stubs/symfony_component_console_command_command.php" />
<file name="tests/stubs/symfony_component_console_helper_table.php" />
<file name="tests/stubs/symfony_component_console_input_inputargument.php" />
<file name="tests/stubs/symfony_component_console_input_inputinterface.php" />
<file name="tests/stubs/symfony_component_console_input_inputoption.php" />
<file name="tests/stubs/symfony_component_console_output_outputinterface.php" />
<file name="tests/stubs/symfony_component_console_question_confirmationquestion.php" />
<file name="tests/stubs/symfony_component_console_question_confirmationquestion.php" />
<file name="tests/stubs/symfony_component_console_question_question.php" />
<file name="tests/stubs/oca_circles_circlesmanager.php" />
<file name="tests/stubs/oca_circles_circlesqueryhelper.php" />
<file name="tests/stubs/oca_circles_events_circledestroyedevent.php" />
<file name="tests/stubs/oca_circles_events_circleresultgenericevent.php" />
<file name="tests/stubs/oca_circles_exceptions_circlenotfoundexception.php" />
<file name="tests/stubs/oca_circles_exceptions_federateditemexception.php" />
<file name="tests/stubs/oca_circles_exceptions_federateditemnotfoundexception.php" />
<file name="tests/stubs/oca_circles_ientity.php" />
<file name="tests/stubs/oca_circles_ifederateduser.php" />
<file name="tests/stubs/oca_circles_iqueryprobe.php" />
<file name="tests/stubs/oca_circles_model_circle.php" />
<file name="tests/stubs/oca_circles_model_federateduser.php" />
<file name="tests/stubs/oca_circles_model_managedmodel.php" />
<file name="tests/stubs/oca_circles_model_probes_basicprobe.php" />
<file name="tests/stubs/oca_circles_model_probes_circleprobe.php" />
<file name="tests/stubs/oca_circles_model_probes_memberprobe.php" />
<file name="tests/stubs/oca_circles_tools_db_iqueryrow.php" />
<file name="tests/stubs/oca_circles_tools_ideserializable.php" />
<file name="tests/stubs/icewind_streams_directory.php" />
<file name="tests/stubs/icewind_streams_iteratordirectory.php" />
<file name="tests/stubs/icewind_streams_wrapperhandler.php" />
<file name="tests/stubs/icewind_streams_directory.php" />
<file name="tests/stubs/oc.php" />
<file name="tests/stubs/oc_appframework_ocs_baseresponse.php" />
<file name="tests/stubs/oc_appframework_ocs_v1response.php" />
Expand All @@ -65,13 +37,17 @@
<file name="tests/stubs/oc_files_node_node.php" />
<file name="tests/stubs/oc_files_objectstore_objectstorescanner.php" />
<file name="tests/stubs/oc_files_objectstore_objectstorestorage.php" />
<file name="tests/stubs/oc_files_setupmanager.php" />
<file name="tests/stubs/oc_files_storage_common.php" />
<file name="tests/stubs/oc_files_storage_local.php" />
<file name="tests/stubs/oc_files_storage_storage.php" />
<file name="tests/stubs/oc_files_storage_temporary.php" />
<file name="tests/stubs/oc_files_storage_wrapper_jail.php" />
<file name="tests/stubs/oc_files_storage_wrapper_permissionsmask.php" />
<file name="tests/stubs/oc_files_storage_wrapper_quota.php" />
<file name="tests/stubs/oc_files_storage_wrapper_wrapper.php" />
<file name="tests/stubs/oc_files_view.php" />
<file name="tests/stubs/oc_group_database.php" />
<file name="tests/stubs/oc_group_manager.php" />
<file name="tests/stubs/oc_hooks_basicemitter.php" />
<file name="tests/stubs/oc_hooks_emitter.php" />
Expand All @@ -81,6 +57,24 @@
<file name="tests/stubs/oc_servercontainer.php" />
<file name="tests/stubs/oc_settings_authorizedgroupmapper.php" />
<file name="tests/stubs/oc_user_user.php" />
<file name="tests/stubs/oca_circles_circlesmanager.php" />
<file name="tests/stubs/oca_circles_circlesqueryhelper.php" />
<file name="tests/stubs/oca_circles_events_circledestroyedevent.php" />
<file name="tests/stubs/oca_circles_events_circleresultgenericevent.php" />
<file name="tests/stubs/oca_circles_exceptions_circlenotfoundexception.php" />
<file name="tests/stubs/oca_circles_exceptions_federateditemexception.php" />
<file name="tests/stubs/oca_circles_exceptions_federateditemnotfoundexception.php" />
<file name="tests/stubs/oca_circles_ientity.php" />
<file name="tests/stubs/oca_circles_ifederateduser.php" />
<file name="tests/stubs/oca_circles_iqueryprobe.php" />
<file name="tests/stubs/oca_circles_model_circle.php" />
<file name="tests/stubs/oca_circles_model_federateduser.php" />
<file name="tests/stubs/oca_circles_model_managedmodel.php" />
<file name="tests/stubs/oca_circles_model_probes_basicprobe.php" />
<file name="tests/stubs/oca_circles_model_probes_circleprobe.php" />
<file name="tests/stubs/oca_circles_model_probes_memberprobe.php" />
<file name="tests/stubs/oca_circles_tools_db_iqueryrow.php" />
<file name="tests/stubs/oca_circles_tools_ideserializable.php" />
<file name="tests/stubs/oca_dav_connector_sabre_directory.php" />
<file name="tests/stubs/oca_dav_connector_sabre_node.php" />
<file name="tests/stubs/oca_dav_connector_sabre_principal.php" />
Expand All @@ -100,19 +94,31 @@
<file name="tests/stubs/oca_files_versions_versions_iversionsimporterbackend.php" />
<file name="tests/stubs/oca_files_versions_versions_version.php" />
<file name="tests/stubs/oca_settings_service_authorizedgroupservice.php" />
<file name="tests/stubs/stecman_component_symfony_console_bashcompletion_completion_completionawareinterface.php" />
<file name="tests/stubs/symfony_component_console_command_command.php" />
<file name="tests/stubs/symfony_component_console_helper_table.php" />
<file name="tests/stubs/symfony_component_console_input_inputargument.php" />
<file name="tests/stubs/symfony_component_console_input_inputinterface.php" />
<file name="tests/stubs/symfony_component_console_input_inputoption.php" />
<file name="tests/stubs/symfony_component_console_output_outputinterface.php" />
<file name="tests/stubs/symfony_component_console_question_confirmationquestion.php" />
<file name="tests/stubs/symfony_component_console_question_confirmationquestion.php" />
<file name="tests/stubs/symfony_component_console_question_question.php" />
<file name="tests/stubs/test_testcase.php" />
<file name="tests/stubs/test_traits_usertrait.php" />
</stubs>
<projectFiles>
<directory name="lib" />
<directory name="tests" />
<ignoreFiles>
<directory name="vendor" />
<directory name="tests/stubs" />
<file name="tests/bootstrap.php" />
</ignoreFiles>
</projectFiles>
<extraFiles>
<directory name="vendor" />
<ignoreFiles>
<directory name="vendor-bin/phpunit/vendor/phpunit/php-code-coverage" />
<directory name="vendor-bin/psalm/vendor/vimeo/psalm" />
</ignoreFiles>
<directory name="vendor-bin/phpunit/vendor/phpunit/phpunit" />
</extraFiles>
<issueHandlers>
<LessSpecificReturnStatement errorLevel="error"/>
Expand Down
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
76 changes: 36 additions & 40 deletions tests/ACL/RuleManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,21 @@
use OCA\GroupFolders\ACL\UserMapping\IUserMappingManager;
use OCA\GroupFolders\ACL\UserMapping\UserMapping;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IDBConnection;
use OCP\IUser;
use OCP\Log\Audit\CriticalActionPerformedEvent;
use OCP\Server;
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,65 +40,62 @@ 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');
} else {
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);
$this->ruleManager = new RuleManager(Server::get(IDBConnection::class), $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)
->willReturn([$mapping]);

$parameters = null;
$this->eventDispatcher->expects($this->any())
->method('dispatchTyped')
->withConsecutive(
[$this->callback(fn (CriticalActionPerformedEvent $event): bool => $event->getParameters() === [
'permissions' => 0b00001001,
'mask' => 0b00001111,
'fileId' => 10,
'user' => 'The User (1)',
])],
[$this->callback(fn (CriticalActionPerformedEvent $event): bool => $event->getParameters() === [
'permissions' => 0b00001000,
'mask' => 0b00001111,
'fileId' => 10,
'user' => 'The User (1)',
])],
[$this->callback(fn (CriticalActionPerformedEvent $event): bool => $event->getParameters() === [
'fileId' => 10,
'user' => 'The User (1)',
])],
);
->willReturnCallback(function (CriticalActionPerformedEvent $event) use (&$parameters): bool {
$parameters = $event->getParameters();
return true;
});

$rule = new Rule($mapping, 10, 0b00001111, 0b00001001);
$this->ruleManager->saveRule($rule);
$this->assertEquals([
'permissions' => 0b00001001,
'mask' => 0b00001111,
'fileId' => 10,
'user' => '1 (1)',
], $parameters);

$result = $this->ruleManager->getRulesForFilesById($this->user, [10]);
$this->assertEquals([10 => [$rule]], $result);

$updatedRule = new Rule($mapping, 10, 0b00001111, 0b00001000);
$this->ruleManager->saveRule($updatedRule);
$this->assertEquals([
'permissions' => 0b00001000,
'mask' => 0b00001111,
'fileId' => 10,
'user' => '1 (1)',
], $parameters);

$result = $this->ruleManager->getRulesForFilesById($this->user, [10]);
$this->assertEquals([10 => [$updatedRule]], $result);

// cleanup
$this->ruleManager->deleteRule($rule);
$this->assertEquals([
'fileId' => 10,
'user' => '1 (1)',
], $parameters);
}

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 +130,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 +170,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 +207,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
Loading

0 comments on commit 64357f8

Please sign in to comment.