Skip to content

Commit a008e27

Browse files
committed
Add tests for PHP 8.1 enum
1 parent edbc519 commit a008e27

File tree

8 files changed

+130
-5
lines changed

8 files changed

+130
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ composer-attribute-collector is a plugin for [Composer][]. Its ambition is to pr
99
way—and near zero-cost—to retrieve targets of PHP 8 attributes. After the autoloader has been
1010
dumped, the plugin collects attribute targets and generates a static file. Later, these targets can
1111
be retrieved through a convenient interface, without involving reflection. The plugin is useful when
12-
you need to _discover_ attributes in a codebase—for known classes you can use reflection.
12+
you need to _discover_ attribute targets in a codebase—for known targets you can use reflection.
1313

1414

1515

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"autoload-dev": {
2020
"psr-4": {
2121
"tests\\olvlvl\\ComposerAttributeCollector\\": "tests",
22-
"Acme\\": "tests/Acme"
22+
"Acme\\": "tests/Acme",
23+
"Acme81\\": "tests/Acme81"
2324
},
2425
"classmap": [
2526
"tests/Acme/ClassMap"

tests/Acme81/Attribute/Get.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Acme81\Attribute;
4+
5+
use Attribute;
6+
7+
#[Attribute(Attribute::TARGET_METHOD)]
8+
class Get extends Route
9+
{
10+
public function __construct(
11+
string $pattern = '',
12+
?string $id = null
13+
) {
14+
parent::__construct($pattern, Method::GET, $id);
15+
}
16+
}

tests/Acme81/Attribute/Method.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Acme81\Attribute;
4+
5+
enum Method: string
6+
{
7+
case GET = 'GET';
8+
case POST = 'POST';
9+
}

tests/Acme81/Attribute/Post.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Acme81\Attribute;
4+
5+
use Attribute;
6+
7+
#[Attribute(Attribute::TARGET_METHOD)]
8+
class Post extends Route
9+
{
10+
public function __construct(
11+
string $pattern = '',
12+
?string $id = null
13+
) {
14+
parent::__construct($pattern, Method::POST, $id);
15+
}
16+
}

tests/Acme81/Attribute/Route.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Acme81\Attribute;
4+
5+
use Attribute;
6+
7+
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
8+
class Route
9+
{
10+
/**
11+
* @param Method|Method[] $method
12+
*/
13+
public function __construct(
14+
public string $pattern,
15+
public Method|array $method = Method::GET,
16+
public ?string $id = null,
17+
) {
18+
}
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Acme81\PSR4\Presentation;
4+
5+
use Acme81\Attribute\Get;
6+
use Acme81\Attribute\Method;
7+
use Acme81\Attribute\Post;
8+
use Acme81\Attribute\Route;
9+
10+
#[Route('/articles')]
11+
class ArticleController
12+
{
13+
#[Route('/:id', method: Method::GET)]
14+
public function show()
15+
{
16+
}
17+
18+
#[Get]
19+
public function list()
20+
{
21+
}
22+
23+
#[Post]
24+
public function new()
25+
{
26+
}
27+
}

tests/PluginTest.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
use function str_contains;
3939
use function usort;
4040

41+
use const PHP_VERSION_ID;
42+
4143
final class PluginTest extends TestCase
4244
{
4345
private static bool $initialized = false;
@@ -57,16 +59,21 @@ protected function setUp(): void
5759
assert(is_string($cwd));
5860
$vendorDir = __DIR__ . '/sandbox';
5961
$filepath = "$vendorDir/attributes.php";
62+
$exclude = [
63+
"$cwd/tests/Acme/PSR4/IncompatibleSignature.php"
64+
];
65+
66+
if (PHP_VERSION_ID < 80100) {
67+
$exclude[] = "$cwd/tests/Acme81";
68+
}
6069

6170
$config = new Config(
6271
vendorDir: $vendorDir,
6372
attributesFile: $filepath,
6473
include: [
6574
"$cwd/tests"
6675
],
67-
exclude: [
68-
"$cwd/tests/Acme/PSR4/IncompatibleSignature.php"
69-
],
76+
exclude: $exclude,
7077
useCache: false,
7178
);
7279

@@ -258,6 +265,36 @@ public function testFilterTargetMethods(): void
258265
], $this->collectMethods($actual));
259266
}
260267

268+
/**
269+
* @requires PHP >= 8.1
270+
*/
271+
public function testFilterTargetMethods81(): void
272+
{
273+
$expected = [
274+
new TargetMethod(
275+
new \Acme81\Attribute\Route('/:id', method: \Acme81\Attribute\Method::GET),
276+
\Acme81\PSR4\Presentation\ArticleController::class,
277+
'show'
278+
),
279+
new TargetMethod(
280+
new \Acme81\Attribute\Get(),
281+
\Acme81\PSR4\Presentation\ArticleController::class,
282+
'list'
283+
),
284+
new TargetMethod(
285+
new \Acme81\Attribute\Post(),
286+
\Acme81\PSR4\Presentation\ArticleController::class,
287+
'new'
288+
),
289+
];
290+
291+
$actual = Attributes::filterTargetMethods(
292+
Attributes::predicateForAttributeInstanceOf(\Acme81\Attribute\Route::class)
293+
);
294+
295+
$this->assertEquals($expected, $actual);
296+
}
297+
261298
public function testFilterTargetProperties(): void
262299
{
263300
$actual = Attributes::filterTargetProperties(

0 commit comments

Comments
 (0)