Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #34 from juriansluiman/feature/pop-with-metadata
Browse files Browse the repository at this point in the history
Feature/pop with metadata
  • Loading branch information
bakura10 committed May 5, 2014
2 parents 58cb3b5 + 68c81d4 commit 4c7eeee
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/SlmQueueBeanstalkd/Queue/BeanstalkdQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ public function pop(array $options = array())
return null;
}

$data = json_decode($job->getData(), true);
$data = json_decode($job->getData(), true);
$metadata = array('id' => $job->getId()) + $data['metadata'];

return $this->createJob($data['class'], $data['content'], array('id' => $job->getId()));
return $this->createJob($data['class'], $data['content'], $metadata);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testCorrectlyCountJobs()
$routeMatch = new RouteMatch(array('queue' => 'newsletter'));
$controller->getEvent()->setRouteMatch($routeMatch);

$pheanstalkJob = new Pheanstalk_Job(4, '{"class":"SlmQueueBeanstalkdTest\\\Asset\\\SimpleJob","content":"s:3:\"Foo\";"}');
$pheanstalkJob = new Pheanstalk_Job(4, '{"class":"SlmQueueBeanstalkdTest\\\Asset\\\SimpleJob","content":"s:3:\"Foo\";","metadata":[]}');

$this->pheanstalkMock->expects($this->once())
->method('reserveFromTube')
Expand Down
62 changes: 51 additions & 11 deletions tests/SlmQueueBeanstalkdTest/Queue/BeanstalkdQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,76 @@

use PHPUnit_Framework_TestCase as TestCase;
use SlmQueueBeanstalkd\Queue\BeanstalkdQueue;
use SlmQueueBeanstalkdTest\Asset\SimpleJob;
use Pheanstalk_Job;

/**
* BeanstalkdQueue Test
*/
class BeanstalkdQueueTest extends TestCase
{
protected $queueName;
protected $pheanstalk;
protected $pluginManager;

public function setUp()
{
$this->queueName = 'testQueueName';
$this->pheanstalk = $this->getMockBuilder('Pheanstalk_Pheanstalk')
->disableOriginalConstructor()
->getMock();

$this->pluginManager = $this->getMockBuilder('SlmQueue\Job\JobPluginManager')
->disableOriginalConstructor()
->getMock();

$this->queue = new BeanstalkdQueue($this->pheanstalk, $this->queueName, $this->pluginManager);
}

public function testSuccessfulKickWithSelectedTube()
{
$queueName = 'testQueueName';
$maxKick = 10;
$pheanstalk = $this->getMockBuilder('Pheanstalk_Pheanstalk')
->disableOriginalConstructor()
->getMock();
$maxKick = 10;
$queueName = $this->queueName;
$pheanstalk = $this->pheanstalk;

$pheanstalk->expects($this->once())
->method('useTube')
->with($this->equalTo($queueName))
->will($this->returnValue($pheanstalk));

$pheanstalk->expects($this->once())
->method('kick')
->with($this->equalTo($maxKick))
->will($this->returnValue($maxKick));

$jobPluginManager = $this->getMockBuilder('SlmQueue\Job\JobPluginManager')
->disableOriginalConstructor()
->getMock();
$result = $this->queue->kick($maxKick);
$this->assertEquals($result, $maxKick);
}

public function testPopPreservesMetadata()
{
$pheanstalk = $this->pheanstalk;
$queueName = $this->queueName;
$pluginManager = $this->pluginManager;

$queue = new BeanstalkdQueue($pheanstalk, $queueName, $jobPluginManager);
$result = $queue->kick($maxKick);
$job = new SimpleJob;
$job->setMetadata('foo', 'bar');

$this->assertEquals($result, $maxKick);
$pheanstalk_job = new Pheanstalk_Job(1, $job->jsonSerialize());

$pheanstalk->expects($this->once())
->method('reserveFromTube')
->with($this->equalTo($queueName))
->will($this->returnValue($pheanstalk_job));

$pluginManager->expects($this->once())
->method('get')
->with(get_class($job))
->will($this->returnValue($job));

$result = $this->queue->pop();

$this->assertEquals($result, $job);
$this->assertEquals('bar', $job->getMetadata('foo'));
}
}

0 comments on commit 4c7eeee

Please sign in to comment.