Skip to content

Commit 472e8f1

Browse files
committed
Test TypeScript synchronization
1 parent 85ac7b7 commit 472e8f1

File tree

9 files changed

+183
-31
lines changed

9 files changed

+183
-31
lines changed

tests/Feature/EnumMakeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
Enums::setBasePath(__DIR__ . '/../Skeleton');
3131
Enums::setPaths('app/Enums', 'domain/*/Enums');
3232

33-
expect(fn() => runEnum("make \"{$enum}\" CaseOne CaseTwo --backed={$backed}"))->toGenerate($enum);
33+
expect(fn() => runEnum("make \"{$enum}\" CaseOne CaseTwo --backed={$backed}"))->toGenerate($enum, cli: true);
3434

3535
(fn() => self::$paths = [])->bindTo(null, Enums::class)();
3636
(fn() => self::$basePath = null)->bindTo(null, Enums::class)();

tests/Feature/EnumTsTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Cerbero\Enum\Enums;
4+
5+
it('warns that no enums were synced if invalid enums are provided', function() {
6+
expect(runEnum('ts InvalidEnum'))
7+
->output->toContain('No enums to synchronize.')
8+
->status->toBe(0);
9+
});
10+
11+
it('warns that no enums were synced if no enums can be found', function() {
12+
expect(runEnum('ts --all'))
13+
->output->toContain('No enums to synchronize.')
14+
->status->toBe(0);
15+
});
16+
17+
it('synchronizes all the discoverable enums', function() {
18+
Enums::setBasePath(__DIR__ . '/../Skeleton');
19+
Enums::setPaths('app/Enums', 'domain/*/Enums');
20+
21+
expect($namespaces = [...Enums::namespaces()])->toBe([
22+
App\Enums\Enum1::class,
23+
App\Enums\Enum2::class,
24+
Domain\Common\Enums\Enum3::class,
25+
Domain\Payouts\Enums\Enum4::class,
26+
]);
27+
28+
$enums = array_map(fn($namespace) => "\"{$namespace}\"", $namespaces);
29+
30+
expect(fn() => runEnum('ts ' . implode(' ', $enums)))->toTypeScript($namespaces);
31+
32+
(fn() => self::$paths = [])->bindTo(null, Enums::class)();
33+
(fn() => self::$basePath = null)->bindTo(null, Enums::class)();
34+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
use Cerbero\Enum\Enums;
4+
use Cerbero\Enum\Services\TypeScript;
5+
6+
use function Cerbero\Enum\className;
7+
8+
it('creates and appends enums', function() {
9+
Enums::setBasePath(__DIR__ . '/../Skeleton');
10+
11+
$enums = [
12+
App\Enums\Enum1::class,
13+
App\Enums\Enum2::class,
14+
Domain\Common\Enums\Enum3::class,
15+
];
16+
17+
foreach ($enums as $enum) {
18+
expect((new TypeScript($enum))->sync())->toBeTrue();
19+
}
20+
21+
expect(fn() => (new TypeScript('Domain\Payouts\Enums\Enum4'))->sync())->toTypeScript([$enum]);
22+
23+
(fn() => self::$basePath = null)->bindTo(null, Enums::class)();
24+
});
25+
26+
it('replaces enums', function() {
27+
Enums::setBasePath(__DIR__ . '/../Skeleton');
28+
29+
$enums = [
30+
App\Enums\Enum1::class,
31+
App\Enums\Enum2::class,
32+
Domain\Common\Enums\Enum3::class,
33+
Domain\Payouts\Enums\Enum4::class,
34+
];
35+
36+
foreach ($enums as $enum) {
37+
expect((new TypeScript($enum))->sync())->toBeTrue();
38+
}
39+
40+
expect(fn() => (new TypeScript('Domain\Payouts\Enums\Enum4'))->sync(overwrite: true))->toTypeScript([$enum]);
41+
42+
(fn() => self::$basePath = null)->bindTo(null, Enums::class)();
43+
});
44+
45+
it('creates custom TypeScript files', function(string $enum) {
46+
Enums::setBasePath(__DIR__ . '/../Skeleton');
47+
Enums::setTypeScript(fn (string $enum) => 'resources/js/enums/' . className($enum) . '.ts');
48+
49+
expect(fn() => (new TypeScript($enum))->sync())->toTypeScript([$enum], oneFile: false);
50+
51+
(fn() => self::$typeScript = 'resources/js/enums/index.ts')->bindTo(null, Enums::class)();
52+
(fn() => self::$basePath = null)->bindTo(null, Enums::class)();
53+
})->with([
54+
App\Enums\Enum1::class,
55+
App\Enums\Enum2::class,
56+
Domain\Common\Enums\Enum3::class,
57+
Domain\Payouts\Enums\Enum4::class,
58+
]);

tests/Pest.php

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
|
2525
*/
2626

27+
use Cerbero\Enum\Enums;
28+
2729
use function Cerbero\Enum\className;
2830
use function Cerbero\Enum\cli;
2931
use function Cerbero\Enum\namespaceToPath;
32+
use function Cerbero\Enum\path;
3033

3134
expect()->extend('toAnnotate', function (array $enums, bool $overwrite = false) {
3235
$oldContents = [];
@@ -38,13 +41,7 @@
3841
}
3942

4043
try {
41-
if (is_bool($value = ($this->value)())) {
42-
expect($value)->toBeTrue();
43-
} else {
44-
expect($value)
45-
->output->toContain(...$enums)
46-
->status->toBe(0);
47-
}
44+
expect($this->value)->toProcessEnums($enums);
4845

4946
foreach ($oldContents as $filename => $oldContent) {
5047
$stub = __DIR__ . '/stubs/annotate/' . basename($filename, '.php') . '.stub';
@@ -53,11 +50,7 @@
5350
$stub = $path;
5451
}
5552

56-
// normalize content to avoid end-of-line incompatibility between OS
57-
$enumContent = str_replace("\r\n", "\n", file_get_contents($filename));
58-
$stubContent = str_replace("\r\n", "\n", file_get_contents($stub));
59-
60-
expect($enumContent)->toBe($stubContent);
53+
expect($filename)->toContainIgnoreEol($stub);
6154
}
6255
} finally {
6356
foreach ($oldContents as $filename => $oldContent) {
@@ -66,35 +59,61 @@
6659
}
6760
});
6861

69-
expect()->extend('toGenerate', function (string $enum) {
70-
expect(class_exists($enum))->toBeFalse();
62+
expect()->extend('toProcessEnums', function (array $enums) {
63+
$value = ($this->value)();
64+
65+
if (is_bool($value)) {
66+
expect($value)->toBeTrue();
67+
} else {
68+
expect($value)
69+
->output->toContain(...$enums)
70+
->status->toBe(0);
71+
}
72+
});
73+
74+
expect()->extend('toContainIgnoreEol', function (string $path) {
75+
// normalize content to avoid end-of-line incompatibility between OS
76+
$actualContent = str_replace("\r\n", "\n", file_get_contents(path($this->value)));
77+
$expectedContent = str_replace("\r\n", "\n", file_get_contents(path($path)));
78+
79+
expect($actualContent)->toBe($expectedContent);
80+
});
7181

72-
$directory = 'make';
82+
expect()->extend('toGenerate', function (string $enum, bool $cli = false) {
83+
expect(class_exists($enum))->toBeFalse();
7384

7485
try {
75-
if (is_bool($value = ($this->value)())) {
76-
expect($value)->toBeTrue();
77-
78-
$directory = 'generator';
79-
} else {
80-
expect($value)
81-
->output->toContain($enum)
82-
->status->toBe(0);
83-
}
86+
expect($this->value)->toProcessEnums([$enum]);
8487

8588
$filename = namespaceToPath($enum);
89+
$directory = $cli ? 'make' : 'generator';
8690
$stub = sprintf('%s/stubs/%s/%s.stub', __DIR__, $directory, className($enum));
8791

88-
// normalize content to avoid end-of-line incompatibility between OS
89-
$enumContent = str_replace("\r\n", "\n", file_get_contents($filename));
90-
$stubContent = str_replace("\r\n", "\n", file_get_contents($stub));
91-
92-
expect($enumContent)->toBe($stubContent);
92+
expect($filename)->toContainIgnoreEol($stub);
9393
} finally {
9494
file_exists($filename) && unlink($filename);
9595
}
9696
});
9797

98+
expect()->extend('toTypeScript', function (array $enums, bool $oneFile = true) {
99+
expect($this->value)->toProcessEnums($enums);
100+
101+
$paths = [];
102+
103+
try {
104+
foreach ($enums as $enum) {
105+
$paths[] = $path = Enums::basePath(Enums::typeScript($enum));
106+
$stub = sprintf('%s/stubs/ts/%s.stub', __DIR__, $oneFile ? 'enums' : className($enum));
107+
108+
expect($path)->toContainIgnoreEol($stub);
109+
}
110+
} finally {
111+
foreach ($paths as $path) {
112+
file_exists($path) && unlink($path);
113+
}
114+
}
115+
});
116+
98117
/*
99118
|--------------------------------------------------------------------------
100119
| Functions
@@ -113,9 +132,11 @@
113132
*/
114133
function runEnum(string $command): stdClass
115134
{
135+
$paths = sprintf('--base-path="%s" --paths="%s"', Enums::basePath(), implode(',', Enums::paths()));
136+
116137
ob_start();
117138

118-
cli($command, $status);
139+
cli("{$command} {$paths}", $status);
119140

120141
$output = ob_get_clean();
121142

tests/stubs/ts/Enum1.stub

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum Enum1 {
2+
One = 1,
3+
Two = 2,
4+
Three = 3,
5+
}

tests/stubs/ts/Enum2.stub

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export enum Enum2 {
2+
3+
}

tests/stubs/ts/Enum3.stub

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum Enum3 {
2+
One = 'number 1',
3+
Two = 'number 2',
4+
Three = 'number 3',
5+
}

tests/stubs/ts/Enum4.stub

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum Enum4 {
2+
One = 1,
3+
Two = 2,
4+
Three = 4,
5+
}

tests/stubs/ts/enums.stub

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export enum Enum1 {
2+
One = 1,
3+
Two = 2,
4+
Three = 3,
5+
}
6+
7+
export enum Enum2 {
8+
9+
}
10+
11+
export enum Enum3 {
12+
One = 'number 1',
13+
Two = 'number 2',
14+
Three = 'number 3',
15+
}
16+
17+
export enum Enum4 {
18+
One = 1,
19+
Two = 2,
20+
Three = 4,
21+
}

0 commit comments

Comments
 (0)