Skip to content

Commit

Permalink
Merge branch 'lorisleiva:main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
jaulz authored Jul 27, 2023
2 parents bf815ee + 02b7731 commit 3522360
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 30 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: ["8.0", "8.1"]
laravel: ["^8.67", "^9.0", "^10.0"]
php: ["8.0", "8.1", "8.2"]
laravel: ["^9.0", "^10.0"]
dependency-version: [prefer-lowest, prefer-stable]
include:
- laravel: "^8.67"
testbench: "^6.23"
- laravel: "^9.0"
testbench: "^7.0"
- laravel: "^10.0"
testbench: "^8.0"
exclude:
- laravel: "^8.67"
dependency-version: prefer-lowest
- php: "8.2"
laravel: "^9.0"
- php: "8.0"
laravel: "^10.0"

Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
}
],
"require": {
"php": "^8.0|^8.1",
"illuminate/contracts": "^8.15 || 9.0 - 9.34 || ^9.36 || ^10.0",
"lorisleiva/lody": "^0.4.0"
"php": "^8.0",
"illuminate/contracts": "9.0 - 9.34 || ^9.36 || ^10.0",
"lorisleiva/lody": "^0.4"
},
"require-dev": {
"orchestra/testbench": "^8.0",
"pestphp/pest": "^1.2",
"phpunit/phpunit": "^9.5"
"orchestra/testbench": "^8.5",
"pestphp/pest": "^1.23",
"phpunit/phpunit": "^9.6"
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 3 additions & 2 deletions src/ActionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Application as Artisan;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Foundation\Application;
use Illuminate\Routing\Router;
use Lorisleiva\Actions\Concerns\AsCommand;
use Lorisleiva\Actions\Concerns\AsController;
Expand Down Expand Up @@ -78,7 +79,7 @@ public function getDesignPatternsMatching(array $usedTraits): array
return array_filter($this->getDesignPatterns(), $filter);
}

public function extend(string $abstract): void
public function extend(Application $app, string $abstract): void
{
if ($this->isExtending($abstract)) {
return;
Expand All @@ -88,7 +89,7 @@ public function extend(string $abstract): void
return;
}

app()->extend($abstract, function ($instance) {
$app->extend($abstract, function ($instance) {
return $this->identifyAndDecorate($instance);
});

Expand Down
30 changes: 18 additions & 12 deletions src/ActionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lorisleiva\Actions;

use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use Lorisleiva\Actions\Console\MakeActionCommand;
use Lorisleiva\Actions\DesignPatterns\CommandDesignPattern;
Expand All @@ -12,14 +13,15 @@ class ActionServiceProvider extends ServiceProvider
{
public function register()
{
$manager = new ActionManager([
new ControllerDesignPattern(),
new ListenerDesignPattern(),
new CommandDesignPattern(),
]);

$this->app->instance(ActionManager::class, $manager);
$this->extendActions($manager);
$this->app->scoped(ActionManager::class, function () {
return new ActionManager([
new ControllerDesignPattern(),
new ListenerDesignPattern(),
new CommandDesignPattern(),
]);
});

$this->extendActions();
}

public function boot()
Expand All @@ -37,9 +39,13 @@ public function boot()
}
}

protected function extendActions(ActionManager $manager)
protected function extendActions()
{
$this->app->beforeResolving(function ($abstract) use ($manager) {
$this->app->beforeResolving(function ($abstract, $parameters, Application $app) {
if ($abstract === ActionManager::class) {
return;
}

try {
// Fix conflict with package: barryvdh/laravel-ide-helper.
// @see https://github.com/lorisleiva/laravel-actions/issues/142
Expand All @@ -48,11 +54,11 @@ protected function extendActions(ActionManager $manager)
return;
}

if (! $classExists || app()->resolved($abstract)) {
if (! $classExists || $app->resolved($abstract)) {
return;
}

$manager->extend($abstract);
$app->make(ActionManager::class)->extend($app, $abstract);
});
}
}
2 changes: 1 addition & 1 deletion src/Concerns/AsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static function dispatchNow(...$arguments)
*/
public static function dispatchAfterResponse(...$arguments): void
{
app(Dispatcher::class)->dispatchAfterResponse(static::makeJob(...$arguments));
static::dispatch(...$arguments)->afterResponse();
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/Decorators/ListenerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public function handle(...$arguments)
}
}

public function shouldQueue(...$arguments)
{
if ($this->hasMethod('shouldQueue')) {
return $this->resolveFromArgumentsAndCall('shouldQueue', $arguments);
}

return true;
}

protected function resolveFromArgumentsAndCall($method, $arguments)
{
$arguments = $this->resolveClassMethodDependencies(
Expand Down
8 changes: 6 additions & 2 deletions tests/AsJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,17 @@ public function handle()
AsJobTest::dispatchAfterResponse();

// Then it is not dispatch immediately.
expect(AsJobTest::$handled)->toBe(0);
Queue::assertNothingPushed();

// But when the app terminates.
app()->terminate();

// Then the job was dispatched.
expect(AsJobTest::$handled)->toBe(1);
if (AsJobTest::$handled === 1) {
expect(AsJobTest::$handled)->toBe(1);
} else {
assertJobPushed(AsJobTest::class);
}
});

it('constructs a new job at every dispatch', function () {
Expand Down
10 changes: 10 additions & 0 deletions tests/AsListenerWithShouldQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class AsListenerWithShouldQueueTest implements ShouldQueue

public static int $constructed = 0;
public static int $handled = 0;
public static int $shouldQueue = 0;
public static ?int $latestResult;

public function __construct()
Expand All @@ -35,6 +36,13 @@ public function asListener(OperationRequestedEvent $event): void
{
$this->handle($event->operation, $event->left, $event->right);
}

public function shouldQueue(OperationRequestedEvent $event): bool
{
static::$shouldQueue++;

return true;
}
}

beforeEach(function () {
Expand All @@ -44,6 +52,7 @@ public function asListener(OperationRequestedEvent $event): void
// And reset the static properties between each test.
AsListenerWithShouldQueueTest::$constructed = 0;
AsListenerWithShouldQueueTest::$handled = 0;
AsListenerWithShouldQueueTest::$shouldQueue = 0;
AsListenerWithShouldQueueTest::$latestResult = null;
});

Expand All @@ -69,6 +78,7 @@ public function asListener(OperationRequestedEvent $event): void
// Then the action was triggered as a queued listener.
expect(AsListenerWithShouldQueueTest::$latestResult)->toBe(3);
expect(AsListenerWithShouldQueueTest::$handled)->toBe(1);
expect(AsListenerWithShouldQueueTest::$shouldQueue)->toBe(1);

// And was constructed twice. Once before and once during the queued job.
expect(AsListenerWithShouldQueueTest::$constructed)->toBe(2);
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ protected function getPackageProviders($app)

public function getEnvironmentSetUp($app)
{
//
$app['config']->set('database.default', 'sqlite');
}
}

0 comments on commit 3522360

Please sign in to comment.