Skip to content

Commit a0f02f3

Browse files
Extra tests
1 parent 8f4b2bc commit a0f02f3

13 files changed

+376
-120
lines changed

src/Collections/TransformedCollection.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,24 @@ public function onlyChanged(): Generator
9393
}
9494
}
9595

96-
public function findTransformedByPath(string $path): ?Transformed
96+
public function findTransformedByFile(string $path): ?Transformed
9797
{
9898
$path = $this->cleanupFilePath($path);
9999

100100
return $this->fileMapping[$path] ?? null;
101101
}
102102

103+
public function findTransformedByDirectory(string $path): Generator
104+
{
105+
$path = $this->cleanupFilePath($path);
106+
107+
foreach ($this->fileMapping as $transformedPath => $transformed) {
108+
if (str_starts_with($transformedPath, $path)) {
109+
yield $transformed;
110+
}
111+
}
112+
}
113+
103114
public function hasChanges(): bool
104115
{
105116
foreach ($this->items as $item) {

src/Handlers/Watch/DirectoryDeletedWatchEventHandler.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,27 @@
22

33
namespace Spatie\TypeScriptTransformer\Handlers\Watch;
44

5+
use Spatie\TypeScriptTransformer\Collections\TransformedCollection;
6+
use Spatie\TypeScriptTransformer\Events\Watch\DirectoryDeletedWatchEvent;
7+
use Spatie\TypeScriptTransformer\TypeScriptTransformer;
8+
59
class DirectoryDeletedWatchEventHandler implements WatchEventHandler
610
{
11+
public function __construct(
12+
protected TypeScriptTransformer $typeScriptTransformer,
13+
protected TransformedCollection $transformedCollection,
14+
) {
15+
}
16+
17+
/**
18+
* @param DirectoryDeletedWatchEvent $event
19+
*/
720
public function handle($event): void
821
{
9-
// TODO: Implement handle() method.
22+
$transformedItems = $this->transformedCollection->findTransformedByDirectory($event->path);
23+
24+
foreach ($transformedItems as $transformed) {
25+
$this->transformedCollection->remove($transformed->reference);
26+
}
1027
}
1128
}

src/Handlers/Watch/FileDeletedWatchEventHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct(
1919
*/
2020
public function handle($event): void
2121
{
22-
$transformed = $this->transformedCollection->findTransformedByPath($event->path);
22+
$transformed = $this->transformedCollection->findTransformedByFile($event->path);
2323

2424
if ($transformed === null) {
2525
return;

src/Handlers/Watch/FileUpdatedOrCreatedWatchEventHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function handle($event): void
4343
throw $throwable;
4444
}
4545

46-
$originalTransformed = $this->transformedCollection->findTransformedByPath(
46+
$originalTransformed = $this->transformedCollection->findTransformedByFile(
4747
$event->path
4848
);
4949

src/Transformed/Transformed.php

+2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ public function markReferenceMissing(
129129
unset($this->references[$key]);
130130

131131
$this->missingReferences[$key] = $typeReferences;
132+
133+
$this->markAsChanged();
132134
}
133135

134136
public function markAsChanged(): void

src/TypeScriptTransformer.php

+15-7
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,11 @@ public function execute(): void
7171
* - Further write docs + check them -> only Laravel specific stuff
7272
* - Check old Laravel tests if we missed something
7373
* - Check in Flare whether everything is working as expected -> PR ready, needs fixing TS
74+
* - Make sure nullables can be exported as optional: https://github.com/spatie/typescript-transformer/pull/88/files
7475
* - Release
7576
*/
7677

77-
$transformedCollection = $this->provideTypesAction->execute();
78-
79-
$this->executeProvidedClosuresAction->execute($transformedCollection);
80-
81-
$this->connectReferencesAction->execute($transformedCollection);
82-
83-
$this->executeConnectedClosuresAction->execute($transformedCollection);
78+
$transformedCollection = $this->resolveTransformedCollection();
8479

8580
$this->outputTransformed($transformedCollection);
8681

@@ -94,6 +89,19 @@ public function execute(): void
9489
}
9590
}
9691

92+
public function resolveTransformedCollection(): TransformedCollection
93+
{
94+
$transformedCollection = $this->provideTypesAction->execute();
95+
96+
$this->executeProvidedClosuresAction->execute($transformedCollection);
97+
98+
$this->connectReferencesAction->execute($transformedCollection);
99+
100+
$this->executeConnectedClosuresAction->execute($transformedCollection);
101+
102+
return $transformedCollection;
103+
}
104+
97105
public function outputTransformed(
98106
TransformedCollection $transformedCollection,
99107
): void {

tests/Actions/ConnectReferencesActionTest.php

-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525

2626
$action->execute($collection);
2727

28-
ray($transformedClass, $transformedEnum);
29-
3028
expect($transformedEnum->references)->toHaveCount(0);
3129
expect($transformedEnum->referencedBy)->toHaveCount(1);
3230
expect($transformedEnum->referencedBy)->toContain($transformedClass->reference->getKey());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?php
2+
3+
use Spatie\TypeScriptTransformer\Collections\TransformedCollection;
4+
use Spatie\TypeScriptTransformer\References\ClassStringReference;
5+
use Spatie\TypeScriptTransformer\References\CustomReference;
6+
use Spatie\TypeScriptTransformer\Tests\Fakes\Circular\CircularA;
7+
use Spatie\TypeScriptTransformer\Tests\Fakes\Circular\CircularB;
8+
use Spatie\TypeScriptTransformer\Tests\Fakes\TypesToProvide\SimpleClass;
9+
use Spatie\TypeScriptTransformer\Tests\Fakes\TypesToProvide\TypeScriptAttributedClass;
10+
use Spatie\TypeScriptTransformer\Tests\Support\AllClassTransformer;
11+
use Spatie\TypeScriptTransformer\Transformed\Transformed;
12+
use Spatie\TypeScriptTransformer\TypeScriptNodes\TypeReference;
13+
use Spatie\TypeScriptTransformer\TypeScriptNodes\TypeScriptString;
14+
use Spatie\TypeScriptTransformer\TypeScriptTransformer;
15+
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfigFactory;
16+
17+
it('can create a transformed collection', function () {
18+
$collection = new TransformedCollection([
19+
$initialTransformed = transformSingle(SimpleClass::class),
20+
]);
21+
22+
expect($collection)->toHaveCount(1);
23+
});
24+
25+
it('can add transformed items to the collection', function () {
26+
$collection = new TransformedCollection();
27+
28+
$collection->add(
29+
$initialTransformed = transformSingle(SimpleClass::class),
30+
);
31+
32+
expect($collection)->toHaveCount(1);
33+
});
34+
35+
it('can get a transformed item by reference', function () {
36+
$collection = new TransformedCollection([
37+
$classTransformed = transformSingle(SimpleClass::class),
38+
$manualTransformed = new Transformed(
39+
new TypeScriptString(),
40+
new CustomReference('vendor', 'package'),
41+
[],
42+
),
43+
]);
44+
45+
expect($collection->has(new ClassStringReference(SimpleClass::class)))->toBeTrue();
46+
expect($collection->get(new ClassStringReference(SimpleClass::class)))->toBe($classTransformed);
47+
expect($collection->has(new CustomReference('vendor', 'package')))->toBeTrue();
48+
expect($collection->get(new CustomReference('vendor', 'package')))->toBe($manualTransformed);
49+
});
50+
51+
it('can loop over items in the collection', function () {
52+
$collection = new TransformedCollection([
53+
$a = transformSingle(SimpleClass::class),
54+
$b = transformSingle(TypeScriptAttributedClass::class),
55+
]);
56+
57+
$found = [];
58+
59+
foreach ($collection as $transformed) {
60+
$found[] = $transformed;
61+
}
62+
63+
expect($found)->toBe([$a, $b]);
64+
});
65+
66+
it('can loop over only changed items in the collection', function () {
67+
$collection = new TransformedCollection([
68+
$a = transformSingle(SimpleClass::class),
69+
$b = transformSingle(TypeScriptAttributedClass::class),
70+
]);
71+
72+
$a->changed = true;
73+
$b->changed = false;
74+
75+
$found = [];
76+
77+
foreach ($collection->onlyChanged() as $transformed) {
78+
$found[] = $transformed;
79+
}
80+
81+
expect($found)->toBe([$a]);
82+
});
83+
84+
it('all items added to the collection are marked as changed', function () {
85+
new TransformedCollection([
86+
$a = transformSingle(SimpleClass::class),
87+
$b = transformSingle(TypeScriptAttributedClass::class),
88+
]);
89+
90+
expect($a->changed)->toBeTrue();
91+
expect($b->changed)->toBeTrue();
92+
});
93+
94+
it('can find transformed items by file path', function () {
95+
$collection = new TransformedCollection([
96+
$transformed = transformSingle(SimpleClass::class),
97+
]);
98+
99+
$path = __DIR__.'/../Fakes/TypesToProvide/SimpleClass.php';
100+
101+
expect($collection->findTransformedByFile($path))->toBe($transformed);
102+
});
103+
104+
it('can find transformed items by directory path', function () {
105+
$collection = new TransformedCollection([
106+
$a = transformSingle(SimpleClass::class),
107+
$b = transformSingle(TypeScriptAttributedClass::class),
108+
$c = transformSingle(CircularA::class),
109+
]);
110+
111+
$path = __DIR__.'/../Fakes/TypesToProvide';
112+
113+
$found = [];
114+
115+
foreach ($collection->findTransformedByDirectory($path) as $transformed) {
116+
$found[] = $transformed;
117+
}
118+
119+
expect($found)->toBe([$a, $b]);
120+
});
121+
122+
it('can check if any items in the collection have changed', function () {
123+
$collection = new TransformedCollection([
124+
$a = transformSingle(SimpleClass::class),
125+
$b = transformSingle(TypeScriptAttributedClass::class),
126+
]);
127+
128+
$a->changed = false;
129+
$b->changed = false;
130+
131+
expect($collection->hasChanges())->toBeFalse();
132+
133+
$a->changed = true;
134+
135+
expect($collection->hasChanges())->toBeTrue();
136+
});
137+
138+
it('can remove a transformed item by reference', function () {
139+
$collection = new TransformedCollection([
140+
transformSingle(SimpleClass::class),
141+
]);
142+
143+
$collection->remove(new ClassStringReference(SimpleClass::class));
144+
145+
expect($collection->has(new ClassStringReference(SimpleClass::class)))->toBeFalse();
146+
});
147+
148+
it('can remove a transformed item by reference and update references', function () {
149+
$collection = TypeScriptTransformer::create(
150+
TypeScriptTransformerConfigFactory::create()
151+
->transformer(new AllClassTransformer())
152+
->watchDirectories(__DIR__.'/../Fakes/Circular')
153+
)->resolveTransformedCollection();
154+
155+
foreach ($collection as $transformed) {
156+
$transformed->changed = false;
157+
}
158+
159+
$collection->remove($referenceA = new ClassStringReference(CircularA::class));
160+
161+
expect($collection)->toHaveCount(1);
162+
163+
$transformedB = $collection->get($referenceB = new ClassStringReference(CircularB::class));
164+
165+
expect($transformedB->changed)->toBeTrue();
166+
expect($transformedB->missingReferences)->toHaveCount(1);
167+
expect($transformedB->missingReferences)->toHaveKey($referenceA->getKey());
168+
expect($transformedB->missingReferences[$referenceA->getKey()])
169+
->toBeArray()
170+
->each()
171+
->toBeInstanceOf(TypeReference::class);
172+
});

tests/Fakes/Integration/Enum.php

-9
This file was deleted.

tests/Fakes/Integration/IntegrationClass.php

-82
This file was deleted.

0 commit comments

Comments
 (0)