Skip to content

Commit

Permalink
Merge pull request #12 from kirschbaum-development/testing
Browse files Browse the repository at this point in the history
Testing
  • Loading branch information
brandonferens authored Oct 21, 2024
2 parents 7a0e3f6 + 61409f6 commit dee57b5
Show file tree
Hide file tree
Showing 25 changed files with 478 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ composer.phar
composer.lock
phpunit.xml
.phpunit.result.cache
.phpunit.cache
.DS_Store
Thumbs.db
Thumbs.db
22 changes: 17 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,27 @@
"require": {
"php": "^8.1",
"illuminate/filesystem": ">=10.0",
"illuminate/support": ">=10.0"
"illuminate/support": ">=10.0",
"symfony/filesystem": "^7.1"
},
"require-dev": {
"laravel/framework": ">=10.0",
"laravel/pint": "^1.18"
"laravel/pint": "^1.18",
"orchestra/testbench": "^9.5",
"pestphp/pest": "^3.2",
"pestphp/pest-plugin-laravel": "^3.0"
},
"autoload": {
"autoload-dev": {
"psr-4": {
"Kirschbaum\\Paragon\\": "src/"
"App\\": "vendor/orchestra/testbench-core/laravel/app",
"Kirschbaum\\Paragon\\": "src/",
"Kirschbaum\\Paragon\\Tests\\": "tests"
}
},
"scripts": {
"pest": [
"./vendor/bin/pest"
],
"pint": [
"./vendor/bin/pint"
],
Expand All @@ -38,7 +47,10 @@
]
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
3 changes: 2 additions & 1 deletion config/paragon.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
'abstract-class' => 'Enum',

'paths' => [
'php' => app_path(),
'php' => '',
'ignore' => [],
'generated' => 'js/enums',
'methods' => 'js/vendors/paragon/enums',
],
Expand Down
16 changes: 15 additions & 1 deletion src/Commands/GenerateEnumsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
namespace Kirschbaum\Paragon\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Kirschbaum\Paragon\Concerns\DiscoverEnums;
use Kirschbaum\Paragon\Concerns\IgnoreParagon;
use Kirschbaum\Paragon\Generators\AbstractEnumGenerator;
use Kirschbaum\Paragon\Generators\EnumGenerator;
use ReflectionEnum;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'paragon:generate-enums', description: 'Generate Typescript versions of existing PHP enums')]
Expand Down Expand Up @@ -35,7 +39,17 @@ public function handle(): int
*/
protected function enums(): Collection
{
return DiscoverEnums::within(config('paragon.enums.paths.php'))
return DiscoverEnums::within(app_path(config('paragon.enums.paths.php')))
->reject(function ($enum) {
$reflector = new ReflectionEnum($enum);

$paths = Arr::map(Arr::wrap(config('paragon.enums.paths.ignore')), function ($path) {
return Str::finish(app_path($path), '/');
});

return $reflector->getAttributes(IgnoreParagon::class)
|| Str::startsWith($reflector->getFileName(), $paths);
})
->values();
}
}
16 changes: 8 additions & 8 deletions src/Commands/MakeEnumMethodCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@
#[AsCommand(name: 'paragon:enum-method', description: 'Create a new typescript enum method')]
class MakeEnumMethodCommand extends GeneratorCommand
{
/**
* Get the stub file for the generator.
*/
protected function getStub(): string
{
return __DIR__ . '/../../stubs/method.stub';
}

/**
* Execute the console command.
*
Expand All @@ -39,6 +31,14 @@ public function handle(): int
return self::SUCCESS;
}

/**
* Get the stub file for the generator.
*/
protected function getStub(): string
{
return __DIR__ . '/../../stubs/method.stub';
}

/**
* Get the console command arguments.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Concerns/IgnoreParagon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Kirschbaum\Paragon\Concerns;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
class IgnoreParagon {}
8 changes: 0 additions & 8 deletions src/Concerns/IgnoreWhenGeneratingTypescript.php

This file was deleted.

20 changes: 9 additions & 11 deletions src/Generators/AbstractEnumGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\Filesystem\Filesystem as FileUtility;
use Symfony\Component\Finder\Exception\DirectoryNotFoundException;
use Symfony\Component\Finder\Finder;

Expand Down Expand Up @@ -41,7 +42,7 @@ protected function contents(): string
/**
* Get the path to the stubs.
*/
public function stubPath(): string
protected function stubPath(): string
{
return __DIR__ . '/../../stubs/abstract-enum.stub';
}
Expand All @@ -61,19 +62,16 @@ protected function imports(): Collection

return collect($files)
->mapWithKeys(function ($file) {
$abstractPath = collect(explode('/', config('paragon.enums.paths.generated')));
$relativeFilePath = str($file->getPath())
->after(resource_path())
->replace('\\', '/')
->ltrim('/')
->explode('/')
->map(fn ($directory, $index) => data_get($abstractPath, $index) === $directory ? '..' : $directory)
->filter()
->join('/');
$filesystem = new FileUtility();

$relativeFilePath = $filesystem->makePathRelative(
$file->getPath(),
resource_path(config('paragon.enums.paths.generated'))
);

$name = (string) str($file->getFileName())->before('.');

return [$name => "import {$name} from '{$relativeFilePath}/{$file->getFilename()}';" . PHP_EOL];
return [$name => "import {$name} from '{$relativeFilePath}{$file->getFilename()}';" . PHP_EOL];
})
->sort();
}
Expand Down
6 changes: 3 additions & 3 deletions src/Generators/EnumGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Fluent;
use Kirschbaum\Paragon\Concerns\IgnoreWhenGeneratingTypescript;
use Kirschbaum\Paragon\Concerns\IgnoreParagon;
use ReflectionEnum;
use ReflectionException;
use ReflectionMethod;
Expand Down Expand Up @@ -75,7 +75,7 @@ protected function contents(): string
/**
* Get the path to the stubs.
*/
public function stubPath(): string
protected function stubPath(): string
{
return __DIR__ . '/../../stubs/enum.stub';
}
Expand Down Expand Up @@ -129,7 +129,7 @@ protected function methods(): Collection
{
return collect($this->reflector->getMethods(ReflectionMethod::IS_PUBLIC))
->reject(function (ReflectionMethod $method) {
return $method->isStatic() || $method->getAttributes(IgnoreWhenGeneratingTypescript::class);
return $method->isStatic() || $method->getAttributes(IgnoreParagon::class);
})
->sortBy(fn (ReflectionMethod $method) => $method->getName());
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Architecture/AttributesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Kirschbaum\Paragon\Concerns\IgnoreParagon;

arch()->expect(IgnoreParagon::class)
->toExtendNothing();

arch()->expect(IgnoreParagon::class)
->toHaveAttribute(Attribute::class);
16 changes: 16 additions & 0 deletions tests/Architecture/CommandsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Symfony\Component\Console\Attribute\AsCommand;

arch()->expect('Kirschbaum\Paragon\Commands')
->classes()
->toHaveSuffix('Command');

arch()->expect('Kirschbaum\Paragon\Commands')
->toHaveAttribute(AsCommand::class);

arch()->expect('Kirschbaum\Paragon\Commands')
->not->toHavePrivateMethodsBesides(['__construct', 'handle']);

arch()->expect('Kirschbaum\Paragon\Commands')
->not->toHavePublicMethodsBesides(['__construct', 'handle']);
12 changes: 12 additions & 0 deletions tests/Architecture/DiscoveryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Kirschbaum\Paragon\Concerns\DiscoverEnums;

arch()->expect(DiscoverEnums::class)
->toExtendNothing();

arch()->expect(DiscoverEnums::class)
->not->toHavePrivateMethodsBesides(['within']);

arch()->expect(DiscoverEnums::class)
->not->toHavePublicMethodsBesides(['within']);
17 changes: 17 additions & 0 deletions tests/Architecture/GeneratorsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

arch()->expect('Kirschbaum\Paragon\Generators')
->classes()
->toHaveSuffix('Generator');

arch()->expect('Kirschbaum\Paragon\Generators')
->toExtendNothing();

arch()->expect('Kirschbaum\Paragon\Generators')
->toBeInvokable();

arch()->expect('Kirschbaum\Paragon\Generators')
->not->toHavePrivateMethodsBesides(['__construct', '__invoke']);

arch()->expect('Kirschbaum\Paragon\Generators')
->not->toHavePublicMethodsBesides(['__construct', '__invoke']);
29 changes: 29 additions & 0 deletions tests/Architecture/PresetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

arch()->preset()->php();

// Security without the md5 method as we need it for caching.
arch()
->expect([
'array_rand',
'assert',
'create_function',
'dl',
'eval',
'exec',
'extract',
'mb_parse_str',
'mt_rand',
'parse_str',
'passthru',
'rand',
'sha1',
'shell_exec',
'shuffle',
'str_shuffle',
'system',
'tempnam',
'uniqid',
'unserialize',
])
->not->toBeUsed();
11 changes: 11 additions & 0 deletions tests/Fixtures/Ignore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Enums;

use Kirschbaum\Paragon\Concerns\IgnoreParagon;

#[IgnoreParagon]
enum Ignore: string
{
case Ignore = 'ignore';
}
8 changes: 8 additions & 0 deletions tests/Fixtures/Ignore/Ignore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Enums\Ignore;

enum Ignore
{
case Ignore;
}
14 changes: 14 additions & 0 deletions tests/Fixtures/IntegerBacked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Enums;

enum IntegerBacked: int
{
case Active = 1;
case Inactive = 0;

public function label(): string
{
return 'label';
}
}
8 changes: 8 additions & 0 deletions tests/Fixtures/Nested/Nested.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Enums\Nested;

enum Nested
{
case Nested;
}
14 changes: 14 additions & 0 deletions tests/Fixtures/NonBacked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Enums;

enum NonBacked
{
case Active;
case Inactive;

public function label(): string
{
return 'label';
}
}
27 changes: 27 additions & 0 deletions tests/Fixtures/StringBacked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Enums;

use Kirschbaum\Paragon\Concerns\IgnoreParagon;

enum StringBacked: string
{
case Active = 'active';
case Inactive = 'inactive';

public function label(): string
{
return 'label';
}

#[IgnoreParagon]
public function ignore(): string
{
return 'ignore';
}

public static function ignoreStatic(): string
{
return 'ignore';
}
}
5 changes: 5 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use Kirschbaum\Paragon\Tests\TestCase;

uses(TestCase::class)->in(__DIR__);
Loading

0 comments on commit dee57b5

Please sign in to comment.