Skip to content

Commit

Permalink
Adding instance check in JobMarshaler
Browse files Browse the repository at this point in the history
  • Loading branch information
mariano committed May 12, 2015
1 parent fc408af commit bc43336
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/Queue/Marshal/JobMarshaler.php
Expand Up @@ -30,6 +30,9 @@ public function unmarshal($source)
*/
public function marshal(JobInterface $job)
{
if (!($job instanceof Job)) {
throw new MarshalException(get_class($job) . ' is not a ' . Job::class);
}
return json_encode($job->getBody());
}
}
24 changes: 20 additions & 4 deletions tests/Queue/Marshaler/JobMarshalerTest.php
Expand Up @@ -2,43 +2,59 @@
namespace Disque\Test\Queue\Marshaler;

use Disque\Queue\Job;
use Disque\Queue\JobInterface;
use Disque\Queue\Marshal\JobMarshaler;
use Disque\Queue\Marshal\MarshalerInterface;
use Disque\Queue\Marshal\MarshalException;
use Mockery as m;
use PHPUnit_Framework_TestCase;

class JobMarshalerTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
parent::tearDown();
m::close();
}

public function testInstance()
{
$m = new JobMarshaler();
$this->assertInstanceOf(MarshalerInterface::class, $m);
}

public function testBodyEmpty()
public function testMarshalInvalid()
{
$job = m::mock(JobInterface::class);
$this->setExpectedException(MarshalException::class, get_class($job) . ' is not a ' . Job::class);
$m = new JobMarshaler();
$m->marshal($job);
}

public function testMarshalEmpty()
{
$m = new JobMarshaler();
$j = new Job();
$result = $m->marshal($j);
$this->assertSame('[]', $result);
}

public function testBodyNotEmpty()
public function testMarshalNotEmpty()
{
$m = new JobMarshaler();
$j = new Job(['test' => 'stuff']);
$result = $m->marshal($j);
$this->assertSame('{"test":"stuff"}', $result);
}

public function testLoadInvalid()
public function testUnmarshalInvalid()
{
$this->setExpectedException(MarshalException::class, 'Could not deserialize {"wrong"!');
$m = new JobMarshaler();
$m->unmarshal('{"wrong"!');
}

public function testLoadEmpty()
public function testUnmarshalEmpty()
{
$m = new JobMarshaler();
$j = $m->unmarshal('{"test":"stuff"}');
Expand Down

0 comments on commit bc43336

Please sign in to comment.