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