|
| 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 | +}); |
0 commit comments