-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
JavaScriptConverterTest.php
230 lines (197 loc) · 6.45 KB
/
JavaScriptConverterTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
declare(strict_types=1);
namespace Bakame\Aide\Enum;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use ValueError;
final class JavaScriptConverterTest extends TestCase
{
#[Test]
public function it_will_fails_with_a_negative_indent_size(): void
{
$this->expectException(ValueError::class);
JavaScriptConverter::new()->indentSize(-1);
}
#[Test]
public function it_will_fails_converting_a_non_enum(): void
{
$this->expectException(ValueError::class);
JavaScriptConverter::new()->convertToObject(self::class); /* @phpstan-ignore-line */
}
#[Test]
public function it_will_convert_to_a_javascript_immutable_object_by_default(): void
{
$expected = <<<JS
const HttpStatusCode = Object.freeze({
HTTP_OK: 200,
HTTP_REDIRECTION: 302,
HTTP_NOT_FOUND: 404,
HTTP_SERVER_ERROR: 500
})
JS;
$converter = JavaScriptConverter::new();
$altConverter = $converter
->useImmutability()
->ignoreExport()
->ignoreSymbol()
->indentSize(2);
self::assertSame($expected, $converter->convertToObject(HttpStatusCode::class));
self::assertSame($expected, $altConverter->convertToObject(HttpStatusCode::class));
}
#[Test]
public function it_can_convert_to_a_javascript_mutable_object(): void
{
$expected = <<<JS
export const Foobar = {
http_ok: Symbol(200),
http_redirection: Symbol(302),
http_not_found: Symbol(404),
http_server_error: Symbol(500)
}
JS;
self::assertSame(
$expected,
JavaScriptConverter::new()
->ignoreImmutability()
->propertyNameCase(strtolower(...))
->useSymbol()
->useExport()
->indentSize(4)
->convertToObject(HttpStatusCode::class, 'Foobar')
);
}
#[Test]
public function it_will_convert_to_a_javascript_class_by_default(): void
{
$expected = <<<JS
class HttpStatusCode {
static HTTP_OK = new HttpStatusCode(200)
static HTTP_REDIRECTION = new HttpStatusCode(302)
static HTTP_NOT_FOUND = new HttpStatusCode(404)
static HTTP_SERVER_ERROR = new HttpStatusCode(500)
constructor(name) {
this.name = name
}
}
JS;
$converter = JavaScriptConverter::new();
$altConverter = $converter
->useImmutability()
->ignoreExport()
->ignoreSymbol()
->indentSize(2);
self::assertSame($expected, $converter->convertToClass(HttpStatusCode::class));
self::assertSame($expected, $altConverter->convertToClass(HttpStatusCode::class));
}
#[Test]
public function it_can_convert_to_a_javascript_class(): void
{
$pascalCase = fn (string $word): string => implode('', array_map(
ucfirst(...),
explode(
' ',
strtolower(str_replace(['_', '-'], [' ', ' '], $word))
)
));
$expected = <<<JS
export default class Foobar {
static Ok = new Foobar(200)
static Redirection = new Foobar(302)
static NotFound = new Foobar(404)
static ServerError = new Foobar(500)
constructor(name) {
this.name = name
}
}
JS;
$converter = JavaScriptConverter::new()
->useImmutability()
->ignoreSymbol()
->indentSize(4)
->useExportDefault()
->propertyNameCase(fn (string $name) => $pascalCase(strtolower(str_replace('HTTP_', '', $name))));
self::assertSame(
$expected,
$converter->convertToClass(HttpStatusCode::class, 'Foobar')
);
}
#[Test]
public function it_can_convert_to_a_javascript_object_with_export_default_and_a_variable_name(): void
{
$pascalCase = fn (string $word): string => implode('', array_map(
ucfirst(...),
explode(
' ',
strtolower(str_replace(['_', '-'], [' ', ' '], $word))
)
));
$expected = <<<JS
const StatusCode = Object.freeze({
Ok: Symbol(200),
Redirection: Symbol(302),
NotFound: Symbol(404),
ServerError: Symbol(500)
});
export default StatusCode;
JS;
$actual = JavaScriptConverter::new()
->useImmutability()
->useExportDefault()
->useSymbol()
->indentSize(4)
->propertyNameCase(fn (string $name) => $pascalCase(strtolower(str_replace('HTTP_', '', $name))))
->convertToObject(HttpStatusCode::class, 'StatusCode');
self::assertSame($expected, $actual);
}
#[Test]
public function it_can_convert_to_a_javascript_object_with_export_default_and_no_variable(): void
{
$pascalCase = fn (string $word): string => implode('', array_map(
ucfirst(...),
explode(
' ',
strtolower(str_replace(['_', '-'], [' ', ' '], $word))
)
));
$expected = <<<JS
export default Object.freeze({Ok: Symbol(200),Redirection: Symbol(302),NotFound: Symbol(404),ServerError: Symbol(500)})
JS;
$actual = JavaScriptConverter::new()
->useImmutability()
->useExportDefault()
->useSymbol()
->indentSize(0)
->propertyNameCase(fn (string $name) => $pascalCase(strtolower(str_replace('HTTP_', '', $name))))
->convertToObject(HttpStatusCode::class, null);
self::assertSame($expected, $actual);
}
#[Test]
public function it_can_convert_a_pure_enum_with_different_starting_value(): void
{
$actualStartAtZero = JavaScriptConverter::new()
->indentSize(0)
->useTrailingComma()
->convertToObject(Dir::class);
$actualStartAtFortyTwo = JavaScriptConverter::new()
->indentSize(0)
->useTrailingComma()
->valueStartAt(42)
->convertToObject(Dir::class);
$expectedZero = <<<JS
const Dir = Object.freeze({Top: Symbol(0),Down: Symbol(1),Left: Symbol(2),Right: Symbol(3),})
JS;
$expectedFortyTwo = <<<JS
const Dir = Object.freeze({Top: Symbol(42),Down: Symbol(43),Left: Symbol(44),Right: Symbol(45),})
JS;
self::assertNotSame($actualStartAtFortyTwo, $actualStartAtZero);
self::assertSame($expectedZero, $actualStartAtZero);
self::assertSame($expectedFortyTwo, $actualStartAtFortyTwo);
}
}
enum Dir
{
case Top;
case Down;
case Left;
case Right;
}