Skip to content

Commit

Permalink
chore: Cleanup and prepare some app tests for PHPUnit 10
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Sep 19, 2024
1 parent 96de697 commit 4ccf62a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
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

0 comments on commit 4ccf62a

Please sign in to comment.