| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | * This file is a part of graphql-youshido project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | * @author Alexandr Viniychuk <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | * created: 11/28/15 2:02 AM | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace Youshido\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Youshido\GraphQL\Schema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Youshido\GraphQL\Type\Object\ObjectType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Youshido\GraphQL\Processor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Youshido\GraphQL\Validator\ResolveValidator\ResolveValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Youshido\Tests\DataProvider\UserType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class ProcessorTest extends \PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @dataProvider predefinedSchemaProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function testPredefinedQueries($query, $response) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $schema = new Schema([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             'query' => new ObjectType([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                 'name'        => 'TestSchema', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 'description' => 'Root of TestSchema' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $schema->addQuery('latest', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             new ObjectType( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                     'name'    => 'latest', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                     'fields'  => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                         'id'   => ['type' => 'int'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                         'name' => ['type' => 'string'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                     'resolve' => function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                             'id'   => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                             'name' => 'Alex' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 ]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 'description' => 'latest description', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 'deprecationReason' => 'for test', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 'isDeprecated' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $validator = new ResolveValidator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $processor = new Processor($validator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $processor->setSchema($schema); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $processor->processQuery($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $a = $processor->getResponseData(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->assertEquals($processor->getResponseData(), $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function predefinedSchemaProvider() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 '{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                   test : __schema { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     queryType { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                       kind, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                       name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                       fields { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                         name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                         isDeprecated, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                         deprecationReason, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                         description, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                         type { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                           name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                       } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 }', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 ['data' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                     'test' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                         'queryType' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                             'name'   => 'TestSchema', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                             'kind'   => 'OBJECT', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                             'fields' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                                 ['name' => 'latest', 'isDeprecated' => true, 'deprecationReason' => 'for test', 'description' => 'for test', 'type' => ['name' => 'latest']], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                                 ['name' => '__schema', 'isDeprecated' => false, 'deprecationReason' => '', 'description' => '', 'type' => ['name' => '__Schema']], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                                 ['name' => '__type', 'isDeprecated' => false, 'deprecationReason' => '', 'description' => '', 'type' => ['name' => '__Type']] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 ]] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 '{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                   __schema { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                     queryType { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                       kind, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                       name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                       description, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                       interfaces { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                         name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                       }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                       possibleTypes { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                         name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                       }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                       inputFields { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                         name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                       }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                       ofType{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                         name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                       } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 }', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 ['data' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                     '__schema' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                         'queryType' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                             'kind'          => 'OBJECT', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                             'name'          => 'TestSchema', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                             'description'   => 'Root of TestSchema', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                             'interfaces'    => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                             'possibleTypes' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                             'inputFields'   => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                             'ofType'        => [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                 ]] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @dataProvider schemaProvider | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 145 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |     public function testProcessor($query, $response) | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |         $schema = new Schema(); | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |         $schema->addQuery('latest', | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |             new ObjectType( | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |                 [ | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |                     'name'    => 'latest', | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |                     'fields'  => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |                         'id'   => ['type' => 'int'], | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |                         'name' => ['type' => 'string'] | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |                     ], | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |                     'resolve' => function () { | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |                         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |                             'id'   => 1, | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |                             'name' => 'Alex' | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |                         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  |                 ])); | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  |         $schema->addQuery('user', new UserType()); | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |         $validator = new ResolveValidator(); | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |         $processor = new Processor($validator); | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |         $processor->setSchema($schema); | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |         $processor->processQuery($query); | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |         $this->assertEquals( | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |             $processor->getResponseData(), | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |             $response | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     public function schemaProvider() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |                 '{ latest { name } }', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |                     'data' => ['latest' => null] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |                 '{ user(id:1) { id, name } }', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |                     'data' => ['user' => ['id' => 1, 'name' => 'John']] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 197 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 198 |  |  |  | 
            
                        
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.