Skip to content

Commit

Permalink
Add tests for PHP 8.1 enum
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Sep 23, 2023
1 parent edbc519 commit a008e27
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ composer-attribute-collector is a plugin for [Composer][]. Its ambition is to pr
way—and near zero-cost—to retrieve targets of PHP 8 attributes. After the autoloader has been
dumped, the plugin collects attribute targets and generates a static file. Later, these targets can
be retrieved through a convenient interface, without involving reflection. The plugin is useful when
you need to _discover_ attributes in a codebase—for known classes you can use reflection.
you need to _discover_ attribute targets in a codebase—for known targets you can use reflection.



Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"autoload-dev": {
"psr-4": {
"tests\\olvlvl\\ComposerAttributeCollector\\": "tests",
"Acme\\": "tests/Acme"
"Acme\\": "tests/Acme",
"Acme81\\": "tests/Acme81"
},
"classmap": [
"tests/Acme/ClassMap"
Expand Down
16 changes: 16 additions & 0 deletions tests/Acme81/Attribute/Get.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Acme81\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_METHOD)]
class Get extends Route
{
public function __construct(
string $pattern = '',
?string $id = null
) {
parent::__construct($pattern, Method::GET, $id);
}
}
9 changes: 9 additions & 0 deletions tests/Acme81/Attribute/Method.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Acme81\Attribute;

enum Method: string
{
case GET = 'GET';
case POST = 'POST';
}
16 changes: 16 additions & 0 deletions tests/Acme81/Attribute/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Acme81\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_METHOD)]
class Post extends Route
{
public function __construct(
string $pattern = '',
?string $id = null
) {
parent::__construct($pattern, Method::POST, $id);
}
}
19 changes: 19 additions & 0 deletions tests/Acme81/Attribute/Route.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Acme81\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
class Route
{
/**
* @param Method|Method[] $method
*/
public function __construct(
public string $pattern,
public Method|array $method = Method::GET,
public ?string $id = null,
) {
}
}
27 changes: 27 additions & 0 deletions tests/Acme81/PSR4/Presentation/ArticleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Acme81\PSR4\Presentation;

use Acme81\Attribute\Get;
use Acme81\Attribute\Method;
use Acme81\Attribute\Post;
use Acme81\Attribute\Route;

#[Route('/articles')]
class ArticleController
{
#[Route('/:id', method: Method::GET)]
public function show()
{
}

#[Get]
public function list()
{
}

#[Post]
public function new()
{
}
}
43 changes: 40 additions & 3 deletions tests/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
use function str_contains;
use function usort;

use const PHP_VERSION_ID;

final class PluginTest extends TestCase
{
private static bool $initialized = false;
Expand All @@ -57,16 +59,21 @@ protected function setUp(): void
assert(is_string($cwd));
$vendorDir = __DIR__ . '/sandbox';
$filepath = "$vendorDir/attributes.php";
$exclude = [
"$cwd/tests/Acme/PSR4/IncompatibleSignature.php"
];

if (PHP_VERSION_ID < 80100) {
$exclude[] = "$cwd/tests/Acme81";
}

$config = new Config(
vendorDir: $vendorDir,
attributesFile: $filepath,
include: [
"$cwd/tests"
],
exclude: [
"$cwd/tests/Acme/PSR4/IncompatibleSignature.php"
],
exclude: $exclude,
useCache: false,
);

Expand Down Expand Up @@ -258,6 +265,36 @@ public function testFilterTargetMethods(): void
], $this->collectMethods($actual));
}

/**
* @requires PHP >= 8.1
*/
public function testFilterTargetMethods81(): void
{
$expected = [
new TargetMethod(
new \Acme81\Attribute\Route('/:id', method: \Acme81\Attribute\Method::GET),
\Acme81\PSR4\Presentation\ArticleController::class,
'show'
),
new TargetMethod(
new \Acme81\Attribute\Get(),
\Acme81\PSR4\Presentation\ArticleController::class,
'list'
),
new TargetMethod(
new \Acme81\Attribute\Post(),
\Acme81\PSR4\Presentation\ArticleController::class,
'new'
),
];

$actual = Attributes::filterTargetMethods(
Attributes::predicateForAttributeInstanceOf(\Acme81\Attribute\Route::class)
);

$this->assertEquals($expected, $actual);
}

public function testFilterTargetProperties(): void
{
$actual = Attributes::filterTargetProperties(
Expand Down

0 comments on commit a008e27

Please sign in to comment.