| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Bunq\Tests\Middleware; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Bunq\Middleware\RefreshSessionMiddleware; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Bunq\Service\InstallationService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Bunq\Token\DefaultToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Bunq\Token\Storage\TokenStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Bunq\Token\TokenType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use GuzzleHttp\Psr7\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Prophecy\Argument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Prophecy\Prophecy\ObjectProphecy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | final class RefreshSessionMiddlewareTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var InstallationService|ObjectProphecy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $installationService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var TokenStorage|ObjectProphecy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $tokenStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var RefreshSessionMiddleware | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $middleware; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->installationService = $this->prophesize(InstallationService::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->tokenStorage        = $this->prophesize(TokenStorage::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->middleware = new RefreshSessionMiddleware( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $this->installationService->reveal(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             $this->tokenStorage->reveal() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 47 |  | View Code Duplication |     public function itWontGetExecutedWhenResponseIsNot401() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $response = new Response(200, [], '{"ok"}'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->tokenStorage->load(Argument::any())->shouldNotBeCalled(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $resultResponse = $this->middleware->__invoke($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->assertSame($response, $resultResponse); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 61 |  | View Code Duplication |     public function itWontGetExecutedWhenResponseIsNotInsufficientAuthorisation() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $body = '{"Error":[{"error_description": "An other error"}]}'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $response = new Response(401, [], $body); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->tokenStorage->load(Argument::any())->shouldNotBeCalled(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $resultResponse = $this->middleware->__invoke($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->assertEquals($response, $resultResponse); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @expectedException \Bunq\Exception\SessionWasExpiredException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     public function itWillRefreshSessionAndThrowsAnExceptionIfInsufficientAuthorisation() | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         $body = '{"Error":[{"error_description": "Insufficient authorisation."}]}'; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         $response = new Response(401, [], $body); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         $installationToken = new DefaultToken('Installation Token'); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         $this->tokenStorage->load(TokenType::INSTALLATION_TOKEN())->willReturn($installationToken); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         $this->installationService->createSession($installationToken)->willReturn('someSessionId'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         $sessionToken = DefaultToken::fromString('someSessionId'); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         $this->tokenStorage->save($sessionToken, TokenType::SESSION_TOKEN())->shouldBeCalled(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         $this->middleware->__invoke($response); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  | 
            
                        
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.