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