Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #135 from basz/finish-id
Browse files Browse the repository at this point in the history
Finish
  • Loading branch information
bakura10 committed Oct 9, 2014
2 parents fe98c60 + 9d7ac58 commit 9cac173
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
12 changes: 9 additions & 3 deletions src/SlmQueue/Job/JobPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

/**
* JobPluginManager
*
* @method JobInterface get($name)
*/
class JobPluginManager extends AbstractPluginManager
{
Expand All @@ -16,11 +14,19 @@ class JobPluginManager extends AbstractPluginManager
*/
protected $shareByDefault = false;

/**
* @inheritdoc
*
* @param string $name
* @param array $options
* @param bool $usePeeringServiceManagers
* @return JobInterface
*/
public function get($name, $options = array(), $usePeeringServiceManagers = true)
{
// parent::get calls validatePlugin() so we're sure $instance is a JobInterface
$instance = parent::get($name, $options, $usePeeringServiceManagers);
$instance->setMetadata('name', $name);
$instance->setMetadata('__name__', $name);

return $instance;
}
Expand Down
6 changes: 3 additions & 3 deletions src/SlmQueue/Queue/AbstractQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getJobPluginManager()
public function unserializeJob($string, array $metadata = array())
{
$data = json_decode($string, true);
$name = $data['__name__'];
$name = $data['metadata']['__name__'];
$metadata += $data['metadata'];
$content = unserialize($data['content']);

Expand Down Expand Up @@ -91,9 +91,9 @@ public function unserializeJob($string, array $metadata = array())
*/
public function serializeJob(JobInterface $job)
{
$name = $job->getMetadata('__name__');
$job->setMetadata('__name__', $job->getMetadata('__name__', get_class($job)));

$data = array(
'__name__' => $name ?: get_class($job),
'content' => serialize($job->getContent()),
'metadata' => $job->getMetadata()
);
Expand Down
2 changes: 1 addition & 1 deletion tests/SlmQueueTest/Job/JobPluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testPluginManagerSetsServiceNameAsMetadata()
$instance = $jobPluginManager->get('SimpleJob');

$this->assertInstanceOf('SlmQueueTest\Asset\SimpleJob', $instance);
$this->assertEquals('SimpleJob', $instance->getMetadata('name'));
$this->assertEquals('SimpleJob', $instance->getMetadata('__name__'));
}

public function testPluginManagerThrowsExceptionOnInvalidJobClasses()
Expand Down
21 changes: 12 additions & 9 deletions tests/SlmQueueTest/Queue/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ public function testCanPushThenPopWithJobMetadata()
$this->queue->push($this->job);
$job = $this->queue->pop();

$this->assertEquals(array('Foo' => 'Bar'), $job->getMetadata());
// metadata will have reserved __name__ key with FQCN
$expected = array('Foo' => 'Bar') + array('__name__' => 'SlmQueueTest\Asset\SimpleJob');

$this->assertEquals($expected, $job->getMetadata());
$this->assertEquals('Bar', $job->getMetadata('Foo'));
}

Expand All @@ -77,7 +80,7 @@ public function testCorrectlySerializeJobContent()
$job = new SimpleJob();
$job->setContent('Foo');

$expected = '{"__name__":"SlmQueueTest\\\Asset\\\SimpleJob","content":"s:3:\"Foo\";","metadata":[]}';
$expected = '{"content":"s:3:\"Foo\";","metadata":{"__name__":"SlmQueueTest\\\Asset\\\SimpleJob"}}';
$actual = $this->queue->serializeJob($job);

$this->assertEquals($expected, $actual);
Expand All @@ -88,7 +91,7 @@ public function testCorrectlySerializeJobMetadata()
$job = new SimpleJob();
$job->setMetadata('Foo', 'Bar');

$expected = '{"__name__":"SlmQueueTest\\\Asset\\\SimpleJob","content":"N;","metadata":{"Foo":"Bar"}}';
$expected = '{"content":"N;","metadata":{"Foo":"Bar","__name__":"SlmQueueTest\\\Asset\\\SimpleJob"}}';
$actual = $this->queue->serializeJob($job);

$this->assertEquals($expected, $actual);
Expand All @@ -100,7 +103,7 @@ public function testCorrectlySerializeJobContentAndMetadata()
$job->setContent('Foo');
$job->setMetadata('Foo', 'Bar');

$expected = '{"__name__":"SlmQueueTest\\\Asset\\\SimpleJob","content":"s:3:\"Foo\";","metadata":{"Foo":"Bar"}}';
$expected = '{"content":"s:3:\"Foo\";","metadata":{"Foo":"Bar","__name__":"SlmQueueTest\\\Asset\\\SimpleJob"}}';
$actual = $this->queue->serializeJob($job);

$this->assertEquals($expected, $actual);
Expand All @@ -111,7 +114,7 @@ public function testCorrectlySerializeJobServiceName()
$job = new SimpleJob();
$job->setMetadata('__name__', 'SimpleJob');

$expected = '{"__name__":"SimpleJob","content":"N;","metadata":{"__name__":"SimpleJob"}}';
$expected = '{"content":"N;","metadata":{"__name__":"SimpleJob"}}';
$actual = $this->queue->serializeJob($job);

$this->assertEquals($expected, $actual);
Expand All @@ -124,7 +127,7 @@ public function testCanCreateJobWithFQCN()
->with($this->jobName)
->will($this->returnValue($this->job));

$payload = '{"__name__":"SlmQueueTest\\\Asset\\\SimpleJob","content":"N;","metadata":[]}';
$payload = '{"content":"N;","metadata":{"__name__":"SlmQueueTest\\\Asset\\\SimpleJob"}}';
$job = $this->queue->unserializeJob($payload);

$expected = spl_object_hash($this->job);
Expand All @@ -139,7 +142,7 @@ public function testCanCreateJobWithStringName()
->with('SimpleJob')
->will($this->returnValue($this->job));

$payload = '{"__name__":"SimpleJob","content":"N;","metadata":[]}';
$payload = '{"content":"N;","metadata":{"__name__":"SimpleJob"}}';
$job = $this->queue->unserializeJob($payload);

$expected = spl_object_hash($this->job);
Expand All @@ -154,7 +157,7 @@ public function testCanCreateJobWithContent()
->with($this->jobName)
->will($this->returnValue($this->job));

$payload = '{"__name__":"SlmQueueTest\\\Asset\\\SimpleJob","content":"s:3:\"Foo\";","metadata":[]}';
$payload = '{"content":"s:3:\"Foo\";","metadata":{"__name__":"SlmQueueTest\\\Asset\\\SimpleJob"}}';
$job = $this->queue->unserializeJob($payload);

$this->assertEquals('Foo', $job->getContent());
Expand All @@ -167,7 +170,7 @@ public function testCanCreateJobWithMetadata()
->with($this->jobName)
->will($this->returnValue($this->job));

$payload = '{"__name__":"SlmQueueTest\\\Asset\\\SimpleJob","content":"N;","metadata":{"Foo":"Bar"}}';
$payload = '{"content":"N;","metadata":{"Foo":"Bar","__name__":"SlmQueueTest\\\Asset\\\SimpleJob"}}';
$job = $this->queue->unserializeJob($payload);

$this->assertEquals('Bar', $job->getMetadata('Foo'));
Expand Down

0 comments on commit 9cac173

Please sign in to comment.