Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #237 from roelvanduijnhoven/feature/loosen-typehint
Browse files Browse the repository at this point in the history
Prepare for a release that does not allow return status codes of jobs
  • Loading branch information
roelvanduijnhoven authored Nov 25, 2020
2 parents 908fdf6 + 6d5e17f commit b06866b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/Job/JobInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ public function setId($id): JobInterface;
public function getId();

/**
* Execute the job
* Execute the job.
*
* TODO Deprecate the usage of int as return value, and introduce exceptions as part of the API to signal a
* non-success result.
*
* @return void|?int Omitting return value, or returning `null` means the job was successful. Otherwise the int
* returned will represent.
*/
public function execute(): ?int;
public function execute();
}
16 changes: 16 additions & 0 deletions tests/Asset/JobWithNoReturnType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace SlmQueueTest\Asset;

use SlmQueue\Job\AbstractJob;

class JobWithNoReturnType extends AbstractJob
{
public function execute()
{
// Just set some stupid metadata
$this->setMetadata('foo', 'bar');

return 999;
}
}
16 changes: 16 additions & 0 deletions tests/Asset/JobWithVoidReturnType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace SlmQueueTest\Asset;

use SlmQueue\Job\AbstractJob;

class JobWithVoidReturnType extends AbstractJob
{
public function execute()
{
// Just set some stupid metadata
$this->setMetadata('foo', 'bar');

return 999;
}
}
24 changes: 20 additions & 4 deletions tests/Strategy/ProcessQueueStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

use Laminas\EventManager\EventManagerInterface;
use PHPUnit\Framework\TestCase;
use SlmQueue\Job\JobInterface;
use SlmQueue\Queue\QueueInterface;
use SlmQueue\Strategy\AbstractStrategy;
use SlmQueue\Strategy\ProcessQueueStrategy;
use SlmQueue\Worker\Event\ProcessJobEvent;
use SlmQueue\Worker\Event\ProcessQueueEvent;
use SlmQueue\Worker\Event\WorkerEventInterface;
use SlmQueue\Worker\Result\ExitWorkerLoopResult;
use SlmQueueTest\Asset\JobWithNoReturnType;
use SlmQueueTest\Asset\JobWithVoidReturnType;
use SlmQueueTest\Asset\SimpleJob;
use SlmQueueTest\Asset\SimpleWorker;

Expand Down Expand Up @@ -101,9 +104,11 @@ function ($e) use (&$triggeredIdle) {
static::assertTrue($event->propagationIsStopped(), "EventPropagation should be stopped");
}

public function testJobPopWithJobTriggersProcessJobEvent(): void
/**
* @dataProvider jobsProvider
*/
public function testJobPopWithJobTriggersProcessJobEvent(JobInterface $job): void
{
$job = new SimpleJob();
$popOptions = [];
$this->queue->expects($this->at(0))
->method('pop')
Expand All @@ -127,9 +132,11 @@ function ($e) use (&$triggeredProcessJobEvent) {
static::assertFalse($event->propagationIsStopped(), "EventPropagation should not be stopped");
}

public function testOnJobProcess(): void
/**
* @dataProvider jobsProvider
*/
public function testOnJobProcess(JobInterface $job): void
{
$job = new SimpleJob();
$event = new ProcessJobEvent($job, $this->worker, $this->queue);

$this->listener->onJobProcess($event);
Expand All @@ -138,4 +145,13 @@ public function testOnJobProcess(): void
static::assertEquals($job, $event->getJob());
static::assertSame('bar', $event->getJob()->getMetadata('foo'));
}

public function jobsProvider(): array
{
return [
[new SimpleJob()],
[new JobWithNoReturnType()],
[new JobWithVoidReturnType()],
];
}
}

0 comments on commit b06866b

Please sign in to comment.