From f77301f34b79686d31a31306575f7a469308455f Mon Sep 17 00:00:00 2001 From: Ayoob Mohammed Date: Fri, 2 Dec 2022 20:58:33 +0300 Subject: [PATCH 1/2] install pest --- .github/workflows/run-tests.yml | 2 +- .gitignore | 2 +- composer.json | 31 +++++++++++-------- tests/Pest.php | 23 ++++++++++++++ tests/TestCase.php | 5 --- ...sformerTest__it_can_transform_a_dto__1.txt | 7 +++++ 6 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 tests/Pest.php create mode 100644 tests/__snapshots__/DtoTransformerTest__it_can_transform_a_dto__1.txt diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 92cc3a8..d93aabe 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -43,4 +43,4 @@ jobs: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest - name: Execute tests - run: vendor/bin/phpunit + run: vendor/bin/pest diff --git a/.gitignore b/.gitignore index e9e2053..7667a6b 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,4 @@ coverage .idea .php_cs.cache .php-cs-fixer.cache - +.idea diff --git a/composer.json b/composer.json index 6f34725..b4f62ba 100644 --- a/composer.json +++ b/composer.json @@ -22,17 +22,19 @@ "spatie/typescript-transformer" : "^2.1.12" }, "require-dev" : { - "orchestra/testbench" : "^6.0|^7.0", - "mockery/mockery" : "^1.4", - "phpunit/phpunit" : "^9.0", - "spatie/temporary-directory" : "^1.2", - "spatie/phpunit-snapshot-assertions": "^4.2", - "spatie/data-transfer-object" : "^2.0", "friendsofphp/php-cs-fixer": "^3.0", - "spatie/laravel-model-states" : "^1.6|^2.0", - "spatie/enum" : "^3.0", - "vimeo/psalm": "^4.2", - "nesbot/carbon" : "^2.63" + "mockery/mockery": "^1.4", + "nesbot/carbon": "^2.63", + "orchestra/testbench": "^6.0|^7.0", + "pestphp/pest": "^1.22", + "phpunit/phpunit": "^9.0", + "spatie/data-transfer-object": "^2.0", + "spatie/enum": "^3.0", + "spatie/laravel-model-states": "^1.6|^2.0", + "spatie/pest-plugin-snapshots": "^1.1", + "spatie/phpunit-snapshot-assertions": "^4.2", + "spatie/temporary-directory": "^1.2", + "vimeo/psalm": "^4.2" }, "autoload" : { "psr-4" : { @@ -45,13 +47,16 @@ } }, "scripts" : { - "test" : "vendor/bin/phpunit", - "test-coverage" : "vendor/bin/phpunit --coverage-html coverage", + "test" : "vendor/bin/pest", + "test-coverage" : "vendor/bin/pest --coverage", "psalm": "./vendor/bin/psalm -c psalm.xml", "format": "./vendor/bin/php-cs-fixer fix --allow-risky=yes" }, "config" : { - "sort-packages" : true + "sort-packages" : true, + "allow-plugins": { + "pestphp/pest-plugin": true + } }, "extra" : { "laravel" : { diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..9ee7797 --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,23 @@ +in('.'); + +/* +|-------------------------------------------------------------------------- +| Expectations +|-------------------------------------------------------------------------- +*/ + +/* +|-------------------------------------------------------------------------- +| Functions +|-------------------------------------------------------------------------- +*/ diff --git a/tests/TestCase.php b/tests/TestCase.php index b1a0282..a3b5bb4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -7,11 +7,6 @@ class TestCase extends Orchestra { - public function setUp(): void - { - parent::setUp(); - } - protected function getPackageProviders($app) { return [ diff --git a/tests/__snapshots__/DtoTransformerTest__it_can_transform_a_dto__1.txt b/tests/__snapshots__/DtoTransformerTest__it_can_transform_a_dto__1.txt new file mode 100644 index 0000000..7ca3b15 --- /dev/null +++ b/tests/__snapshots__/DtoTransformerTest__it_can_transform_a_dto__1.txt @@ -0,0 +1,7 @@ +{ +other_dto_array: Array<{%Spatie\LaravelTypeScriptTransformer\Tests\FakeClasses\Dto\OtherDto%}>; +other_dto_collection: {%Spatie\TypeScriptTransformer\Tests\FakeClasses\Integration\OtherDtoCollection%}; +non_typed_laravel_collection: Array; +other_dto_laravel_collection: Array<{%Spatie\LaravelTypeScriptTransformer\Tests\FakeClasses\Dto\OtherDto%}>; +other_dto_laravel_eloquent_collection: Array<{%Spatie\LaravelTypeScriptTransformer\Tests\FakeClasses\Dto\OtherDto%}>; +} \ No newline at end of file From 57a8f7199705240308d608103694af80cbe1ad85 Mon Sep 17 00:00:00 2001 From: Ayoob Mohammed Date: Fri, 2 Dec 2022 21:10:30 +0300 Subject: [PATCH 2/2] refactor tests --- tests/Transformers/DtoTransformerTest.php | 55 +++---- .../SpatieStateTransformerTest.php | 79 ++++------ .../LaravelCollectionTypeProcessorTest.php | 146 ++++++++---------- tests/TypescriptTransformerTest.php | 121 ++++++--------- 4 files changed, 162 insertions(+), 239 deletions(-) diff --git a/tests/Transformers/DtoTransformerTest.php b/tests/Transformers/DtoTransformerTest.php index 37d922a..c67a12e 100644 --- a/tests/Transformers/DtoTransformerTest.php +++ b/tests/Transformers/DtoTransformerTest.php @@ -1,44 +1,27 @@ 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(); +}); diff --git a/tests/Transformers/SpatieStateTransformerTest.php b/tests/Transformers/SpatieStateTransformerTest.php index 2fc9d27..984ee3a 100644 --- a/tests/Transformers/SpatieStateTransformerTest.php +++ b/tests/Transformers/SpatieStateTransformerTest.php @@ -1,54 +1,37 @@ 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(); +}); diff --git a/tests/TypeProcessors/LaravelCollectionTypeProcessorTest.php b/tests/TypeProcessors/LaravelCollectionTypeProcessorTest.php index 1d81d1c..20a2b1b 100644 --- a/tests/TypeProcessors/LaravelCollectionTypeProcessorTest.php +++ b/tests/TypeProcessors/LaravelCollectionTypeProcessorTest.php @@ -1,93 +1,75 @@ 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[]'); +}); diff --git a/tests/TypescriptTransformerTest.php b/tests/TypescriptTransformerTest.php index 486fc52..7f50412 100644 --- a/tests/TypescriptTransformerTest.php +++ b/tests/TypescriptTransformerTest.php @@ -1,99 +1,74 @@ temporaryDirectory = (new TemporaryDirectory())->create(); - } +beforeEach(function () { + $this->temporaryDirectory = (new TemporaryDirectory())->create(); +}); - /** @test */ - public function it_will_register_the_config_correctly() - { - config()->set('typescript-transformer.auto_discover_types', 'fake-searching-path'); - config()->set('typescript-transformer.transformers', [SpatieStateTransformer::class]); - config()->set('typescript-transformer.output_file', 'index.d.ts'); - config()->set('typescript-transformer.writer', ModuleWriter::class); +it('will register the config correctly', function () { + config()->set('typescript-transformer.auto_discover_types', 'fake-searching-path'); + config()->set('typescript-transformer.transformers', [SpatieStateTransformer::class]); + config()->set('typescript-transformer.output_file', 'index.d.ts'); + config()->set('typescript-transformer.writer', ModuleWriter::class); - $config = resolve(TypeScriptTransformerConfig::class); + $config = resolve(TypeScriptTransformerConfig::class); - $this->assertEquals(['fake-searching-path'], $config->getAutoDiscoverTypesPaths()); - $this->assertEquals([new SpatieStateTransformer()], $config->getTransformers()); - $this->assertEquals('index.d.ts', $config->getOutputFile()); - $this->assertInstanceOf(ModuleWriter::class, $config->getWriter()); - } + expect($config->getAutoDiscoverTypesPaths())->toEqual(['fake-searching-path']); + expect($config->getTransformers())->toEqual([new SpatieStateTransformer()]); + expect($config->getOutputFile())->toEqual('index.d.ts'); + expect($config->getWriter())->toBeInstanceOf(ModuleWriter::class); +}); - /** @test */ - public function it_will_crash_if_an_older_version_of_searching_paths_was_defined() - { - config()->set('typescript-transformer.searching_path', 'fake-searching-path'); - config()->set('typescript-transformer.transformers', [SpatieStateTransformer::class]); - config()->set('typescript-transformer.output_file', 'index.d.ts'); +it('will crash if an older version of searching paths was defined', function () { + config()->set('typescript-transformer.searching_path', 'fake-searching-path'); + config()->set('typescript-transformer.transformers', [SpatieStateTransformer::class]); + config()->set('typescript-transformer.output_file', 'index.d.ts'); - $this->artisan('typescript:transform')->assertExitCode(1); - } + $this->artisan('typescript:transform')->assertExitCode(1); +}); - /** @test */ - public function it_can_transform_to_typescript() - { - config()->set('typescript-transformer.auto_discover_types', __DIR__ . '/FakeClasses'); - config()->set('typescript-transformer.output_file', $this->temporaryDirectory->path('index.d.ts')); +it('can transform to typescript', function () { + config()->set('typescript-transformer.auto_discover_types', __DIR__ . '/FakeClasses'); + config()->set('typescript-transformer.output_file', $this->temporaryDirectory->path('index.d.ts')); - $this->artisan('typescript:transform')->assertExitCode(0); + $this->artisan('typescript:transform')->assertExitCode(0); - $this->assertMatchesFileSnapshot($this->temporaryDirectory->path('index.d.ts')); - } + expect($this->temporaryDirectory->path('index.d.ts'))->toMatchFileSnapshot(); +}); - /** @test */ - public function it_can_define_the_input_path() - { - config()->set('typescript-transformer.searching_paths', __DIR__ . '/FakeClasses'); - config()->set('typescript-transformer.output_file', $this->temporaryDirectory->path('index.d.ts')); +it('can define the input path', function () { + config()->set('typescript-transformer.searching_paths', __DIR__ . '/FakeClasses'); + config()->set('typescript-transformer.output_file', $this->temporaryDirectory->path('index.d.ts')); - $this->artisan('typescript:transform --path='. __DIR__ . '/FakeClasses')->assertExitCode(0); + $this->artisan('typescript:transform --path='. __DIR__ . '/FakeClasses')->assertExitCode(0); - $this->assertMatchesFileSnapshot($this->temporaryDirectory->path('index.d.ts')); - } + expect($this->temporaryDirectory->path('index.d.ts'))->toMatchFileSnapshot(); +}); - /** @test */ - public function it_can_define_a_relative_input_path() - { - config()->set('typescript-transformer.searching_paths', __DIR__ . '/FakeClasses'); - config()->set('typescript-transformer.output_file', $this->temporaryDirectory->path('index.d.ts')); +it('can define a relative input path', function () { + config()->set('typescript-transformer.searching_paths', __DIR__ . '/FakeClasses'); + config()->set('typescript-transformer.output_file', $this->temporaryDirectory->path('index.d.ts')); - $this->app->useAppPath(__DIR__); - $this->app->setBasePath($this->temporaryDirectory->path('js')); + $this->app->useAppPath(__DIR__); + $this->app->setBasePath($this->temporaryDirectory->path('js')); - $this->artisan('typescript:transform --path=FakeClasses')->assertExitCode(0); + $this->artisan('typescript:transform --path=FakeClasses')->assertExitCode(0); - $this->assertMatchesFileSnapshot($this->temporaryDirectory->path('index.d.ts')); - } + expect($this->temporaryDirectory->path('index.d.ts'))->toMatchFileSnapshot(); +}); - /** @test */ - public function it_can_define_the_relative_output_path() - { - config()->set('typescript-transformer.searching_paths', __DIR__ . '/FakeClasses'); - config()->set('typescript-transformer.output_file', $this->temporaryDirectory->path('index.d.ts')); +it('can define the relative output path', function () { + config()->set('typescript-transformer.searching_paths', __DIR__ . '/FakeClasses'); + config()->set('typescript-transformer.output_file', $this->temporaryDirectory->path('index.d.ts')); - $this->app->useAppPath(__DIR__); - $this->app->setBasePath($this->temporaryDirectory->path()); + $this->app->useAppPath(__DIR__); + $this->app->setBasePath($this->temporaryDirectory->path()); - $this->artisan('typescript:transform --path=FakeClasses --output=other-index.d.ts')->assertExitCode(0); + $this->artisan('typescript:transform --path=FakeClasses --output=other-index.d.ts')->assertExitCode(0); - $this->assertMatchesFileSnapshot($this->temporaryDirectory->path('resources/other-index.d.ts')); - } -} + expect($this->temporaryDirectory->path('resources/other-index.d.ts'))->toMatchFileSnapshot(); +});