Skip to content

Commit 75f5cb7

Browse files
authored
Merge pull request #48218 from nextcloud/ci/noid/prepare-phpunit-10
chore: Cleanup and prepare some app tests for PHPUnit 10
2 parents 00a27af + 4ccf62a commit 75f5cb7

File tree

5 files changed

+45
-28
lines changed

5 files changed

+45
-28
lines changed

apps/user_status/tests/Unit/Service/PredefinedStatusServiceTest.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,9 @@ protected function setUp(): void {
3131
public function testGetDefaultStatuses(): void {
3232
$this->l10n->expects($this->exactly(7))
3333
->method('t')
34-
->withConsecutive(
35-
['In a meeting'],
36-
['Commuting'],
37-
['Working remotely'],
38-
['Out sick'],
39-
['Vacationing'],
40-
['In a call'],
41-
)
42-
->willReturnArgument(0);
34+
->willReturnCallback(function ($text, $parameters = []) {
35+
return vsprintf($text, $parameters);
36+
});
4337

4438
$actual = $this->service->getDefaultStatuses();
4539
$this->assertEquals([
@@ -186,15 +180,9 @@ public function isValidIdDataProvider(): array {
186180
public function testGetDefaultStatusById(): void {
187181
$this->l10n->expects($this->exactly(7))
188182
->method('t')
189-
->withConsecutive(
190-
['In a meeting'],
191-
['Commuting'],
192-
['Working remotely'],
193-
['Out sick'],
194-
['Vacationing'],
195-
['In a call'],
196-
)
197-
->willReturnArgument(0);
183+
->willReturnCallback(function ($text, $parameters = []) {
184+
return vsprintf($text, $parameters);
185+
});
198186

199187
$this->assertEquals([
200188
'id' => 'call',

apps/webhook_listeners/tests/Service/PHPMongoQueryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Test\TestCase;
1515

1616
class PHPMongoQueryTest extends TestCase {
17-
private function dataExecuteQuery() {
17+
public static function dataExecuteQuery(): array {
1818
$event = [
1919
'event' => [
2020
'class' => NodeWrittenEvent::class,

apps/workflowengine/tests/Check/AbstractStringCheckTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ protected function getCheckMock() {
2222
->setConstructorArgs([
2323
$l,
2424
])
25-
->setMethods([
26-
'setPath',
25+
->onlyMethods([
2726
'executeCheck',
2827
'getActualValue',
2928
])

apps/workflowengine/tests/ManagerTest.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -362,16 +362,22 @@ public function testUpdateOperation(): void {
362362
$cache->expects($this->exactly(4))
363363
->method('remove')
364364
->with('events');
365-
$this->cacheFactory->method('createDistributed')->willReturn($cache);
365+
$this->cacheFactory->method('createDistributed')
366+
->willReturn($cache);
366367

368+
$expectedCalls = [
369+
[IManager::SCOPE_ADMIN],
370+
[IManager::SCOPE_USER],
371+
];
372+
$i = 0;
367373
$operationMock = $this->createMock(IOperation::class);
368374
$operationMock->expects($this->any())
369375
->method('isAvailableForScope')
370-
->withConsecutive(
371-
[IManager::SCOPE_ADMIN],
372-
[IManager::SCOPE_USER]
373-
)
374-
->willReturn(true);
376+
->willReturnCallback(function () use (&$expectedCalls, &$i): bool {
377+
$this->assertEquals($expectedCalls[$i], func_get_args());
378+
$i++;
379+
return true;
380+
});
375381

376382
$this->container->expects($this->any())
377383
->method('query')
@@ -390,7 +396,7 @@ public function testUpdateOperation(): void {
390396
$this->createMock(UserMountCache::class),
391397
$this->createMock(IMountManager::class),
392398
])
393-
->setMethodsExcept(['getEvents'])
399+
->onlyMethods($this->filterClassMethods(File::class, ['getEvents']))
394400
->getMock();
395401
}
396402
return $this->createMock(ICheck::class);

tests/lib/TestCase.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,30 @@ protected static function getUniqueID($prefix = '', $length = 13) {
271271
);
272272
}
273273

274+
/**
275+
* Filter methods
276+
*
277+
* Returns all methods of the given class,
278+
* that are public or abstract and not in the ignoreMethods list,
279+
* to be able to fill onlyMethods() with an inverted list.
280+
*
281+
* @param string $className
282+
* @param string[] $filterMethods
283+
* @return string[]
284+
*/
285+
public function filterClassMethods(string $className, array $filterMethods): array {
286+
$class = new \ReflectionClass($className);
287+
288+
$methods = [];
289+
foreach ($class->getMethods() as $method) {
290+
if (($method->isPublic() || $method->isAbstract()) && !in_array($method->getName(), $filterMethods, true)) {
291+
$methods[] = $method->getName();
292+
}
293+
}
294+
295+
return $methods;
296+
}
297+
274298
public static function tearDownAfterClass(): void {
275299
if (!self::$wasDatabaseAllowed && self::$realDatabase !== null) {
276300
// in case an error is thrown in a test, PHPUnit jumps straight to tearDownAfterClass,

0 commit comments

Comments
 (0)