| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Pachico\SlimCorrelationId\Middleware; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use \Psr\Http\Message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use \Pachico\SlimCorrelationId\Model; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | class CorrelationIdTest extends \PHPUnit_Framework_TestCase | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     const TEST_ID = 'abc123'; | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |      * @var CorrelationId | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     private $sut; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * @var \PHPUnit_Framework_MockObject_MockObject | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     private $generatorMock; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @var callback|null | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     private $callback; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @var \PHPUnit_Framework_MockObject_MockObject | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     private $requestMock; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @var \PHPUnit_Framework_MockObject_MockObject | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     private $responseMock; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->generatorMock = $this->getMockBuilder('\Pachico\SlimCorrelationId\Generator\IdInterface')->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->requestMock = $this->getMockBuilder('\Psr\Http\Message\RequestInterface')->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->responseMock = $this->getMockBuilder('\Psr\Http\Message\ResponseInterface')->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->sut = new CorrelationId([], $this->callback, $this->generatorMock); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @return callable | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     private function getFakeNextCallable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return function (Message\RequestInterface $request, Message\ResponseInterface $response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         }; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function testInvokeReturnsResponse() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         // Arrange | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->generatorMock->expects($this->once())->method('create')->will($this->returnValue(static::TEST_ID)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->requestMock->expects($this->once())->method('withHeader')->will($this->returnSelf()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->responseMock->expects($this->once())->method('withHeader')->will($this->returnSelf()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         // Act | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $response = call_user_func_array($this->sut, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $this->requestMock, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $this->responseMock, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $this->getFakeNextCallable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         // Assert | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->assertInstanceOf('\Psr\Http\Message\ResponseInterface', $response); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 72 |  | View Code Duplication |     public function testInvokeWillGenerateNewId() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         // Arrange | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->generatorMock->expects($this->once())->method('create')->will($this->returnValue(static::TEST_ID)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->requestMock->expects($this->once())->method('withHeader')->will($this->returnSelf()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $this->requestMock->expects($this->once())->method('getHeaderLine')->will($this->returnValue(null)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->responseMock->expects($this->once())->method('withHeader')->will($this->returnSelf()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         // Act | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $response = call_user_func_array($this->sut, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $this->requestMock, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $this->responseMock, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $this->getFakeNextCallable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         // Assert | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $this->assertInstanceOf('\Psr\Http\Message\ResponseInterface', $response); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 89 |  | View Code Duplication |     public function testInvokeFetchesIdFromRequest() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         // Arrange | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->generatorMock->expects($this->never())->method('create')->will($this->returnValue(static::TEST_ID)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->requestMock->expects($this->once())->method('withHeader')->will($this->returnSelf()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $this->requestMock->expects($this->once())->method('getHeaderLine') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             ->will($this->returnValue(static::TEST_ID)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $this->responseMock->expects($this->once())->method('withHeader')->will($this->returnSelf()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         // Act | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $response = call_user_func_array($this->sut, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             $this->requestMock, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $this->responseMock, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $this->getFakeNextCallable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         // Assert | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $this->assertInstanceOf('\Psr\Http\Message\ResponseInterface', $response); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     public function testInvokeFetchesIdFromCustomHeaderKey() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         // Arrange | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $customHeaderKey = 'X-CustomKey-Id'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $this->sut = new CorrelationId( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 'header_key' => $customHeaderKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $this->callback, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             $this->generatorMock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $this->generatorMock->expects($this->never())->method('create')->will($this->returnValue(static::TEST_ID)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $this->requestMock->expects($this->once())->method('withHeader')->will($this->returnSelf()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $this->requestMock->expects($this->once())->method('getHeaderLine')->with($customHeaderKey) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             ->will($this->returnValue(static::TEST_ID)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $this->responseMock->expects($this->once())->method('withHeader')->with($customHeaderKey) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             ->will($this->returnSelf()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         // Act | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $response = call_user_func_array($this->sut, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $this->requestMock, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $this->responseMock, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $this->getFakeNextCallable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         // Assert | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $this->assertInstanceOf('\Psr\Http\Message\ResponseInterface', $response); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     public function testInvokeWillInvokeCallableWithRequestId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         // Arrange | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $dummy = (object) [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |            'foo' => null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $this->callback = function (Model\CorrelationId $correlationid) use ($dummy) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             $dummy->foo = $correlationid->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $this->sut = new CorrelationId([], $this->callback, $this->generatorMock); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $this->generatorMock->expects($this->never())->method('create')->will($this->returnValue(static::TEST_ID)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $this->requestMock->expects($this->once())->method('withHeader')->will($this->returnSelf()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $this->requestMock->expects($this->once())->method('getHeaderLine') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             ->will($this->returnValue(static::TEST_ID)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $this->responseMock->expects($this->once())->method('withHeader')->will($this->returnSelf()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         // Act | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         call_user_func_array($this->sut, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             $this->requestMock, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             $this->responseMock, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             $this->getFakeNextCallable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         // Assert | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $this->assertSame(static::TEST_ID, $dummy->foo); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 159 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |  | 
            
                        
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.