| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the OverblogGraphQLBundle package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Overblog <http://github.com/overblog/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Overblog\GraphQLBundle\Tests\Functional\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Overblog\GraphQLBundle\Command\GraphQLDumpSchemaCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Overblog\GraphQLBundle\Tests\Functional\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Bundle\FrameworkBundle\Console\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Console\Tester\CommandTester; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class GraphDumpSchemaCommandTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var CommandTester | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $commandTester; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private $cacheDir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         parent::setUp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $client = static::createClient(['test_case' => 'connection']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $kernel = $client->getKernel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $application = new Application($kernel); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $application->add(new GraphQLDumpSchemaCommand()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->command = $application->find('graphql:dump-schema'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->commandTester = new CommandTester($this->command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->cacheDir = $kernel->getCacheDir(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param $format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param bool $withFormatOption | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @dataProvider formatDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function testDump($format, $withFormatOption = true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $file = $this->cacheDir.'/schema.'.$format; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $input = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             'command' => $this->command->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             '--file' => $file, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         if ($withFormatOption) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $input['--format'] = $format; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->assertCommandExecution( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $input, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             __DIR__.'/fixtures/schema.'.$format, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $file, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 75 |  | View Code Duplication |     public function testClassicJsonFormat() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $file = $this->cacheDir.'/schema.json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->assertCommandExecution( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 'command' => $this->command->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 '--file' => $file, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 '--classic' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 '--format' => 'json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             __DIR__.'/fixtures/schema.json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $file, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             'json' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 91 |  | View Code Duplication |     public function testModernJsonFormat() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $file = $this->cacheDir.'/schema.json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $this->assertCommandExecution( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 'command' => $this->command->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 '--file' => $file, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 '--modern' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 '--format' => 'json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             __DIR__.'/fixtures/schema.modern.json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $file, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             'json' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @expectedException \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @expectedExceptionMessage Unknown format "fake". | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     public function testInvalidFormat() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $this->commandTester->execute([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             'command' => $this->command->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             '--format' => 'fake', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @expectedException \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @expectedExceptionMessage "modern" and "classic" options should not be used together. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     public function testInvalidModernAndClassicUsedTogether() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $this->commandTester->execute([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             'command' => $this->command->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             '--format' => 'json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             '--classic' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             '--modern' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     public function formatDataProvider() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             ['json', false], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             ['json', true], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             ['graphql'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     private function assertCommandExecution(array $input, $expectedFile, $actualFile, $format, $expectedStatusCode = 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $this->commandTester->execute($input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $this->assertEquals($expectedStatusCode, $this->commandTester->getStatusCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $expected = trim(file_get_contents($expectedFile)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $actual = trim(file_get_contents($actualFile)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         if ('json' === $format) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             $expected = json_decode($expected, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             $actual = json_decode($actual, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             $this->sortSchemaEntry($expected, 'types', 'name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             $this->sortSchemaEntry($actual, 'types', 'name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $this->assertEquals($expected, $actual); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 159 |  |  |     private function sortSchemaEntry(array &$entries, $entryKey, $sortBy) | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |         if (isset($entries['data']['__schema'][$entryKey])) { | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |             $data = &$entries['data']['__schema'][$entryKey]; | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |         } else { | 
            
                                                        
            
                                    
            
            
                | 164 |  |  |             $data = &$entries['__schema'][$entryKey]; | 
            
                                                        
            
                                    
            
            
                | 165 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |         usort($data, function ($data1, $data2) use ($sortBy) { | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |             return strcmp($data1[$sortBy], $data2[$sortBy]); | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |         }); | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 171 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 172 |  |  |  | 
            
                        
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.