| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Gandung\JWT\Tests\Token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Gandung\JWT\Token\JoseBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @author Paulus Gandung Prakosa <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class JoseBuilderTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     private function callCollectJoseDirectly($name, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         $refl = new \ReflectionClass(JoseBuilder::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $builder = $refl->getMethod('collectJose'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         $builder->setAccessible(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $instance = $builder->invokeArgs(new JoseBuilder, [$name, $value]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function testCanGetInstance() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->assertInstanceOf(JoseBuilder::class, new JoseBuilder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @expectedException \Gandung\JWT\Exception\AlgorithmHeaderValueMismatchException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function testCanRaiseExceptionWhenGiveInvalidAlgorithm() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $builder = (new JoseBuilder) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             ->algorithm('invalid_algo'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     public function testCanBuildJoseHeaderAndGetItAfterwards() | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $builder = (new JoseBuilder) | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             ->algorithm(\Gandung\JWT\Token\Algorithm::HS256) | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             ->jwkSetUrl(new \Gandung\Psr7\Uri('http://example.org/jwk/set_url')) | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             ->keyID(md5(uniqid())) | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             ->x509Url(new \Gandung\Psr7\Uri('http://example.org/x509/cert.pem')) | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             ->type('JWT') | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             ->contentType('application/json'); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $this->assertInstanceOf(JoseBuilder::class, $builder); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $jose = $builder->getValue(); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $this->assertInternalType('array', $jose); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $this->assertNotEmpty($jose); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @expectedException \Gandung\JWT\Exception\JoseTokenMismatchException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function testCanRaiseExceptionWhileStoreValueInWrongJoseHeaderName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->callCollectJoseDirectly('invalid_jose_header_name', 'shit'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |  | 
            
                        
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.