aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFerdinand Thiessen <opensource@fthiessen.de>2024-09-19 19:28:07 +0200
committerGitHub <noreply@github.com>2024-09-19 19:28:07 +0200
commit75f5cb76300ca754f141573e76e029efb9369e51 (patch)
tree2e0eba97df761ca0da2d84ba959ca053090b8961
parent00a27afa264c98cd1ca65a682f459f60ef91b497 (diff)
parent4ccf62a2249495b5f01582fa529762e631051c56 (diff)
downloadnextcloud-server-75f5cb76300ca754f141573e76e029efb9369e51.tar.gz
nextcloud-server-75f5cb76300ca754f141573e76e029efb9369e51.zip
Merge pull request #48218 from nextcloud/ci/noid/prepare-phpunit-10
chore: Cleanup and prepare some app tests for PHPUnit 10
-rw-r--r--apps/user_status/tests/Unit/Service/PredefinedStatusServiceTest.php24
-rw-r--r--apps/webhook_listeners/tests/Service/PHPMongoQueryTest.php2
-rw-r--r--apps/workflowengine/tests/Check/AbstractStringCheckTest.php3
-rw-r--r--apps/workflowengine/tests/ManagerTest.php20
-rw-r--r--tests/lib/TestCase.php24
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 455d687d4c2..12b34ed0b4d 100644
--- a/apps/user_status/tests/Unit/Service/PredefinedStatusServiceTest.php
+++ b/apps/user_status/tests/Unit/Service/PredefinedStatusServiceTest.php
@@ -31,15 +31,9 @@ class PredefinedStatusServiceTest extends TestCase {
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 @@ class PredefinedStatusServiceTest extends TestCase {
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 b1094731733..e56bdb6a4cf 100644
--- a/apps/webhook_listeners/tests/Service/PHPMongoQueryTest.php
+++ b/apps/webhook_listeners/tests/Service/PHPMongoQueryTest.php
@@ -14,7 +14,7 @@ use OCP\Files\Events\Node\NodeWrittenEvent;
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 eb40b164176..c598c756bed 100644
--- a/apps/workflowengine/tests/Check/AbstractStringCheckTest.php
+++ b/apps/workflowengine/tests/Check/AbstractStringCheckTest.php
@@ -22,8 +22,7 @@ class AbstractStringCheckTest extends \Test\TestCase {
->setConstructorArgs([
$l,
])
- ->setMethods([
- 'setPath',
+ ->onlyMethods([
'executeCheck',
'getActualValue',
])
diff --git a/apps/workflowengine/tests/ManagerTest.php b/apps/workflowengine/tests/ManagerTest.php
index 334666af039..2d078c75490 100644
--- a/apps/workflowengine/tests/ManagerTest.php
+++ b/apps/workflowengine/tests/ManagerTest.php
@@ -362,16 +362,22 @@ class ManagerTest extends TestCase {
$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 @@ class ManagerTest extends TestCase {
$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 03886d4a0be..d74dacd76c1 100644
--- a/tests/lib/TestCase.php
+++ b/tests/lib/TestCase.php
@@ -271,6 +271,30 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
);
}
+ /**
+ * 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,