From 4ccf62a2249495b5f01582fa529762e631051c56 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 19 Sep 2024 18:19:32 +0200 Subject: [PATCH] chore: Cleanup and prepare some app tests for PHPUnit 10 Signed-off-by: Joas Schilling --- .../Service/PredefinedStatusServiceTest.php | 24 +++++-------------- .../tests/Service/PHPMongoQueryTest.php | 2 +- .../tests/Check/AbstractStringCheckTest.php | 3 +-- apps/workflowengine/tests/ManagerTest.php | 20 ++++++++++------ tests/lib/TestCase.php | 24 +++++++++++++++++++ 5 files changed, 45 insertions(+), 28 deletions(-) diff --git a/apps/user_status/tests/Unit/Service/PredefinedStatusServiceTest.php b/apps/user_status/tests/Unit/Service/PredefinedStatusServiceTest.php index 455d687d4c203..12b34ed0b4daf 100644 --- a/apps/user_status/tests/Unit/Service/PredefinedStatusServiceTest.php +++ b/apps/user_status/tests/Unit/Service/PredefinedStatusServiceTest.php @@ -31,15 +31,9 @@ protected function setUp(): void { public function testGetDefaultStatuses(): void { $this->l10n->expects($this->exactly(7)) ->method('t') - ->withConsecutive( - ['In a meeting'], - ['Commuting'], - ['Working remotely'], - ['Out sick'], - ['Vacationing'], - ['In a call'], - ) - ->willReturnArgument(0); + ->willReturnCallback(function ($text, $parameters = []) { + return vsprintf($text, $parameters); + }); $actual = $this->service->getDefaultStatuses(); $this->assertEquals([ @@ -186,15 +180,9 @@ public function isValidIdDataProvider(): array { public function testGetDefaultStatusById(): void { $this->l10n->expects($this->exactly(7)) ->method('t') - ->withConsecutive( - ['In a meeting'], - ['Commuting'], - ['Working remotely'], - ['Out sick'], - ['Vacationing'], - ['In a call'], - ) - ->willReturnArgument(0); + ->willReturnCallback(function ($text, $parameters = []) { + return vsprintf($text, $parameters); + }); $this->assertEquals([ 'id' => 'call', diff --git a/apps/webhook_listeners/tests/Service/PHPMongoQueryTest.php b/apps/webhook_listeners/tests/Service/PHPMongoQueryTest.php index b10947317336d..e56bdb6a4cfdb 100644 --- a/apps/webhook_listeners/tests/Service/PHPMongoQueryTest.php +++ b/apps/webhook_listeners/tests/Service/PHPMongoQueryTest.php @@ -14,7 +14,7 @@ use Test\TestCase; class PHPMongoQueryTest extends TestCase { - private function dataExecuteQuery() { + public static function dataExecuteQuery(): array { $event = [ 'event' => [ 'class' => NodeWrittenEvent::class, diff --git a/apps/workflowengine/tests/Check/AbstractStringCheckTest.php b/apps/workflowengine/tests/Check/AbstractStringCheckTest.php index eb40b16417680..c598c756bedef 100644 --- a/apps/workflowengine/tests/Check/AbstractStringCheckTest.php +++ b/apps/workflowengine/tests/Check/AbstractStringCheckTest.php @@ -22,8 +22,7 @@ protected function getCheckMock() { ->setConstructorArgs([ $l, ]) - ->setMethods([ - 'setPath', + ->onlyMethods([ 'executeCheck', 'getActualValue', ]) diff --git a/apps/workflowengine/tests/ManagerTest.php b/apps/workflowengine/tests/ManagerTest.php index 334666af03968..2d078c7549018 100644 --- a/apps/workflowengine/tests/ManagerTest.php +++ b/apps/workflowengine/tests/ManagerTest.php @@ -362,16 +362,22 @@ public function testUpdateOperation(): void { $cache->expects($this->exactly(4)) ->method('remove') ->with('events'); - $this->cacheFactory->method('createDistributed')->willReturn($cache); + $this->cacheFactory->method('createDistributed') + ->willReturn($cache); + $expectedCalls = [ + [IManager::SCOPE_ADMIN], + [IManager::SCOPE_USER], + ]; + $i = 0; $operationMock = $this->createMock(IOperation::class); $operationMock->expects($this->any()) ->method('isAvailableForScope') - ->withConsecutive( - [IManager::SCOPE_ADMIN], - [IManager::SCOPE_USER] - ) - ->willReturn(true); + ->willReturnCallback(function () use (&$expectedCalls, &$i): bool { + $this->assertEquals($expectedCalls[$i], func_get_args()); + $i++; + return true; + }); $this->container->expects($this->any()) ->method('query') @@ -390,7 +396,7 @@ public function testUpdateOperation(): void { $this->createMock(UserMountCache::class), $this->createMock(IMountManager::class), ]) - ->setMethodsExcept(['getEvents']) + ->onlyMethods($this->filterClassMethods(File::class, ['getEvents'])) ->getMock(); } return $this->createMock(ICheck::class); diff --git a/tests/lib/TestCase.php b/tests/lib/TestCase.php index 03886d4a0be5c..d74dacd76c112 100644 --- a/tests/lib/TestCase.php +++ b/tests/lib/TestCase.php @@ -271,6 +271,30 @@ protected static function getUniqueID($prefix = '', $length = 13) { ); } + /** + * Filter methods + * + * Returns all methods of the given class, + * that are public or abstract and not in the ignoreMethods list, + * to be able to fill onlyMethods() with an inverted list. + * + * @param string $className + * @param string[] $filterMethods + * @return string[] + */ + public function filterClassMethods(string $className, array $filterMethods): array { + $class = new \ReflectionClass($className); + + $methods = []; + foreach ($class->getMethods() as $method) { + if (($method->isPublic() || $method->isAbstract()) && !in_array($method->getName(), $filterMethods, true)) { + $methods[] = $method->getName(); + } + } + + return $methods; + } + public static function tearDownAfterClass(): void { if (!self::$wasDatabaseAllowed && self::$realDatabase !== null) { // in case an error is thrown in a test, PHPUnit jumps straight to tearDownAfterClass,