diff --git a/lib/Resque/Job.php b/lib/Resque/Job.php index dfd8869..6c39e18 100755 --- a/lib/Resque/Job.php +++ b/lib/Resque/Job.php @@ -6,7 +6,7 @@ * @author Chris Boulton * @license http://www.opensource.org/licenses/mit-license.php */ -class Resque_Job +class Resque_Job implements Resque_JobInterface { /** * @var string The name of the queue that this job belongs to. @@ -24,10 +24,15 @@ class Resque_Job public $payload; /** - * @var object Instance of the class performing work for this job. + * @var Resque_JobInterface Instance of the class performing work for this job. */ private $instance; + /** + * @var Resque_Job_FactoryInterface + */ + private $jobFactory; + /** * Instantiate a new instance of a job. * @@ -40,17 +45,18 @@ class Resque_Job $this->payload = $payload; } - /** - * Create a new job and save it to the specified queue. - * - * @param string $queue The name of the queue to place the job in. - * @param string $class The name of the class that contains the code to execute the job. - * @param array $args Any optional arguments that should be passed when the job is executed. - * @param boolean $monitor Set to true to be able to monitor the status of a job. - * @param string $id Unique identifier for tracking the job. Generated if not supplied. - * - * @return string - */ + /** + * Create a new job and save it to the specified queue. + * + * @param string $queue The name of the queue to place the job in. + * @param string $class The name of the class that contains the code to execute the job. + * @param array $args Any optional arguments that should be passed when the job is executed. + * @param boolean $monitor Set to true to be able to monitor the status of a job. + * @param string $id Unique identifier for tracking the job. Generated if not supplied. + * + * @return string + * @throws \InvalidArgumentException + */ public static function create($queue, $class, $args = null, $monitor = false, $id = null) { if (is_null($id)) { @@ -81,7 +87,7 @@ class Resque_Job * instance of Resque_Job for it. * * @param string $queue The name of the queue to check for a job in. - * @return null|object Null when there aren't any waiting jobs, instance of Resque_Job when a job was found. + * @return false|object Null when there aren't any waiting jobs, instance of Resque_Job when a job was found. */ public static function reserve($queue) { @@ -99,7 +105,7 @@ class Resque_Job * * @param array $queues * @param int $timeout - * @return null|object Null when there aren't any waiting jobs, instance of Resque_Job when a job was found. + * @return false|object Null when there aren't any waiting jobs, instance of Resque_Job when a job was found. */ public static function reserveBlocking(array $queues, $timeout = null) { @@ -152,11 +158,11 @@ class Resque_Job return $this->payload['args'][0]; } - /** - * Get the instantiated object for this job that will be performing work. - * - * @return object Instance of the object that this job belongs to. - */ + /** + * Get the instantiated object for this job that will be performing work. + * @return Resque_JobInterface Instance of the object that this job belongs to. + * @throws Resque_Exception + */ public function getInstance() { if (!is_null($this->instance)) { @@ -175,7 +181,11 @@ class Resque_Job ); } - $this->instance = new $this->payload['class']; + if ($this->jobFactory !== null) { + $this->instance = $this->jobFactory->create(); + } else { + $this->instance = new $this->payload['class']; + } $this->instance->job = $this; $this->instance->args = $this->getArguments(); $this->instance->queue = $this->queue; @@ -272,4 +282,15 @@ class Resque_Job } return '(' . implode(' | ', $name) . ')'; } + + /** + * @param Resque_Job_FactoryInterface $jobFactory + * @return Resque_Job + */ + public function setJobFactory(Resque_Job_FactoryInterface $jobFactory) + { + $this->jobFactory = $jobFactory; + + return $this; + } } diff --git a/lib/Resque/Job/FactoryInterface.php b/lib/Resque/Job/FactoryInterface.php new file mode 100644 index 0000000..9a2b14c --- /dev/null +++ b/lib/Resque/Job/FactoryInterface.php @@ -0,0 +1,9 @@ +assertEquals(Resque::size($queue), 2); } + public function testUseFactoryToGetJobInstance() + { + $payload = array( + 'class' => Some_Job_Class::class, + 'args' => null + ); + $job = new Resque_Job('jobs', $payload); + $factory = $this->getMock(Resque_Job_FactoryInterface::class); + $job->setJobFactory($factory); + $testJob = $this->getMock(Resque_JobInterface::class); + $factory->expects(self::once())->method('create')->will($this->returnValue($testJob)); + $instance = $job->getInstance(); + $this->assertInstanceOf(Resque_JobInterface::class, $instance); + } + + public function testDoNotUseFactoryToGetInstance() + { + $payload = array( + 'class' => Some_Job_Class::class, + 'args' => null + ); + $job = new Resque_Job('jobs', $payload); + $factory = $this->getMock(Resque_Job_FactoryInterface::class); + $testJob = $this->getMock(Resque_JobInterface::class); + $factory->expects(self::never())->method('create')->will(self::returnValue($testJob)); + $instance = $job->getInstance(); + $this->assertInstanceOf(Resque_JobInterface::class, $instance); + } +} + +class Some_Job_Class implements Resque_JobInterface +{ + + /** + * @return bool + */ + public function perform() + { + return true; + } } diff --git a/test/Resque/Tests/TestCase.php b/test/Resque/Tests/TestCase.php index 4ed65de..c828f6f 100644 --- a/test/Resque/Tests/TestCase.php +++ b/test/Resque/Tests/TestCase.php @@ -11,6 +11,11 @@ class Resque_Tests_TestCase extends PHPUnit_Framework_TestCase protected $resque; protected $redis; + public static function setUpBeforeClass() + { + date_default_timezone_set('UTC'); + } + public function setUp() { $config = file_get_contents(REDIS_CONF); @@ -20,4 +25,4 @@ class Resque_Tests_TestCase extends PHPUnit_Framework_TestCase // Flush redis $this->redis->flushAll(); } -} \ No newline at end of file +}