Skip to content

Commit 8a90735

Browse files
committed
Add tests for universal serializer trait
1 parent e4fa24e commit 8a90735

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

EProcess/Behaviour/UniversalSerializer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ trait UniversalSerializer
1313
public function serialize($data)
1414
{
1515
$pack = [];
16+
$type = gettype($data);
1617

1718
if ($data instanceof ArrayCollection) {
1819
$data = $data->toArray();
1920
} elseif (!is_array($data)) {
2021
$data = [$data];
2122
}
2223

24+
$data['type'] = $type;
25+
2326
foreach ($data as $key => $piece) {
2427
switch (gettype($piece)) {
2528
case 'object':
@@ -47,6 +50,9 @@ public function unserialize($data)
4750
$unpack = [];
4851
$data = is_array($data) ? $data : unserialize($data);
4952

53+
$type = unserialize($data['type']);
54+
unset($data['type']);
55+
5056
foreach ($data as $key => $piece) {
5157
$piece = unserialize($piece);
5258

@@ -70,7 +76,7 @@ public function unserialize($data)
7076
}
7177
}
7278

73-
return 1 === count($unpack) ? current($unpack) : $unpack;
79+
return $type !== 'array' && 1 === count($unpack) ? current($unpack) : $unpack;
7480
}
7581

7682
public function findSerializer()

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
},
2222
"license": "MIT",
2323
"require-dev": {
24-
"divi/pthreads-stub": "dev-master"
24+
"divi/pthreads-stub": "dev-master",
25+
"phpunit/phpunit": "^5.3"
2526
},
2627
"autoload": {
2728
"psr-4": {
@@ -30,7 +31,8 @@
3031
},
3132
"autoload-dev": {
3233
"psr-4": {
33-
"Examples\\": "examples/"
34+
"Examples\\": "examples/",
35+
"Tests\\": "tests/"
3436
}
3537
}
3638
}

phpunit.xml.dist

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+

0 commit comments

Comments
 (0)