diff options
author | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-09-19 20:50:35 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-19 20:50:35 +0200 |
commit | 9813cbf6164a81e1054d3225d135261bca260daa (patch) | |
tree | e5eba7524fb525136a164d083459fa408147e63f | |
parent | 75f5cb76300ca754f141573e76e029efb9369e51 (diff) | |
parent | 349a9fc5f6f65ad4b0a8278af54757903bd1a170 (diff) | |
download | nextcloud-server-9813cbf6164a81e1054d3225d135261bca260daa.tar.gz nextcloud-server-9813cbf6164a81e1054d3225d135261bca260daa.zip |
Merge pull request #48217 from nextcloud/chore/prepare-oc_repair-unit10
chore: Cleanup and prepare `\OC\Repair\RepairMimeTypes` tests for PHPUnit 10
-rw-r--r-- | tests/lib/Repair/CleanTagsTest.php | 41 | ||||
-rw-r--r-- | tests/lib/Repair/ClearFrontendCachesTest.php | 14 | ||||
-rw-r--r-- | tests/lib/Repair/ClearGeneratedAvatarCacheTest.php | 16 | ||||
-rw-r--r-- | tests/lib/Repair/OldGroupMembershipSharesTest.php | 27 | ||||
-rw-r--r-- | tests/lib/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php (renamed from tests/Test/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php) | 89 | ||||
-rw-r--r-- | tests/lib/Repair/Owncloud/CleanPreviewsTest.php (renamed from tests/Test/Repair/Owncloud/CleanPreviewsTest.php) | 33 | ||||
-rw-r--r-- | tests/lib/Repair/Owncloud/UpdateLanguageCodesTest.php (renamed from tests/Test/Repair/Owncloud/UpdateLanguageCodesTest.php) | 45 | ||||
-rw-r--r-- | tests/lib/Repair/RepairCollationTest.php | 38 | ||||
-rw-r--r-- | tests/lib/Repair/RepairDavSharesTest.php | 38 | ||||
-rw-r--r-- | tests/lib/Repair/RepairInvalidSharesTest.php | 42 | ||||
-rw-r--r-- | tests/lib/Repair/RepairMimeTypesTest.php | 24 | ||||
-rw-r--r-- | tests/phpunit-autotest.xml | 1 |
12 files changed, 185 insertions, 223 deletions
diff --git a/tests/lib/Repair/CleanTagsTest.php b/tests/lib/Repair/CleanTagsTest.php index 13aca706a5a..adb14b16fc4 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 @@ use OCP\Migration\IOutput; * @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 @@ class CleanTagsTest extends \Test\TestCase { ->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 @@ class CleanTagsTest extends \Test\TestCase { 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 @@ class CleanTagsTest extends \Test\TestCase { $qb = $this->connection->getQueryBuilder(); $result = $qb->select($qb->func()->count('*')) ->from($tableName) - ->execute(); + ->executeQuery(); $this->assertEquals($expected, $result->fetchOne(), $message); } @@ -140,7 +135,7 @@ class CleanTagsTest extends \Test\TestCase { 'category' => $qb->createNamedParameter($category), 'type' => $qb->createNamedParameter($type), ]) - ->execute(); + ->executeStatement(); return $qb->getLastInsertId(); } @@ -159,7 +154,7 @@ class CleanTagsTest extends \Test\TestCase { 'categoryid' => $qb->createNamedParameter($category, IQueryBuilder::PARAM_INT), 'type' => $qb->createNamedParameter($type), ]) - ->execute(); + ->executeStatement(); } /** @@ -167,7 +162,7 @@ class CleanTagsTest extends \Test\TestCase { * @return int */ protected function getFileID() { - if ($this->createdFile) { + if ($this->createdFile !== null) { return $this->createdFile; } @@ -181,7 +176,7 @@ class CleanTagsTest extends \Test\TestCase { '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 @@ class CleanTagsTest extends \Test\TestCase { '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 e48db295d45..2e4681c6e5d 100644 --- a/tests/lib/Repair/ClearFrontendCachesTest.php +++ b/tests/lib/Repair/ClearFrontendCachesTest.php @@ -10,19 +10,15 @@ use OC\Template\JSCombiner; 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 c03b9082446..810fa1fe4e8 100644 --- a/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php +++ b/tests/lib/Repair/ClearGeneratedAvatarCacheTest.php @@ -10,27 +10,19 @@ use OC\Avatar\AvatarManager; 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 5d3590499e6..594a8bf0b66 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 @@ use OCP\Share\IShare; * @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 @@ class OldGroupMembershipSharesTest extends \Test\TestCase { protected function deleteAllShares() { $qb = $this->connection->getQueryBuilder(); - $qb->delete('share')->execute(); + $qb->delete('share')->executeStatement(); } public function testRun(): void { @@ -76,7 +73,7 @@ class OldGroupMembershipSharesTest extends \Test\TestCase { $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 @@ class OldGroupMembershipSharesTest extends \Test\TestCase { $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 @@ class OldGroupMembershipSharesTest extends \Test\TestCase { $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 index 64db9459390..3c5583c49da 100644 --- a/tests/Test/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php +++ b/tests/lib/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php @@ -13,27 +13,18 @@ use OCP\Files\IRootFolder; 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 @@ class CleanPreviewsBackgroundJobTest extends TestCase { $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 @@ class CleanPreviewsBackgroundJobTest extends TestCase { $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 @@ class CleanPreviewsBackgroundJobTest extends TestCase { ->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 @@ class CleanPreviewsBackgroundJobTest extends TestCase { ->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 @@ class CleanPreviewsBackgroundJobTest extends TestCase { $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 index 02f257a89b3..3a1936076a0 100644 --- a/tests/Test/Repair/Owncloud/CleanPreviewsTest.php +++ b/tests/lib/Repair/Owncloud/CleanPreviewsTest.php @@ -12,17 +12,14 @@ use OCP\IConfig; 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 @@ class CleanPreviewsTest extends TestCase { $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 @@ class CleanPreviewsTest extends TestCase { ); $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 index 5bfd2e1e6d0..a7907308d93 100644 --- a/tests/Test/Repair/Owncloud/UpdateLanguageCodesTest.php +++ b/tests/lib/Repair/Owncloud/UpdateLanguageCodesTest.php @@ -9,7 +9,9 @@ namespace Test\Repair\Owncloud; 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 @@ use Test\TestCase; * @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 @@ class UpdateLanguageCodesTest extends TestCase { 'appid' => 'core', 'configkey' => 'lang', 'configvalue' => $user['configvalue'], - ])->execute(); + ])->executeStatement(); } // check if test data is written to DB @@ -70,26 +70,30 @@ class UpdateLanguageCodesTest extends TestCase { ->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 @@ class UpdateLanguageCodesTest extends TestCase { ->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 @@ class UpdateLanguageCodesTest extends TestCase { ->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 2b5091ad927..3f007fa6310 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 @@ class TestCollationRepair extends Collation { * @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 @@ class RepairCollationTest extends TestCase { $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 3a97de4d25d..73e71d75055 100644 --- a/tests/lib/Repair/RepairDavSharesTest.php +++ b/tests/lib/Repair/RepairDavSharesTest.php @@ -16,23 +16,19 @@ use OCP\IConfig; 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 @@ class RepairDavSharesTest extends TestCase { ->method('execute') ->willReturn($shareResults); + $updateCalls = []; $updateMock = $this->createMock(IQueryBuilder::class); $updateMock->expects($this->any()) ->method('expr') @@ -153,13 +150,10 @@ class RepairDavSharesTest extends TestCase { ->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 @@ class RepairDavSharesTest extends TestCase { }); $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 d728e0c7ecd..dfcdce3801a 100644 --- a/tests/lib/Repair/RepairInvalidSharesTest.php +++ b/tests/lib/Repair/RepairInvalidSharesTest.php @@ -9,8 +9,8 @@ namespace Test\Repair; 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 @@ use Test\TestCase; * @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 @@ class RepairInvalidSharesTest extends TestCase { ->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 @@ class RepairInvalidSharesTest extends TestCase { protected function deleteAllShares() { $qb = $this->connection->getQueryBuilder(); - $qb->delete('share')->execute(); + $qb->delete('share')->executeStatement(); } /** @@ -80,30 +77,30 @@ class RepairInvalidSharesTest extends TestCase { $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 @@ class RepairInvalidSharesTest extends TestCase { $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 @@ class RepairInvalidSharesTest extends TestCase { '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 @@ class RepairInvalidSharesTest extends TestCase { ->select('*') ->from('share') ->orderBy('permissions', 'ASC') - ->execute() + ->executeQuery() ->fetchAll(); $this->assertCount(1, $results); @@ -191,11 +186,4 @@ class RepairInvalidSharesTest extends TestCase { $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 6bf59b9c102..4f044c054d3 100644 --- a/tests/lib/Repair/RepairMimeTypesTest.php +++ b/tests/lib/Repair/RepairMimeTypesTest.php @@ -14,7 +14,6 @@ use OCP\IAppConfig; 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 @@ use OCP\Migration\IRepairStep; * @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 @@ class RepairMimeTypesTest extends \Test\TestCase { 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 @@ class RepairMimeTypesTest extends \Test\TestCase { } 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 46de05abd7b..b3d615ca0dc 100644 --- a/tests/phpunit-autotest.xml +++ b/tests/phpunit-autotest.xml @@ -16,7 +16,6 @@ <testsuite name="Nextcloud Server"> <directory suffix=".php">lib/</directory> <directory suffix=".php">Core/</directory> - <directory suffix=".php">Test/</directory> <file>apps.php</file> </testsuite> <coverage> |