Skip to content

Commit

Permalink
refactor: Update database types in EloquentTypeParserTest
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Jul 25, 2024
1 parent 453fd53 commit 4face42
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 23 deletions.
24 changes: 17 additions & 7 deletions src/Typed/Settings/Schemas/SettingItemProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,36 @@ protected function __construct(

public static function make(ReflectionProperty $property): self
{
$phpType = $property->getType();
$extra = $property->getDocComment();
$reflectionPropertyType = $property->getType();
$docComment = $property->getDocComment();
$typeDoc = null;
if ($extra) {
if ($docComment) {
$phpDoc = "/**\n* @var array<string, string>\n*/";
$regex = '/@var\s+(.*)/';
preg_match($regex, $phpDoc, $matches);
$typeDoc = $matches[1];
}

$phpType = $phpType instanceof ReflectionNamedType ? $phpType->getName() : 'mixed';
$name = $property->getName();
$phpType = 'mixed';
$isNullable = false;
$isBuiltin = false;

if ($reflectionPropertyType instanceof ReflectionNamedType) {
$phpType = $reflectionPropertyType->getName();
$isNullable = $reflectionPropertyType->allowsNull();
$isBuiltin = $reflectionPropertyType->isBuiltin();
}

if ($typeDoc) {
$phpType = $typeDoc;
}

$self = new self(
name: $property->getName(),
name: $name,
phpType: $phpType,
// isNullable: $phpType->allowsNull(),
// isBuiltin: $phpType instanceof ReflectionNamedType ? $phpType->isBuiltin() : false,
isNullable: $isNullable,
isBuiltin: $isBuiltin,
);

$self->typescriptType = ParserPhpType::toTypescript($self->phpType);
Expand Down
24 changes: 12 additions & 12 deletions src/Typed/SettingsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
class SettingsType
{
/**
* @param array<string,SettingsItem> $items
* @param array<string,SettingsItem> $settings
*/
protected function __construct(
protected SettingsConfig $config,
protected array $items = [],
protected array $settings = [],
protected ?string $typescript = null,
) {}

Expand All @@ -30,15 +30,15 @@ public static function make(SettingsConfig $config = new SettingsConfig): ?self
}

$collect = SchemaCollection::make($self->config->directory, $self->config->toSkip);
$items = array_filter($collect->items(), fn (SchemaClass $item) => $item->extends() === $self->config->extends);
$settings = array_filter($collect->items(), fn (SchemaClass $item) => $item->extends() === $self->config->extends);

foreach ($items as $item) {
$item = SettingsItem::make($item);
$self->items[$item->name()] = $item;
foreach ($settings as $setting) {
$setting = SettingsItem::make($setting);
$self->settings[$setting->name()] = $setting;
}

$printer = PrinterSettings::make($self->items);
TypescriptableUtils::print($printer, TypescriptableConfig::setPath($self->config->filename));
$self->typescript = PrinterSettings::make($self->settings);
TypescriptableUtils::print($self->typescript, TypescriptableConfig::setPath($self->config->filename));

return $self;
}
Expand All @@ -51,14 +51,14 @@ public function config(): SettingsConfig
/**
* @return array<string,SettingsItem>
*/
public function items(): array
public function settings(): array
{
return $this->items;
return $this->settings;
}

public function item(string $name): SettingsItem
public function setting(string $name): SettingsItem
{
return $this->items[$name];
return $this->settings[$name];
}

public function typescript(): ?string
Expand Down
1 change: 0 additions & 1 deletion tests/EloquentConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
skipModels: ['App\\Models\\SushiTest'],
typescriptFilename: 'eloquent.d.ts',
);
ray($config);

expect($config->modelsPath)->toBe(models());
expect($config->phpPath)->toBe(outputDir('php'));
Expand Down
70 changes: 69 additions & 1 deletion tests/RouteTypeTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use Kiwilan\Typescriptable\Typed\Route\RouteConfig;
use Kiwilan\Typescriptable\Typed\Route\Schemas\RouteTypeItem;
use Kiwilan\Typescriptable\Typed\Route\Schemas\RouteTypeItemParam;
use Kiwilan\Typescriptable\Typed\RouteType;
use Kiwilan\Typescriptable\TypescriptableConfig;

Expand All @@ -19,5 +21,71 @@
$type = RouteType::make(new RouteConfig(
json: json_decode(file_get_contents(routes()), true),
));
ray($type);
$routes = $type->routes();

$config = $type->config();

expect($config->filenameTypes)->toBe(TypescriptableConfig::routesFilename());
expect($config->filenameList)->toBe(TypescriptableConfig::routesFilenameList());
expect($config->printList)->toBeTrue();
expect($config->json)->toBeArray();
expect($config->namesToSkip)->toBeArray();
expect($config->pathsToSkip)->toBeArray();

expect($routes->toArray())->toBeArray();
expect($routes->toArray())->not->toBeEmpty();
expect($routes->count())->toBe(60);

$typescriptList = $type->typescriptList();
expect($typescriptList)->toBeString();

$typescriptTypes = $type->typescriptTypes();
expect($typescriptTypes)->toBeString();

$getHome = $routes->get('get.home');
expect($getHome)->toBeInstanceOf(RouteTypeItem::class);
expect($getHome->uri())->toBe('/');
expect($getHome->name())->toBe('home');
expect($getHome->action())->toBe('App\Http\Controllers\App\HomeController@index');
expect($getHome->methodMain())->toBe('GET');
expect($getHome->methods())->toBeArray();
expect($getHome->methods())->toContain('GET');
expect($getHome->methods())->toContain('HEAD');
expect($getHome->middlewares())->toBeArray();
expect($getHome->middlewares())->toContain('web');
expect($getHome->middlewares())->toContain('Illuminate\Routing\Middleware\SubstituteBindings');
expect($getHome->parameters())->toBeArray();
expect($getHome->parameters())->toBeEmpty();
expect($getHome->id())->toBe('get.home');

$getLibrariesShow = $routes->get('get.libraries.show');
expect($getLibrariesShow)->toBeInstanceOf(RouteTypeItem::class);
expect($getLibrariesShow->uri())->toBe('/libraries/{library}');
expect($getLibrariesShow->name())->toBe('libraries.show');
expect($getLibrariesShow->action())->toBe('App\Http\Controllers\App\LibraryController@show');
expect($getLibrariesShow->parameters())->toBeArray();
expect($getLibrariesShow->parameters())->not->toBeEmpty();
expect($getLibrariesShow->parameters())->toHaveCount(1);
$param = $getLibrariesShow->parameters()[0];
expect($param)->toBeInstanceOf(RouteTypeItemParam::class);
expect($param->name())->toBe('library');
expect($param->type())->toBe('string');
expect($param->required())->toBeTrue();
expect($param->default())->toBeNull();
expect($getLibrariesShow->id())->toBe('get.libraries.show');

$putUserPasswordUpdate = $routes->get('put.user.password.update');
expect($putUserPasswordUpdate)->toBeInstanceOf(RouteTypeItem::class);
expect($putUserPasswordUpdate->uri())->toBe('/user/password');
expect($putUserPasswordUpdate->name())->toBe('user-password.update');
expect($putUserPasswordUpdate->action())->toBe('Laravel\Fortify\Http\Controllers\PasswordController@update');
expect($putUserPasswordUpdate->methodMain())->toBe('PUT');
expect($putUserPasswordUpdate->methods())->toBeArray();
expect($putUserPasswordUpdate->methods())->toContain('PUT');
expect($putUserPasswordUpdate->middlewares())->toBeArray();
expect($putUserPasswordUpdate->middlewares())->toContain('web');
expect($putUserPasswordUpdate->middlewares())->toContain('Illuminate\Auth\Middleware\Authenticate:web');
expect($putUserPasswordUpdate->parameters())->toBeArray();
expect($putUserPasswordUpdate->parameters())->toBeEmpty();
expect($putUserPasswordUpdate->id())->toBe('put.user.password.update');
});
29 changes: 27 additions & 2 deletions tests/SettingsTypeTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
<?php

