2
2
3
3
use phpDocumentor \Reflection \Type ;
4
4
use phpDocumentor \Reflection \Types \String_ ;
5
- use function PHPUnit \Framework \assertEquals ;
6
- use function Spatie \Snapshots \assertMatchesSnapshot ;
7
- use function Spatie \Snapshots \assertMatchesTextSnapshot ;
8
5
use Spatie \TypeScriptTransformer \Attributes \Hidden ;
9
6
use Spatie \TypeScriptTransformer \Attributes \LiteralTypeScriptType ;
10
7
use Spatie \TypeScriptTransformer \Attributes \Optional ;
20
17
use Spatie \TypeScriptTransformer \TypeProcessors \TypeProcessor ;
21
18
use Spatie \TypeScriptTransformer \TypeScriptTransformerConfig ;
22
19
20
+ use function PHPUnit \Framework \assertEquals ;
21
+ use function Spatie \Snapshots \assertMatchesSnapshot ;
22
+ use function Spatie \Snapshots \assertMatchesTextSnapshot ;
23
+
23
24
beforeEach (function () {
24
25
$ config = TypeScriptTransformerConfig::create ()
25
26
->defaultTypeReplacements ([
48
49
it ('a type processor can remove properties ' , function () {
49
50
$ config = TypeScriptTransformerConfig::create ();
50
51
51
- $ transformer = new class ($ config ) extends DtoTransformer {
52
+ $ transformer = new class ($ config ) extends DtoTransformer {
52
53
protected function typeProcessors (): array
53
54
{
54
- $ onlyStringPropertiesProcessor = new class implements TypeProcessor {
55
+ $ onlyStringPropertiesProcessor = new class () implements TypeProcessor {
55
56
public function process (
56
57
Type $ type ,
57
58
ReflectionProperty | ReflectionParameter | ReflectionMethod $ reflection ,
@@ -74,7 +75,7 @@ public function process(
74
75
});
75
76
76
77
it ('will take transform as typescript attributes into account ' , function () {
77
- $ class = new class {
78
+ $ class = new class () {
78
79
#[TypeScriptType('int ' )]
79
80
public $ int ;
80
81
@@ -102,7 +103,7 @@ public function process(
102
103
});
103
104
104
105
it ('transforms properties to optional ones when using optional attribute ' , function () {
105
- $ class = new class {
106
+ $ class = new class () {
106
107
#[Optional]
107
108
public string $ string ;
108
109
};
@@ -133,7 +134,7 @@ class DummyOptionalDto
133
134
134
135
135
136
it ('transforms properties to hidden ones when using hidden attribute ' , function () {
136
- $ class = new class () {
137
+ $ class = new class () {
137
138
public string $ visible ;
138
139
#[Hidden]
139
140
public string $ hidden ;
@@ -146,3 +147,17 @@ class DummyOptionalDto
146
147
147
148
assertMatchesSnapshot ($ type ->transformed );
148
149
});
150
+
151
+ it ('transforms nullable properties to optional ones according to config ' , function () {
152
+ $ class = new class () {
153
+ public ?string $ string ;
154
+ };
155
+
156
+ $ config = TypeScriptTransformerConfig::create ()->nullToOptional (true );
157
+ $ type = (new DtoTransformer ($ config ))->transform (
158
+ new ReflectionClass ($ class ),
159
+ 'Typed '
160
+ );
161
+
162
+ $ this ->assertMatchesSnapshot ($ type ->transformed );
163
+ });
0 commit comments