| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace VGirol\JsonApiAssert\Laravel\Asserts\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Testing\TestResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use VGirol\JsonApiAssert\Laravel\HttpHeader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use VGirol\JsonApiConstant\Members; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * This trait adds the ability to test response returned after resource update. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | trait AssertUpdated | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      * Asserts that a response object is a valid '200 OK' response following an update request. | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @param TestResponse $response | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * @param array        $expected     The expected updated resource object | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * @param boolean      $relationship If true, response content must be valid resource linkage | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * @param boolean      $strict       If true, unsafe characters are not allowed when checking members name. | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @throws \PHPUnit\Framework\AssertionFailedError | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 36 |  |     public static function assertIsUpdatedResponse( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         TestResponse $response, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $expected, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         bool $relationship, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         bool $strict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 36 |  |         $response->assertStatus(200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 33 |  |         $response->assertHeader( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 33 |  |             HttpHeader::HEADER_NAME, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 33 |  |             HttpHeader::MEDIA_TYPE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         // Decode JSON response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 30 |  |         $json = $response->json(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         // Checks response structure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 30 |  |         static::assertHasValidStructure( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 30 |  |             $json, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $strict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         // Checks presence of "meta" or "data" member | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 24 |  |         static::assertContainsAtLeastOneMember( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 24 |  |                 Members::META, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 Members::DATA | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         // Checks data member | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 21 |  |         if (!array_key_exists(Members::DATA, $json)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 6 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 15 |  |         $data = $json[Members::DATA]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         // Check if the response contains resource linkage ... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 15 |  |         if ($relationship) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 6 |  |             static::assertResourceLinkageEquals($expected, $data, $strict); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 3 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         // ... or a single resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 9 |  |         static::assertIsNotArrayOfObjects($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 6 |  |         static::assertResourceObjectEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 6 |  |             $expected, | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  |             $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 6 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |  |