-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(documentator): New instruction
include:graphql-directive
(#158)
- Loading branch information
Showing
15 changed files
with
276 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -259,4 +259,5 @@ MD053: | |
"include:package-list", | ||
"include:template", | ||
"include:docblock", | ||
"include:graphql-directive" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/documentator/src/Preprocessor/Exceptions/DependencyIsMissing.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions; | ||
|
||
use Throwable; | ||
|
||
use function sprintf; | ||
|
||
class DependencyIsMissing extends InstructionFailed { | ||
/** | ||
* @param class-string $class | ||
*/ | ||
public function __construct( | ||
string $path, | ||
string $target, | ||
private readonly string $class, | ||
Throwable $previous = null, | ||
) { | ||
parent::__construct( | ||
$path, | ||
$target, | ||
sprintf( | ||
'The dependency `%s` is missed (in `%s`).', | ||
$this->class, | ||
$path, | ||
), | ||
$previous, | ||
); | ||
} | ||
|
||
public function getClass(): string { | ||
return $this->class; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/documentator/src/Preprocessor/Exceptions/TargetIsNotDirective.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions; | ||
|
||
use Throwable; | ||
|
||
use function sprintf; | ||
|
||
class TargetIsNotDirective extends InstructionFailed { | ||
public function __construct(string $path, string $target, Throwable $previous = null) { | ||
parent::__construct( | ||
$path, | ||
$target, | ||
sprintf( | ||
'The `%s` is not a directive (in `%s`).', | ||
$target, | ||
$path, | ||
), | ||
$previous, | ||
); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
packages/documentator/src/Preprocessor/Instructions/IncludeGraphqlDirective/Instruction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Documentator\Preprocessor\Instructions\IncludeGraphqlDirective; | ||
|
||
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Contracts\ProcessableInstruction; | ||
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions\DependencyIsMissing; | ||
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions\TargetIsNotDirective; | ||
use LastDragon_ru\LaraASP\GraphQLPrinter\Contracts\Printer; | ||
use LastDragon_ru\LaraASP\GraphQLPrinter\Settings\ImmutableSettings; | ||
use Override; | ||
|
||
use function mb_substr; | ||
use function trim; | ||
|
||
class Instruction implements ProcessableInstruction { | ||
public function __construct( | ||
protected readonly ?Printer $printer = null, | ||
) { | ||
// empty | ||
} | ||
|
||
#[Override] | ||
public static function getName(): string { | ||
return 'include:graphql-directive'; | ||
} | ||
|
||
#[Override] | ||
public static function getDescription(): string { | ||
return <<<'DESC' | ||
Includes the definition of the directive as a Markdown code block. | ||
DESC; | ||
} | ||
|
||
#[Override] | ||
public static function getTargetDescription(): ?string { | ||
return 'Directive name (started with `@` sign)'; | ||
} | ||
|
||
#[Override] | ||
public function process(string $path, string $target): string { | ||
// Dependencies? | ||
if (!$this->printer) { | ||
throw new DependencyIsMissing($path, $target, Printer::class); | ||
} | ||
|
||
// Directive? | ||
$directive = mb_substr($target, 1); | ||
$definition = $this->printer->getDirectiveResolver()?->getDefinition($directive); | ||
|
||
if ($definition === null) { | ||
throw new TargetIsNotDirective($path, $target); | ||
} | ||
|
||
$origin = $this->printer->getSettings(); | ||
|
||
try { | ||
$settings = ImmutableSettings::createFrom($origin)->setPrintDirectives(false); | ||
$exported = trim((string) $this->printer->setSettings($settings)->export($definition)); | ||
$markdown = <<<MARKDOWN | ||
```graphql | ||
{$exported} | ||
``` | ||
MARKDOWN; | ||
} finally { | ||
$this->printer->setSettings($origin); | ||
} | ||
|
||
// Return | ||
return $markdown; | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
...es/documentator/src/Preprocessor/Instructions/IncludeGraphqlDirective/InstructionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Documentator\Preprocessor\Instructions\IncludeGraphqlDirective; | ||
|
||
use GraphQL\Language\Parser; | ||
use Illuminate\Container\Container; | ||
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions\DependencyIsMissing; | ||
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions\TargetIsNotDirective; | ||
use LastDragon_ru\LaraASP\Documentator\Testing\Package\TestCase; | ||
use LastDragon_ru\LaraASP\GraphQLPrinter\Contracts\DirectiveResolver; | ||
use LastDragon_ru\LaraASP\GraphQLPrinter\Contracts\Printer as PrinterContract; | ||
use LastDragon_ru\LaraASP\GraphQLPrinter\Printer; | ||
use Mockery; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[CoversClass(Instruction::class)] | ||
final class InstructionTest extends TestCase { | ||
// <editor-fold desc="Tests"> | ||
// ========================================================================= | ||
public function testProcess(): void { | ||
$directive = <<<'GRAPHQL' | ||
directive @test | ||
on | ||
| SCALAR | ||
GRAPHQL; | ||
|
||
$this->override(PrinterContract::class, static function () use ($directive): PrinterContract { | ||
$resolver = Mockery::mock(DirectiveResolver::class); | ||
$resolver | ||
->shouldReceive('getDefinition') | ||
->with('test') | ||
->atLeast() | ||
->once() | ||
->andReturn( | ||
Parser::directiveDefinition($directive), | ||
); | ||
|
||
return (new Printer())->setDirectiveResolver($resolver); | ||
}); | ||
|
||
$instance = Container::getInstance()->make(Instruction::class); | ||
$actual = $instance->process('path/to/file.md', '@test'); | ||
|
||
self::assertEquals( | ||
<<<MARKDOWN | ||
```graphql | ||
{$directive} | ||
``` | ||
MARKDOWN, | ||
$actual, | ||
); | ||
} | ||
|
||
public function testProcessNoPrinter(): void { | ||
unset(Container::getInstance()[PrinterContract::class]); | ||
|
||
$path = 'path/to/file.md'; | ||
$target = '@test'; | ||
$instance = Container::getInstance()->make(Instruction::class); | ||
|
||
self::expectExceptionObject( | ||
new DependencyIsMissing($path, $target, PrinterContract::class), | ||
); | ||
|
||
$instance->process($path, $target); | ||
} | ||
|
||
public function testProcessNoDirective(): void { | ||
$this->override(PrinterContract::class, static function (): PrinterContract { | ||
$resolver = Mockery::mock(DirectiveResolver::class); | ||
$resolver | ||
->shouldReceive('getDefinition') | ||
->with('test') | ||
->once() | ||
->andReturn( | ||
null, | ||
); | ||
|
||
return (new Printer())->setDirectiveResolver($resolver); | ||
}); | ||
|
||
$path = 'path/to/file.md'; | ||
$target = '@test'; | ||
$instance = Container::getInstance()->make(Instruction::class); | ||
|
||
self::expectExceptionObject( | ||
new TargetIsNotDirective($path, $target), | ||
); | ||
|
||
$instance->process($path, $target); | ||
} | ||
|
||
public function testProcessNoDirectiveResolver(): void { | ||
$this->override(PrinterContract::class, static function (): PrinterContract { | ||
return (new Printer())->setDirectiveResolver(null); | ||
}); | ||
|
||
$path = 'path/to/file.md'; | ||
$target = '@test'; | ||
$instance = Container::getInstance()->make(Instruction::class); | ||
|
||
self::expectExceptionObject( | ||
new TargetIsNotDirective($path, $target), | ||
); | ||
|
||
$instance->process($path, $target); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.