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\GetterSetterTrait; |
8
|
|
|
use Dtc\QueueBundle\Tests\StaticJobManager; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
class JobTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
use GetterSetterTrait; |
14
|
|
|
|
15
|
|
|
public function testSetArgs() |
16
|
|
|
{ |
17
|
|
|
$worker = new FibonacciWorker(); |
18
|
|
|
$job = new Job($worker, false, null); |
19
|
|
|
$job->setArgs(array(1)); |
20
|
|
|
$job->setArgs(array(1, array(1, 2))); |
21
|
|
|
|
22
|
|
|
$this->callSetArgs($job, array($job)); |
23
|
|
|
$this->callSetArgs($job, array(1, array($job))); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
View Code Duplication |
protected function callSetArgs(Job $job, $args) |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$failed = false; |
29
|
|
|
try { |
30
|
|
|
$job->setArgs($args); |
31
|
|
|
$failed = true; |
32
|
|
|
} catch (\Exception $e) { |
33
|
|
|
self::assertTrue(true); |
34
|
|
|
} |
35
|
|
|
self::assertFalse($failed); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGettersSetters() |
39
|
|
|
{ |
40
|
|
|
$this->runGetterSetterTests('\Dtc\QueueBundle\Model\Job'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testChainJobCall() |
44
|
|
|
{ |
45
|
|
|
$jobManager = new StaticJobManager(); |
46
|
|
|
$worker = new FibonacciWorker(); |
47
|
|
|
$worker->setJobManager($jobManager); |
48
|
|
|
|
49
|
|
|
$job = new Job($worker, false, null); |
50
|
|
|
self::assertNull($job->getId(), 'Job id should be null'); |
51
|
|
|
|
52
|
|
|
$job->fibonacci(1); |
|
|
|
|
53
|
|
|
self::assertNotNull($job->getId(), 'Job id should be generated'); |
54
|
|
|
|
55
|
|
|
$failed = false; |
56
|
|
|
try { |
57
|
|
|
$job->invalidFunctionCall(); |
|
|
|
|
58
|
|
|
$failed = true; |
59
|
|
|
} catch (\Exception $e) { |
60
|
|
|
self::assertTrue(true); |
61
|
|
|
} |
62
|
|
|
self::assertFalse($failed); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.