1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MabeEnumTest; |
4
|
|
|
|
5
|
|
|
use MabeEnumTest\TestAsset\SerializableEnum; |
6
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Unit tests for the trait MabeEnum\EnumSerializableTrait |
11
|
|
|
* |
12
|
|
|
* @link http://github.com/marc-mabe/php-enum for the canonical source repository |
13
|
|
|
* @copyright Copyright (c) 2015 Marc Bennewitz |
14
|
|
|
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License |
15
|
|
|
*/ |
16
|
|
|
class EnumSerializableTraitTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
public function setUp() |
19
|
|
|
{ |
20
|
|
|
if (PHP_VERSION_ID < 50400) { |
21
|
|
|
$this->markTestSkipped('Traits require PHP-5.4'); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testSerializeSerializableEnum() |
26
|
|
|
{ |
27
|
|
|
$serialized = serialize(SerializableEnum::get(SerializableEnum::NIL)); |
28
|
|
|
$this->assertInternalType('string', $serialized); |
29
|
|
|
|
30
|
|
|
$unserialized = unserialize($serialized); |
31
|
|
|
$this->assertInstanceOf('MabeEnumTest\TestAsset\SerializableEnum', $unserialized); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testUnserializeFirstWillHoldTheSameInstance() |
35
|
|
|
{ |
36
|
|
|
$serialized = serialize(SerializableEnum::get(SerializableEnum::STR)); |
37
|
|
|
$this->assertInternalType('string', $serialized); |
38
|
|
|
|
39
|
|
|
// clear all instantiated instances so we can virtual test unserializing first |
40
|
|
|
$this->clearEnumeration('MabeEnumTest\TestAsset\SerializableEnum'); |
41
|
|
|
|
42
|
|
|
// First unserialize |
43
|
|
|
$unserialized = unserialize($serialized); |
44
|
|
|
$this->assertInstanceOf('MabeEnumTest\TestAsset\SerializableEnum', $unserialized); |
45
|
|
|
|
46
|
|
|
// second instantiate |
47
|
|
|
$enum = SerializableEnum::get($unserialized->getValue()); |
48
|
|
|
|
49
|
|
|
// check if it's the same instance |
50
|
|
|
$this->assertSame($enum, $unserialized); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testUnserializeThrowsRuntimeExceptionOnUnknownValue() |
54
|
|
|
{ |
55
|
|
|
$this->setExpectedException('RuntimeException'); |
56
|
|
|
unserialize('C:39:"MabeEnumTest\TestAsset\SerializableEnum":11:{s:4:"test";}'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testUnserializeThrowsRuntimeExceptionOnInvalidValue() |
60
|
|
|
{ |
61
|
|
|
$this->setExpectedException('RuntimeException'); |
62
|
|
|
unserialize('C:39:"MabeEnumTest\TestAsset\SerializableEnum":19:{O:8:"stdClass":0:{}}'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testUnserializeThrowsLogicExceptionOnChangingValue() |
66
|
|
|
{ |
67
|
|
|
$this->setExpectedException('LogicException'); |
68
|
|
|
$enum = SerializableEnum::get(SerializableEnum::INT); |
69
|
|
|
$enum->unserialize(serialize(SerializableEnum::STR)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Clears all instantiated enumerations and detected constants of the given enumerator |
74
|
|
|
* @param string $enumeration |
75
|
|
|
*/ |
76
|
|
|
private function clearEnumeration($enumeration) |
77
|
|
|
{ |
78
|
|
|
$reflClass = new ReflectionClass($enumeration); |
79
|
|
|
while ($reflClass->getName() !== 'MabeEnum\Enum') { |
80
|
|
|
$reflClass = $reflClass->getParentClass(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$reflPropInstances = $reflClass->getProperty('instances'); |
84
|
|
|
$reflPropInstances->setAccessible(true); |
85
|
|
|
$reflPropInstances->setValue(null, array()); |
86
|
|
|
$reflPropConstants = $reflClass->getProperty('constants'); |
87
|
|
|
$reflPropConstants->setAccessible(true); |
88
|
|
|
$reflPropConstants->setValue(null, array()); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|