| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Liip\MonitorBundle\Tests\Helper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Liip\MonitorBundle\Helper\RunnerManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class RunnerManagerTest extends \PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @var \PHPUnit_Framework_MockObject_MockObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var RunnerManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $runnerManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function groupProvider() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             array(null), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             array('default'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             array('test') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @dataProvider groupProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param string $group | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function testGetRunner($group) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             ->expects($this->any()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             ->method('getParameter') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             ->with('liip_monitor.default_group') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             ->willReturn('default'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             ->expects($this->any()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             ->method('has') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             ->with('liip_monitor.runner_'.($group?:'default')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             ->willReturn(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $expectedResult = $this->getMock('Liip\MonitorBundle\Runner'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ->expects($this->any()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ->method('get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             ->with('liip_monitor.runner_'.($group?:'default')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ->willReturn($expectedResult); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $result = $this->runnerManager->getRunner($group); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->assertSame($expectedResult, $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function testGetRunnerReturnsNull() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             ->expects($this->any()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             ->method('has') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             ->with('liip_monitor.runner_testgroup') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             ->willReturn(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $result = $this->runnerManager->getRunner('testgroup'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->assertNull($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     public function testGetRunners() | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         $this->container | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             ->expects($this->any()) | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             ->method('getParameter') | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             ->with('liip_monitor.runners') | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             ->willReturn(array('liip_monitor.runner_group_1', 'liip_monitor.runner_group_2')); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         $runner1 = $this->getMock('Liip\MonitorBundle\Runner'); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         $runner2 = $this->getMock('Liip\MonitorBundle\Runner'); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         $this->container | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |             ->expects($this->exactly(2)) | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             ->method('get') | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |             ->withConsecutive( | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |                 array('liip_monitor.runner_group_1'), | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |                 array('liip_monitor.runner_group_2') | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |             ->willReturnOnConsecutiveCalls($runner1, $runner2); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         $result = $this->runnerManager->getRunners(); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         $this->assertInternalType('array', $result); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         $this->assertCount(2, $result); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         $this->assertArrayHasKey('group_1', $result); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         $this->assertArrayHasKey('group_2', $result); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         $this->assertSame($runner1, $result['group_1']); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         $this->assertSame($runner2, $result['group_2']); | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     public function testGetGroups() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $this->container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             ->expects($this->any()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             ->method('getParameter') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             ->with('liip_monitor.runners') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             ->willReturn(array('liip_monitor.runner_group_1', 'liip_monitor.runner_group_2')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $result = $this->runnerManager->getGroups(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $this->assertInternalType('array', $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $this->assertCount(2, $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $this->assertContains('group_1', $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $this->assertContains('group_2', $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public function testGetDefaultGroup() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $expectedResult = 'default'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $this->container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             ->expects($this->any()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             ->method('getParameter') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             ->with('liip_monitor.default_group') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             ->willReturn($expectedResult); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $result = $this->runnerManager->getDefaultGroup(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $this->assertEquals($expectedResult, $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     protected function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $this->container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $this->runnerManager = new RunnerManager($this->container); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 139 |  |  | } |