Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Cleanup and prepare some app tests for PHPUnit 10 #48218

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion apps/webhook_listeners/tests/Service/PHPMongoQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Test\TestCase;

class PHPMongoQueryTest extends TestCase {
private function dataExecuteQuery() {
public static function dataExecuteQuery(): array {
$event = [
'event' => [
'class' => NodeWrittenEvent::class,
Expand Down
3 changes: 1 addition & 2 deletions apps/workflowengine/tests/Check/AbstractStringCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ protected function getCheckMock() {
->setConstructorArgs([
$l,
])
->setMethods([
'setPath',
->onlyMethods([
'executeCheck',
'getActualValue',
])
Expand Down
20 changes: 13 additions & 7 deletions apps/workflowengine/tests/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading