Skip to content

Commit

Permalink
Tests fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed Aug 25, 2023
1 parent 206e218 commit 6df7bdf
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
24 changes: 20 additions & 4 deletions packages/documentator/src/Commands/CommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ protected function getPackageProviders(mixed $app): array {
// <editor-fold desc="Tests">
// =========================================================================
public function testInvoke(): void {
$directory = self::getTempDirectory();
$directory = Path::normalize(self::getTempDirectory());

self::assertNotFalse(
file_put_contents(Path::join($directory, 'file.txt'), static::class),
);

$this->artisan("lara-asp-documentator:commands test-namespace {$directory}");
$result = $this
->withoutMockingConsoleOutput()
->artisan("lara-asp-documentator:commands test-namespace {$directory}");

self::assertEquals(Command::SUCCESS, $result);

$files = iterator_to_array(Finder::create()->in($directory)->files());
$files = array_reduce($files, static function (array $combined, SplFileInfo $file): array {
Expand Down Expand Up @@ -107,16 +111,28 @@ public function __invoke(): void {
* @noinspection PhpMultipleClassesDeclarationsInOneFile
*/
#[AsCommand(
name : 'test-namespace:command-b',
description: 'Command B description.',
name: 'test-namespace:command-b',
)]
class CommandsTest_CommandB extends Command {
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
* @var string|null
*/
protected $description = 'Command B description.';

/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
* @var array<array-key, mixed>
*/
protected $aliases = ['command-b-alias'];

public function __construct() {
parent::__construct();

// @phpstan-ignore-next-line Required for Laravel v9
$this->setAliases((array) $this->aliases);
}

public function __invoke(): void {
throw new Exception('Should not be called.');
}
Expand Down
51 changes: 39 additions & 12 deletions packages/documentator/src/Utils/ArtisanSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Console\Parser;
use LastDragon_ru\LaraASP\Documentator\Testing\Package\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\TestWith;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

Expand All @@ -16,11 +15,11 @@
*/
#[CoversClass(ArtisanSerializer::class)]
class ArtisanSerializerTest extends TestCase {
#[TestWith(['user'])]
#[TestWith(['user?'])]
#[TestWith(['user*?'])]
#[TestWith(['user=default'])]
#[TestWith(['user=*default'])]
// <editor-fold desc="Tests">
// =========================================================================
/**
* @dataProvider dataProviderGetArgumentSignature
*/
public function testGetArgumentSignature(string $signature): void {
$parsed = Parser::parse("command {{$signature}}")[1] ?? [];
$argument = reset($parsed);
Expand All @@ -29,17 +28,45 @@ public function testGetArgumentSignature(string $signature): void {
self::assertEquals($signature, (new ArtisanSerializer())->getArgumentSignature($argument));
}

#[TestWith(['--user'])]
#[TestWith(['--u|user'])]
#[TestWith(['--user=*'])]
#[TestWith(['--u|user=*'])]
#[TestWith(['--u|user=default'])]
#[TestWith(['--u|user=*default'])]
/**
* @dataProvider dataProviderGetOptionSignature
*/
public function testGetOptionSignature(string $signature): void {
$parsed = Parser::parse("command {{$signature}}")[2] ?? [];
$option = reset($parsed);

self::assertInstanceOf(InputOption::class, $option);
self::assertEquals($signature, (new ArtisanSerializer())->getOptionSignature($option));
}
// </editor-fold>

// <editor-fold desc="DataProviders">
// =========================================================================
/**
* @return array<int, array{string}>
*/
public static function dataProviderGetArgumentSignature(): array {
return [
['user'],
['user?'],
['user*?'],
['user=default'],
['user=*default'],
];
}

/**
* @return array<int, array{string}>
*/
public static function dataProviderGetOptionSignature(): array {
return [
['--user'],
['--u|user'],
['--user=*'],
['--u|user=*'],
['--u|user=default'],
['--u|user=*default'],
];
}
// </editor-fold>
}
8 changes: 4 additions & 4 deletions packages/documentator/src/Utils/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@
class Path {
public static function getPath(string $root, string $path): string {
$path = static::isRelative($path)
? SymfonyPath::join(static::getDirname($root), $path)
? static::join(static::getDirname($root), $path)
: $path;
$path = static::normalize($path);

return $path;
}

public static function getDirname(string $path): string {
return is_file($path) ? dirname($path) : $path;
return static::normalize(is_file($path) ? dirname($path) : $path);
}

public static function isRelative(string $path): bool {
return SymfonyPath::isRelative($path);
return SymfonyPath::isRelative(static::normalize($path));
}

public static function isAbsolute(string $path): bool {
return SymfonyPath::isAbsolute($path);
return SymfonyPath::isAbsolute(static::normalize($path));
}

public static function normalize(string $path): string {
Expand Down
6 changes: 5 additions & 1 deletion packages/documentator/src/Utils/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPUnit\Framework\Attributes\CoversClass;

use function dirname;
use function str_replace;

/**
* @internal
Expand All @@ -15,6 +16,9 @@ class PathTest extends TestCase {
public function testGetPath(): void {
self::assertEquals('/absolute/path/to/file', Path::getPath('any/path', '/absolute/path/./to/file'));
self::assertEquals('/absolute/path/to/file', Path::getPath('/absolute/path', 'to/./file'));
self::assertEquals(dirname(__FILE__).'/to/file', Path::getPath(__FILE__, 'to/./file'));
self::assertEquals(
str_replace('\\', '/', dirname(__FILE__).'/to/file'),
Path::getPath(__FILE__, 'to/./file'),
);
}
}

0 comments on commit 6df7bdf

Please sign in to comment.