mirror of
https://github.com/archtechx/jobpipeline.git
synced 2025-12-12 21:54:04 +00:00
test: add tests
This commit is contained in:
parent
85e7d8a6b9
commit
ebeef52b4b
2 changed files with 49 additions and 2 deletions
|
|
@ -61,13 +61,15 @@ class JobPipeline implements ShouldQueue
|
||||||
{
|
{
|
||||||
foreach ($this->jobs as $job) {
|
foreach ($this->jobs as $job) {
|
||||||
if (is_string($job)) {
|
if (is_string($job)) {
|
||||||
$job = [new $job(...$this->passable), 'handle'];
|
if (!str_contains($job, '@')) {
|
||||||
|
$job = [new $job(...$this->passable), 'handle'];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$result = app()->call($job, $this->passable);
|
$result = app()->call($job, $this->passable);
|
||||||
} catch (Throwable $exception) {
|
} 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]);
|
call_user_func_array([$job[0], 'failed'], [$exception]);
|
||||||
} else {
|
} else {
|
||||||
throw $exception;
|
throw $exception;
|
||||||
|
|
|
||||||
|
|
@ -167,6 +167,40 @@ class JobPipelineTest extends TestCase
|
||||||
|
|
||||||
$this->assertTrue($passes);
|
$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
|
class FooJob
|
||||||
|
|
@ -287,3 +321,14 @@ class ExceptionJob
|
||||||
$this->valuestore->put('exeception', $e->getMessage());
|
$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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue