Executing jobs within tests:¶
Greater difficulty in test execution: since our environment must provide for the execution of long running processes and ensure they are active during the entire test execution.
Decreased test execution speed: In case tests depend on the result of job execution we would have to wait for their execution by workers, which no matter how reactive they may be cannot be instantaneous; we must also consider that jobs could be scheduled in the future and therefore workers will not be able to execute them until the scheduling date has passed.
Impossibility of depending on jobs scheduled far in the future: If jobs scheduled a few seconds away from the current moment only lead to slower test execution, jobs scheduled far in the future (e.g. the next day or next month) make test execution impossible (we certainly can’t wait so long to finish executing a test).
Recruiter\Recruiter class, which allows the execution, in the current process, of all previously queued jobs.flushJobsSynchronously() and can be called on any instance of the Recruiter\Recruiter class (so not necessarily the same instance used to queue the jobs).<?php
namespace Tests;
use Core\DomainService;
$mongodbInstance = new MongoDB\Client(...);
$recruiter = new Recruiter($mongodbInstance);
$domainService = new DomainService($recruiter);
$domainService->methodThatQueuesJobs();
$recruiter->flushJobsSynchronously(); // Here all previously queued jobs are executed