|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Spatie\TypeScriptTransformer\Structures\TransformedType; |
| 6 | +use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig; |
| 7 | +use WendellAdriel\ValidatedDTO\Support\TypeScriptTransformer; |
| 8 | + |
| 9 | +it('returns null when class does not extend SimpleDTO', function () { |
| 10 | + $class = new class() |
| 11 | + { |
| 12 | + public string $name; |
| 13 | + }; |
| 14 | + |
| 15 | + $reflection = new ReflectionClass($class); |
| 16 | + |
| 17 | + $transformer = new TypeScriptTransformer(TypeScriptTransformerConfig::create()); |
| 18 | + $type = $transformer->transform($reflection, 'IrrelevantName'); |
| 19 | + |
| 20 | + expect($type)->toBeNull(); |
| 21 | +}); |
| 22 | + |
| 23 | +it('transforms a SimpleDTO with public properties into a TransformedType', function () { |
| 24 | + eval(' |
| 25 | + namespace App\Data { |
| 26 | + use WendellAdriel\ValidatedDTO\SimpleDTO; |
| 27 | + use WendellAdriel\ValidatedDTO\Concerns\EmptyRules; |
| 28 | + use WendellAdriel\ValidatedDTO\Concerns\EmptyCasts; |
| 29 | + use WendellAdriel\ValidatedDTO\Concerns\EmptyDefaults; |
| 30 | + class TestTransformerDTO extends SimpleDTO { |
| 31 | + use EmptyRules, EmptyCasts, EmptyDefaults; |
| 32 | +
|
| 33 | + public string $name; |
| 34 | + public int $age; |
| 35 | + public static string $shouldNotAppear = "excluded"; |
| 36 | + protected string $invisible = "excluded"; |
| 37 | + } |
| 38 | + } |
| 39 | + '); |
| 40 | + |
| 41 | + $reflection = new ReflectionClass(\App\Data\TestTransformerDTO::class); |
| 42 | + |
| 43 | + $transformer = new TypeScriptTransformer(TypeScriptTransformerConfig::create()); |
| 44 | + $type = $transformer->transform($reflection, 'TransformedDTO'); |
| 45 | + |
| 46 | + // Should only include public, non-static properties |
| 47 | + expect($type)->toBeInstanceOf(TransformedType::class) |
| 48 | + ->and($type->name)->toBe('TransformedDTO') |
| 49 | + ->and($type->transformed)->toContain('name: string;') |
| 50 | + ->and($type->transformed)->toContain('age: number;') |
| 51 | + ->and($type->transformed)->not->toContain('shouldNotAppear') |
| 52 | + ->and($type->transformed)->not->toContain('invisible'); |
| 53 | +}); |
| 54 | + |
| 55 | +it('excludes properties listed in excludedProperties', function () { |
| 56 | + eval(' |
| 57 | + namespace App\Data { |
| 58 | + use WendellAdriel\ValidatedDTO\ValidatedDTO; |
| 59 | + use WendellAdriel\ValidatedDTO\Concerns\EmptyRules; |
| 60 | + use WendellAdriel\ValidatedDTO\Concerns\EmptyCasts; |
| 61 | + use WendellAdriel\ValidatedDTO\Concerns\EmptyDefaults; |
| 62 | + class ExcludedPropertyDTO extends ValidatedDTO { |
| 63 | + use EmptyRules, EmptyCasts, EmptyDefaults; |
| 64 | +
|
| 65 | + public bool $lazyValidation = true; // excluded by default |
| 66 | + public string $title; |
| 67 | + } |
| 68 | + } |
| 69 | + '); |
| 70 | + |
| 71 | + $reflection = new ReflectionClass(\App\Data\ExcludedPropertyDTO::class); |
| 72 | + |
| 73 | + $transformer = new TypeScriptTransformer(TypeScriptTransformerConfig::create()); |
| 74 | + $type = $transformer->transform($reflection, 'ExcludedProps'); |
| 75 | + |
| 76 | + expect($type->transformed)->not->toContain('lazyValidation:') |
| 77 | + ->and($type->transformed)->toContain('title: string;') |
| 78 | + ->and($type->getTypeScriptName())->toBe('App.Data.ExcludedProps'); |
| 79 | +}); |
| 80 | + |
| 81 | +it('transforms a ValidatedDTO with nested DTO and enum property', function () { |
| 82 | + eval(' |
| 83 | + namespace App\Enums { |
| 84 | + enum FakeStatusEnum: string { |
| 85 | + case FIRST = "first"; |
| 86 | + case SECOND = "second"; |
| 87 | + } |
| 88 | + } |
| 89 | + '); |
| 90 | + |
| 91 | + eval(' |
| 92 | + namespace App\Data { |
| 93 | + use WendellAdriel\ValidatedDTO\ValidatedDTO; |
| 94 | + use WendellAdriel\ValidatedDTO\Concerns\EmptyRules; |
| 95 | + use WendellAdriel\ValidatedDTO\Concerns\EmptyCasts; |
| 96 | + use WendellAdriel\ValidatedDTO\Concerns\EmptyDefaults; |
| 97 | + class ChildDTO extends ValidatedDTO { |
| 98 | + use EmptyRules, EmptyCasts, EmptyDefaults; |
| 99 | +
|
| 100 | + public string $childField; |
| 101 | + } |
| 102 | + } |
| 103 | + '); |
| 104 | + |
| 105 | + eval(' |
| 106 | + namespace App\Data { |
| 107 | + use WendellAdriel\ValidatedDTO\ValidatedDTO; |
| 108 | + use WendellAdriel\ValidatedDTO\Concerns\EmptyRules; |
| 109 | + use WendellAdriel\ValidatedDTO\Concerns\EmptyCasts; |
| 110 | + use WendellAdriel\ValidatedDTO\Concerns\EmptyDefaults; |
| 111 | + use App\Enums\FakeStatusEnum; |
| 112 | +
|
| 113 | + class ParentDTO extends ValidatedDTO { |
| 114 | + use EmptyRules, EmptyCasts, EmptyDefaults; |
| 115 | +
|
| 116 | + public FakeStatusEnum $status; |
| 117 | + public ChildDTO $child; |
| 118 | + } |
| 119 | + } |
| 120 | + '); |
| 121 | + |
| 122 | + $reflection = new ReflectionClass(\App\Data\ParentDTO::class); |
| 123 | + $transformer = new TypeScriptTransformer(TypeScriptTransformerConfig::create()); |
| 124 | + $type = $transformer->transform($reflection, 'ComplexDTO'); |
| 125 | + |
| 126 | + expect($type)->toBeInstanceOf(TransformedType::class) |
| 127 | + ->and($type->name)->toBe('ComplexDTO') |
| 128 | + ->and($type->transformed)->toContain('status: {%App\Enums\FakeStatusEnum%};') |
| 129 | + ->and($type->transformed)->toContain('child: {%App\Data\ChildDTO%};') |
| 130 | + ->and($type->missingSymbols->all()) |
| 131 | + // Missing Symbols contain references to other types. Once all types are |
| 132 | + // transformed, the package will replace these references with their |
| 133 | + // TypeScript types. When no type is found the type will default to any. |
| 134 | + ->toContain(\App\Enums\FakeStatusEnum::class) |
| 135 | + ->and($type->missingSymbols->all())->toContain(\App\Data\ChildDTO::class); |
| 136 | +}); |
0 commit comments