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