From ebeef52b4b4fbdd78d25f24479cc9822442265bf Mon Sep 17 00:00:00 2001 From: jaulz <5358638+jaulz@users.noreply.github.com> Date: Wed, 30 Oct 2024 20:51:53 +0000 Subject: [PATCH] test: add tests --- src/JobPipeline.php | 6 ++++-- tests/JobPipelineTest.php | 45 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/JobPipeline.php b/src/JobPipeline.php index 1b904ca..47b69df 100644 --- a/src/JobPipeline.php +++ b/src/JobPipeline.php @@ -61,13 +61,15 @@ class JobPipeline implements ShouldQueue { foreach ($this->jobs as $job) { if (is_string($job)) { - $job = [new $job(...$this->passable), 'handle']; + if (!str_contains($job, '@')) { + $job = [new $job(...$this->passable), 'handle']; + } } try { $result = app()->call($job, $this->passable); } catch (Throwable $exception) { - if (method_exists(get_class($job[0]), 'failed')) { + if (is_array($job) && method_exists(get_class($job[0]), 'failed')) { call_user_func_array([$job[0], 'failed'], [$exception]); } else { throw $exception; diff --git a/tests/JobPipelineTest.php b/tests/JobPipelineTest.php index 6331770..ebab227 100644 --- a/tests/JobPipelineTest.php +++ b/tests/JobPipelineTest.php @@ -167,6 +167,40 @@ class JobPipelineTest extends TestCase $this->assertTrue($passes); } + + /** @test */ + public function send_can_pass_parameters_to_method() + { + Event::listen(TestEvent::class, JobPipeline::make([ + [new FooJobWithMethod(), 'foo'] + ])->send(function () { + return $this->valuestore; + })->toListener()); + + $this->assertFalse($this->valuestore->has('foo')); + + event(new TestEvent(new TestModel())); + + $this->assertSame('bar', $this->valuestore->get('foo')); + } + + /** @test */ + public function send_can_pass_parameters_to_method_with_at_syntax() + { + app()->bind('FooJobWithMethod', fn () => new FooJobWithMethod()); + + Event::listen(TestEvent::class, JobPipeline::make([ + 'FooJobWithMethod@foo' + ])->send(function () { + return $this->valuestore; + })->toListener()); + + $this->assertFalse($this->valuestore->has('foo')); + + event(new TestEvent(new TestModel())); + + $this->assertSame('bar', $this->valuestore->get('foo')); + } } class FooJob @@ -287,3 +321,14 @@ class ExceptionJob $this->valuestore->put('exeception', $e->getMessage()); } } + +class FooJobWithMethod +{ + // variadic arguments must be used so the container doesn't try to inject any value + public function foo(...$arguments) + { + [$valuestore] = $arguments; + + $valuestore->put('foo', 'bar'); + } +}