Skip to content

Commit

Permalink
add schema render command
Browse files Browse the repository at this point in the history
  • Loading branch information
wakebit committed Apr 16, 2022
1 parent 650fd37 commit a947508
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ See more on [the official Cycle ORM documentation](https://cycle-orm.dev/docs/re
| `cycle:schema:cache` | Compile and cache ORM schema | | `\Wakebit\CycleBridge\Console\Command\Schema\CacheCommand::class` |
| `cycle:schema:clear` | Clear cached schema (schema will be generated every request now) | | `\Wakebit\CycleBridge\Console\Command\Schema\ClearCommand::class` |
| `cycle:schema:sync` | Sync ORM schema with database without intermediate migration (risk operation!) | | `\Wakebit\CycleBridge\Console\Command\Schema\SyncCommand::class` |
| `cycle:schema:render` | Render available CycleORM schemas. | - `--no-color`: Display output without colors. | `\Wakebit\CycleBridge\Console\Command\Schema\RenderCommand::class` |


### Database migrations
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"cycle/orm": "^2.0",
"cycle/schema-builder": "^2.0",
"cycle/schema-migrations-generator": "^2.0",
"cycle/schema-renderer": "^1.1",
"psr/container": "^1.0|^2.0",
"psr/simple-cache": "^1.0|^2.0|^3.0",
"spiral/core": "^2.9",
Expand Down
57 changes: 57 additions & 0 deletions src/Console/Command/Schema/RenderCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* Credits: Spiral Cycle Bridge.
*
* @see https://github.com/spiral/cycle-bridge
*/

declare(strict_types=1);

namespace Wakebit\CycleBridge\Console\Command\Schema;

use Cycle\ORM\SchemaInterface;
use Cycle\Schema\Renderer\OutputSchemaRenderer;
use Cycle\Schema\Renderer\SchemaToArrayConverter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

final class RenderCommand extends Command
{
private string $name = 'cycle:schema:render';
private string $description = 'Render available CycleORM schemas.';

public function __construct(
private SchemaInterface $schema,
private SchemaToArrayConverter $converter,
) {
parent::__construct();
}

protected function configure(): void
{
$this
->setName($this->name)
->setDescription($this->description)
->addOption('no-color', 'nc', InputOption::VALUE_NONE, 'Display output without colors.');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$format = $input->getOption('no-color') ?
OutputSchemaRenderer::FORMAT_PLAIN_TEXT :
OutputSchemaRenderer::FORMAT_CONSOLE_COLOR;

$renderer = new OutputSchemaRenderer($format);

$output->writeln(
$renderer->render(
$this->converter->convert($this->schema),
),
);

return Command::SUCCESS;
}
}
46 changes: 46 additions & 0 deletions tests/src/Console/Command/Schema/RenderCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Wakebit\CycleBridge\Tests\Console\Command\Schema;

use Cycle\ORM\Mapper\Mapper;
use Cycle\ORM\Select\Repository;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Wakebit\CycleBridge\Console\Command\Schema\RenderCommand;
use Wakebit\CycleBridge\TestApp\Entity\Article;
use Wakebit\CycleBridge\TestApp\Entity\Customer;
use Wakebit\CycleBridge\Tests\Constraints\SeeInOrder;
use Wakebit\CycleBridge\Tests\TestCase;

final class RenderCommandTest extends TestCase
{
public function testRender(): void
{
$commandTester = new CommandTester($this->container->get(RenderCommand::class));
$exitCode = $commandTester->execute(['-nc' => true], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
$realOutput = $commandTester->getDisplay();
$expectedOutput = [
'[customer] :: default.customers',
'Entity:', Customer::class,
'Mapper:', Mapper::class,
'Repository:', Repository::class,
'Primary key:', 'id',
'Fields', '(property -> db.field -> typecast)', 'id -> id -> int', 'name -> name',
'Relations:', 'not defined',

'[article] :: default.articles',
'Entity:', Article::class,
'Mapper:', Mapper::class,
'Repository:', Repository::class,
'Primary key:', 'id',
'Fields', '(property -> db.field -> typecast)', 'id -> id -> int', 'title -> title', 'description -> description',
'Relations:', 'not defined',
];

$this->assertSame(Command::SUCCESS, $exitCode);
$this->assertThat($expectedOutput, new SeeInOrder($realOutput));
}
}

0 comments on commit a947508

Please sign in to comment.