Skip to content

Commit cad9728

Browse files
tcitworldnickvergessen
authored andcommitted
feat(test-case): allow to use PHPUnit Group attributes to mark tests requiring database
Before that, only the annotation @group('DB') is possible to mark test classes as requiring the database. Now the \PHPUnit\Framework\Attributes\Group attribute can be used as long as PHPUnit 10+ is used. Signed-off-by: Thomas Citharel <[email protected]>
1 parent a0fd631 commit cad9728

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

tests/lib/TestCase.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use OCP\IL10N;
2727
use OCP\Lock\ILockingProvider;
2828
use OCP\Security\ISecureRandom;
29+
use PHPUnit\Framework\Attributes\Group;
2930

3031
if (version_compare(\PHPUnit\Runner\Version::id(), 10, '>=')) {
3132
trait OnNotSuccessfulTestTrait {
@@ -529,11 +530,22 @@ protected function getGroupAnnotations(): array {
529530

530531
$r = new \ReflectionClass($this);
531532
$doc = $r->getDocComment();
533+
534+
if (class_exists(Group::class)) {
535+
$attributes = array_map(function (\ReflectionAttribute $attribute) {
536+
/** @var Group $group */
537+
$group = $attribute->newInstance();
538+
return $group->name();
539+
}, $r->getAttributes(Group::class));
540+
if (count($attributes) > 0) {
541+
return $attributes;
542+
}
543+
}
532544
preg_match_all('#@group\s+(.*?)\n#s', $doc, $annotations);
533545
return $annotations[1] ?? [];
534546
}
535547

536-
protected function IsDatabaseAccessAllowed() {
548+
protected function IsDatabaseAccessAllowed(): bool {
537549
$annotations = $this->getGroupAnnotations();
538550
if (isset($annotations)) {
539551
if (in_array('DB', $annotations) || in_array('SLOWDB', $annotations)) {

0 commit comments

Comments
 (0)