Skip to content

Commit

Permalink
Serialization tests for Job class (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
dafeder authored and fmizzell committed Aug 6, 2019
1 parent 1dfcf24 commit dba614f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/Job/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ public function setTimeLimit(int $seconds)

public function unsetTimeLimit()
{
unset($this->timeLimit);
$this->timeLimit = null;
}

public function getTimeLimit()
{
return $this->timeLimit;
}

public function getState()
Expand Down Expand Up @@ -84,27 +89,23 @@ public function setStateProperty($property, $value)

public function jsonSerialize()
{
return (object) ['timeLimit' => $this->timeLimit, 'result' => $this->getResult()];
return (object) [
'timeLimit' => $this->timeLimit,
'result' => $this->getResult()->jsonSerialize()
];
}

public static function hydrate($json)
{
/**
* Hydrate an object from the json created by jsonSerialize().
* You will want to override this method when implementing specific jobs.
* You can use this function for the initial JSON decoding by calling
* parent::hydrate() in your implementation.
*
* @param string $json
* JSON string used to hydrate a new instance of the class.
*/
public static function hydrate($json) {
$data = json_decode($json);

$reflector = new \ReflectionClass(self::class);
$object = $reflector->newInstanceWithoutConstructor();

$reflector = new \ReflectionClass($object);

$p = $reflector->getProperty('timeLimit');
$p->setAccessible(true);
$p->setValue($object, $data->timeLimit);

$class = $reflector->getParentClass();
$p = $class->getProperty('result');
$p->setAccessible(true);
$p->setValue($object, Result::hydrate(json_encode($data->result)));

return $object;
return $data;
}
}
22 changes: 22 additions & 0 deletions src/Job/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Procrastinator\Job;

use Procrastinator\Result;

class Method extends Job
{
private $object;
Expand All @@ -19,4 +21,24 @@ protected function runIt()
{
return call_user_func([$this->object, $this->methodName]);
}

public static function hydrate($json): Method
{
$data = parent::hydrate($json);

$reflector = new \ReflectionClass(self::class);
$object = $reflector->newInstanceWithoutConstructor();

$reflector = new \ReflectionClass($object);

$p = $reflector->getParentClass()->getProperty('timeLimit');
$p->setAccessible(true);
$p->setValue($object, $data->timeLimit);

$p = $reflector->getParentClass()->getProperty('result');
$p->setAccessible(true);
$p->setValue($object, Result::hydrate(json_encode($data->result)));

return $object;
}
}
42 changes: 42 additions & 0 deletions test/Job/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ public function testError()
$this->assertEquals("I always fail", $result->getError());
}

public function testTimeLimit()
{
$timeLimit = 10;
$job = new Method($this, "callError");
$job->setTimeLimit($timeLimit);
$result = $job->run();
$this->assertTimeLimit($job, $timeLimit);

$job->unsetTimeLimit();
$this->assertTimeLimit($job, null);
}

private function assertTimeLimit(Job $job, $timeLimit)
{
$r = new \ReflectionObject($job);
$p = $r->getParentClass()->getProperty('timeLimit');
$p->setAccessible(true);
$this->assertEquals($timeLimit, $p->getValue($job));
}

public function testReturn()
{
$job = new Method($this, "callReturn");
Expand All @@ -43,6 +63,28 @@ public function testTwoStage()
$this->assertEquals(json_encode(['a', 'b', 'c', 'd']), $result->getData());
}

public function testSerialization()
{
$statePropertyA = 1;
$statePropertyB = 2;
$timeLimit = 10;

$job = new Method($this, "callMe");
$job->setTimeLimit($timeLimit);
$job->setStateProperty('a', $statePropertyA);
$job->setStateProperty('b', $statePropertyB);
$job->run();

$json = json_encode($job->jsonSerialize());

$job2 = Method::hydrate($json);

$this->assertEquals($statePropertyA, $job2->getStateProperty('a'));
$this->assertEquals($statePropertyB, $job2->getStateProperty('b'));

$this->assertTimeLimit($job2, $timeLimit);
}

public function callMe()
{
}
Expand Down

0 comments on commit dba614f

Please sign in to comment.