VOOZH about

URL: https://deepwiki.com/hypervel/queue/7.2-job-interaction-and-testing

⇱ Job Interaction and Testing | hypervel/queue | DeepWiki


Loading...
Menu

Job Interaction and Testing

Purpose and Scope

This document covers the InteractsWithQueue trait, which provides jobs with methods to manage their own lifecycle (delete, fail, release) and comprehensive testing support through fake queue interactions. Jobs that use this trait gain the ability to control their execution within the queue system and can be easily tested in isolation.

For information about job processing and worker behavior, see Worker System. For information about job lifecycle events, see Events and Extension Points.


Overview

The InteractsWithQueue trait provides a standardized interface for jobs to interact with the queue system and enables testing of job behavior without requiring an actual queue backend. The trait exposes four primary lifecycle operations and a complete testing framework with assertion methods.

Sources: src/InteractsWithQueue.php1-16


Trait Structure


Diagram: Class structure of the InteractsWithQueue trait showing dependencies and relationships

The trait maintains a nullable JobContract reference that is injected by the worker when the job is processed. All lifecycle methods delegate to this job instance.

Sources: src/InteractsWithQueue.php17-24


Job Lifecycle Management

Checking Attempt Count

The attempts() method returns the number of times the job has been attempted. If no job instance is set, it defaults to 1 to allow jobs to function correctly before being queued.


Diagram: Logic flow for the attempts() method

Sources: src/InteractsWithQueue.php29-32

Deleting Jobs

The delete() method removes a job from the queue permanently. This is typically called when a job completes successfully or determines it should not be retried.

MethodParametersBehavior
delete()NoneCalls job->delete() if job instance exists

Sources: src/InteractsWithQueue.php37-43

Failing Jobs

The fail() method marks a job as failed. It accepts either a string message or a Throwable instance. String messages are automatically wrapped in a ManuallyFailedException.

MethodParametersBehavior
fail()string|Throwable|nullConverts strings to ManuallyFailedException, calls job->fail(exception)

Usage Pattern:


Sources: src/InteractsWithQueue.php48-57

Releasing Jobs

The release() method returns a job to the queue with an optional delay before it becomes available again. This is useful for implementing retry logic with backoff strategies or deferring jobs temporarily.

The method accepts three delay types:

  • int: Delay in seconds
  • DateInterval: PHP date interval
  • DateTimeInterface: Specific datetime (calculates seconds until that time)

Sources: src/InteractsWithQueue.php62-72


Testing Support

Enabling Fake Queue Interactions

The withFakeQueueInteractions() method enables testing mode by replacing the real JobContract instance with a FakeJob that tracks all operations without performing actual queue operations.


Diagram: Sequence showing how fake queue interactions work during testing

Sources: src/InteractsWithQueue.php78-82


Assertion Methods

The trait provides six assertion methods for verifying job behavior in tests. All assertions internally call ensureQueueInteractionsHaveBeenFaked() to verify that testing mode is enabled.

Deletion Assertions

MethodPurposeAssertion
assertDeleted()Verify job was deletedPHPUnit::assertTrue($this->job->isDeleted())
assertNotDeleted()Verify job was not deletedPHPUnit::assertTrue(!$this->job->isDeleted())

Sources: src/InteractsWithQueue.php88-112

Failure Assertions

MethodPurposeAssertion
assertFailed()Verify job was manually failedPHPUnit::assertTrue($this->job->hasFailed())
assertNotFailed()Verify job was not manually failedPHPUnit::assertTrue(!$this->job->hasFailed())

Sources: src/InteractsWithQueue.php117-142

Release Assertions

MethodPurposeParametersAssertion
assertReleased()Verify job was releasedOptional delay (int|DateInterval|DateTimeInterface|null)Checks release state and optionally verifies delay matches
assertNotReleased()Verify job was not releasedNonePHPUnit::assertTrue(!$this->job->isReleased())

The assertReleased() method can optionally verify the release delay:


Sources: src/InteractsWithQueue.php147-184


Testing Workflow


Diagram: State diagram showing the testing workflow from setup through assertions

Sources: src/InteractsWithQueue.php78-194


Internal Implementation

FakeJob Guard

The ensureQueueInteractionsHaveBeenFaked() private method enforces that testing mode is enabled before assertions can be used. It throws a RuntimeException if called when the job property is not a FakeJob instance.

Sources: src/InteractsWithQueue.php189-194

Job Instance Injection

The setJob() method allows the worker system to inject the actual JobContract instance when processing a job. This is called automatically by the worker during job execution.

Sources: src/InteractsWithQueue.php199-204


Integration with Job Classes


Diagram: How the InteractsWithQueue trait functions in both production and testing contexts

Sources: src/InteractsWithQueue.php17-204


Common Testing Patterns

Testing Successful Completion


Testing Failure Conditions


Testing Release Logic


Testing Conditional Behavior


Sources: src/InteractsWithQueue.php78-184


Summary Table

FeatureProduction BehaviorTesting Behavior
job propertySet by worker via setJob()Set to FakeJob via withFakeQueueInteractions()
delete()Removes job from queueRecords deletion state in FakeJob
fail()Marks job as failed, logs to FailedJobProviderRecords failure state in FakeJob
release()Returns job to queue with delayRecords release state and delay in FakeJob
AssertionsNot available (throws RuntimeException)Available after withFakeQueueInteractions()
Queue backendRequires actual queue driverNo queue backend needed

Sources: src/InteractsWithQueue.php1-205