| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Demv\JSend\Test; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use BadMethodCallException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Demv\JSend\JSend; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Dgame\Ensurance\Exception\EnsuranceException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | final class JSendBasicTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     public function testEncodeSuccess(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         $json = '{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |             "status" : "success", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |             "data" : { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |                 "post" : { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |                     "id" : 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |                     "title" : "A blog post", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |                     "body" : "Some useful content" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         }'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $result = JSend::encode( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 'status' => 'success', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 'data'   => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                     'post' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                         'id'    => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                         'title' => 'A blog post', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                         'body'  => 'Some useful content' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->assertJsonStringEqualsJsonString($json, $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function testEncodeError(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $json = '{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             "status" : "error", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             "message" : "Unable to communicate with database" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         }'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $result = JSend::encode(['status' => 'error', 'message' => 'Unable to communicate with database']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->assertJsonStringEqualsJsonString($json, $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     public function testEncodeErrorWithoutMessage(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $this->expectException(EnsuranceException::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $this->expectExceptionMessage('Need a descriptive error-message'); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         JSend::encode(['status' => 'error']); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function testEncodeEmpty(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->expectException(EnsuranceException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this->expectExceptionMessage('Empty response cannot be converted to valid JSend-JSON'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         JSend::encode([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function testEncodeWithoutStatus(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->expectException(EnsuranceException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->expectExceptionMessage('Key "status" is required'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         JSend::encode(['msg' => 'Foo']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public function testSuccessResponse(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $json = '{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             "status" : "success", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             "data" : { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 "post" : { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                     "id" : 2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                     "title" : "Another blog post", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     "body" : "More content" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         }'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $response = JSend::decode($json); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $this->assertTrue($response->getStatus()->isSuccess()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $this->assertFalse($response->getStatus()->isFail()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->assertFalse($response->getStatus()->isError()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->assertNotEmpty($response->getData()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 'post' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                     'id'    => 2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                     'title' => 'Another blog post', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                     'body'  => 'More content' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $response->getData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $this->assertJsonStringEqualsJsonString($json, json_encode($response)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     public function testSuccessResponseWithoutData(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $json = '{ "status": "success" }'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $this->expectException(EnsuranceException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $this->expectExceptionMessage('Key "data" is required'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         JSend::decode($json); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 115 |  | View Code Duplication |     public function testSuccessResponseWithNullData(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $json     = '{ "status": "success", "data": null }'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $response = JSend::decode($json); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $this->assertTrue($response->getStatus()->isSuccess()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $this->assertEmpty($response->getData()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $this->assertJsonStringEqualsJsonString($json, json_encode($response)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 124 |  | View Code Duplication |     public function testSuccessResponseWithEmptyData(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $json     = '{ "status": "success", "data": [] }'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $response = JSend::decode($json); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $this->assertTrue($response->getStatus()->isSuccess()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $this->assertEmpty($response->getData()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $this->assertJsonStringEqualsJsonString($json, json_encode($response)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 133 |  | View Code Duplication |     public function testFailResponse(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $json = '{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             "status" : "fail", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             "data" : { "title" : "A title is required" } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         }'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $response = JSend::decode($json); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $this->assertTrue($response->getStatus()->isFail()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $this->assertEquals(['title' => 'A title is required'], $response->getData()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $this->assertJsonStringEqualsJsonString($json, json_encode($response)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     public function testGetErrorOnNoneError(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $response = Jsend::decode('{"status": "success", "data": null}'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $this->expectException(BadMethodCallException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $this->expectExceptionMessage('This is not a JSend-Error'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $response->getError(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 154 |  | View Code Duplication |     public function testErrorResponse(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $json = '{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             "status" : "error", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             "message" : "Unable to communicate with database" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         }'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         $response = JSend::decode($json); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         $this->assertTrue($response->getStatus()->isError()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         $this->assertEquals('Unable to communicate with database', $response->getError()->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         $this->assertJsonStringEqualsJsonString($json, json_encode($response)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 165 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 166 |  |  | } |