| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Demv\JSend\Test; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Demv\JSend\JSend; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Demv\JSend\ResponseFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | final class ResponseTest extends TestCase | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     public function testDefaultHttpStatusCode(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |         $this->assertEquals(200, JSend::getDefaultHttpStatusCode(ResponseFactory::instance()->success())); | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |         $this->assertEquals(null, JSend::getDefaultHttpStatusCode(ResponseFactory::instance()->fail())); | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |         $this->assertEquals(400, JSend::getDefaultHttpStatusCode(ResponseFactory::instance()->error(['message' => 'wtf']))); | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         $this->assertEquals(500, JSend::getDefaultHttpStatusCode(ResponseFactory::instance()->error(['message' => 'wtf', 'code' => 500]))); | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function testSuccessConversion(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $success = new DummyResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $success->withBody(new DummyStream('{"data": ["Holy", "Moly"]}')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $success->withStatus(214); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $response = ResponseFactory::instance()->convert($success); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->assertTrue($response->getStatus()->isSuccess()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->assertEquals(['Holy', 'Moly'], $response->getData()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->assertJsonStringEqualsJsonString('{"status": "success", "data": ["Holy", "Moly"]}', json_encode($response)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function testFailConversion(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $fail = new DummyResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $fail->withBody(new DummyStream('{}')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $response = ResponseFactory::instance()->convert($fail); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->assertTrue($response->getStatus()->isFail()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->assertEmpty($response->getData()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->assertJsonStringEqualsJsonString('{"status": "fail", "data": null}', json_encode($response)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function testErrorConversion(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $json = '{"data": ["Invalid"], "message": "Something is not right..."}'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $error = new DummyResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $error->withBody(new DummyStream($json)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $error->withStatus(501); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $result           = json_decode($json, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $result['status'] = 'error'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $result['code']   = $error->getStatusCode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $response = ResponseFactory::instance()->convert($error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->assertTrue($response->getStatus()->isError()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->assertEquals(['Invalid'], $response->getData()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->assertJsonStringEqualsJsonString(json_encode($result), json_encode($response)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->assertEquals('Something is not right...', $response->getError()->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->assertEquals($error->getStatusCode(), $response->getError()->getCode()); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 61 |  |  | } |