File tree Expand file tree Collapse file tree 4 files changed +88
-3
lines changed Expand file tree Collapse file tree 4 files changed +88
-3
lines changed Original file line number Diff line number Diff line change @@ -13,13 +13,16 @@ trait UniversalSerializer
13
13
public function serialize ($ data )
14
14
{
15
15
$ pack = [];
16
+ $ type = gettype ($ data );
16
17
17
18
if ($ data instanceof ArrayCollection) {
18
19
$ data = $ data ->toArray ();
19
20
} elseif (!is_array ($ data )) {
20
21
$ data = [$ data ];
21
22
}
22
23
24
+ $ data ['type ' ] = $ type ;
25
+
23
26
foreach ($ data as $ key => $ piece ) {
24
27
switch (gettype ($ piece )) {
25
28
case 'object ' :
@@ -47,6 +50,9 @@ public function unserialize($data)
47
50
$ unpack = [];
48
51
$ data = is_array ($ data ) ? $ data : unserialize ($ data );
49
52
53
+ $ type = unserialize ($ data ['type ' ]);
54
+ unset($ data ['type ' ]);
55
+
50
56
foreach ($ data as $ key => $ piece ) {
51
57
$ piece = unserialize ($ piece );
52
58
@@ -70,7 +76,7 @@ public function unserialize($data)
70
76
}
71
77
}
72
78
73
- return 1 === count ($ unpack ) ? current ($ unpack ) : $ unpack ;
79
+ return $ type !== ' array ' && 1 === count ($ unpack ) ? current ($ unpack ) : $ unpack ;
74
80
}
75
81
76
82
public function findSerializer ()
Original file line number Diff line number Diff line change 21
21
},
22
22
"license" : " MIT" ,
23
23
"require-dev" : {
24
- "divi/pthreads-stub" : " dev-master"
24
+ "divi/pthreads-stub" : " dev-master" ,
25
+ "phpunit/phpunit" : " ^5.3"
25
26
},
26
27
"autoload" : {
27
28
"psr-4" : {
30
31
},
31
32
"autoload-dev" : {
32
33
"psr-4" : {
33
- "Examples\\ " : " examples/"
34
+ "Examples\\ " : " examples/" ,
35
+ "Tests\\ " : " tests/"
34
36
}
35
37
}
36
38
}
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+
3
+ <!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
4
+ <phpunit xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
5
+ xsi : noNamespaceSchemaLocation =" http://schema.phpunit.de/4.1/phpunit.xsd"
6
+ backupGlobals =" false"
7
+ colors =" true"
8
+ bootstrap =" examples/autoload.php"
9
+ >
10
+ <php >
11
+ <ini name =" error_reporting" value =" -1" />
12
+ <server name =" KERNEL_DIR" value =" app/" />
13
+ </php >
14
+
15
+ <testsuites >
16
+ <testsuite name =" Project Test Suite" >
17
+ <directory >tests</directory >
18
+ </testsuite >
19
+ </testsuites >
20
+ </phpunit >
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Tests \EProcess \Behaviour ;
4
+
5
+ use EProcess \Behaviour \UniversalSerializer ;
6
+ use Examples \Simple \Model \Transaction ;
7
+
8
+ class UniversalSerializerTest extends \PHPUnit_Framework_TestCase
9
+ {
10
+ /**
11
+ * @test
12
+ */
13
+ public function should_serialize_array ()
14
+ {
15
+ $ this ->assertData (['abcde ' => 'dbce ' ]);
16
+ }
17
+
18
+ /**
19
+ * @test
20
+ */
21
+ public function should_serialize_scalar ()
22
+ {
23
+ $ this ->assertData ('asdasd ' );
24
+ }
25
+
26
+ /**
27
+ * @test
28
+ */
29
+ public function should_serialize_integer ()
30
+ {
31
+ $ this ->assertData (5123123 );
32
+ }
33
+
34
+ /**
35
+ * @test
36
+ */
37
+ public function should_serialize_object ()
38
+ {
39
+ $ this ->assertData (new Transaction ('EUR ' , 1235 ));
40
+ }
41
+
42
+ private function assertData ($ data )
43
+ {
44
+ $ serializer = new SomeSerializer ();
45
+
46
+ $ serialized = $ serializer ->serialize ($ data );
47
+ $ unserialized = $ serializer ->unserialize ($ serialized );
48
+
49
+ $ this ->assertEquals ($ data , $ unserialized );
50
+ }
51
+ }
52
+
53
+ class SomeSerializer
54
+ {
55
+ use UniversalSerializer;
56
+ }
57
+
You can’t perform that action at this time.
0 commit comments