diff --git a/tests/lib/Repair/CleanTagsTest.php b/tests/lib/Repair/CleanTagsTest.php index 13aca706a5a4f..adb14b16fc462 100644 --- a/tests/lib/Repair/CleanTagsTest.php +++ b/tests/lib/Repair/CleanTagsTest.php @@ -8,8 +8,10 @@ namespace Test\Repair; use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; use OCP\IUserManager; use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; /** * Tests for the cleaning the tags tables @@ -19,25 +21,18 @@ * @see \OC\Repair\CleanTags */ class CleanTagsTest extends \Test\TestCase { - /** @var \OC\Repair\CleanTags */ - protected $repair; - /** @var \OCP\IDBConnection */ - protected $connection; + private ?int $createdFile = null; + private \OC\Repair\CleanTags $repair; + private IDBConnection $connection; - /** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $userManager; - - /** @var int */ - protected $createdFile; - - /** @var IOutput */ - private $outputMock; + private IUserManager&MockObject $userManager; + private IOutput&MockObject $outputMock; protected function setUp(): void { parent::setUp(); - $this->outputMock = $this->getMockBuilder('\OCP\Migration\IOutput') + $this->outputMock = $this->getMockBuilder(IOutput::class) ->disableOriginalConstructor() ->getMock(); @@ -45,7 +40,7 @@ protected function setUp(): void { ->disableOriginalConstructor() ->getMock(); - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = \OCP\Server::get(IDBConnection::class); $this->repair = new \OC\Repair\CleanTags($this->connection, $this->userManager); $this->cleanUpTables(); } @@ -59,14 +54,14 @@ protected function tearDown(): void { protected function cleanUpTables() { $qb = $this->connection->getQueryBuilder(); $qb->delete('vcategory') - ->execute(); + ->executeStatement(); $qb->delete('vcategory_to_object') - ->execute(); + ->executeStatement(); $qb->delete('filecache') ->runAcrossAllShards() - ->execute(); + ->executeStatement(); } public function testRun(): void { @@ -119,7 +114,7 @@ protected function assertEntryCount($tableName, $expected, $message = '') { $qb = $this->connection->getQueryBuilder(); $result = $qb->select($qb->func()->count('*')) ->from($tableName) - ->execute(); + ->executeQuery(); $this->assertEquals($expected, $result->fetchOne(), $message); } @@ -140,7 +135,7 @@ protected function addTagCategory($category, $type, $user = 'TestRepairCleanTags 'category' => $qb->createNamedParameter($category), 'type' => $qb->createNamedParameter($type), ]) - ->execute(); + ->executeStatement(); return $qb->getLastInsertId(); } @@ -159,7 +154,7 @@ protected function addTagEntry($objectId, $category, $type) { 'categoryid' => $qb->createNamedParameter($category, IQueryBuilder::PARAM_INT), 'type' => $qb->createNamedParameter($type), ]) - ->execute(); + ->executeStatement(); } /** @@ -167,7 +162,7 @@ protected function addTagEntry($objectId, $category, $type) { * @return int */ protected function getFileID() { - if ($this->createdFile) { + if ($this->createdFile !== null) { return $this->createdFile; } @@ -181,7 +176,7 @@ protected function getFileID() { 'path' => $qb->createNamedParameter($fileName), 'path_hash' => $qb->createNamedParameter(md5($fileName)), ]) - ->execute(); + ->executeStatement(); $fileName = $this->getUniqueID('TestRepairCleanTags', 12); $qb->insert('filecache') ->values([ @@ -189,7 +184,7 @@ protected function getFileID() { 'path' => $qb->createNamedParameter($fileName), 'path_hash' => $qb->createNamedParameter(md5($fileName)), ]) - ->execute(); + ->executeStatement(); $this->createdFile = $qb->getLastInsertId(); return $this->createdFile; diff --git a/tests/lib/Repair/ClearFrontendCachesTest.php b/tests/lib/Repair/ClearFrontendCachesTest.php index e48db295d45e7..2e4681c6e5d79 100644 --- a/tests/lib/Repair/ClearFrontendCachesTest.php +++ b/tests/lib/Repair/ClearFrontendCachesTest.php @@ -10,19 +10,15 @@ use OCP\ICache; use OCP\ICacheFactory; use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; class ClearFrontendCachesTest extends \Test\TestCase { - /** @var ICacheFactory */ - private $cacheFactory; - /** @var JSCombiner */ - private $jsCombiner; + private ICacheFactory&MockObject $cacheFactory; + private JSCombiner&MockObject $jsCombiner; + private IOutput&MockObject $outputMock; - /** @var \OC\Repair\ClearFrontendCaches */ - protected $repair; - - /** @var IOutput */ - private $outputMock; + protected \OC\Repair\ClearFrontendCaches $repair; protected function setUp(): void { parent::setUp(); diff --git a/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php b/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php index c03b90824460b..810fa1fe4e8fb 100644 --- a/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php +++ b/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php @@ -10,27 +10,19 @@ use OC\Repair\ClearGeneratedAvatarCache; use OCP\BackgroundJob\IJobList; use OCP\IConfig; -use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; class ClearGeneratedAvatarCacheTest extends \Test\TestCase { - /** @var AvatarManager */ - private $avatarManager; - /** @var IOutput */ - private $outputMock; - - /** @var IConfig */ - private $config; - - /** @var IJobList */ - private $jobList; + private AvatarManager&MockObject $avatarManager; + private IConfig&MockObject $config; + private IJobList&MockObject $jobList; protected ClearGeneratedAvatarCache $repair; protected function setUp(): void { parent::setUp(); - $this->outputMock = $this->createMock(IOutput::class); $this->avatarManager = $this->createMock(AvatarManager::class); $this->config = $this->createMock(IConfig::class); $this->jobList = $this->createMock(IJobList::class); diff --git a/tests/lib/Repair/OldGroupMembershipSharesTest.php b/tests/lib/Repair/OldGroupMembershipSharesTest.php index 5d3590499e685..594a8bf0b666c 100644 --- a/tests/lib/Repair/OldGroupMembershipSharesTest.php +++ b/tests/lib/Repair/OldGroupMembershipSharesTest.php @@ -8,8 +8,11 @@ namespace Test\Repair; use OC\Repair\OldGroupMembershipShares; +use OCP\IDBConnection; +use OCP\IGroupManager; use OCP\Migration\IOutput; use OCP\Share\IShare; +use PHPUnit\Framework\MockObject\MockObject; /** * Class OldGroupMembershipSharesTest @@ -19,23 +22,17 @@ * @package Test\Repair */ class OldGroupMembershipSharesTest extends \Test\TestCase { - /** @var OldGroupMembershipShares */ - protected $repair; - /** @var \OCP\IDBConnection */ - protected $connection; - - /** @var \OCP\IGroupManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $groupManager; + private IDBConnection $connection; + private IGroupManager&MockObject $groupManager; protected function setUp(): void { parent::setUp(); - /** \OCP\IGroupManager|\PHPUnit\Framework\MockObject\MockObject */ - $this->groupManager = $this->getMockBuilder('OCP\IGroupManager') + $this->groupManager = $this->getMockBuilder(IGroupManager::class) ->disableOriginalConstructor() ->getMock(); - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = \OCP\Server::get(IDBConnection::class); $this->deleteAllShares(); } @@ -48,7 +45,7 @@ protected function tearDown(): void { protected function deleteAllShares() { $qb = $this->connection->getQueryBuilder(); - $qb->delete('share')->execute(); + $qb->delete('share')->executeStatement(); } public function testRun(): void { @@ -76,7 +73,7 @@ public function testRun(): void { $result = $query->select('id') ->from('share') ->orderBy('id', 'ASC') - ->execute(); + ->executeQuery(); $rows = $result->fetchAll(); $this->assertEquals([['id' => $parent], ['id' => $group2], ['id' => $user1], ['id' => $member], ['id' => $notAMember]], $rows); $result->closeCursor(); @@ -92,7 +89,7 @@ public function testRun(): void { $result = $query->select('id') ->from('share') ->orderBy('id', 'ASC') - ->execute(); + ->executeQuery(); $rows = $result->fetchAll(); $this->assertEquals([['id' => $parent], ['id' => $group2], ['id' => $user1], ['id' => $member]], $rows); $result->closeCursor(); @@ -127,8 +124,8 @@ protected function createShare($shareType, $shareWith, $parent) { $qb = $this->connection->getQueryBuilder(); $qb->insert('share') ->values($shareValues) - ->execute(); + ->executeStatement(); - return $this->connection->lastInsertId('*PREFIX*share'); + return $qb->getLastInsertId(); } } diff --git a/tests/Test/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php b/tests/lib/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php similarity index 76% rename from tests/Test/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php rename to tests/lib/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php index 64db9459390b9..3c5583c49dac4 100644 --- a/tests/Test/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php +++ b/tests/lib/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php @@ -13,27 +13,18 @@ use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\IUserManager; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; class CleanPreviewsBackgroundJobTest extends TestCase { - /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */ - private $rootFolder; - /** @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $logger; - - /** @var IJobList|\PHPUnit_Framework_MockObject_MockObject */ - private $jobList; - - /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */ - private $timeFactory; - - /** @var CleanPreviewsBackgroundJob */ - private $job; - - /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ - private $userManager; + private IRootFolder&MockObject $rootFolder; + private LoggerInterface&MockObject $logger; + private IJobList&MockObject $jobList; + private ITimeFactory&MockObject $timeFactory; + private IUserManager&MockObject $userManager; + private CleanPreviewsBackgroundJob $job; public function setUp(): void { parent::setUp(); @@ -90,14 +81,18 @@ public function testCleanupPreviewsUnfinished(): void { $this->equalTo(['uid' => 'myuid']) ); + $loggerCalls = []; $this->logger->expects($this->exactly(2)) ->method('info') - ->withConsecutive( - [$this->equalTo('Started preview cleanup for myuid')], - [$this->equalTo('New preview cleanup scheduled for myuid')], - ); + ->willReturnCallback(function () use (&$loggerCalls) { + $loggerCalls[] = func_get_args(); + }); $this->job->run(['uid' => 'myuid']); + self::assertEquals([ + ['Started preview cleanup for myuid', []], + ['New preview cleanup scheduled for myuid', []], + ], $loggerCalls); } public function testCleanupPreviewsFinished(): void { @@ -129,17 +124,21 @@ public function testCleanupPreviewsFinished(): void { $this->jobList->expects($this->never()) ->method('add'); + $loggerCalls = []; $this->logger->expects($this->exactly(2)) ->method('info') - ->withConsecutive( - [$this->equalTo('Started preview cleanup for myuid')], - [$this->equalTo('Preview cleanup done for myuid')], - ); + ->willReturnCallback(function () use (&$loggerCalls) { + $loggerCalls[] = func_get_args(); + }); $thumbnailFolder->expects($this->once()) ->method('delete'); $this->job->run(['uid' => 'myuid']); + self::assertEquals([ + ['Started preview cleanup for myuid', []], + ['Preview cleanup done for myuid', []], + ], $loggerCalls); } @@ -148,14 +147,18 @@ public function testNoUserFolder(): void { ->with($this->equalTo('myuid')) ->willThrowException(new NotFoundException()); + $loggerCalls = []; $this->logger->expects($this->exactly(2)) ->method('info') - ->withConsecutive( - [$this->equalTo('Started preview cleanup for myuid')], - [$this->equalTo('Preview cleanup done for myuid')], - ); + ->willReturnCallback(function () use (&$loggerCalls) { + $loggerCalls[] = func_get_args(); + }); $this->job->run(['uid' => 'myuid']); + self::assertEquals([ + ['Started preview cleanup for myuid', []], + ['Preview cleanup done for myuid', []], + ], $loggerCalls); } public function testNoThumbnailFolder(): void { @@ -172,14 +175,18 @@ public function testNoThumbnailFolder(): void { ->with($this->equalTo('thumbnails')) ->willThrowException(new NotFoundException()); + $loggerCalls = []; $this->logger->expects($this->exactly(2)) ->method('info') - ->withConsecutive( - [$this->equalTo('Started preview cleanup for myuid')], - [$this->equalTo('Preview cleanup done for myuid')], - ); + ->willReturnCallback(function () use (&$loggerCalls) { + $loggerCalls[] = func_get_args(); + }); $this->job->run(['uid' => 'myuid']); + self::assertEquals([ + ['Started preview cleanup for myuid', []], + ['Preview cleanup done for myuid', []], + ], $loggerCalls); } public function testNotPermittedToDelete(): void { @@ -212,17 +219,21 @@ public function testNotPermittedToDelete(): void { $this->jobList->expects($this->never()) ->method('add'); - $this->logger->expects($this->exactly(2)) - ->method('info') - ->withConsecutive( - [$this->equalTo('Started preview cleanup for myuid')], - [$this->equalTo('Preview cleanup done for myuid')], - ); - $thumbnailFolder->expects($this->once()) ->method('delete') ->willThrowException(new NotPermittedException()); + $loggerCalls = []; + $this->logger->expects($this->exactly(2)) + ->method('info') + ->willReturnCallback(function () use (&$loggerCalls) { + $loggerCalls[] = func_get_args(); + }); + $this->job->run(['uid' => 'myuid']); + self::assertEquals([ + ['Started preview cleanup for myuid', []], + ['Preview cleanup done for myuid', []], + ], $loggerCalls); } } diff --git a/tests/Test/Repair/Owncloud/CleanPreviewsTest.php b/tests/lib/Repair/Owncloud/CleanPreviewsTest.php similarity index 80% rename from tests/Test/Repair/Owncloud/CleanPreviewsTest.php rename to tests/lib/Repair/Owncloud/CleanPreviewsTest.php index 02f257a89b302..3a1936076a0f9 100644 --- a/tests/Test/Repair/Owncloud/CleanPreviewsTest.php +++ b/tests/lib/Repair/Owncloud/CleanPreviewsTest.php @@ -12,17 +12,14 @@ use OCP\IUser; use OCP\IUserManager; use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class CleanPreviewsTest extends TestCase { - /** @var IJobList|\PHPUnit_Framework_MockObject_MockObject */ - private $jobList; - /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ - private $userManager; - - /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ - private $config; + private IJobList&MockObject $jobList; + private IUserManager&MockObject $userManager; + private IConfig&MockObject $config; /** @var CleanPreviews */ private $repair; @@ -55,23 +52,17 @@ public function testRun(): void { $this->userManager->expects($this->once()) ->method('callForSeenUsers') - ->will($this->returnCallback(function (\Closure $function) use ($user1, $user2) { + ->will($this->returnCallback(function (\Closure $function) use (&$user1, $user2) { $function($user1); $function($user2); })); + $jobListCalls = []; $this->jobList->expects($this->exactly(2)) ->method('add') - ->withConsecutive( - [ - $this->equalTo(CleanPreviewsBackgroundJob::class), - $this->equalTo(['uid' => 'user1']) - ], - [ - $this->equalTo(CleanPreviewsBackgroundJob::class), - $this->equalTo(['uid' => 'user2']) - ], - ); + ->willReturnCallback(function () use (&$jobListCalls) { + $jobListCalls[] = func_get_args(); + }); $this->config->expects($this->once()) ->method('getAppValue') @@ -89,10 +80,14 @@ public function testRun(): void { ); $this->repair->run($this->createMock(IOutput::class)); + $this->assertEqualsCanonicalizing([ + [CleanPreviewsBackgroundJob::class, ['uid' => 'user1']], + [CleanPreviewsBackgroundJob::class, ['uid' => 'user2']], + ], $jobListCalls); } - public function testRunAlreadyDoone(): void { + public function testRunAlreadyDone(): void { $this->userManager->expects($this->never()) ->method($this->anything()); diff --git a/tests/Test/Repair/Owncloud/UpdateLanguageCodesTest.php b/tests/lib/Repair/Owncloud/UpdateLanguageCodesTest.php similarity index 78% rename from tests/Test/Repair/Owncloud/UpdateLanguageCodesTest.php rename to tests/lib/Repair/Owncloud/UpdateLanguageCodesTest.php index 5bfd2e1e6d00c..a7907308d93e9 100644 --- a/tests/Test/Repair/Owncloud/UpdateLanguageCodesTest.php +++ b/tests/lib/Repair/Owncloud/UpdateLanguageCodesTest.php @@ -9,7 +9,9 @@ use OC\Repair\Owncloud\UpdateLanguageCodes; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IConfig; +use OCP\IDBConnection; use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; /** @@ -20,16 +22,14 @@ * @package Test\Repair */ class UpdateLanguageCodesTest extends TestCase { - /** @var \OCP\IDBConnection */ - protected $connection; - /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */ - private $config; + protected IDBConnection $connection; + private IConfig&MockObject $config; protected function setUp(): void { parent::setUp(); - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = \OCP\Server::get(IDBConnection::class); $this->config = $this->createMock(IConfig::class); } @@ -60,7 +60,7 @@ public function testRun(): void { 'appid' => 'core', 'configkey' => 'lang', 'configvalue' => $user['configvalue'], - ])->execute(); + ])->executeStatement(); } // check if test data is written to DB @@ -70,26 +70,30 @@ public function testRun(): void { ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core'))) ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang'))) ->orderBy('userid') - ->execute(); + ->executeQuery(); $rows = $result->fetchAll(); $result->closeCursor(); $this->assertSame($users, $rows, 'Asserts that the entries are the ones from the test data set'); - /** @var IOutput|\PHPUnit_Framework_MockObject_MockObject $outputMock */ + $expectedOutput = [ + ['Changed 1 setting(s) from "bg_BG" to "bg" in preferences table.'], + ['Changed 0 setting(s) from "cs_CZ" to "cs" in preferences table.'], + ['Changed 1 setting(s) from "fi_FI" to "fi" in preferences table.'], + ['Changed 0 setting(s) from "hu_HU" to "hu" in preferences table.'], + ['Changed 0 setting(s) from "nb_NO" to "nb" in preferences table.'], + ['Changed 0 setting(s) from "sk_SK" to "sk" in preferences table.'], + ['Changed 2 setting(s) from "th_TH" to "th" in preferences table.'], + ]; + $outputMessages = []; + /** @var IOutput&MockObject $outputMock */ $outputMock = $this->createMock(IOutput::class); $outputMock->expects($this->exactly(7)) ->method('info') - ->withConsecutive( - ['Changed 1 setting(s) from "bg_BG" to "bg" in preferences table.'], - ['Changed 0 setting(s) from "cs_CZ" to "cs" in preferences table.'], - ['Changed 1 setting(s) from "fi_FI" to "fi" in preferences table.'], - ['Changed 0 setting(s) from "hu_HU" to "hu" in preferences table.'], - ['Changed 0 setting(s) from "nb_NO" to "nb" in preferences table.'], - ['Changed 0 setting(s) from "sk_SK" to "sk" in preferences table.'], - ['Changed 2 setting(s) from "th_TH" to "th" in preferences table.'], - ); + ->willReturnCallback(function () use (&$outputMessages) { + $outputMessages[] = func_get_args(); + }); $this->config->expects($this->once()) ->method('getSystemValueString') @@ -107,7 +111,7 @@ public function testRun(): void { ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core'))) ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang'))) ->orderBy('userid') - ->execute(); + ->executeQuery(); $rows = $result->fetchAll(); $result->closeCursor(); @@ -127,12 +131,13 @@ public function testRun(): void { ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter('core'))) ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang'))) ->andWhere($qb->expr()->eq('configvalue', $qb->createNamedParameter($user['configvalue']), IQueryBuilder::PARAM_STR)) - ->execute(); + ->executeStatement(); } + self::assertEquals($expectedOutput, $outputMessages); } public function testSecondRun(): void { - /** @var IOutput|\PHPUnit_Framework_MockObject_MockObject $outputMock */ + /** @var IOutput&MockObject $outputMock */ $outputMock = $this->createMock(IOutput::class); $outputMock->expects($this->never()) ->method('info'); diff --git a/tests/lib/Repair/RepairCollationTest.php b/tests/lib/Repair/RepairCollationTest.php index 2b5091ad927a6..3f007fa6310b4 100644 --- a/tests/lib/Repair/RepairCollationTest.php +++ b/tests/lib/Repair/RepairCollationTest.php @@ -7,9 +7,12 @@ namespace Test\Repair; +use OC\DB\ConnectionAdapter; use OC\Repair\Collation; +use OCP\IConfig; use OCP\IDBConnection; use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; @@ -31,39 +34,25 @@ public function getAllNonUTF8BinTables(IDBConnection $connection) { * @see \OC\Repair\RepairMimeTypes */ class RepairCollationTest extends TestCase { - /** - * @var TestCollationRepair - */ - private $repair; - /** - * @var IDBConnection - */ - private $connection; + private TestCollationRepair $repair; + private ConnectionAdapter $connection; + private string $tableName; + private IConfig $config; - /** - * @var string - */ - private $tableName; - - /** - * @var \OCP\IConfig - */ - private $config; - - /** @var LoggerInterface */ - private $logger; + private LoggerInterface&MockObject $logger; protected function setUp(): void { parent::setUp(); - $this->connection = \OC::$server->get(IDBConnection::class); - $this->logger = $this->createMock(LoggerInterface::class); - $this->config = \OC::$server->getConfig(); + $this->connection = \OCP\Server::get(ConnectionAdapter::class); + $this->config = \OCP\Server::get(IConfig::class); if ($this->connection->getDatabaseProvider() !== IDBConnection::PLATFORM_MYSQL) { $this->markTestSkipped('Test only relevant on MySql'); } + $this->logger = $this->createMock(LoggerInterface::class); + $dbPrefix = $this->config->getSystemValueString('dbtableprefix'); $this->tableName = $this->getUniqueID($dbPrefix . '_collation_test'); $this->connection->prepare("CREATE TABLE $this->tableName(text VARCHAR(16)) COLLATE utf8_unicode_ci")->execute(); @@ -80,8 +69,7 @@ public function testCollationConvert(): void { $tables = $this->repair->getAllNonUTF8BinTables($this->connection); $this->assertGreaterThanOrEqual(1, count($tables)); - /** @var IOutput | \PHPUnit\Framework\MockObject\MockObject $outputMock */ - $outputMock = $this->getMockBuilder('\OCP\Migration\IOutput') + $outputMock = $this->getMockBuilder(IOutput::class) ->disableOriginalConstructor() ->getMock(); diff --git a/tests/lib/Repair/RepairDavSharesTest.php b/tests/lib/Repair/RepairDavSharesTest.php index 3a97de4d25d6d..73e71d750553e 100644 --- a/tests/lib/Repair/RepairDavSharesTest.php +++ b/tests/lib/Repair/RepairDavSharesTest.php @@ -16,23 +16,19 @@ use OCP\IDBConnection; use OCP\IGroupManager; use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; use function in_array; class RepairDavSharesTest extends TestCase { - /** @var IOutput|\PHPUnit\Framework\MockObject\MockObject */ - protected $output; - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ - protected $config; - /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */ - protected $dbc; - /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $groupManager; - /** @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface */ - protected $logger; - /** @var RepairDavSharesTest */ - protected $repair; + + private IOutput&MockObject $output; + private IConfig&MockObject $config; + private IDBConnection&MockObject $dbc; + private LoggerInterface&MockObject $logger; + private IGroupManager&MockObject $groupManager; + private RepairDavShares $repair; public function setUp(): void { parent::setUp(); @@ -138,6 +134,7 @@ public function testRun(): void { ->method('execute') ->willReturn($shareResults); + $updateCalls = []; $updateMock = $this->createMock(IQueryBuilder::class); $updateMock->expects($this->any()) ->method('expr') @@ -153,13 +150,10 @@ public function testRun(): void { ->willReturnSelf(); $updateMock->expects($this->exactly(4)) ->method('setParameter') - ->withConsecutive( - ['updatedPrincipalUri', 'principals/groups/' . urlencode('family friends')], - ['shareId', 7], - ['updatedPrincipalUri', 'principals/groups/' . urlencode('Wants Repair')], - ['shareId', 1], - ) - ->willReturnSelf(); + ->willReturnCallback(function () use (&$updateCalls, &$updateMock) { + $updateCalls[] = func_get_args(); + return $updateMock; + }); $updateMock->expects($this->exactly(2)) ->method('execute'); @@ -174,5 +168,11 @@ public function testRun(): void { }); $this->repair->run($this->output); + self::assertEquals([ + ['updatedPrincipalUri', 'principals/groups/' . urlencode('family friends'), null], + ['shareId', 7, null], + ['updatedPrincipalUri', 'principals/groups/' . urlencode('Wants Repair'), null], + ['shareId', 1, null] + ], $updateCalls); } } diff --git a/tests/lib/Repair/RepairInvalidSharesTest.php b/tests/lib/Repair/RepairInvalidSharesTest.php index d728e0c7ecd62..dfcdce3801a9b 100644 --- a/tests/lib/Repair/RepairInvalidSharesTest.php +++ b/tests/lib/Repair/RepairInvalidSharesTest.php @@ -9,8 +9,8 @@ use OC\Repair\RepairInvalidShares; use OCP\IConfig; +use OCP\IDBConnection; use OCP\Migration\IOutput; -use OCP\Migration\IRepairStep; use OCP\Share\IShare; use Test\TestCase; @@ -22,11 +22,9 @@ * @see \OC\Repair\RepairInvalidShares */ class RepairInvalidSharesTest extends TestCase { - /** @var IRepairStep */ - private $repair; - /** @var \OCP\IDBConnection */ - private $connection; + private RepairInvalidShares $repair; + private IDBConnection $connection; protected function setUp(): void { parent::setUp(); @@ -39,10 +37,9 @@ protected function setUp(): void { ->with('version') ->willReturn('12.0.0.0'); - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = \OCP\Server::get(IDBConnection::class); $this->deleteAllShares(); - /** @var \OCP\IConfig $config */ $this->repair = new RepairInvalidShares($config, $this->connection); } @@ -54,7 +51,7 @@ protected function tearDown(): void { protected function deleteAllShares() { $qb = $this->connection->getQueryBuilder(); - $qb->delete('share')->execute(); + $qb->delete('share')->executeStatement(); } /** @@ -80,30 +77,30 @@ public function testSharesNonExistingParent(): void { $qb = $this->connection->getQueryBuilder(); $qb->insert('share') ->values($shareValues) - ->execute(); - $parent = $this->getLastShareId(); + ->executeStatement(); + $parent = $qb->getLastInsertId(); // share with existing parent $qb = $this->connection->getQueryBuilder(); $qb->insert('share') ->values(array_merge($shareValues, [ 'parent' => $qb->expr()->literal($parent), - ]))->execute(); - $validChild = $this->getLastShareId(); + ]))->executeStatement(); + $validChild = $qb->getLastInsertId(); // share with non-existing parent $qb = $this->connection->getQueryBuilder(); $qb->insert('share') ->values(array_merge($shareValues, [ 'parent' => $qb->expr()->literal($parent + 100), - ]))->execute(); - $invalidChild = $this->getLastShareId(); + ]))->executeStatement(); + $invalidChild = $qb->getLastInsertId(); $query = $this->connection->getQueryBuilder(); $result = $query->select('id') ->from('share') ->orderBy('id', 'ASC') - ->execute(); + ->executeQuery(); $rows = $result->fetchAll(); $this->assertEquals([['id' => $parent], ['id' => $validChild], ['id' => $invalidChild]], $rows); $result->closeCursor(); @@ -119,7 +116,7 @@ public function testSharesNonExistingParent(): void { $result = $query->select('id') ->from('share') ->orderBy('id', 'ASC') - ->execute(); + ->executeQuery(); $rows = $result->fetchAll(); $this->assertEquals([['id' => $parent], ['id' => $validChild]], $rows); $result->closeCursor(); @@ -167,9 +164,7 @@ public function testFileSharePermissions($itemType, $testPerms, $expectedPerms): 'permissions' => $qb->expr()->literal($testPerms), 'stime' => $qb->expr()->literal(time()), ]) - ->execute(); - - $shareId = $this->getLastShareId(); + ->executeStatement(); /** @var IOutput | \PHPUnit\Framework\MockObject\MockObject $outputMock */ $outputMock = $this->getMockBuilder('\OCP\Migration\IOutput') @@ -182,7 +177,7 @@ public function testFileSharePermissions($itemType, $testPerms, $expectedPerms): ->select('*') ->from('share') ->orderBy('permissions', 'ASC') - ->execute() + ->executeQuery() ->fetchAll(); $this->assertCount(1, $results); @@ -191,11 +186,4 @@ public function testFileSharePermissions($itemType, $testPerms, $expectedPerms): $this->assertEquals($expectedPerms, $updatedShare['permissions']); } - - /** - * @return int - */ - protected function getLastShareId() { - return $this->connection->lastInsertId('*PREFIX*share'); - } } diff --git a/tests/lib/Repair/RepairMimeTypesTest.php b/tests/lib/Repair/RepairMimeTypesTest.php index 6bf59b9c10253..4f044c054d3c4 100644 --- a/tests/lib/Repair/RepairMimeTypesTest.php +++ b/tests/lib/Repair/RepairMimeTypesTest.php @@ -14,7 +14,6 @@ use OCP\IConfig; use OCP\IDBConnection; use OCP\Migration\IOutput; -use OCP\Migration\IRepairStep; /** * Tests for the converting of legacy storages to home storages. @@ -24,21 +23,18 @@ * @see \OC\Repair\RepairMimeTypes */ class RepairMimeTypesTest extends \Test\TestCase { - /** @var IRepairStep */ - private $repair; - /** @var Temporary */ - private $storage; - - /** @var IMimeTypeLoader */ - private $mimetypeLoader; + private RepairMimeTypes $repair; + private Temporary $storage; + private IMimeTypeLoader $mimetypeLoader; + private IDBConnection $db; protected function setUp(): void { parent::setUp(); - $this->mimetypeLoader = \OC::$server->getMimeTypeLoader(); + $this->mimetypeLoader = \OCP\Server::get(IMimeTypeLoader::class); + $this->db = \OCP\Server::get(IDBConnection::class); - /** @var IConfig | \PHPUnit\Framework\MockObject\MockObject $config */ $config = $this->getMockBuilder(IConfig::class) ->disableOriginalConstructor() ->getMock(); @@ -66,10 +62,10 @@ protected function setUp(): void { protected function tearDown(): void { $this->storage->getCache()->clear(); - $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $qb = $this->db->getQueryBuilder(); $qb->delete('storages') ->where($qb->expr()->eq('id', $qb->createNamedParameter($this->storage->getId()))); - $qb->execute(); + $qb->executeStatement(); $this->clearMimeTypes(); @@ -77,9 +73,9 @@ protected function tearDown(): void { } private function clearMimeTypes() { - $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $qb = $this->db->getQueryBuilder(); $qb->delete('mimetypes'); - $qb->execute(); + $qb->executeStatement(); $this->mimetypeLoader->reset(); } diff --git a/tests/phpunit-autotest.xml b/tests/phpunit-autotest.xml index 46de05abd7be4..b3d615ca0dcd0 100644 --- a/tests/phpunit-autotest.xml +++ b/tests/phpunit-autotest.xml @@ -16,7 +16,6 @@ lib/ Core/ - Test/ apps.php