use Kiwilan\Typescriptable\Typed\Settings\SettingsConfig;
use Kiwilan\Typescriptable\Typed\SettingsType;
use Kiwilan\Typescriptable\TypescriptableConfig;

beforeEach(function () {
deleteFile(outputDir('types-routes.d.ts'));
deleteFile(outputDir(TypescriptableConfig::settingsFilename()));

config()->set('typescriptable.settings.filename', TypescriptableConfig::settingsFilename());
config()->set('typescriptable.settings.directory', settingsDir());
config()->set('typescriptable.settings.extends', settingsExtends());
config()->set('typescriptable.settings.skip', []);
});

it('can type settings', function () {
// settingsDir(), setttingsOutputDir(), settingsExtends()
$type = SettingsType::make();

$settings = $type->settings();
expect($settings)->toBeArray();
expect(count($settings))->toBe(1);

$homeSetting = $type->setting('HomeSettings');
expect($homeSetting->name())->toBe('HomeSettings');
expect($homeSetting->properties())->toBeArray();
expect($homeSetting->properties())->toHaveKey('hero_title_main');

$hero_title_main = $homeSetting->properties()['hero_title_main'];
expect($hero_title_main->name())->toBe('hero_title_main');
expect($hero_title_main->phpType())->toBe('string');
expect($hero_title_main->isNullable())->toBeTrue();
expect($hero_title_main->isBuiltin())->toBeTrue();
expect($hero_title_main->typescriptType())->toBe('string');

expect($type->config())->toBeInstanceOf(SettingsConfig::class);
expect($type->typescript())->toBeString();
});

0 comments on commit 4face42

Please sign in to comment.