Skip to content

Commit fe9685e

Browse files
Merge branch '11.1'
2 parents 8fd78be + b4fce24 commit fe9685e

File tree

6 files changed

+134
-14
lines changed

6 files changed

+134
-14
lines changed

src/Metadata/Parser/Annotation/DocBlock.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ private function __construct(string $docComment, array $symbolAnnotations, int $
120120
* string,
121121
* string|array{version: string, operator: string}|array{constraint: string}|array<int|string, string>
122122
* >
123+
*
124+
* @throws InvalidVersionRequirementException
123125
*/
124126
public function requirements(): array
125127
{

src/Metadata/Parser/AnnotationParser.php

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use PHPUnit\Event\Facade as EventFacade;
2626
use PHPUnit\Metadata\Annotation\Parser\Registry as AnnotationRegistry;
2727
use PHPUnit\Metadata\AnnotationsAreNotSupportedForInternalClassesException;
28+
use PHPUnit\Metadata\InvalidVersionRequirementException;
2829
use PHPUnit\Metadata\Metadata;
2930
use PHPUnit\Metadata\MetadataCollection;
3031
use PHPUnit\Metadata\ReflectionException;
@@ -159,13 +160,23 @@ public function forClass(string $className): MetadataCollection
159160
}
160161
}
161162

162-
$result = array_merge(
163-
$result,
164-
$this->parseRequirements(
165-
AnnotationRegistry::getInstance()->forClassName($className)->requirements(),
166-
'class',
167-
),
168-
);
163+
try {
164+
$result = array_merge(
165+
$result,
166+
$this->parseRequirements(
167+
AnnotationRegistry::getInstance()->forClassName($className)->requirements(),
168+
'class',
169+
),
170+
);
171+
} catch (InvalidVersionRequirementException $e) {
172+
EventFacade::emitter()->testRunnerTriggeredWarning(
173+
sprintf(
174+
'Class %s is annotated using an invalid version requirement: %s',
175+
$className,
176+
$e->getMessage(),
177+
),
178+
);
179+
}
169180

170181
if (!empty($result) &&
171182
!isset(self::$deprecationEmittedForClass[$className]) &&
@@ -389,13 +400,24 @@ public function forMethod(string $className, string $methodName): MetadataCollec
389400
}
390401

391402
if (method_exists($className, $methodName)) {
392-
$result = array_merge(
393-
$result,
394-
$this->parseRequirements(
395-
AnnotationRegistry::getInstance()->forMethod($className, $methodName)->requirements(),
396-
'method',
397-
),
398-
);
403+
try {
404+
$result = array_merge(
405+
$result,
406+
$this->parseRequirements(
407+
AnnotationRegistry::getInstance()->forMethod($className, $methodName)->requirements(),
408+
'method',
409+
),
410+
);
411+
} catch (InvalidVersionRequirementException $e) {
412+
EventFacade::emitter()->testRunnerTriggeredWarning(
413+
sprintf(
414+
'Method %s::%s is annotated using an invalid version requirement: %s',
415+
$className,
416+
$methodName,
417+
$e->getMessage(),
418+
),
419+
);
420+
}
399421
}
400422

401423
if (!empty($result) &&
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture;
11+
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* @requires PHP ~~9.0
16+
*/
17+
final class InvalidRequirementsTest extends TestCase
18+
{
19+
/**
20+
* @requires PHP ~~9.0
21+
*/
22+
public function testInvalidVersionConstraint(): void
23+
{
24+
$this->assertTrue(true);
25+
}
26+
}

tests/_files/Metadata/Annotation/tests/BackupStaticPropertiesTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ final class BackupStaticPropertiesTest extends TestCase
2222
* @backupStaticAttributes disabled
2323
*
2424
* @excludeStaticPropertyFromBackup anotherClassName propertyName
25+
* @excludeStaticPropertyFromBackup ...
2526
*/
2627
public function testOne(): void
2728
{
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
phpunit --no-configuration ../_files/InvalidRequirementsTest.php
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
require_once(__DIR__ . '/../../bootstrap.php');
6+
7+
$_SERVER['argv'][] = '--do-not-cache-result';
8+
$_SERVER['argv'][] = '--no-configuration';
9+
$_SERVER['argv'][] = \realpath(__DIR__ . '/../../_files/InvalidRequirementsTest.php');
10+
11+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
12+
--EXPECTF--
13+
PHPUnit %s by Sebastian Bergmann and contributors.
14+
15+
Runtime: %s
16+
17+
. 1 / 1 (100%)
18+
19+
Time: %s, Memory: %s
20+
21+
There were 2 PHPUnit test runner warnings:
22+
23+
1) Method PHPUnit\TestFixture\InvalidRequirementsTest::testInvalidVersionConstraint is annotated using an invalid version requirement: Version constraint ~~9.0 is not supported.
24+
25+
2) Class PHPUnit\TestFixture\InvalidRequirementsTest is annotated using an invalid version requirement: Version constraint ~~9.0 is not supported.
26+
27+
WARNINGS!
28+
Tests: 1, Assertions: 1, Warnings: 2.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Metadata\Annotation;
11+
12+
use Exception;
13+
use PHPUnit\Framework\Attributes\CoversClass;
14+
use PHPUnit\Framework\Attributes\Group;
15+
use PHPUnit\Framework\Attributes\Small;
16+
use PHPUnit\Framework\TestCase;
17+
use PHPUnit\Metadata\Annotation\Parser\DocBlock;
18+
use PHPUnit\Metadata\AnnotationsAreNotSupportedForInternalClassesException;
19+
use ReflectionClass;
20+
use ReflectionMethod;
21+
22+
#[CoversClass(DocBlock::class)]
23+
#[Small]
24+
#[Group('metadata')]
25+
#[Group('metadata/annotations')]
26+
final class DocBlockTest extends TestCase
27+
{
28+
public function testDoesNotSupportInternalClasses(): void
29+
{
30+
$this->expectException(AnnotationsAreNotSupportedForInternalClassesException::class);
31+
32+
DocBlock::ofClass(new ReflectionClass(Exception::class));
33+
}
34+
35+
public function testDoesNotSupportInternalMethods(): void
36+
{
37+
$this->expectException(AnnotationsAreNotSupportedForInternalClassesException::class);
38+
39+
DocBlock::ofMethod(new ReflectionMethod(Exception::class, 'getMessage'));
40+
}
41+
}

0 commit comments

Comments
 (0)