1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Tests\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Dtc\QueueBundle\DependencyInjection\Compiler\WorkerCompilerPass; |
6
|
|
|
use Dtc\QueueBundle\EventDispatcher\EventDispatcher; |
7
|
|
|
use Dtc\QueueBundle\Model\WorkerManager; |
8
|
|
|
use Dtc\QueueBundle\ODM\JobManager; |
9
|
|
|
use Dtc\QueueBundle\ODM\JobTimingManager; |
10
|
|
|
use Dtc\QueueBundle\ODM\RunManager; |
11
|
|
|
use Dtc\QueueBundle\Tests\FibonacciWorker; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
14
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
15
|
|
|
|
16
|
|
|
class WorkerCompilerPassTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
protected function getBaseContainer($type = 'odm', $runManagerType = 'odm', $jobTimingManagerType = 'odm') |
19
|
|
|
{ |
20
|
|
|
$container = new ContainerBuilder(); |
21
|
|
|
|
22
|
|
|
$count = count($container->getDefinitions()); |
23
|
|
|
$compilerPass = new WorkerCompilerPass(); |
24
|
|
|
$compilerPass->process($container); |
25
|
|
|
self::assertEquals($count, count($container->getDefinitions())); |
26
|
|
|
|
27
|
|
|
$container = new ContainerBuilder(); |
28
|
|
|
$definition1 = new Definition(); |
29
|
|
|
$definition1->setClass(WorkerManager::class); |
30
|
|
|
$container->setParameter('dtc_queue.default_manager', $type); |
31
|
|
|
$container->setParameter('dtc_queue.run_manager', $runManagerType); |
32
|
|
|
$container->setParameter('dtc_queue.job_timing_manager', $jobTimingManagerType); |
33
|
|
|
$container->setParameter('dtc_queue.class_job', null); |
34
|
|
|
$container->setParameter('dtc_queue.class_job_archive', null); |
35
|
|
|
$container->setParameter('dtc_queue.document_manager', 'default'); |
36
|
|
|
$container->setParameter('dtc_queue.entity_manager', 'default'); |
37
|
|
|
$definition2 = new Definition(); |
38
|
|
|
$definition2->setClass(JobManager::class); |
39
|
|
|
$definition3 = new Definition(); |
40
|
|
|
$definition3->setClass(RunManager::class); |
41
|
|
|
$definition4 = new Definition(); |
42
|
|
|
$definition4->setClass(EventDispatcher::class); |
43
|
|
|
$definition5 = new Definition(); |
44
|
|
|
$definition5->setClass(JobTimingManager::class); |
45
|
|
|
$container->addDefinitions([ |
46
|
|
|
'dtc_queue.worker_manager' => $definition1, |
47
|
|
|
'dtc_queue.job_manager.' . $type => $definition2, |
48
|
|
|
'dtc_queue.job_timing_manager.' . $jobTimingManagerType => $definition5, |
49
|
|
|
'dtc_queue.run_manager.' . $runManagerType => $definition3, |
50
|
|
|
'dtc_queue.event_dispatcher' => $definition4, |
51
|
|
|
]); |
52
|
|
|
|
53
|
|
|
return $container; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testProcess() |
57
|
|
|
{ |
58
|
|
|
$container = $this->getBaseContainer(); |
59
|
|
|
$count = count($container->getAliases()); |
60
|
|
|
$compilerPass = new WorkerCompilerPass(); |
61
|
|
|
$compilerPass->process($container); |
62
|
|
|
|
63
|
|
|
self::assertNotEquals($count, count($container->getAliases())); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testProcessInvalidWorker() |
67
|
|
|
{ |
68
|
|
|
$container = $this->getBaseContainer(); |
69
|
|
|
$definition5 = new Definition(); |
70
|
|
|
$definition5->setClass(JobManager::class); |
71
|
|
|
$definition5->addTag('dtc_queue.worker'); |
72
|
|
|
$container->addDefinitions(['some.worker' => $definition5]); |
73
|
|
|
|
74
|
|
|
$count = count($container->getAliases()); |
75
|
|
|
$compilerPass = new WorkerCompilerPass(); |
76
|
|
|
$failed = false; |
77
|
|
|
try { |
78
|
|
|
$compilerPass->process($container); |
79
|
|
|
$failed = true; |
80
|
|
|
} catch (\Exception $e) { |
81
|
|
|
self::assertTrue(true); |
82
|
|
|
} |
83
|
|
|
self::assertFalse($failed); |
84
|
|
|
self::assertNotEquals($count, count($container->getAliases())); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testProcessValidWorker() { |
88
|
|
|
$this->runProcessValidWorker('odm'); |
89
|
|
|
$this->runProcessValidWorker('odm', 'orm', 'orm'); |
90
|
|
|
$this->runProcessValidWorker('orm'); |
91
|
|
|
$this->runProcessValidWorker('orm', 'odm', 'orm'); |
92
|
|
|
$this->runProcessValidWorker('beanstalkd'); |
93
|
|
|
$this->runProcessValidWorker('rabbit_mq'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testBadManagerType() |
97
|
|
|
{ |
98
|
|
|
$failure = false; |
99
|
|
|
try { |
100
|
|
|
$this->runProcessValidWorker('bad'); |
101
|
|
|
} |
102
|
|
|
catch (\Exception $e) { |
103
|
|
|
$failure = true; |
104
|
|
|
} |
105
|
|
|
self::assertTrue($failure); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function runProcessValidWorker($type, $runManagerType = 'odm', $jobTimingManagerType = 'odm') |
109
|
|
|
{ |
110
|
|
|
$container = $this->getBaseContainer($type, $runManagerType, $jobTimingManagerType); |
111
|
|
|
$definition5 = new Definition(); |
112
|
|
|
$definition5->setClass(FibonacciWorker::class); |
113
|
|
|
$definition5->addTag('dtc_queue.worker'); |
114
|
|
|
$container->addDefinitions(['some.worker' => $definition5]); |
115
|
|
|
|
116
|
|
|
$count = count($container->getAliases()); |
117
|
|
|
$compilerPass = new WorkerCompilerPass(); |
118
|
|
|
$compilerPass->process($container); |
119
|
|
|
self::assertNotEquals($count, count($container->getAliases())); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|