| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the phpunit-json-assertions package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Enrico Stahn <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace EnricoStahn\JsonAssert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use JsonSchema\RefResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use JsonSchema\Uri\UriRetriever; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use JsonSchema\Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Asserts to validate JSON data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * - All assert methods expect deserialised JSON data (an actual object or array) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  *   since the deserialisation method should be up to the user. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * - We provide a convenience method to transfer whatever into a JSON object (see ::getJsonObject(mixed)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | trait Assert | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * Asserts that json content is valid according to the provided schema file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Example: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      *   static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param string       $schema  Path to the schema file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param array|object $content JSON array or object | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 3 |  |     public static function assertJsonMatchesSchema($schema, $content) | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 3 |  |         $retriever = new UriRetriever(); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 3 |  |         $schema = $retriever->retrieve('file://'.realpath($schema)); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 3 |  |         $refResolver = new RefResolver($retriever); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 3 |  |         $refResolver->resolve($schema, 'file://'.__DIR__.'/../Resources/schemas/'); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 3 |  |         $validator = new Validator(); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 3 |  |         $validator->check($content, $schema); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 3 |  |         $message = '- Property: %s, Contraint: %s, Message: %s'; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 3 |  |         $messages = array_map(function ($e) use ($message) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 2 |  |             return sprintf($message, $e['property'], $e['constraint'], $e['message']); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 3 |  |         }, $validator->getErrors()); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 3 |  |         $messages[] = '- Response: '.json_encode($content); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 3 |  |         self::assertTrue($validator->isValid(), implode("\n", $messages)); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Asserts that json content is valid according to the provided schema string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param string       $schema  Schema data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param array|object $content JSON content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     public static function assertJsonMatchesSchemaString($schema, $content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $file = tempnam(sys_get_temp_dir(), 'json-schema-'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         file_put_contents($file, $schema); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         self::assertJsonMatchesSchema($file, $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * Asserts if the value retrieved with the expression equals the expected value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * Example: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      *     static::assertJsonValueEquals(33, 'foo.bar[0]', $json); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param mixed        $expected   Expected value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param string       $expression Expression to retrieve the result (e.g. locations[?state == 'WA'].name | sort(@) | {WashingtonCities: join(', ', @)}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param array|object $json       JSON Content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 3 |  |     public static function assertJsonValueEquals($expected, $expression, $json) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 3 |  |         $result = \JmesPath\Env::search($expression, $json); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 3 |  |         self::assertEquals($expected, $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 2 |  |         self::assertInternalType(gettype($expected), $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * Helper method to deserialise a JSON string into an object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param mixed $data The JSON string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @return array|object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 4 |  |     public static function getJsonObject($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 4 |  |         return (is_array($data) || is_object($data)) ? $data : json_decode($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |  |