Skip to content

Commit

Permalink
Add support for parameters and multiple jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubKermes committed Jun 6, 2024
1 parent 8fd338c commit 3cae73c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/Features/Traits/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Blumilk\BLT\Features\Traits;

use Blumilk\BLT\Helpers\ArrayHelper;
use Illuminate\Support\Facades\Bus;

trait Dispatcher
Expand All @@ -17,25 +18,41 @@ public function runBus(): void
}

/**
* @Then :jobName is dispatched
* @Then job :jobName is dispatched
* @When I dispatch :count :jobName jobs with parameters: :parameters
* @When I dispatch :count :jobName jobs
* @when I dispatch :jobName job
*/
public function jobIsDispatched(string $jobName): void
public function dispatchJobs(string $jobName, int $count = 1, string|array $parameters = []): void
{
$parameters = ArrayHelper::toArray($parameters);

$jobClass = $this->recognizeJobClass($jobName);
$job = new $jobClass(...$parameters);

for ($i = 0; $i < $count; $i++) {
Bus::dispatch($job);
}
}

/**
* @Then I should see :jobName job was dispatched
* @Then I should see :count :jobName jobs were dispatched
*/
public function assertJobsDispatched(string $jobName, int $count = 1): void
{
$jobClass = $this->recognizeJobClass($jobName);
$jobClass::dispatch();
Bus::assertDispatched($jobClass);
Bus::assertDispatched($jobClass, $count);
}

private function recognizeJobClass(string $job): string
private function recognizeJobClass(string $jobName): string
{
if (strpos($job, "\\")) {
return $job;
if (strpos($jobName, "\\")) {
return $jobName;
}

$job = ucfirst($job);
$jobName = ucfirst($jobName);

return $this->getJobNamespace() . $job;
return $this->getJobNamespace() . $jobName;
}

private function getJobNamespace(): string
Expand Down
26 changes: 26 additions & 0 deletions src/Helpers/ArrayHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Blumilk\BLT\Helpers;

class ArrayHelper
{
public static function toArray(string|array $input, string $separator = " "): array
{
if (is_array($input)) {
return $input;
}

return explode($separator, $input);
}

public static function toString(string|array $input, $separator = " "): string
{
if (is_string($input)) {
return $input;
}

return implode($separator, $input);
}
}

0 comments on commit 3cae73c

Please sign in to comment.