Skip to content

Commit

Permalink
The 'perform' method for a task can now be passed the Job
Browse files Browse the repository at this point in the history
If you typecast the first parameter to the 'perform' method with the JobInterface,
it will be included for your pleasure.
  • Loading branch information
justinrainbow committed Jul 26, 2012
1 parent 97c880c commit 1858f7b
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 83 deletions.
70 changes: 49 additions & 21 deletions src/Presque/Job/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,6 @@ public function __construct($class, array $args = array())

$method = $refl->getMethod('perform');

if (!$method->isPublic()) {
$visibility = 'unknown';
if ($method->isPrivate()) {
$visibility = 'private';
} elseif ($method->isProtected()) {
$visibility = 'protected';
}

throw new InvalidArgumentException(
'The "perform" method for class "' . $class . '" must be public, not ' . $visibility
);
}

if (count($args) < $method->getNumberOfRequiredParameters()) {
throw new InvalidArgumentException(
'The ' . $class . ' has ' . $method->getNumberOfRequiredParameters() . ' required '.
'arguments, but only ' . count($args) . ' were provided.'
);
}

$this->class = $class;
$this->args = $args;

Expand Down Expand Up @@ -128,9 +108,13 @@ public function getInstance()
public function perform()
{
try {
$args = $this->getArguments();
if ($this->isFirstParameterJobInterface()) {
array_unshift($args, $this);
}
$this->lastResult = $this->reflMethod->invokeArgs(
$this->getInstance(),
$this->getArguments()
$args
);

$this->setStatus(StatusInterface::SUCCESS);
Expand All @@ -142,8 +126,52 @@ public function perform()
return $this;
}

/**
* {@inheritDoc}
*/
public function validate()
{
if (!$this->reflMethod->isPublic()) {
$visibility = 'unknown';
if ($this->reflMethod->isPrivate()) {
$visibility = 'private';
} elseif ($this->reflMethod->isProtected()) {
$visibility = 'protected';
}

throw new InvalidArgumentException(
'The "perform" method for class "' . $this->class . '" must be public, not ' . $visibility
);
}

$numberOfRequiredParameters = $this->reflMethod->getNumberOfRequiredParameters();
if ($this->isFirstParameterJobInterface()) {
$numberOfRequiredParameters--;
}

if (count($this->args) < $numberOfRequiredParameters) {
throw new InvalidArgumentException(
'The ' . $this->class . ' has ' . $numberOfRequiredParameters . ' required '.
'arguments, but only ' . count($this->args) . ' were provided.'
);
}

return true;
}

public function __toString()
{
return (string) $this->getClass();
}

protected function isFirstParameterJobInterface()
{
$params = $this->reflMethod->getParameters();

if (0 < count($params)) {
return $params[0]->getClass() && $params[0]->getClass()->implementsInterface('Presque\\Job\\JobInterface');
}

return false;
}
}
22 changes: 16 additions & 6 deletions tests/Presque/Tests/Job/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,53 @@ class JobTest extends TestCase
*/
public function testCreatingAnInvalidJob()
{
Job::create('NonExistant\Class', array(1, 2, 3));
Job::create('NonExistant\Class', array(1, 2, 3))->validate();
}

/**
* @expectedException Presque\Exception\InvalidArgumentException
*/
public function testCreatingASimpleJobWithNoArguments()
{
Job::create('Presque\Tests\Jobs\SimpleJob');
Job::create('Presque\Tests\Jobs\SimpleJob')->validate();
}

/**
* @expectedException Presque\Exception\InvalidArgumentException
*/
public function testCreatingASimpleJobWithTooFewArguments()
{
Job::create('Presque\Tests\Jobs\SimpleJob', array(1));
Job::create('Presque\Tests\Jobs\SimpleJob', array(1))->validate();
}

/**
* @expectedException Presque\Exception\InvalidArgumentException
*/
public function testCreatingAJobWithInvalidPerformMethod()
{
Job::create('Presque\Tests\Jobs\InvalidJob');
Job::create('Presque\Tests\Jobs\InvalidJob')->validate();
}

/**
* @expectedException Presque\Exception\InvalidArgumentException
*/
public function testCreatingAJobWithPrivatePerformMethod()
{
Job::create('Presque\Tests\Jobs\PrivateJob');
Job::create('Presque\Tests\Jobs\PrivateJob')->validate();
}

/**
* @expectedException Presque\Exception\InvalidArgumentException
*/
public function testCreatingAnIncompleteJob()
{
Job::create('Presque\Tests\Jobs\IncompleteJob');
Job::create('Presque\Tests\Jobs\IncompleteJob')->validate();
}

public function testCreatingASimpleJob()
{
$job = Job::create('Presque\Tests\Jobs\SimpleJob', array('simple', 'jobs'));
$job->validate();

try {
$job->perform();
Expand All @@ -86,4 +87,13 @@ public function testCreatingASimpleJob()
$this->assertTrue($job->perform()->isSuccessful());
}

public function testSendingJobEventToInstance()
{
$job = Job::create('Presque\Tests\Jobs\EventedJob');
$job->validate();
$job->perform();

$this->assertTrue($job->isSuccessful());
}

}
16 changes: 0 additions & 16 deletions tests/Presque/Tests/Jobs/IncompleteJob.php

This file was deleted.

20 changes: 0 additions & 20 deletions tests/Presque/Tests/Jobs/InvalidJob.php

This file was deleted.

20 changes: 0 additions & 20 deletions tests/Presque/Tests/Jobs/PrivateJob.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,35 @@

namespace Presque\Tests\Jobs;

use Presque\Job\JobInterface;

class EventedJob
{
public function perform(JobInterface $job)
{
}
}

class IncompleteJob
{
}

class InvalidJob
{
protected function perform()
{
throw new \RuntimeException('The InvalidJob should never be performed!');
}
}

class PrivateJob
{
private function perform()
{
throw new \RuntimeException('The PrivateJob should never be performed!');
}
}

class SimpleJob
{
public function perform($arg1, $arg2)
Expand Down
3 changes: 3 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@
$loader = require_once __DIR__.'/../vendor/autoload.php';
$loader->add('Presque\Tests', __DIR__);
$loader->register();

// The test jobs are little... so they are all in one file
require_once __DIR__.'/Presque/Tests/Jobs/TestJobs.php';

0 comments on commit 1858f7b

Please sign in to comment.