diff options
Diffstat (limited to 'apps/dav/tests/unit/Migration')
5 files changed, 171 insertions, 232 deletions
diff --git a/apps/dav/tests/unit/Migration/CalDAVRemoveEmptyValueTest.php b/apps/dav/tests/unit/Migration/CalDAVRemoveEmptyValueTest.php index 478be5b4294..1852d2709c1 100644 --- a/apps/dav/tests/unit/Migration/CalDAVRemoveEmptyValueTest.php +++ b/apps/dav/tests/unit/Migration/CalDAVRemoveEmptyValueTest.php @@ -1,34 +1,19 @@ <?php + +declare(strict_types=1); /** - * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCA\DAV\Tests\Unit\DAV\Migration; +namespace OCA\DAV\Tests\unit\DAV\Migration; use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\Migration\CalDAVRemoveEmptyValue; -use OCP\ILogger; +use OCP\IDBConnection; use OCP\Migration\IOutput; +use OCP\Server; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; use Sabre\VObject\InvalidDataException; use Test\TestCase; @@ -39,18 +24,10 @@ use Test\TestCase; * @group DB */ class CalDAVRemoveEmptyValueTest extends TestCase { - - /** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */ - private $logger; - - /** @var CalDavBackend|\PHPUnit\Framework\MockObject\MockObject */ - private $backend; - - /** @var IOutput|\PHPUnit\Framework\MockObject\MockObject */ - private $output; - - /** @var string */ - private $invalid = 'BEGIN:VCALENDAR + private LoggerInterface&MockObject $logger; + private CalDavBackend&MockObject $backend; + private IOutput&MockObject $output; + private string $invalid = 'BEGIN:VCALENDAR VERSION:2.0 PRODID:-//Apple Inc.//Mac OS X 10.11.2//EN CALSCALE:GREGORIAN @@ -70,8 +47,7 @@ CREATED;VALUE=:20151214T091032Z END:VEVENT END:VCALENDAR'; - /** @var string */ - private $valid = 'BEGIN:VCALENDAR + private string $valid = 'BEGIN:VCALENDAR VERSION:2.0 PRODID:-//Apple Inc.//Mac OS X 10.11.2//EN CALSCALE:GREGORIAN @@ -94,20 +70,20 @@ END:VCALENDAR'; protected function setUp(): void { parent::setUp(); - $this->logger = $this->createMock(ILogger::class); + $this->logger = $this->createMock(LoggerInterface::class); $this->backend = $this->createMock(CalDavBackend::class); $this->output = $this->createMock(IOutput::class); } - public function testRunAllValid() { - /** @var CalDAVRemoveEmptyValue|\PHPUnit\Framework\MockObject\MockObject $step */ + public function testRunAllValid(): void { + /** @var CalDAVRemoveEmptyValue&MockObject $step */ $step = $this->getMockBuilder(CalDAVRemoveEmptyValue::class) ->setConstructorArgs([ - \OC::$server->getDatabaseConnection(), + Server::get(IDBConnection::class), $this->backend, $this->logger ]) - ->setMethods(['getInvalidObjects']) + ->onlyMethods(['getInvalidObjects']) ->getMock(); $step->expects($this->once()) @@ -123,15 +99,15 @@ END:VCALENDAR'; $step->run($this->output); } - public function testRunInvalid() { - /** @var CalDAVRemoveEmptyValue|\PHPUnit\Framework\MockObject\MockObject $step */ + public function testRunInvalid(): void { + /** @var CalDAVRemoveEmptyValue&MockObject $step */ $step = $this->getMockBuilder(CalDAVRemoveEmptyValue::class) ->setConstructorArgs([ - \OC::$server->getDatabaseConnection(), + Server::get(IDBConnection::class), $this->backend, $this->logger ]) - ->setMethods(['getInvalidObjects']) + ->onlyMethods(['getInvalidObjects']) ->getMock(); $step->expects($this->once()) @@ -166,15 +142,15 @@ END:VCALENDAR'; $step->run($this->output); } - public function testRunValid() { - /** @var CalDAVRemoveEmptyValue|\PHPUnit\Framework\MockObject\MockObject $step */ + public function testRunValid(): void { + /** @var CalDAVRemoveEmptyValue&MockObject $step */ $step = $this->getMockBuilder(CalDAVRemoveEmptyValue::class) ->setConstructorArgs([ - \OC::$server->getDatabaseConnection(), + Server::get(IDBConnection::class), $this->backend, $this->logger ]) - ->setMethods(['getInvalidObjects']) + ->onlyMethods(['getInvalidObjects']) ->getMock(); $step->expects($this->once()) @@ -208,15 +184,15 @@ END:VCALENDAR'; $step->run($this->output); } - public function testRunStillInvalid() { - /** @var CalDAVRemoveEmptyValue|\PHPUnit\Framework\MockObject\MockObject $step */ + public function testRunStillInvalid(): void { + /** @var CalDAVRemoveEmptyValue&MockObject $step */ $step = $this->getMockBuilder(CalDAVRemoveEmptyValue::class) ->setConstructorArgs([ - \OC::$server->getDatabaseConnection(), + Server::get(IDBConnection::class), $this->backend, $this->logger ]) - ->setMethods(['getInvalidObjects']) + ->onlyMethods(['getInvalidObjects']) ->getMock(); $step->expects($this->once()) diff --git a/apps/dav/tests/unit/Migration/CreateSystemAddressBookStepTest.php b/apps/dav/tests/unit/Migration/CreateSystemAddressBookStepTest.php new file mode 100644 index 00000000000..667d2e39d3a --- /dev/null +++ b/apps/dav/tests/unit/Migration/CreateSystemAddressBookStepTest.php @@ -0,0 +1,47 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCA\DAV\Tests\unit\Migration; + +use OCA\DAV\CardDAV\SyncService; +use OCA\DAV\Migration\CreateSystemAddressBookStep; +use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; + +class CreateSystemAddressBookStepTest extends TestCase { + + private SyncService&MockObject $syncService; + private CreateSystemAddressBookStep $step; + + protected function setUp(): void { + parent::setUp(); + + $this->syncService = $this->createMock(SyncService::class); + + $this->step = new CreateSystemAddressBookStep( + $this->syncService, + ); + } + + public function testGetName(): void { + $name = $this->step->getName(); + + self::assertEquals('Create system address book', $name); + } + + public function testRun(): void { + $output = $this->createMock(IOutput::class); + + $this->step->run($output); + + $this->addToAssertionCount(1); + } + +} diff --git a/apps/dav/tests/unit/Migration/RefreshWebcalJobRegistrarTest.php b/apps/dav/tests/unit/Migration/RefreshWebcalJobRegistrarTest.php index 427bf6da145..8e7bf366cbf 100644 --- a/apps/dav/tests/unit/Migration/RefreshWebcalJobRegistrarTest.php +++ b/apps/dav/tests/unit/Migration/RefreshWebcalJobRegistrarTest.php @@ -1,28 +1,9 @@ <?php + +declare(strict_types=1); /** - * @copyright Copyright (c) 2018, Georg Ehrke - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Georg Ehrke <oc.list@georgehrke.com> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\DAV\Tests\unit\DAV\Migration; @@ -33,18 +14,13 @@ use OCP\DB\IResult; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class RefreshWebcalJobRegistrarTest extends TestCase { - - /** @var IDBConnection | \PHPUnit\Framework\MockObject\MockObject */ - private $db; - - /** @var IJobList | \PHPUnit\Framework\MockObject\MockObject */ - private $jobList; - - /** @var RefreshWebcalJobRegistrar */ - private $migration; + private IDBConnection&MockObject $db; + private IJobList&MockObject $jobList; + private RefreshWebcalJobRegistrar $migration; protected function setUp(): void { parent::setUp(); @@ -55,11 +31,11 @@ class RefreshWebcalJobRegistrarTest extends TestCase { $this->migration = new RefreshWebcalJobRegistrar($this->db, $this->jobList); } - public function testGetName() { + public function testGetName(): void { $this->assertEquals($this->migration->getName(), 'Registering background jobs to update cache for webcal calendars'); } - public function testRun() { + public function testRun(): void { $output = $this->createMock(IOutput::class); $queryBuilder = $this->createMock(IQueryBuilder::class); @@ -69,77 +45,70 @@ class RefreshWebcalJobRegistrarTest extends TestCase { ->method('getQueryBuilder') ->willReturn($queryBuilder); - $queryBuilder->expects($this->at(0)) + $queryBuilder->expects($this->once()) ->method('select') ->with(['principaluri', 'uri']) ->willReturn($queryBuilder); - $queryBuilder->expects($this->at(1)) + $queryBuilder->expects($this->once()) ->method('from') ->with('calendarsubscriptions') ->willReturn($queryBuilder); - $queryBuilder->expects($this->at(2)) + $queryBuilder->expects($this->once()) ->method('execute') ->willReturn($statement); - $statement->expects($this->at(0)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->willReturn([ - 'principaluri' => 'foo1', - 'uri' => 'bar1', - ]); - $statement->expects($this->at(1)) + $statement->expects($this->exactly(4)) ->method('fetch') ->with(\PDO::FETCH_ASSOC) - ->willReturn([ - 'principaluri' => 'foo2', - 'uri' => 'bar2', - ]); - $statement->expects($this->at(2)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->willReturn([ - 'principaluri' => 'foo3', - 'uri' => 'bar3', + ->willReturnOnConsecutiveCalls( + [ + 'principaluri' => 'foo1', + 'uri' => 'bar1', + ], + [ + 'principaluri' => 'foo2', + 'uri' => 'bar2', + ], + [ + 'principaluri' => 'foo3', + 'uri' => 'bar3', + ], + null + ); + + $this->jobList->expects($this->exactly(3)) + ->method('has') + ->willReturnMap([ + [RefreshWebcalJob::class, [ + 'principaluri' => 'foo1', + 'uri' => 'bar1', + ], false], + [RefreshWebcalJob::class, [ + 'principaluri' => 'foo2', + 'uri' => 'bar2', + ], true ], + [RefreshWebcalJob::class, [ + 'principaluri' => 'foo3', + 'uri' => 'bar3', + ], false], ]); - $statement->expects($this->at(0)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->willReturn(null); - $this->jobList->expects($this->at(0)) - ->method('has') - ->with(RefreshWebcalJob::class, [ - 'principaluri' => 'foo1', - 'uri' => 'bar1', - ]) - ->willReturn(false); - $this->jobList->expects($this->at(1)) - ->method('add') - ->with(RefreshWebcalJob::class, [ + $calls = [ + [RefreshWebcalJob::class, [ 'principaluri' => 'foo1', 'uri' => 'bar1', - ]); - $this->jobList->expects($this->at(2)) - ->method('has') - ->with(RefreshWebcalJob::class, [ - 'principaluri' => 'foo2', - 'uri' => 'bar2', - ]) - ->willReturn(true); - $this->jobList->expects($this->at(3)) - ->method('has') - ->with(RefreshWebcalJob::class, [ + ]], + [RefreshWebcalJob::class, [ 'principaluri' => 'foo3', 'uri' => 'bar3', - ]) - ->willReturn(false); - $this->jobList->expects($this->at(4)) + ]] + ]; + $this->jobList->expects($this->exactly(2)) ->method('add') - ->with(RefreshWebcalJob::class, [ - 'principaluri' => 'foo3', - 'uri' => 'bar3', - ]); + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $output->expects($this->once()) ->method('info') diff --git a/apps/dav/tests/unit/Migration/RegenerateBirthdayCalendarsTest.php b/apps/dav/tests/unit/Migration/RegenerateBirthdayCalendarsTest.php index bb5210f18fb..6f681badb8b 100644 --- a/apps/dav/tests/unit/Migration/RegenerateBirthdayCalendarsTest.php +++ b/apps/dav/tests/unit/Migration/RegenerateBirthdayCalendarsTest.php @@ -1,28 +1,9 @@ <?php + +declare(strict_types=1); /** - * @copyright Copyright (c) 2018, Georg Ehrke - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author François Freitag <mail@franek.fr> - * @author Georg Ehrke <oc.list@georgehrke.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\DAV\Tests\unit\DAV\Migration; @@ -31,18 +12,13 @@ use OCA\DAV\Migration\RegenerateBirthdayCalendars; use OCP\BackgroundJob\IJobList; use OCP\IConfig; use OCP\Migration\IOutput; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class RegenerateBirthdayCalendarsTest extends TestCase { - - /** @var IJobList | \PHPUnit\Framework\MockObject\MockObject */ - private $jobList; - - /** @var IConfig | \PHPUnit\Framework\MockObject\MockObject */ - private $config; - - /** @var RegenerateBirthdayCalendars */ - private $migration; + private IJobList&MockObject $jobList; + private IConfig&MockObject $config; + private RegenerateBirthdayCalendars $migration; protected function setUp(): void { parent::setUp(); @@ -54,14 +30,14 @@ class RegenerateBirthdayCalendarsTest extends TestCase { $this->config); } - public function testGetName() { + public function testGetName(): void { $this->assertEquals( 'Regenerating birthday calendars to use new icons and fix old birthday events without year', $this->migration->getName() ); } - public function testRun() { + public function testRun(): void { $this->config->expects($this->once()) ->method('getAppValue') ->with('dav', 'regeneratedBirthdayCalendarsForYearFix') @@ -83,7 +59,7 @@ class RegenerateBirthdayCalendarsTest extends TestCase { $this->migration->run($output); } - public function testRunSecondTime() { + public function testRunSecondTime(): void { $this->config->expects($this->once()) ->method('getAppValue') ->with('dav', 'regeneratedBirthdayCalendarsForYearFix') diff --git a/apps/dav/tests/unit/Migration/RemoveDeletedUsersCalendarSubscriptionsTest.php b/apps/dav/tests/unit/Migration/RemoveDeletedUsersCalendarSubscriptionsTest.php index c0d6518d205..a9758470573 100644 --- a/apps/dav/tests/unit/Migration/RemoveDeletedUsersCalendarSubscriptionsTest.php +++ b/apps/dav/tests/unit/Migration/RemoveDeletedUsersCalendarSubscriptionsTest.php @@ -3,25 +3,8 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2021 Thomas Citharel <nextcloud@tcit.fr> - * - * @author Thomas Citharel <nextcloud@tcit.fr> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\DAV\Tests\unit\DAV\Migration; @@ -39,23 +22,10 @@ use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class RemoveDeletedUsersCalendarSubscriptionsTest extends TestCase { - /** - * @var IDBConnection|MockObject - */ - private $dbConnection; - /** - * @var IUserManager|MockObject - */ - private $userManager; - - /** - * @var IOutput|MockObject - */ - private $output; - /** - * @var RemoveDeletedUsersCalendarSubscriptions - */ - private $migration; + private IDBConnection&MockObject $dbConnection; + private IUserManager&MockObject $userManager; + private IOutput&MockObject $output; + private RemoveDeletedUsersCalendarSubscriptions $migration; protected function setUp(): void { @@ -75,13 +45,7 @@ class RemoveDeletedUsersCalendarSubscriptionsTest extends TestCase { ); } - /** - * @dataProvider dataTestRun - * @param array $subscriptions - * @param array $userExists - * @param int $deletions - * @throws \Exception - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestRun')] public function testRun(array $subscriptions, array $userExists, int $deletions): void { $qb = $this->createMock(IQueryBuilder::class); @@ -111,7 +75,7 @@ class RemoveDeletedUsersCalendarSubscriptionsTest extends TestCase { $qb->method('execute') ->willReturn($result); - $result->expects($this->at(0)) + $result->expects($this->once()) ->method('fetchOne') ->willReturn(count($subscriptions)); @@ -149,21 +113,28 @@ class RemoveDeletedUsersCalendarSubscriptionsTest extends TestCase { $this->migration->run($this->output); } - public function dataTestRun(): array { + public static function dataTestRun(): array { return [ [[], [], 0], - [[[ - 'id' => 1, - 'principaluri' => 'users/principals/foo1', - ], + [ [ - 'id' => 2, - 'principaluri' => 'users/principals/bar1', + [ + 'id' => 1, + 'principaluri' => 'users/principals/foo1', + ], + [ + 'id' => 2, + 'principaluri' => 'users/principals/bar1', + ], + [ + 'id' => 3, + 'principaluri' => 'users/principals/bar1', + ], + [], ], - [ - 'id' => 3, - 'principaluri' => 'users/principals/bar1', - ]], ['foo1' => true, 'bar1' => false], 2] + ['foo1' => true, 'bar1' => false], + 2 + ], ]; } } |