|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Tests\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Dtc\QueueBundle\Model\Job; |
|
6
|
|
|
use Dtc\QueueBundle\Tests\FibonacciWorker; |
|
7
|
|
|
use Dtc\QueueBundle\Tests\StaticJobManager; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
class JobTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
public function testSetArgs() |
|
13
|
|
|
{ |
|
14
|
|
|
$worker = new FibonacciWorker(); |
|
15
|
|
|
$job = new Job($worker, false, null); |
|
16
|
|
|
$job->setArgs(array(1)); |
|
17
|
|
|
$job->setArgs(array(1, array(1, 2))); |
|
18
|
|
|
|
|
19
|
|
|
try { |
|
20
|
|
|
$job->setArgs(array($job)); |
|
21
|
|
|
$this->fail('Invalid job argument passed'); |
|
22
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
try { |
|
26
|
|
|
$job->setArgs(array(1, array($job))); |
|
27
|
|
|
$this->fail('Invalid job argument passed'); |
|
28
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testGettersSetters() |
|
33
|
|
|
{ |
|
34
|
|
|
$reflection = new \ReflectionClass('\Dtc\QueueBundle\Model\Job'); |
|
35
|
|
|
$properties = $reflection->getProperties(); |
|
36
|
|
|
foreach ($properties as $property) { |
|
37
|
|
|
$name = $property->getName(); |
|
38
|
|
|
$getMethod = 'get'.ucfirst($name); |
|
39
|
|
|
$setMethod = 'set'.ucfirst($name); |
|
40
|
|
|
self::assertTrue($reflection->hasMethod($getMethod), $getMethod); |
|
41
|
|
|
self::assertTrue($reflection->hasMethod($setMethod), $setMethod); |
|
42
|
|
|
|
|
43
|
|
|
$job = new Job(); |
|
44
|
|
|
|
|
45
|
|
|
$parameters = $reflection->getMethod($setMethod)->getParameters(); |
|
46
|
|
|
if ($parameters && count($parameters) == 1) { |
|
47
|
|
|
$parameter = $parameters[0]; |
|
48
|
|
|
if (!$parameter->getClass()) { |
|
49
|
|
|
$someValue = 'somevalue'; |
|
50
|
|
|
$job->$setMethod($someValue); |
|
51
|
|
|
self::assertSame($someValue, $job->$getMethod(), "$setMethod, $getMethod"); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testChainJobCall() |
|
58
|
|
|
{ |
|
59
|
|
|
$jobManager = new StaticJobManager(); |
|
60
|
|
|
$worker = new FibonacciWorker(); |
|
61
|
|
|
$worker->setJobManager($jobManager); |
|
62
|
|
|
|
|
63
|
|
|
$job = new Job($worker, false, null); |
|
64
|
|
|
$this->assertNull($job->getId(), 'Job id should be null'); |
|
65
|
|
|
|
|
66
|
|
|
$job->fibonacci(1); |
|
|
|
|
|
|
67
|
|
|
$this->assertNotNull($job->getId(), 'Job id should be generated'); |
|
68
|
|
|
|
|
69
|
|
|
try { |
|
70
|
|
|
$job->invalidFunctionCall(); |
|
|
|
|
|
|
71
|
|
|
$this->fail('invalid chain, should fail'); |
|
72
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|