1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Thunder\Platenum\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Thunder\Platenum\Enum\AbstractCallbackEnum; |
7
|
|
|
use Thunder\Platenum\Enum\AbstractConstantsEnum; |
8
|
|
|
use Thunder\Platenum\Enum\AbstractDocblockEnum; |
9
|
|
|
use Thunder\Platenum\Enum\AbstractStaticEnum; |
10
|
|
|
use Thunder\Platenum\Enum\CallbackEnumTrait; |
11
|
|
|
use Thunder\Platenum\Enum\ConstantsEnumTrait; |
12
|
|
|
use Thunder\Platenum\Enum\DocblockEnumTrait; |
13
|
|
|
use Thunder\Platenum\Enum\EnumTrait; |
14
|
|
|
use Thunder\Platenum\Enum\StaticEnumTrait; |
15
|
|
|
use Thunder\Platenum\Tests\Fake\FakeEnum; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractTestCase extends TestCase |
21
|
|
|
{ |
22
|
|
|
/** @return FakeEnum */ |
23
|
|
|
protected function makeRawEnum(array $members): string |
24
|
|
|
{ |
25
|
|
|
$entries = []; |
26
|
|
|
foreach($members as $member => $value) { |
27
|
|
|
$entries[] = sprintf('%s => %s', '\''.$member.'\'', is_string($value) ? '\''.$value.'\'' : $value); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$class = $this->computeUniqueClassName('RawEnum'); |
31
|
|
|
eval('final class '.$class.' implements \JsonSerializable { |
|
|
|
|
32
|
|
|
use '.EnumTrait::class.'; |
33
|
|
|
private static function resolve(): array { return ['.implode(', ', $entries).']; } |
34
|
|
|
}'); |
35
|
|
|
|
36
|
|
|
return $class; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** @return FakeEnum */ |
40
|
|
|
protected function makeDocblockTraitEnum(array $members): string |
41
|
|
|
{ |
42
|
|
|
$entries = []; |
43
|
|
|
foreach($members as $member => $value) { |
44
|
|
|
$entries[] = sprintf(' * @method static self %s()', $member); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$class = $this->computeUniqueClassName('DocblockTrait'); |
48
|
|
|
eval('/**'."\n".implode("\n", $entries)."\n".'*/ final class '.$class.' { use '.DocblockEnumTrait::class.'; }'); |
|
|
|
|
49
|
|
|
|
50
|
|
|
return $class; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** @return FakeEnum */ |
54
|
|
|
protected function makeDocblockExtendsEnum(array $members): string |
55
|
|
|
{ |
56
|
|
|
$entries = []; |
57
|
|
|
foreach($members as $member => $value) { |
58
|
|
|
$entries[] = sprintf(' * @method static self %s()', $member); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$class = $this->computeUniqueClassName('DocblockExtends'); |
62
|
|
|
eval('/**'."\n".implode("\n", $entries)."\n".'*/ final class '.$class.' extends '.AbstractDocblockEnum::class.' {}'); |
|
|
|
|
63
|
|
|
|
64
|
|
|
return $class; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** @return FakeEnum */ |
68
|
|
|
protected function makeConstantsTraitEnum(array $members): string |
69
|
|
|
{ |
70
|
|
|
$entries = []; |
71
|
|
|
foreach($members as $member => $value) { |
72
|
|
|
$entries[] = sprintf('private const %s = %s;', $member, is_string($value) ? '\''.$value.'\'' : $value); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$class = $this->computeUniqueClassName('ConstantsTrait'); |
76
|
|
|
eval('final class '.$class.' implements \JsonSerializable { '.implode("\n", $entries).' use '.ConstantsEnumTrait::class.'; }'); |
|
|
|
|
77
|
|
|
|
78
|
|
|
return $class; |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** @return FakeEnum */ |
82
|
|
|
protected function makeConstantsExtendsEnum(array $members): string |
83
|
|
|
{ |
84
|
|
|
$entries = []; |
85
|
|
|
foreach($members as $member => $value) { |
86
|
|
|
$entries[] = sprintf('private const %s = %s;', $member, is_string($value) ? '\''.$value.'\'' : $value); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$class = $this->computeUniqueClassName('ConstantsExtends'); |
90
|
|
|
eval('final class '.$class.' extends '.AbstractConstantsEnum::class.' { '.implode("\n", $entries).' }'); |
|
|
|
|
91
|
|
|
|
92
|
|
|
return $class; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** @return FakeEnum */ |
96
|
|
|
protected function makeStaticTraitEnum(array $members): string |
97
|
|
|
{ |
98
|
|
|
$entries = []; |
99
|
|
|
foreach($members as $member => $value) { |
100
|
|
|
$entries[] = sprintf('%s => %s', '\''.$member.'\'', is_string($value) ? '\''.$value.'\'' : $value); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$class = $this->computeUniqueClassName('StaticTrait'); |
104
|
|
|
eval('final class '.$class.' implements \JsonSerializable { private static $mapping = ['.implode(', ', $entries).']; use '.StaticEnumTrait::class.'; }'); |
|
|
|
|
105
|
|
|
|
106
|
|
|
return $class; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** @return FakeEnum */ |
110
|
|
|
protected function makeStaticExtendsEnum(array $members): string |
111
|
|
|
{ |
112
|
|
|
$entries = []; |
113
|
|
|
foreach($members as $member => $value) { |
114
|
|
|
$entries[] = sprintf('%s => %s', '\''.$member.'\'', is_string($value) ? '\''.$value.'\'' : $value); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$class = $this->computeUniqueClassName('StaticExtends'); |
118
|
|
|
eval('final class '.$class.' extends '.AbstractStaticEnum::class.' { protected static $mapping = ['.implode(', ', $entries).']; }'); |
|
|
|
|
119
|
|
|
|
120
|
|
|
return $class; |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** @return FakeEnum */ |
124
|
|
|
protected function makeCallbackTraitEnum(array $members): string |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$class = $this->computeUniqueClassName('CallbackTrait'); |
127
|
|
|
eval('final class '.$class.' implements \JsonSerializable { use '.CallbackEnumTrait::class.'; }'); |
|
|
|
|
128
|
|
|
|
129
|
|
|
return $class; |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** @return FakeEnum */ |
133
|
|
|
protected function makeCallbackExtendsEnum(array $members): string |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
$class = $this->computeUniqueClassName('CallbackExtends'); |
136
|
|
|
eval('final class '.$class.' extends '.AbstractCallbackEnum::class.' { }'); |
|
|
|
|
137
|
|
|
|
138
|
|
|
return $class; |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function computeUniqueClassName(string $prefix): string |
142
|
|
|
{ |
143
|
|
|
while(true === class_exists($class = $prefix.random_int(1, 1000000))) { |
144
|
|
|
continue; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $class; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|