Skip to content

Commit

Permalink
Fix tests, improve configs
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Jul 25, 2024
1 parent 794f7db commit 9f02530
Show file tree
Hide file tree
Showing 19 changed files with 151 additions and 134 deletions.
23 changes: 19 additions & 4 deletions src/Typed/Eloquent/EloquentConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,24 @@ public function __construct(
public array $skipModels = [],
public ?string $typescriptFilename = null,
) {
$this->modelsPath = TypescriptableConfig::eloquentDirectory();
$this->typescriptFilename = TypescriptableConfig::eloquentFilename();
$this->phpPath = TypescriptableConfig::eloquentPhpPath();
$this->skipModels = TypescriptableConfig::eloquentSkip();
if (! $this->modelsPath) {
$this->modelsPath = TypescriptableConfig::eloquentDirectory();
}

if (! $this->useParser) {
$this->useParser = TypescriptableConfig::engineEloquent() === 'parser';
}

if (! $this->phpPath) {
$this->phpPath = TypescriptableConfig::eloquentPhpPath();
}

if (! $this->skipModels) {
$this->skipModels = TypescriptableConfig::eloquentSkip();
}

if (! $this->typescriptFilename) {
$this->typescriptFilename = TypescriptableConfig::eloquentFilename();
}
}
}
23 changes: 18 additions & 5 deletions src/Typed/Route/RouteConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,23 @@ public function __construct(
public array $namesToSkip = [],
public array $pathsToSkip = [],
) {
$this->filenameTypes = TypescriptableConfig::routesFilename();
$this->filenameList = TypescriptableConfig::routesFilenameList();
$this->printList = TypescriptableConfig::routesPrintList();
$this->namesToSkip = TypescriptableConfig::routesSkipName();
$this->pathsToSkip = TypescriptableConfig::routesSkipPath();
if (! $this->filenameTypes) {
$this->filenameTypes = TypescriptableConfig::routesFilename();
}

if (! $this->filenameList) {
$this->filenameList = TypescriptableConfig::routesFilenameList();
}

if (! $this->printList) {
$this->printList = TypescriptableConfig::routesPrintList();
}
if (! $this->namesToSkip) {
$this->namesToSkip = TypescriptableConfig::routesSkipName();
}

if (! $this->pathsToSkip) {
$this->pathsToSkip = TypescriptableConfig::routesSkipPath();
}
}
}
2 changes: 1 addition & 1 deletion src/Typed/RouteType.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private function parseRoutes(): Collection
$skipPaths = $this->toSkip($this->config->pathsToSkip);

if (! $routes) {
throw new \Exception('No routes found.');
return collect();
}
$routes = array_filter($routes, fn ($route) => $this->filterBy($route, 'uri', $skipPaths));
$routes = array_filter($routes, fn ($route) => $this->filterBy($route, 'name', $skipNames));
Expand Down
19 changes: 15 additions & 4 deletions src/Typed/Settings/SettingsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,20 @@ public function __construct(
public ?string $extends = null,
public array $toSkip = [],
) {
$this->filename = TypescriptableConfig::settingsFilename();
$this->directory = TypescriptableConfig::settingsDirectory();
$this->extends = TypescriptableConfig::settingsExtends();
$this->toSkip = TypescriptableConfig::settingsSkip();
if (! $this->filename) {
$this->filename = TypescriptableConfig::settingsFilename();
}

if (! $this->directory) {
$this->directory = TypescriptableConfig::settingsDirectory();
}

if (! $this->extends) {
$this->extends = TypescriptableConfig::settingsExtends();
}

if (! $this->toSkip) {
$this->toSkip = TypescriptableConfig::settingsSkip();
}
}
}
10 changes: 6 additions & 4 deletions src/TypescriptableConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,25 @@ public static function routesUsePath(): bool
public static function routesSkipName(): array
{
return config('typescriptable.routes.skip.name') ?? [
'__clockwork.*',
'debugbar.*',
'horizon.*',
'telescope.*',
'nova.*',
'lighthouse.*',
'livewire.*',
'ignition.*',
'filament.*',
'log-viewer.*',
'two-factor.*',
];
}

public static function routesSkipPath(): array
{
return config('typescriptable.routes.skip.path') ?? [
'api/*',
'_ignition/*',
'__clockwork/*',
'clockwork/*',
'two-factor-challenge',
'livewire',
];
}

