diff --git a/src/Job/Job.php b/src/Job/Job.php index e1eedbe..c400cdb 100644 --- a/src/Job/Job.php +++ b/src/Job/Job.php @@ -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() @@ -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; } } diff --git a/src/Job/Method.php b/src/Job/Method.php index 1f60a49..8b63ee5 100644 --- a/src/Job/Method.php +++ b/src/Job/Method.php @@ -3,6 +3,8 @@ namespace Procrastinator\Job; +use Procrastinator\Result; + class Method extends Job { private $object; @@ -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; + } } diff --git a/test/Job/JobTest.php b/test/Job/JobTest.php index a236047..1a19a69 100644 --- a/test/Job/JobTest.php +++ b/test/Job/JobTest.php @@ -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"); @@ -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() { }