From a947508fd65d6a0e7fe8f202cfc65072c9237b1c Mon Sep 17 00:00:00 2001 From: wakebit <32976842+wakebit@users.noreply.github.com> Date: Sat, 16 Apr 2022 13:04:04 +0500 Subject: [PATCH] add schema render command --- README.md | 1 + composer.json | 1 + src/Console/Command/Schema/RenderCommand.php | 57 +++++++++++++++++++ .../Command/Schema/RenderCommandTest.php | 46 +++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 src/Console/Command/Schema/RenderCommand.php create mode 100644 tests/src/Console/Command/Schema/RenderCommandTest.php diff --git a/README.md b/README.md index 0258300..a6ce7f6 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/composer.json b/composer.json index 0877238..040a802 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Console/Command/Schema/RenderCommand.php b/src/Console/Command/Schema/RenderCommand.php new file mode 100644 index 0000000..09c74a7 --- /dev/null +++ b/src/Console/Command/Schema/RenderCommand.php @@ -0,0 +1,57 @@ +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; + } +} diff --git a/tests/src/Console/Command/Schema/RenderCommandTest.php b/tests/src/Console/Command/Schema/RenderCommandTest.php new file mode 100644 index 0000000..46b24bc --- /dev/null +++ b/tests/src/Console/Command/Schema/RenderCommandTest.php @@ -0,0 +1,46 @@ +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)); + } +}