Expand Down
7 changes: 3 additions & 4 deletions tests/CommandSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
use Kiwilan\Typescriptable\TypescriptableConfig;

beforeEach(function () {
deleteFile(outputDir('types-settings.d.ts'));

deleteFile(outputDir(TypescriptableConfig::settingsFilename()));
config()->set('typescriptable.settings.filename', settingsDir());
config()->set('typescriptable.settings.directory', setttingsOutputDir());

config()->set('typescriptable.settings.filename', 'types-settings.d.ts');
config()->set('typescriptable.settings.directory', settingsDir());
config()->set('typescriptable.settings.extends', settingsExtends());
config()->set('typescriptable.settings.skip', []);
});
Expand Down
7 changes: 4 additions & 3 deletions tests/CommandTypescriptableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
use Illuminate\Support\Facades\Artisan;
use Kiwilan\Typescriptable\Commands\TypescriptableCommand;
use Kiwilan\Typescriptable\Tests\TestCase;
use Kiwilan\Typescriptable\TypescriptableConfig;

beforeEach(function () {
deleteFile(outputDir('types-eloquent.d.ts'));
deleteFile(outputDir('types-routes.d.ts'));
deleteFile(outputDir('types-settings.d.ts'));
deleteFile(outputDir(TypescriptableConfig::eloquentFilename()));
deleteFile(outputDir(TypescriptableConfig::routesFilename()));
deleteFile(outputDir(TypescriptableConfig::settingsFilename()));
});

