Skip to content

Commit

Permalink
Extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Oct 4, 2024
1 parent 8f4b2bc commit a0f02f3
Show file tree
Hide file tree
Showing 13 changed files with 376 additions and 120 deletions.
13 changes: 12 additions & 1 deletion src/Collections/TransformedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,24 @@ public function onlyChanged(): Generator
}
}

public function findTransformedByPath(string $path): ?Transformed
public function findTransformedByFile(string $path): ?Transformed
{
$path = $this->cleanupFilePath($path);

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

public function findTransformedByDirectory(string $path): Generator
{
$path = $this->cleanupFilePath($path);

foreach ($this->fileMapping as $transformedPath => $transformed) {
if (str_starts_with($transformedPath, $path)) {
yield $transformed;
}
}
}

public function hasChanges(): bool
{
foreach ($this->items as $item) {
Expand Down
19 changes: 18 additions & 1 deletion src/Handlers/Watch/DirectoryDeletedWatchEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@

namespace Spatie\TypeScriptTransformer\Handlers\Watch;

use Spatie\TypeScriptTransformer\Collections\TransformedCollection;
use Spatie\TypeScriptTransformer\Events\Watch\DirectoryDeletedWatchEvent;
use Spatie\TypeScriptTransformer\TypeScriptTransformer;

class DirectoryDeletedWatchEventHandler implements WatchEventHandler
{
public function __construct(
protected TypeScriptTransformer $typeScriptTransformer,
protected TransformedCollection $transformedCollection,
) {
}

/**
* @param DirectoryDeletedWatchEvent $event
*/
public function handle($event): void
{
// TODO: Implement handle() method.
$transformedItems = $this->transformedCollection->findTransformedByDirectory($event->path);

foreach ($transformedItems as $transformed) {
$this->transformedCollection->remove($transformed->reference);
}
}
}
2 changes: 1 addition & 1 deletion src/Handlers/Watch/FileDeletedWatchEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(
*/
public function handle($event): void
{
$transformed = $this->transformedCollection->findTransformedByPath($event->path);
$transformed = $this->transformedCollection->findTransformedByFile($event->path);

if ($transformed === null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function handle($event): void
throw $throwable;
}

$originalTransformed = $this->transformedCollection->findTransformedByPath(
$originalTransformed = $this->transformedCollection->findTransformedByFile(
$event->path
);

Expand Down
2 changes: 2 additions & 0 deletions src/Transformed/Transformed.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public function markReferenceMissing(
unset($this->references[$key]);

$this->missingReferences[$key] = $typeReferences;

$this->markAsChanged();
}

public function markAsChanged(): void
Expand Down
22 changes: 15 additions & 7 deletions src/TypeScriptTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,11 @@ public function execute(): void
* - Further write docs + check them -> only Laravel specific stuff
* - Check old Laravel tests if we missed something
* - Check in Flare whether everything is working as expected -> PR ready, needs fixing TS
* - Make sure nullables can be exported as optional: https://github.com/spatie/typescript-transformer/pull/88/files
* - Release
*/

$transformedCollection = $this->provideTypesAction->execute();

$this->executeProvidedClosuresAction->execute($transformedCollection);

$this->connectReferencesAction->execute($transformedCollection);

$this->executeConnectedClosuresAction->execute($transformedCollection);
$transformedCollection = $this->resolveTransformedCollection();

$this->outputTransformed($transformedCollection);

Expand All @@ -94,6 +89,19 @@ public function execute(): void
}
}

public function resolveTransformedCollection(): TransformedCollection
{
$transformedCollection = $this->provideTypesAction->execute();

$this->executeProvidedClosuresAction->execute($transformedCollection);

$this->connectReferencesAction->execute($transformedCollection);

$this->executeConnectedClosuresAction->execute($transformedCollection);

return $transformedCollection;
}

public function outputTransformed(
TransformedCollection $transformedCollection,
): void {
Expand Down
2 changes: 0 additions & 2 deletions tests/Actions/ConnectReferencesActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

$action->execute($collection);

ray($transformedClass, $transformedEnum);

expect($transformedEnum->references)->toHaveCount(0);
expect($transformedEnum->referencedBy)->toHaveCount(1);
expect($transformedEnum->referencedBy)->toContain($transformedClass->reference->getKey());
Expand Down
172 changes: 172 additions & 0 deletions tests/Collections/TransformedCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

use Spatie\TypeScriptTransformer\Collections\TransformedCollection;
use Spatie\TypeScriptTransformer\References\ClassStringReference;
use Spatie\TypeScriptTransformer\References\CustomReference;
use Spatie\TypeScriptTransformer\Tests\Fakes\Circular\CircularA;
use Spatie\TypeScriptTransformer\Tests\Fakes\Circular\CircularB;
use Spatie\TypeScriptTransformer\Tests\Fakes\TypesToProvide\SimpleClass;
use Spatie\TypeScriptTransformer\Tests\Fakes\TypesToProvide\TypeScriptAttributedClass;
use Spatie\TypeScriptTransformer\Tests\Support\AllClassTransformer;
use Spatie\TypeScriptTransformer\Transformed\Transformed;
use Spatie\TypeScriptTransformer\TypeScriptNodes\TypeReference;
use Spatie\TypeScriptTransformer\TypeScriptNodes\TypeScriptString;
use Spatie\TypeScriptTransformer\TypeScriptTransformer;
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfigFactory;

it('can create a transformed collection', function () {
$collection = new TransformedCollection([
$initialTransformed = transformSingle(SimpleClass::class),
]);

expect($collection)->toHaveCount(1);
});

it('can add transformed items to the collection', function () {
$collection = new TransformedCollection();

$collection->add(
$initialTransformed = transformSingle(SimpleClass::class),
);

expect($collection)->toHaveCount(1);
});

it('can get a transformed item by reference', function () {
$collection = new TransformedCollection([
$classTransformed = transformSingle(SimpleClass::class),
$manualTransformed = new Transformed(
new TypeScriptString(),
new CustomReference('vendor', 'package'),
[],
),
]);

expect($collection->has(new ClassStringReference(SimpleClass::class)))->toBeTrue();
expect($collection->get(new ClassStringReference(SimpleClass::class)))->toBe($classTransformed);
expect($collection->has(new CustomReference('vendor', 'package')))->toBeTrue();
expect($collection->get(new CustomReference('vendor', 'package')))->toBe($manualTransformed);
});

it('can loop over items in the collection', function () {
$collection = new TransformedCollection([
$a = transformSingle(SimpleClass::class),
$b = transformSingle(TypeScriptAttributedClass::class),
]);

$found = [];

foreach ($collection as $transformed) {
$found[] = $transformed;
}

expect($found)->toBe([$a, $b]);
});

it('can loop over only changed items in the collection', function () {
$collection = new TransformedCollection([
$a = transformSingle(SimpleClass::class),
$b = transformSingle(TypeScriptAttributedClass::class),
]);

$a->changed = true;
$b->changed = false;

$found = [];

foreach ($collection->onlyChanged() as $transformed) {
$found[] = $transformed;
}

expect($found)->toBe([$a]);
});

it('all items added to the collection are marked as changed', function () {
new TransformedCollection([
$a = transformSingle(SimpleClass::class),
$b = transformSingle(TypeScriptAttributedClass::class),
]);

expect($a->changed)->toBeTrue();
expect($b->changed)->toBeTrue();
});

it('can find transformed items by file path', function () {
$collection = new TransformedCollection([
$transformed = transformSingle(SimpleClass::class),
]);

$path = __DIR__.'/../Fakes/TypesToProvide/SimpleClass.php';

expect($collection->findTransformedByFile($path))->toBe($transformed);
});

it('can find transformed items by directory path', function () {
$collection = new TransformedCollection([
$a = transformSingle(SimpleClass::class),
$b = transformSingle(TypeScriptAttributedClass::class),
$c = transformSingle(CircularA::class),
]);

$path = __DIR__.'/../Fakes/TypesToProvide';

$found = [];

foreach ($collection->findTransformedByDirectory($path) as $transformed) {
$found[] = $transformed;
}

expect($found)->toBe([$a, $b]);
});

it('can check if any items in the collection have changed', function () {
$collection = new TransformedCollection([
$a = transformSingle(SimpleClass::class),
$b = transformSingle(TypeScriptAttributedClass::class),
]);

$a->changed = false;
$b->changed = false;

expect($collection->hasChanges())->toBeFalse();

$a->changed = true;

expect($collection->hasChanges())->toBeTrue();
});

it('can remove a transformed item by reference', function () {
$collection = new TransformedCollection([
transformSingle(SimpleClass::class),
]);

$collection->remove(new ClassStringReference(SimpleClass::class));

expect($collection->has(new ClassStringReference(SimpleClass::class)))->toBeFalse();
});

it('can remove a transformed item by reference and update references', function () {
$collection = TypeScriptTransformer::create(
TypeScriptTransformerConfigFactory::create()
->transformer(new AllClassTransformer())
->watchDirectories(__DIR__.'/../Fakes/Circular')
)->resolveTransformedCollection();

foreach ($collection as $transformed) {
$transformed->changed = false;
}

$collection->remove($referenceA = new ClassStringReference(CircularA::class));

expect($collection)->toHaveCount(1);

$transformedB = $collection->get($referenceB = new ClassStringReference(CircularB::class));

expect($transformedB->changed)->toBeTrue();
expect($transformedB->missingReferences)->toHaveCount(1);
expect($transformedB->missingReferences)->toHaveKey($referenceA->getKey());
expect($transformedB->missingReferences[$referenceA->getKey()])
->toBeArray()
->each()
->toBeInstanceOf(TypeReference::class);
});
9 changes: 0 additions & 9 deletions tests/Fakes/Integration/Enum.php

This file was deleted.

82 changes: 0 additions & 82 deletions tests/Fakes/Integration/IntegrationClass.php

This file was deleted.

Loading

0 comments on commit a0f02f3

Please sign in to comment.