| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This is part of the webuni/composer-yaml-plugin package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Martin Hasoň <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * (c) Webuni s.r.o. <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | namespace Webuni\ComposerYamlPlugin\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Console\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Console\Input\ArrayInput; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Console\Output\BufferedOutput; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Webuni\ComposerYamlPlugin\YamlConvertCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | final class YamlConvertCommandTest extends \PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $app; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $output; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $cwd; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     protected function setUp() | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         $this->app = new Application(); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $this->app->setAutoExit(false); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         $this->app->add(new YamlConvertCommand()); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         $this->output = new BufferedOutput(); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $this->cwd = getcwd(); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         chdir(sys_get_temp_dir()); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         @unlink('composer.json'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         @unlink('composer.yml'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected function tearDown() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         chdir(sys_get_temp_dir()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         @unlink('composer.json'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         @unlink('composer.yml'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         chdir($this->cwd); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function testDefaultInputFileDoesNotExists() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->app->run(new ArrayInput(['yaml-convert']), $this->output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->assertContains('The input file "composer.yml" does not exist.', $this->output->fetch()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function testCustomInputFileDoesNotExists() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->app->run(new ArrayInput(['yaml-convert', 'input' => 'composer.json']), $this->output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->assertContains('The input file "composer.json" does not exist.', $this->output->fetch()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function testInvalidFormat() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->app->run(new ArrayInput(['yaml-convert', 'input' => 'composer.json']), $this->output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->assertContains('The input file "composer.json" does not exist.', $this->output->fetch()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function testSameFormat() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         file_put_contents('composer.yml', 'name: package'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->app->run(new ArrayInput(['yaml-convert', 'output' => 'composer.yml']), $this->output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->assertContains('Input format is same as output format.', $this->output->fetch()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function testConvertYamlToJson() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         file_put_contents('composer.yml', 'name: package'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->app->run(new ArrayInput(['yaml-convert']), $this->output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->assertContains('Converted "composer.yml" to "composer.json"', $this->output->fetch()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $this->assertFileExists('composer.json'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->assertEquals("{\n    \"name\": \"package\"\n}", file_get_contents('composer.json')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     public function testConvertJsonToYaml() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         file_put_contents('composer.json', "{\n    \"name\": \"package\"\n}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->app->run(new ArrayInput(['yaml-convert', 'input' => 'composer.json', 'output' => 'composer.yml']), $this->output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $this->assertContains('Converted "composer.json" to "composer.yml"', $this->output->fetch()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $this->assertFileExists('composer.yml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $this->assertEquals("name: package\n", file_get_contents('composer.yml')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  |