it('can use command', function () {
Expand Down
22 changes: 11 additions & 11 deletions tests/EloquentConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@
use Kiwilan\Typescriptable\Typed\Eloquent\EloquentConfig;

it('can use default config', function () {
config(['typescriptable.eloquent.skip' => [
config()->set('typescriptable.eloquent.php_path', null);
config()->set('typescriptable.eloquent.skip', [
'Kiwilan\\Typescriptable\\Tests\\Data\\Models\\SushiTest',
]]);
]);

$config = new EloquentConfig;
$config = new EloquentConfig();

expect($config->modelsPath)->toContain('vendor/orchestra/testbench-core/laravel/app/Models');
expect($config->outputPath)->toContain('vendor/orchestra/testbench-core/laravel/resources/js');
expect($config->modelsPath)->toContain('tests/Data/Models');
expect($config->phpPath)->toBeNull();
expect($config->useParser)->toBeFalse();
expect($config->tsFilename)->toBe('types-eloquent.d.ts');
expect($config->typescriptFilename)->toBe('types-eloquent.d.ts');
expect($config->skipModels)->toBeArray();
expect(count($config->skipModels))->toBe(1);
expect($config->skipModels[0])->toBe('Kiwilan\Typescriptable\Tests\Data\Models\SushiTest');

$config = new EloquentConfig(
modelsPath: models(),
outputPath: outputDir(),
phpPath: outputDir().'/php',
phpPath: outputDir('php'),
useParser: false,
skipModels: ['App\\Models\\SushiTest'],
typescriptFilename: 'eloquent.d.ts',
);
ray($config);

expect($config->modelsPath)->toBe(models());
expect($config->outputPath)->toBe(outputDir());
expect($config->phpPath)->toBe(outputDir().'/php');
expect($config->phpPath)->toBe(outputDir('php'));
expect($config->useParser)->toBeFalse();
expect($config->tsFilename)->toBe('types-eloquent.d.ts');
expect($config->typescriptFilename)->toBe('eloquent.d.ts');
expect($config->skipModels)->toBeArray();
expect(count($config->skipModels))->toBe(1);
expect($config->skipModels[0])->toBe('App\Models\SushiTest');
Expand Down
10 changes: 6 additions & 4 deletions tests/EloquentListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
use Illuminate\Support\Facades\Artisan;
use Kiwilan\Typescriptable\Typed\Utils\EloquentList;

beforeEach(function () {
eloquentConfig();
});

it('can list models', function () {
$list = EloquentList::make(models());

expect($list->models())->toBeArray();
expect($list->path())->toBe(models());
expect(count($list->models()))->toBe(9);
expect(count($list->models()))->toBe(8);
});

it('can use command', function () {
Artisan::call('eloquent:list', [
'modelPath' => models(),
]);
Artisan::call('eloquent:list');

$output = Artisan::output();
expect($output)->toContain('Name');
Expand Down
33 changes: 0 additions & 33 deletions tests/EloquentTypeArtisanMysqlTest.php

This file was deleted.

13 changes: 5 additions & 8 deletions tests/EloquentTypeArtisanTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
<?php

use Kiwilan\Typescriptable\Tests\TestCase;
use Kiwilan\Typescriptable\Typed\Eloquent\EloquentConfig;
use Kiwilan\Typescriptable\Typed\EloquentType;
use Kiwilan\Typescriptable\Typed\Utils\Schema\SchemaClass;

beforeEach(function () {
eloquentConfig();
});

it('can be run with artisan', function (string $driver) {
TestCase::setupDatabase($driver);

$type = EloquentType::make(new EloquentConfig(
modelsPath: models(),
outputPath: outputDir(),
phpPath: outputDir().'/php',
useParser: false,
skipModels: ['Kiwilan\\Typescriptable\\Tests\\Data\\Models\\SushiTest'],
))->execute();
$type = EloquentType::make()->execute();

$app = $type->app();
$movie = $app->getModel('Kiwilan\Typescriptable\Tests\Data\Models\Movie');
Expand Down
13 changes: 5 additions & 8 deletions tests/EloquentTypeParserTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
<?php

use Kiwilan\Typescriptable\Tests\TestCase;
use Kiwilan\Typescriptable\Typed\Eloquent\EloquentConfig;
use Kiwilan\Typescriptable\Typed\EloquentType;
use Kiwilan\Typescriptable\Typed\Utils\Schema\SchemaClass;

beforeEach(function () {
eloquentConfig('parser');
});

it('can be run with parser', function (string $driver) {
TestCase::setupDatabase($driver);

$type = EloquentType::make(new EloquentConfig(
modelsPath: models(),
outputPath: outputDir(),
phpPath: outputDir().'/php',
useParser: true,
skipModels: ['Kiwilan\\Typescriptable\\Tests\\Data\\Models\\SushiTest'],
))->execute();
$type = EloquentType::make()->execute();

$app = $type->app();
$movie = $app->getModel('Kiwilan\Typescriptable\Tests\Data\Models\Movie');
Expand Down
12 changes: 2 additions & 10 deletions tests/EloquentTypescriptTest.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
<?php

use Kiwilan\Typescriptable\Tests\TestCase;
use Kiwilan\Typescriptable\Typed\Eloquent\EloquentConfig;
use Kiwilan\Typescriptable\Typed\Utils\TypescriptToPhp;
use Kiwilan\Typescriptable\Typescriptable;
use Kiwilan\Typescriptable\TypescriptableConfig;

beforeEach(function () {
deleteFile(outputDir('types-eloquent.d.ts'));
deleteDir(outputDir('php'));
eloquentConfig();
});

it('can be run', function (string $driver) {
TestCase::setupDatabase($driver);

$type = Typescriptable::models(new EloquentConfig(
modelsPath: models(),
outputPath: outputDir(),
phpPath: outputDir().'/php',
useParser: false,
skipModels: ['Kiwilan\\Typescriptable\\Tests\\Data\\Models\\SushiTest'],
));
$type = Typescriptable::models();

$app = $type->app();
expect($app->driver())->toBe($driver);
Expand Down
15 changes: 2 additions & 13 deletions tests/MongodbTest.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
<?php

use Kiwilan\Typescriptable\Tests\TestCase;
use Kiwilan\Typescriptable\Typed\Eloquent\EloquentConfig;
use Kiwilan\Typescriptable\Typed\EloquentType;
use Kiwilan\Typescriptable\Typed\Utils\TypescriptToPhp;
use Kiwilan\Typescriptable\TypescriptableConfig;

beforeEach(function () {
deleteFile(outputDir('types-eloquent.d.ts'));
deleteDir(outputDir('php'));
});

it('can be run', function (string $engine) {
eloquentConfig($engine);
TestCase::setupDatabase('mongodb');

$type = EloquentType::make(new EloquentConfig(
modelsPath: models(),
outputPath: outputDir(),
phpPath: outputDir().'/php',
useParser: $engine === 'parser',
skipModels: ['Kiwilan\\Typescriptable\\Tests\\Data\\Models\\SushiTest'],
))->execute();
$type = EloquentType::make()->execute();

$app = $type->app();
$movie = $app->getModel('Kiwilan\Typescriptable\Tests\Data\Models\Movie');
Expand Down
Loading

0 comments on commit 9f02530

Please sign in to comment.