Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"cakephp/migrations": "^4.0.1",
"dereuromark/cakephp-ide-helper": "^2.0.0",
"dereuromark/cakephp-tools": "^3.0.0",
"dereuromark/cakephp-dto": "^2.1.0",
"fig-r/psr2r-sniffer": "dev-next",
"friendsofcake/search": "^7.0.0",
"phpunit/phpunit": "^10.1"
Expand Down
19 changes: 15 additions & 4 deletions src/Model/Table/QueuedJobsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Cake\ORM\Query\SelectQuery;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use CakeDto\Dto\FromArrayToArrayInterface;
use InvalidArgumentException;
use Queue\Config\JobConfig;
use Queue\Model\Entity\QueuedJob;
Expand Down Expand Up @@ -217,18 +218,28 @@ public function createConfig(): JobConfig {
* - notBefore: Optional date which must not be preceded
* - group: Used to group similar QueuedJobs
* - reference: An optional reference string
* - status: To set an initial status text
*
* @param string $jobTask Job task name or FQCN
* @param array<string, mixed>|null $data Array of data
* @param \Queue\Config\JobConfig|array<string, mixed> $config Config to save along with the job
* @param string $jobTask Job task name or FQCN.
* @param object|array<string, mixed>|null $data Array of data or DTO like object.
* @param \Queue\Config\JobConfig|array<string, mixed> $config Config to save along with the job.
*
* @return \Queue\Model\Entity\QueuedJob Saved job entity
*/
public function createJob(string $jobTask, ?array $data = null, array|JobConfig $config = []): QueuedJob {
public function createJob(string $jobTask, array|object|null $data = null, array|JobConfig $config = []): QueuedJob {
if (!$config instanceof JobConfig) {
$config = $this->createConfig()->fromArray($config);
}

if ($data instanceof FromArrayToArrayInterface) {
$data = $data->toArray();
} elseif (is_object($data) && method_exists($data, 'toArray')) {
$data = $data->toArray();
}
if ($data !== null && !is_array($data)) {
throw new InvalidArgumentException('Data must be `array|null`, implement `' . FromArrayToArrayInterface::class . '` or provide a `toArray()` method');
}

$queuedJob = [
'job_task' => $this->jobTask($jobTask),
'data' => $data,
Expand Down
41 changes: 41 additions & 0 deletions tests/TestCase/Model/Table/QueuedJobsTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Cake\TestSuite\TestCase;
use Queue\Model\Table\QueuedJobsTable;
use Queue\Queue\Task\ExampleTask;
use TestApp\Dto\MyTaskDto;

/**
* Queue\Model\Table\QueuedJobsTable Test Case
Expand Down Expand Up @@ -223,6 +224,46 @@ public function testCreateWithSkipExistenceCheck() {
$this->assertTrue((bool)$queuedJob);
}

/**
* @return void
*/
public function testCreateWithDto() {
$array = [
'some' => 'random',
'test' => 'data',
];
$dto = new MyTaskDto($array);

$queuedJob = $this->QueuedJobs->createJob(ExampleTask::class, $dto);
$this->assertTrue((bool)$queuedJob);
$this->assertSame($array, $queuedJob->data);
}

/**
* @return void
*/
public function testCreateWithObject() {
$array = [
'some' => 'random',
'test' => 'data',
];
$dto = new class() {
/**
* @return string[]
*/
public function toArray(): array {
return [
'some' => 'random',
'test' => 'data',
];
}
};

$queuedJob = $this->QueuedJobs->createJob(ExampleTask::class, $dto);
$this->assertTrue((bool)$queuedJob);
$this->assertSame($array, $queuedJob->data);
}

/**
* Test the delivery of jobs in sequence, skipping fetched but not completed tasks.
*
Expand Down
42 changes: 42 additions & 0 deletions tests/test_app/src/Dto/MyTaskDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);

namespace TestApp\Dto;

use CakeDto\Dto\FromArrayToArrayInterface;

class MyTaskDto implements FromArrayToArrayInterface {

protected array $data;

/**
* @param array $data
*/
public function __construct(array $data) {
$this->data = $data;
}

/**
* @return string
*/
public function getFoo(): string {
return 'foo';
}

/**
* @param array $array
*
* @return static
*/
public static function createFromArray(array $array): static {
return new static($array);
}

/**
* @return array
*/
public function toArray(): array {
return $this->data;
}

}