-
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.
perf(documentator)!: Minor
Markdown
mutations improvements (#184)
- Loading branch information
Showing
12 changed files
with
237 additions
and
64 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
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
36 changes: 36 additions & 0 deletions
36
packages/documentator/src/Markdown/Mutations/Composite.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,36 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Documentator\Markdown\Mutations; | ||
|
||
use LastDragon_ru\LaraASP\Documentator\Markdown\Contracts\Mutation; | ||
use LastDragon_ru\LaraASP\Documentator\Markdown\Document; | ||
use League\CommonMark\Node\Block\Document as DocumentNode; | ||
use Override; | ||
|
||
/** | ||
* Merges all mutations into one. | ||
*/ | ||
readonly class Composite implements Mutation { | ||
/** | ||
* @var array<array-key, Mutation> | ||
*/ | ||
protected array $mutations; | ||
|
||
public function __construct(Mutation ...$mutations) { | ||
$this->mutations = $mutations; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
#[Override] | ||
public function __invoke(Document $document, DocumentNode $node): iterable { | ||
// Just in case | ||
yield from []; | ||
|
||
// Process all | ||
foreach ($this->mutations as $mutation) { | ||
yield from $mutation($document, $node); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
packages/documentator/src/Markdown/Mutations/CompositeTest.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,55 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Documentator\Markdown\Mutations; | ||
|
||
use LastDragon_ru\LaraASP\Core\Utils\Cast; | ||
use LastDragon_ru\LaraASP\Documentator\Markdown\Contracts\Mutation; | ||
use LastDragon_ru\LaraASP\Documentator\Markdown\Document; | ||
use LastDragon_ru\LaraASP\Documentator\Markdown\Location\Location; | ||
use LastDragon_ru\LaraASP\Documentator\Testing\Package\TestCase; | ||
use League\CommonMark\Node\Block\Document as DocumentNode; | ||
use Mockery; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use ReflectionProperty; | ||
|
||
use function array_merge; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[CoversClass(Composite::class)] | ||
final class CompositeTest extends TestCase { | ||
public function testInvoke(): void { | ||
$document = new Document(''); | ||
$node = Cast::to(DocumentNode::class, (new ReflectionProperty($document, 'node'))->getValue($document)); | ||
$aChanges = [ | ||
[new Location(1, 1, 10), 'a'], | ||
]; | ||
$bChanges = [ | ||
[new Location(2, 3, 0), 'b'], | ||
]; | ||
$aMutation = Mockery::mock(Mutation::class); | ||
$aMutation | ||
->shouldReceive('__invoke') | ||
->with($document, $node) | ||
->once() | ||
->andReturn($aChanges); | ||
$bMutation = Mockery::mock(Mutation::class); | ||
$bMutation | ||
->shouldReceive('__invoke') | ||
->with($document, $node) | ||
->once() | ||
->andReturn($bChanges); | ||
|
||
$mutation = new Composite($aMutation, $bMutation); | ||
$changes = $mutation($document, $node); | ||
$expected = array_merge($aChanges, $bChanges); | ||
$actual = []; | ||
|
||
foreach ($changes as $change) { | ||
$actual[] = $change; | ||
} | ||
|
||
self::assertEquals($expected, $actual); | ||
} | ||
} |
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
Oops, something went wrong.