| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace ShlinkioTest\Shlink\Rest\Action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Doctrine\DBAL\Connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Prophecy\Prophecy\ObjectProphecy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Shlinkio\Shlink\Core\Options\AppOptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Shlinkio\Shlink\Rest\Action\HealthAction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Zend\Diactoros\Response\JsonResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Zend\Diactoros\ServerRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class HealthActionTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** @var HealthAction */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** @var ObjectProphecy */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $conn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->conn = $this->prophesize(Connection::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $this->action = new HealthAction($this->conn->reveal(), new AppOptions(['version' => '1.2.3'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @test | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     public function passResponseIsReturnedWhenConnectionSucceeds() | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $ping = $this->conn->ping()->willReturn(true); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         /** @var JsonResponse $resp */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         $resp = $this->action->handle(new ServerRequest()); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         $payload = $resp->getPayload(); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $this->assertEquals(200, $resp->getStatusCode()); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $this->assertEquals('pass', $payload['status']); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $this->assertEquals('1.2.3', $payload['version']); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $this->assertEquals([ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             'about' => 'https://shlink.io', | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             'project' => 'https://github.com/shlinkio/shlink', | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         ], $payload['links']); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $this->assertEquals('application/health+json', $resp->getHeaderLine('Content-type')); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $ping->shouldHaveBeenCalledOnce(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function failResponseIsReturnedWhenConnectionFails() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $ping = $this->conn->ping()->willReturn(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         /** @var JsonResponse $resp */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $resp = $this->action->handle(new ServerRequest()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $payload = $resp->getPayload(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->assertEquals(503, $resp->getStatusCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->assertEquals('fail', $payload['status']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->assertEquals('1.2.3', $payload['version']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this->assertEquals([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             'about' => 'https://shlink.io', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'project' => 'https://github.com/shlinkio/shlink', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         ], $payload['links']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->assertEquals('application/health+json', $resp->getHeaderLine('Content-type')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $ping->shouldHaveBeenCalledOnce(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |  |