| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SLLH\ComposerVersionsCheck\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Composer\Command\UpdateCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Composer\Composer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Composer\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Composer\EventDispatcher\EventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Composer\IO\BufferIO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Composer\Package\RootPackage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Composer\Plugin\CommandEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Composer\Plugin\PluginEvents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Composer\Plugin\PluginInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Composer\Plugin\PluginManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Composer\Repository\ArrayRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Composer\Repository\RepositoryManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Composer\Repository\WritableArrayRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Composer\Script\ScriptEvents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use SLLH\ComposerVersionsCheck\VersionsCheckPlugin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Console\Input\ArrayInput; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\Console\Output\NullOutput; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * The only goal of this class is to test composer version check. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @author Sullivan Senechal <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | class VersionsCheckPluginComposerVersionTest extends \PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var BufferIO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $io; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @var Composer|\PHPUnit_Framework_MockObject_MockObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private $composer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     protected function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->io = new BufferIO(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->composer = $this->getMock('Composer\Composer'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->composer->expects($this->any())->method('getConfig') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ->willReturn(new Config(false)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->composer->expects($this->any())->method('getPackage') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ->willReturn(new RootPackage('my/project', '1.0.0', '1.0.0')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->composer->expects($this->any())->method('getPluginManager') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ->willReturn(new PluginManager($this->io, $this->composer)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->composer->expects($this->any())->method('getEventDispatcher') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             ->willReturn(new EventDispatcher($this->composer, $this->io)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->composer->expects($this->any())->method('getRepositoryManager') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             ->willReturn(new RepositoryManager($this->io, new Config())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function testComposerVersionMessage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->addComposerPlugin(new VersionsCheckPlugin()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->composer->getRepositoryManager()->setLocalRepository(new WritableArrayRepository()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->composer->getRepositoryManager()->addRepository(new ArrayRepository()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $updateCommand = new UpdateCommand(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $input = new ArrayInput(array('update'), $updateCommand->getDefinition()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'update', $input, new NullOutput()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_UPDATE_CMD); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if (VersionsCheckPlugin::satisfiesComposerVersion() && '@package_version@' === Composer::VERSION) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $this->assertSame( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 "<warning>You are running an unstable version of composer. The sllh/composer-versions-check plugin might not works as expected.</warning>\n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 ."All packages are up to date.\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 $this->io->getOutput()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } elseif (VersionsCheckPlugin::satisfiesComposerVersion()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $this->assertSame("All packages are up to date.\n", $this->io->getOutput()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $this->assertSame( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 'Composer v'.Composer::VERSION.' is not supported by sllh/composer-versions-check plugin, please upgrade to v'.VersionsCheckPlugin::COMPOSER_MIN_VERSION." or higher.\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 $this->io->getOutput() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 88 |  | View Code Duplication |     private function addComposerPlugin(PluginInterface $plugin) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |         $pluginManagerReflection = new \ReflectionClass($this->composer->getPluginManager()); | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |         $addPluginReflection = $pluginManagerReflection->getMethod('addPlugin'); | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |         $addPluginReflection->setAccessible(true); | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         $addPluginReflection->invoke($this->composer->getPluginManager(), $plugin); | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 95 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  | 
            
                        
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.