|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Koded\Stdlib\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
use Koded\Exceptions\SerializerException; |
|
6
|
|
|
use Koded\Stdlib\Interfaces\Serializer; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class SerializerFactoryTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
public function test_native() |
|
13
|
|
|
{ |
|
14
|
|
|
$json = SerializerFactory::new(Serializer::JSON); |
|
15
|
|
|
$xml = SerializerFactory::new(Serializer::XML, 'root'); |
|
16
|
|
|
$php = SerializerFactory::new(Serializer::PHP); |
|
17
|
|
|
|
|
18
|
|
|
$this->assertSame(Serializer::JSON, $json->type()); |
|
19
|
|
|
$this->assertSame(Serializer::XML, $xml->type()); |
|
20
|
|
|
$this->assertSame(Serializer::PHP, $php->type()); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function test_igbinary() |
|
24
|
|
|
{ |
|
25
|
|
|
if (false === function_exists('igbinary_serialize')) { |
|
26
|
|
|
$this->markTestSkipped('igbinary extension is not loaded'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$igbinary = SerializerFactory::new(Serializer::IGBINARY); |
|
30
|
|
|
$this->assertSame(Serializer::IGBINARY, $igbinary->type()); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function test_msgpack() |
|
34
|
|
|
{ |
|
35
|
|
|
if (false === extension_loaded('msgpack')) { |
|
36
|
|
|
$this->markTestSkipped('msgpack extension is not loaded'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$msgpack = SerializerFactory::new(Serializer::MSGPACK); |
|
40
|
|
|
$this->assertSame(Serializer::MSGPACK, $msgpack->type()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function test_custom() |
|
44
|
|
|
{ |
|
45
|
|
|
$serializer = SerializerFactory::new(TestSerializer::class); |
|
46
|
|
|
$this->assertSame(TestSerializer::class, $serializer->type()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function test_exception() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->expectException(SerializerException::class); |
|
52
|
|
|
$this->expectExceptionCode(409); |
|
53
|
|
|
$this->expectExceptionMessage('Failed to create a serializer for "fubar"'); |
|
54
|
|
|
|
|
55
|
|
|
SerializerFactory::new('fubar'); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
class TestSerializer implements Serializer |
|
61
|
|
|
{ |
|
62
|
|
|
public function serialize($value): string {} |
|
63
|
|
|
public function unserialize($value) {} |
|
64
|
|
|
public function type(): string { return self::class; } |
|
65
|
|
|
} |
|
66
|
|
|
|