|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Majora\Framework\Serializer\Tests\Model; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Test class for serializable trait. |
|
9
|
|
|
*/ |
|
10
|
|
|
class SerializableTraitTest extends PHPUnit_Framework_TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* tests serialize methods with an unsupported scope. |
|
14
|
|
|
* |
|
15
|
|
|
* @expectedException InvalidArgumentException |
|
16
|
|
|
* @expectedExceptionMessageRegExp #Invalid scope for .+ "fake_scope" given.# |
|
17
|
|
|
*/ |
|
18
|
|
|
public function testUnsupportedSerializeScope() |
|
19
|
|
|
{ |
|
20
|
|
|
$entityMock = new SerializableMock1(); |
|
21
|
|
|
$entityMock->serialize('fake_scope'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* tests serialize trait method, on a mocked class using it. |
|
26
|
|
|
* |
|
27
|
|
|
* @dataProvider scopeProvider |
|
28
|
|
|
*/ |
|
29
|
|
|
public function testSerialize($scope, $expectedData) |
|
30
|
|
|
{ |
|
31
|
|
|
$entityMock = new SerializableMock1(); |
|
32
|
|
|
$this->assertEquals( |
|
33
|
|
|
$expectedData, |
|
34
|
|
|
$entityMock->serialize($scope) |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function scopeProvider() |
|
39
|
|
|
{ |
|
40
|
|
|
return array( |
|
41
|
|
|
array('default', array('id' => 1, 'label' => 'mock_1_label')), |
|
42
|
|
|
array('id', 1), |
|
43
|
|
|
array('full', array( |
|
44
|
|
|
'id' => 1, |
|
45
|
|
|
'label' => 'mock_1_label', |
|
46
|
|
|
'table' => array('mock_1_1', 'mock_1_2'), |
|
47
|
|
|
'mock2' => 2, |
|
48
|
|
|
'optionnal_defined' => 'optionnal_mocked', |
|
49
|
|
|
'date' => (new \DateTime('2015-01-01'))->format(\DateTime::ISO8601), |
|
50
|
|
|
)), |
|
51
|
|
|
array('extra', array( |
|
52
|
|
|
'id' => 1, |
|
53
|
|
|
'label' => 'mock_1_label', |
|
54
|
|
|
'table' => array('mock_1_1', 'mock_1_2'), |
|
55
|
|
|
'mock2' => array( |
|
56
|
|
|
'id' => 2, |
|
57
|
|
|
'label' => 'mock_2_label', |
|
58
|
|
|
'table' => array('mock_2_1', 'mock_2_1'), |
|
59
|
|
|
), |
|
60
|
|
|
'optionnal_defined' => 'optionnal_mocked', |
|
61
|
|
|
'date' => (new \DateTime('2015-01-01'))->format(\DateTime::ISO8601), |
|
62
|
|
|
)), |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* tests unserialize methods with an unsupported scope. |
|
68
|
|
|
* |
|
69
|
|
|
* @expectedException InvalidArgumentException |
|
70
|
|
|
* @expectedExceptionMessageRegExp #Unable to set "fake_member" property into a ".+" object, any existing property path to write it in.# |
|
71
|
|
|
*/ |
|
72
|
|
|
public function testUnsupportedDeserializeData() |
|
73
|
|
|
{ |
|
74
|
|
|
$entityMock = new SerializableMock1(); |
|
75
|
|
|
$entityMock->deserialize(array( |
|
76
|
|
|
'fake_member' => 'fake_data', |
|
77
|
|
|
)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* tests deserialize trait method, with a mocked class using it. |
|
82
|
|
|
* |
|
83
|
|
|
* @dataProvider serializedDataProvider |
|
84
|
|
|
*/ |
|
85
|
|
|
public function testDeserialize($data, $expectedObject) |
|
86
|
|
|
{ |
|
87
|
|
|
$entityMock = new SerializableMock1(); |
|
88
|
|
|
$this->assertEquals( |
|
89
|
|
|
$expectedObject, |
|
90
|
|
|
$entityMock->deserialize($data) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function serializedDataProvider() |
|
95
|
|
|
{ |
|
96
|
|
|
$ganonDorf = new \StdClass(); |
|
97
|
|
|
$ganonDorf->ganon = 'dorf'; |
|
98
|
|
|
|
|
99
|
|
|
return array( |
|
100
|
|
|
|
|
101
|
|
|
// simple case : simple types |
|
102
|
|
|
array( |
|
103
|
|
|
array('id' => 50, 'label' => 'zelda', 'table' => array('ocarina' => 'of-time')), |
|
104
|
|
|
(new SerializableMock1()) |
|
105
|
|
|
->setId(50) |
|
106
|
|
|
->setLabel('zelda') |
|
107
|
|
|
->setTable(array('ocarina' => 'of-time')), |
|
108
|
|
|
), |
|
109
|
|
|
|
|
110
|
|
|
// test direct property setting method |
|
111
|
|
|
array( |
|
112
|
|
|
array('id' => 60, 'protect' => 'link'), |
|
113
|
|
|
(new SerializableMock1()) |
|
114
|
|
|
->setId(60) |
|
115
|
|
|
->setProtectedProtect('link'), |
|
116
|
|
|
), |
|
117
|
|
|
|
|
118
|
|
|
// test with a non serializable class |
|
119
|
|
|
array( |
|
120
|
|
|
array('id' => 70, 'mock3' => array('ganon' => 'dorf')), |
|
121
|
|
|
(new SerializableMock1()) |
|
122
|
|
|
->setId(70) |
|
123
|
|
|
->setMock3($ganonDorf), |
|
124
|
|
|
), |
|
125
|
|
|
|
|
126
|
|
|
// test with a datetime |
|
127
|
|
|
array( |
|
128
|
|
|
array('id' => 75, 'date' => (new \DateTime('2015-07-01'))->format(\DateTime::ISO8601)), |
|
129
|
|
|
(new SerializableMock1()) |
|
130
|
|
|
->setId(75) |
|
131
|
|
|
->setDate(new \DateTime('2015-07-01')), |
|
132
|
|
|
), |
|
133
|
|
|
|
|
134
|
|
|
// tests with unsupported hinting |
|
135
|
|
|
array( |
|
136
|
|
|
array('id' => 80, 'callback' => array($this, 'serializedDataProvider')), |
|
137
|
|
|
(new SerializableMock1()) |
|
138
|
|
|
->setId(80) |
|
139
|
|
|
->setCallback(array($this, 'serializedDataProvider')), |
|
140
|
|
|
), |
|
141
|
|
|
|
|
142
|
|
|
// serializable child class |
|
143
|
|
|
array( |
|
144
|
|
|
array('id' => 90, 'mock2' => array('id' => 100)), |
|
145
|
|
|
(new SerializableMock1()) |
|
146
|
|
|
->setId(90) |
|
147
|
|
|
->setMock2((new SerializableMock2())->setId(100)), |
|
148
|
|
|
), |
|
149
|
|
|
); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|