Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/phpunit.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Tests

env:
PHP_VERSION: 8.2
PHP_VERSION: 8.3

on:
push:
Expand All @@ -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
run: composer test
15 changes: 10 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
}
}
2 changes: 1 addition & 1 deletion src/InvalidTaskAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use function implode;
use function sprintf;

class InvalidTaskAttribute extends InvalidArgumentException
final class InvalidTaskAttribute extends InvalidArgumentException
{
/** @param array<int, int|string> $keys */
public static function duplicatedKey(Task $task, array $keys): self
Expand Down
10 changes: 5 additions & 5 deletions src/TaskManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions tests/Strategy/ContinueOnFailureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions tests/Strategy/InvalidPermission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/*
Task Manager Library
Copyright (C) 2019 CustomerGauge
foss@customergauge.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

declare(strict_types=1);

namespace Tests\CustomerGauge\TaskManager\Strategy;

use Exception;

class InvalidPermission extends Exception
{
}
31 changes: 31 additions & 0 deletions tests/Strategy/ReversibleTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*
Task Manager Library
Copyright (C) 2019 CustomerGauge
foss@customergauge.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

declare(strict_types=1);

namespace Tests\CustomerGauge\TaskManager\Strategy;

use CustomerGauge\TaskManager\Reversible;
use CustomerGauge\TaskManager\Task;

interface ReversibleTask extends Task, Reversible
{
}
16 changes: 3 additions & 13 deletions tests/Strategy/RollbackOnFailureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@

namespace Tests\CustomerGauge\TaskManager\Strategy;

use CustomerGauge\TaskManager\Reversible;
use CustomerGauge\TaskManager\Strategy\RollbackOnFailure;
use CustomerGauge\TaskManager\Task;
use Exception;
use PHPUnit\Framework\TestCase;

class RollbackOnFailureTest extends TestCase
{
public function test_it_rollback_when_a_task_fail(): void
public function testItRollbackWhenATaskFail(): void
{
$createEmail = $this->createMock(ReversibleTask::class);
$createFolder = $this->createMock(Task::class);
Expand All @@ -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();

Expand All @@ -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
{
}
2 changes: 1 addition & 1 deletion tests/Strategy/StopOnFailureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
16 changes: 6 additions & 10 deletions tests/TaskManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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');
Expand All @@ -115,7 +115,3 @@ public function test_task_can_be_revertable(): void
$manager->reverse([]);
}
}

interface ReversibleTask extends Task, Reversible
{
}