1
0
Fork 0
mirror of https://github.com/archtechx/jobpipeline.git synced 2025-12-12 17:14:04 +00:00

test: add tests

This commit is contained in:
jaulz 2024-10-30 20:51:53 +00:00
parent 85e7d8a6b9
commit ebeef52b4b
2 changed files with 49 additions and 2 deletions

View file

@ -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;

View file

@ -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');
}
}