| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Majora\Framework\Serializer\Tests\Handler\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Majora\Framework\Normalizer\MajoraNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Majora\Framework\Normalizer\Model\NormalizableInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Majora\Framework\Serializer\Handler\Json\JsonHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PHPUnit_Framework_TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Unit test class for JsonHandler.php. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @see Majora\Framework\Serializer\Handler\Json\JsonHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @group legacy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class JsonHandlerTest extends PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * tests serialize() method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |      * @dataProvider serializationSuccessCaseProvider | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     public function testSerialize($normalizedData, $json) | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         $object = $this->prophesize('StdClass'); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         $object->willImplement(NormalizableInterface::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         $object = $object->reveal(); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         $scope = 'test'; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $normalizer = $this->prophesize(MajoraNormalizer::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $normalizer->normalize($object, $scope) | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             ->willReturn($normalizedData) | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             ->shouldBeCalled() | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $jsonHandler = new JsonHandler($normalizer->reveal()); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $this->assertEquals( | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             $json, | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             $jsonHandler->serialize($object, $scope) | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function serializationSuccessCaseProvider() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             'string_as_array' => array(array('string'), '["string"]'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             'int_as_array' => array(array(42), '[42]'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             'array_as_array' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 $raw = array('hello', 'foo' => 'bar', 42, 'nested' => array('child' => 'value')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 json_encode($raw), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * tests deserialize() json decoding exception. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @expectedException              Majora\Framework\Serializer\Handler\Json\Exception\JsonDeserializationException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @expectedExceptionMessageRegExp #Invalid json data, error*# | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function testDecodeException() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $collectionHandler = new JsonHandler( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $this->prophesize(MajoraNormalizer::class)->reveal() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $collectionHandler->deserialize('THIS IS NOT JSOOOOOOOOON !', 'StdClass'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * tests deserialize() method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @dataProvider deserializationSuccessCaseProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function testDeserialize($json, $normalizedData) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $normalizer = $this->prophesize(MajoraNormalizer::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $normalizer->denormalize($normalizedData, \StdClass::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             ->shouldBeCalled() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $collectionHandler = new JsonHandler($normalizer->reveal()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $collectionHandler->deserialize($json, \StdClass::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     public function deserializationSuccessCaseProvider() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             'string_as_array' => array('["string"]', array('string')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             'int_as_array' => array('[42]', array(42)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             'array_as_array' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 json_encode($raw = array('hello', 'foo' => 'bar', 42, 'nested' => array('child' => 'value'))), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 $raw, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  |