| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of the KleijnWeb\SwaggerBundle package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace KleijnWeb\SwaggerBundle\Tests\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use KleijnWeb\SwaggerBundle\Document\OperationObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use KleijnWeb\SwaggerBundle\Request\RequestValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @author John Kleijn <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class RequestValidatorTest extends \PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function canValidateDate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $dateTime = new \DateTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->runTimeTest('date', '2015-12-12', $dateTime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @expectedException \KleijnWeb\SwaggerBundle\Exception\InvalidParametersException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function canInvalidateDate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->runTimeTest('date', '2016-01-01T00:00:00Z', '2016-01-01T00:00:00Z'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $dateTime = new \DateTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->runTimeTest('date', '2016-01-01T00:00:00Z', $dateTime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function canValidateDateTime() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $dateTime = new \DateTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->runTimeTest('date-time', $dateTime->format(\DateTime::W3C), $dateTime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->runTimeTest('date-time', '2016-01-01T00:00:00Z', '2016-01-01T00:00:00Z'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @expectedException \KleijnWeb\SwaggerBundle\Exception\InvalidParametersException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     public function canInvalidateDateTime() | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $this->runTimeTest('date-time', 'm-d-Y', new \DateTime()); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $this->runTimeTest('date-time', '01-01-2014T00:00:00Z', new \DateTime()); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 63 |  | View Code Duplication |     public function canOmitParameterWhenNotExplicitlyMarkedAsRequired() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $operationDefinition = (object)[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             'parameters' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 (object)[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     'name'   => 'foo', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     'in'     => 'body', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     'schema' => (object)[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                         'type' => 'integer' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $validator = new RequestValidator(OperationObject::createFromOperationDefinition($operationDefinition)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $request = new Request(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $validator->validateRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @expectedException \KleijnWeb\SwaggerBundle\Exception\InvalidParametersException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 85 |  | View Code Duplication |     public function cannotOmitParameterWhenExplicitlyMarkedAsRequired() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $request = new Request(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $operationDefinition = (object)[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             'parameters' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 (object)[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                     'name'     => 'foo', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                     'required' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     'in'       => 'query', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                     'type'     => 'int' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $validator = new RequestValidator(OperationObject::createFromOperationDefinition($operationDefinition)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $validator->validateRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @expectedException \KleijnWeb\SwaggerBundle\Exception\InvalidParametersException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 107 |  | View Code Duplication |     public function cannotOmitBodyWhenExplicitlyMarkedAsRequired() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $request = new Request(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $operationDefinition = (object)[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             'parameters' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 (object)[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                     'name'     => 'foo', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                     'required' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                     'in'       => 'query', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                     'type'     => 'int' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $validator = new RequestValidator(OperationObject::createFromOperationDefinition($operationDefinition)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $validator->validateRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @param string           $format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @param string           $rawDateTime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @param \DateTime|string $dateTime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @throws \KleijnWeb\SwaggerBundle\Exception\InvalidParametersException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     private function runTimeTest($format, $rawDateTime, $dateTime) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         $paramBagMapping = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             'query'  => 'query', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             'path'   => 'attributes', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             'header' => 'headers' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $parameterName = 'time'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         foreach (['query', 'path', 'header'] as $source) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             $operationDefinition = (object)[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 'parameters' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                     (object)[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                         'name'   => $parameterName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                         'in'     => $source, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                         'type'   => 'string', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                         'format' => $format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             $validator = new RequestValidator(OperationObject::createFromOperationDefinition($operationDefinition)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             $request = new Request(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             $bagName = $paramBagMapping[$source]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             $request->$bagName->set($parameterName, $rawDateTime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             $request->attributes->set($parameterName, $dateTime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             $validator->validateRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 162 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |  | 
            
                        
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.