Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
162 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,27 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelTypeScriptTransformer\Tests\Transformers; | ||
|
||
use ReflectionClass; | ||
use Spatie\LaravelTypeScriptTransformer\Tests\FakeClasses\Dto\Dto; | ||
use Spatie\LaravelTypeScriptTransformer\Tests\FakeClasses\Dto\OtherDto; | ||
use Spatie\LaravelTypeScriptTransformer\Tests\TestCase; | ||
use Spatie\LaravelTypeScriptTransformer\Transformers\DtoTransformer; | ||
use Spatie\Snapshots\MatchesSnapshots; | ||
use Spatie\TypeScriptTransformer\Tests\FakeClasses\Integration\OtherDtoCollection; | ||
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig; | ||
|
||
class DtoTransformerTest extends TestCase | ||
{ | ||
private DtoTransformer $transformer; | ||
|
||
use MatchesSnapshots; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->transformer = new DtoTransformer( | ||
resolve(TypeScriptTransformerConfig::class) | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_transform_a_dto() | ||
{ | ||
$type = $this->transformer->transform( | ||
new ReflectionClass(Dto::class), | ||
'FakeDto' | ||
); | ||
|
||
$this->assertMatchesSnapshot($type->transformed); | ||
$this->assertEquals([ | ||
OtherDto::class, | ||
OtherDtoCollection::class, | ||
], $type->missingSymbols->all()); | ||
$this->assertFalse($type->isInline); | ||
} | ||
} | ||
beforeEach(function () { | ||
$this->transformer = new DtoTransformer( | ||
resolve(TypeScriptTransformerConfig::class) | ||
); | ||
}); | ||
|
||
it('can transform a dto', function () { | ||
$type = $this->transformer->transform( | ||
new ReflectionClass(Dto::class), | ||
'FakeDto' | ||
); | ||
|
||
expect($type->transformed)->toMatchSnapshot(); | ||
expect([ | ||
OtherDto::class, | ||
OtherDtoCollection::class, | ||
])->toEqual($type->missingSymbols->all()); | ||
expect($type->isInline)->toBeFalse(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,37 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelTypeScriptTransformer\Tests\Transformers; | ||
|
||
use DateTime; | ||
use ReflectionClass; | ||
use Spatie\LaravelTypeScriptTransformer\Tests\FakeClasses\ChildState; | ||
use Spatie\LaravelTypeScriptTransformer\Tests\FakeClasses\State; | ||
use Spatie\LaravelTypeScriptTransformer\Tests\TestCase; | ||
use Spatie\LaravelTypeScriptTransformer\Transformers\SpatieStateTransformer; | ||
|
||
class SpatieStateTransformerTest extends TestCase | ||
{ | ||
private SpatieStateTransformer $transformer; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->transformer = new SpatieStateTransformer(); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_only_convert_states() | ||
{ | ||
$this->assertNotNull($this->transformer->transform( | ||
new ReflectionClass(State::class), | ||
'State' | ||
)); | ||
|
||
$this->assertNull($this->transformer->transform( | ||
new ReflectionClass(ChildState::class), | ||
'State' | ||
)); | ||
|
||
$this->assertNull($this->transformer->transform( | ||
new ReflectionClass(DateTime::class), | ||
'State' | ||
)); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_transform_an_state() | ||
{ | ||
$type = $this->transformer->transform( | ||
new ReflectionClass(State::class), | ||
'FakeState' | ||
); | ||
|
||
$this->assertEquals("'child' | 'other_child'", $type->transformed); | ||
$this->assertTrue($type->missingSymbols->isEmpty()); | ||
$this->assertFalse($type->isInline); | ||
} | ||
} | ||
beforeEach(function () { | ||
$this->transformer = new SpatieStateTransformer(); | ||
}); | ||
|
||
it('will only convert states', function () { | ||
expect($this->transformer->transform( | ||
new ReflectionClass(State::class), | ||
'State' | ||
))->not->toBeNull(); | ||
|
||
expect($this->transformer->transform( | ||
new ReflectionClass(ChildState::class), | ||
'State' | ||
))->toBeNull(); | ||
|
||
expect($this->transformer->transform( | ||
new ReflectionClass(DateTime::class), | ||
'State' | ||
))->toBeNull(); | ||
}); | ||
|
||
it('can transform an state', function () { | ||
$type = $this->transformer->transform( | ||
new ReflectionClass(State::class), | ||
'FakeState' | ||
); | ||
|
||
expect($type->transformed)->toEqual("'child' | 'other_child'"); | ||
expect($type->missingSymbols->isEmpty())->toBeTrue(); | ||
expect($type->isInline)->toBeFalse(); | ||
}); |
146 changes: 64 additions & 82 deletions
146
tests/TypeProcessors/LaravelCollectionTypeProcessorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,75 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelTypeScriptTransformer\Tests\TypeProcessors; | ||
|
||
use Illuminate\Support\Collection; | ||
use phpDocumentor\Reflection\Type; | ||
use phpDocumentor\Reflection\TypeResolver; | ||
use ReflectionProperty; | ||
use Spatie\LaravelTypeScriptTransformer\Tests\TestCase; | ||
use Spatie\LaravelTypeScriptTransformer\TypeProcessors\LaravelCollectionTypeProcessor; | ||
use Spatie\TypeScriptTransformer\Structures\MissingSymbolsCollection; | ||
use Spatie\TypeScriptTransformer\TypeReflectors\TypeReflector; | ||
|
||
class LaravelCollectionTypeProcessorTest extends TestCase | ||
function processType(object $class, string $property): Type | ||
{ | ||
private LaravelCollectionTypeProcessor $processor; | ||
|
||
private TypeResolver $typeResolver; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->processor = new LaravelCollectionTypeProcessor(); | ||
|
||
$this->typeResolver = new TypeResolver(); | ||
} | ||
|
||
/** @test */ | ||
public function it_works_with_single_types() | ||
{ | ||
$class = new class { | ||
/** @var int[] */ | ||
public Collection $propertyA; | ||
|
||
/** @var ?int[] */ | ||
public Collection $propertyB; | ||
|
||
/** @var int[]|null */ | ||
public ?Collection $propertyC; | ||
|
||
/** @var array */ | ||
public Collection $propertyD; | ||
|
||
/** @var ?array */ | ||
public ?Collection $propertyE; | ||
|
||
/** @var array|null */ | ||
public ?Collection $propertyF; | ||
|
||
/** @var \Illuminate\Support\Collection */ | ||
public Collection $propertyG; | ||
|
||
/** @var \Illuminate\Support\Collection|int[] */ | ||
public Collection $propertyH; | ||
|
||
/** @var \Illuminate\Support\Collection|int[]|null */ | ||
public ?Collection $propertyI; | ||
}; | ||
|
||
$this->assertEquals('int[]', (string) $this->processType($class, 'propertyA')); | ||
$this->assertEquals('?int[]', (string) $this->processType($class, 'propertyB')); | ||
$this->assertEquals('int[]|null', (string) $this->processType($class, 'propertyC')); | ||
$this->assertEquals('array', (string) $this->processType($class, 'propertyD')); | ||
$this->assertEquals('?array', (string) $this->processType($class, 'propertyE')); | ||
$this->assertEquals('array|null', (string) $this->processType($class, 'propertyF')); | ||
$this->assertEquals('array', (string) $this->processType($class, 'propertyG')); | ||
$this->assertEquals('int[]', (string) $this->processType($class, 'propertyH')); | ||
$this->assertEquals('int[]|null', (string) $this->processType($class, 'propertyI')); | ||
} | ||
|
||
/** @test */ | ||
public function it_works_with_union_types() | ||
{ | ||
$class = new class { | ||
/** @var \Illuminate\Support\Collection|int[] */ | ||
public Collection|array $property; | ||
}; | ||
|
||
$this->assertEquals('int[]', (string) $this->processType($class, 'property')); | ||
} | ||
|
||
private function processType(object $class, string $property): Type | ||
{ | ||
$reflection = new ReflectionProperty($class, $property); | ||
|
||
return $this->processor->process( | ||
TypeReflector::new($reflection)->reflectFromDocblock(), | ||
$reflection, | ||
new MissingSymbolsCollection() | ||
); | ||
} | ||
$reflection = new ReflectionProperty($class, $property); | ||
|
||
return test()->processor->process( | ||
TypeReflector::new($reflection)->reflectFromDocblock(), | ||
$reflection, | ||
new MissingSymbolsCollection() | ||
); | ||
} | ||
|
||
beforeEach(function () { | ||
$this->processor = new LaravelCollectionTypeProcessor(); | ||
|
||
$this->typeResolver = new TypeResolver(); | ||
}); | ||
|
||
it('works with single types', function () { | ||
$class = new class { | ||
/** @var int[] */ | ||
public Collection $propertyA; | ||
|
||
/** @var ?int[] */ | ||
public Collection $propertyB; | ||
|
||
/** @var int[]|null */ | ||
public ?Collection $propertyC; | ||
|
||
/** @var array */ | ||
public Collection $propertyD; | ||
|
||
/** @var ?array */ | ||
public ?Collection $propertyE; | ||
|
||
/** @var array|null */ | ||
public ?Collection $propertyF; | ||
|
||
/** @var \Illuminate\Support\Collection */ | ||
public Collection $propertyG; | ||
|
||
/** @var \Illuminate\Support\Collection|int[] */ | ||
public Collection $propertyH; | ||
|
||
/** @var \Illuminate\Support\Collection|int[]|null */ | ||
public ?Collection $propertyI; | ||
}; | ||
|
||
expect((string) processType($class, 'propertyA'))->toEqual('int[]'); | ||
expect((string) processType($class, 'propertyB'))->toEqual('?int[]'); | ||
expect((string) processType($class, 'propertyC'))->toEqual('int[]|null'); | ||
expect((string) processType($class, 'propertyD'))->toEqual('array'); | ||
expect((string) processType($class, 'propertyE'))->toEqual('?array'); | ||
expect((string) processType($class, 'propertyF'))->toEqual('array|null'); | ||
expect((string) processType($class, 'propertyG'))->toEqual('array'); | ||
expect((string) processType($class, 'propertyH'))->toEqual('int[]'); | ||
expect((string) processType($class, 'propertyI'))->toEqual('int[]|null'); | ||
}); | ||
|
||
it('works with union types', function () { | ||
$class = new class { | ||
/** @var \Illuminate\Support\Collection|int[] */ | ||
public Collection|array $property; | ||
}; | ||
|
||
expect((string) processType($class, 'property'))->toEqual('int[]'); | ||
}); |
Oops, something went wrong.