diff --git a/.github/workflows/phpunit.yml b/.github/workflows/ci.yml similarity index 73% rename from .github/workflows/phpunit.yml rename to .github/workflows/ci.yml index 39c176a..29d2083 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/ci.yml @@ -1,7 +1,7 @@ name: Tests env: - PHP_VERSION: 8.2 + PHP_VERSION: 8.3 on: push: @@ -23,5 +23,11 @@ jobs: - name: Install dependencies run: composer install --prefer-dist --no-progress --no-suggest + - name: Lint + run: composer lint + + - name: Static analysis + run: composer stan + - name: Test - run: ./vendor/bin/phpunit \ No newline at end of file + run: composer test diff --git a/composer.json b/composer.json index 569d241..baf5c2b 100644 --- a/composer.json +++ b/composer.json @@ -13,12 +13,12 @@ } ], "require": { - "php": ">=8.2" + "php": ">=8.3" }, "require-dev": { - "phpunit/phpunit": "^10.0", - "doctrine/coding-standard": "^12.0", - "phpstan/phpstan": "^1.0" + "phpunit/phpunit": "^12.0", + "doctrine/coding-standard": "^13.0", + "phpstan/phpstan": "^2.0" }, "autoload": { "psr-4": { @@ -27,12 +27,17 @@ }, "autoload-dev": { "psr-4": { - "Test\\CustomerGauge\\TaskManager\\": "tests/" + "Tests\\CustomerGauge\\TaskManager\\": "tests/" } }, "config": { "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true } + }, + "scripts": { + "lint": "phpcs", + "stan": "phpstan analyze --level=9 ./src ./tests", + "test": "phpunit" } } diff --git a/src/InvalidTaskAttribute.php b/src/InvalidTaskAttribute.php index 4134970..0688640 100644 --- a/src/InvalidTaskAttribute.php +++ b/src/InvalidTaskAttribute.php @@ -28,7 +28,7 @@ use function implode; use function sprintf; -class InvalidTaskAttribute extends InvalidArgumentException +final class InvalidTaskAttribute extends InvalidArgumentException { /** @param array $keys */ public static function duplicatedKey(Task $task, array $keys): self diff --git a/src/TaskManager.php b/src/TaskManager.php index 5f83f8d..cb4b41a 100644 --- a/src/TaskManager.php +++ b/src/TaskManager.php @@ -46,7 +46,7 @@ public function __construct(Strategy|null $strategy = null) $this->strategy = $strategy ?? new StopOnFailure(); } - public function add(Task $task): self + public function add(Task|Reversible $task): self { $this->tasks[] = $task; @@ -62,7 +62,9 @@ public function run(array $attributes): array { $this->attributes = $attributes; - foreach ($this->tasks as $task) { + $tasks = array_filter($this->tasks, static fn ($task) => $task instanceof Task); + + foreach ($tasks as $task) { $this->strategy->execute(function () use ($task): Task { $attributes = $task->run($this->attributes); @@ -82,9 +84,7 @@ public function reverse(array $attributes): void { $tasks = array_reverse($this->tasks); - $tasks = array_filter($tasks, static function ($task): bool { - return $task instanceof Reversible; - }); + $tasks = array_filter($tasks, static fn ($task) => $task instanceof Reversible); foreach ($tasks as $task) { $this->strategy->execute(static function () use ($task, $attributes): void { diff --git a/tests/Strategy/ContinueOnFailureTest.php b/tests/Strategy/ContinueOnFailureTest.php index 249a66b..00b0d57 100644 --- a/tests/Strategy/ContinueOnFailureTest.php +++ b/tests/Strategy/ContinueOnFailureTest.php @@ -30,7 +30,7 @@ class ContinueOnFailureTest extends TestCase { - public function test_it_continues_when_a_task_fail(): void + public function testItContinuesWhenATaskFail(): void { $createEmail = $this->createMock(Task::class); $createFolder = $this->createMock(Task::class); @@ -52,7 +52,7 @@ public function test_it_continues_when_a_task_fail(): void }); } - public function test_it_stores_exceptions(): void + public function testItStoresExceptions(): void { $createEmail = $this->createMock(Task::class); $createFolder = $this->createMock(Task::class); diff --git a/tests/Strategy/InvalidPermission.php b/tests/Strategy/InvalidPermission.php new file mode 100644 index 0000000..c451e98 --- /dev/null +++ b/tests/Strategy/InvalidPermission.php @@ -0,0 +1,30 @@ +createMock(ReversibleTask::class); $createFolder = $this->createMock(Task::class); @@ -43,7 +41,7 @@ public function test_it_rollback_when_a_task_fail(): void ->method('reverse'); $createFolder->method('run') - ->willThrowException(new CreateFolderException()); + ->willThrowException(new InvalidPermission()); $strategy = new RollbackOnFailure(); @@ -53,18 +51,10 @@ public function test_it_rollback_when_a_task_fail(): void return $createEmail; }); - $this->expectException(CreateFolderException::class); + $this->expectException(InvalidPermission::class); $strategy->execute(static function () use ($createFolder): void { $createFolder->run([]); }); } } - -class CreateFolderException extends Exception -{ -} - -interface ReversibleTask extends Task, Reversible -{ -} diff --git a/tests/Strategy/StopOnFailureTest.php b/tests/Strategy/StopOnFailureTest.php index 6106477..7ff0d90 100644 --- a/tests/Strategy/StopOnFailureTest.php +++ b/tests/Strategy/StopOnFailureTest.php @@ -31,7 +31,7 @@ class StopOnFailureTest extends TestCase { - public function test_it_stops_when_a_task_fail(): void + public function testItStopsWhenATaskFail(): void { self::expectException(Throwable::class); diff --git a/tests/TaskManagerTest.php b/tests/TaskManagerTest.php index 5653d80..79059c5 100644 --- a/tests/TaskManagerTest.php +++ b/tests/TaskManagerTest.php @@ -31,7 +31,7 @@ class TaskManagerTest extends TestCase { - public function test_it_can_run_tasks(): void + public function testItCanRunTasks(): void { $createEmail = $this->createMock(Task::class); $createFolder = $this->createMock(Task::class); @@ -50,7 +50,7 @@ public function test_it_can_run_tasks(): void $manager->run([]); } - public function test_it_can_manage_attributes(): void + public function testItCanManageAttributes(): void { $createEmail = $this->createMock(Task::class); $createFolder = $this->createMock(Task::class); @@ -75,7 +75,7 @@ public function test_it_can_manage_attributes(): void $manager->run([]); } - public function test_task_can_not_use_same_key_in_attributes(): void + public function testTaskCanNotUseSameKeyInAttributes(): void { $this->expectException(InvalidTaskAttribute::class); @@ -96,10 +96,10 @@ public function test_task_can_not_use_same_key_in_attributes(): void $manager->run([]); } - public function test_task_can_be_revertable(): void + public function testTaskCanBeRevertable(): void { - $createEmail = $this->createMock(ReversibleTask::class); - $createFolder = $this->createMock(ReversibleTask::class); + $createEmail = $this->createMock(Reversible::class); + $createFolder = $this->createMock(Reversible::class); $createEmail->expects($this->once()) ->method('reverse'); @@ -115,7 +115,3 @@ public function test_task_can_be_revertable(): void $manager->reverse([]); } } - -interface ReversibleTask extends Task, Reversible -{ -}