Skip to content

Commit

Permalink
Addsunit tests for property renderers (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster authored Nov 22, 2021
1 parent 8fae316 commit 46b8abb
Show file tree
Hide file tree
Showing 15 changed files with 441 additions and 41 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Cycle ORM Schema renderer

[![Latest Stable Version](https://poser.pugx.org/cycle/schema-renderer/v/stable)](https://packagist.org/packages/cycle/schema-renderer)
[![build](https://github.com/cycle/schema-renderer/actions/workflows/main.yml/badge.svg)](https://github.com/cycle/schema-renderer/actions/workflows/main.yml)
[![static analysis](https://github.com/cycle/schema-renderer/actions/workflows/static.yml/badge.svg)](https://github.com/cycle/schema-renderer/actions/workflows/static.yml)
[![Codecov](https://codecov.io/gh/cycle/schema-renderer/branch/master/graph/badge.svg)](https://codecov.io/gh/cycle/schema-renderer/)
[![StyleCI](https://github.styleci.io/repos/401633317/shield?branch=master)](https://github.styleci.io/repos/401633317?branch=master)

This package may be used to render Cycle ORM Schema in a terminal or generate php representation.

## Installation
Expand Down
12 changes: 8 additions & 4 deletions src/ConsoleRenderer/Renderer/ColumnsRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ public function render(Formatter $formatter, array $schema, string $role): ?stri
$columns = $schema[SchemaInterface::COLUMNS] ?? [];
$title = sprintf('%s:', $formatter->title('Fields'));

if (count($columns) === 0) {
if (! \is_array($columns) || count($columns) === 0) {
return $title . ' ' . $formatter->error('not defined');
}

$padding = $formatter->title('') . ' ';

$rows[] = $title;

$rows[] = sprintf(
' (%s -> %s -> %s)',
'%s(%s -> %s -> %s)',
$padding,
$formatter->property('property'),
$formatter->column('db.field'),
$formatter->typecast('typecast')
Expand All @@ -35,12 +38,13 @@ public function render(Formatter $formatter, array $schema, string $role): ?stri
foreach ($columns as $property => $field) {
$typecast = $types[$property] ?? $types[$field] ?? null;
$row = sprintf(
' %s -> %s',
'%s%s -> %s',
$padding,
$formatter->property((string)$property),
$formatter->column($field)
);

if ($typecast !== null) {
if ($typecast !== null && $typecast !== [] && $typecast !== '') {
$row .= sprintf(
' -> %s',
$formatter->typecast(\implode('::', (array)$typecast))
Expand Down
2 changes: 1 addition & 1 deletion src/ConsoleRenderer/Renderer/KeysRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function render(Formatter $formatter, array $schema, string $role): ?stri

$row = sprintf('%s: ', $formatter->title($this->title));

if ($keys === null) {
if ($keys === null || $keys === '' || $keys === []) {
return $this->required ? $row . $formatter->error('not defined') : null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/ConsoleRenderer/Renderer/MacrosRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MacrosRenderer implements Renderer
{
public function render(Formatter $formatter, array $schema, string $role): ?string
{
if (!\defined(SchemaInterface::class . '::MACROS')) {
if (! \defined(SchemaInterface::class . '::MACROS')) {
return null;
}
$macrosList = (array)($schema[SchemaInterface::MACROS] ?? []);
Expand Down Expand Up @@ -57,7 +57,7 @@ public function render(Formatter $formatter, array $schema, string $role): ?stri
}

/**
* @param mixed $value
* @param mixed $value
*/
private function printValue(Formatter $formatter, $value): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/ConsoleRenderer/Renderer/PropertyRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function render(Formatter $formatter, array $schema, string $role): ?stri
{
$row = sprintf('%s: ', $formatter->title($this->title));

if (!isset($schema[$this->property])) {
if (! isset($schema[$this->property])) {
return $this->required ? $row . $formatter->error('not defined') : null;
}

Expand Down
3 changes: 2 additions & 1 deletion src/ConsoleRenderer/Renderer/RelationsRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function render(Formatter $formatter, array $schema, string $role): ?stri
}

/**
* @param array<string>|string $keys
* @param array<string>|string $keys
*/
private function renderKeys(Formatter $formatter, $keys): string
{
Expand All @@ -140,6 +140,7 @@ private function renderKeys(Formatter $formatter, $keys): string
static fn (string $key) => $formatter->property($key),
$keys
);

return sprintf(
'%s%s%s',
$braces ? '[' : '',
Expand Down
17 changes: 6 additions & 11 deletions src/ConsoleRenderer/Renderer/TitleRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@ public function render(Formatter $formatter, array $schema, string $role): ?stri
$database = $schema[SchemaInterface::DATABASE] ?? '<undefined databse>';
$table = $schema[SchemaInterface::TABLE] ?? '<undefined table>';

$row = $formatter->entity("[{$role}]");

if ($database !== null) {
$row .= sprintf(
' :: %s.%s',
$formatter->column($database),
$formatter->column($table)
);
}

return $row;
return sprintf(
'%s :: %s.%s',
$formatter->entity("[{$role}]"),
$formatter->column($database),
$formatter->column($table)
);
}
}
102 changes: 102 additions & 0 deletions tests/Schema/Renderer/ConsoleRenderer/Renderer/ColumnsRendererTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace Cycle\Schema\Renderer\Tests\ConsoleRenderer\Renderer;

use Cycle\ORM\SchemaInterface;
use Cycle\Schema\Renderer\ConsoleRenderer\Formatter;
use Cycle\Schema\Renderer\ConsoleRenderer\Formatter\PlainFormatter;
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\ColumnsRenderer;
use PHPUnit\Framework\TestCase;

class ColumnsRendererTest extends TestCase
{
private Formatter $formatter;

protected function setUp(): void
{
parent::setUp();

$this->formatter = new PlainFormatter();
}

public function testRenderNullValue()
{
$renderer = new ColumnsRenderer();

$result = $renderer->render($this->formatter, [SchemaInterface::COLUMNS => null], 'bar');

$this->assertSame(' Fields: not defined', $result);
}

public function testRenderStringValue()
{
$renderer = new ColumnsRenderer();

$result = $renderer->render($this->formatter, [SchemaInterface::COLUMNS => 'foo'], 'bar');

$this->assertSame(' Fields: not defined', $result);
}

public function testRenderArrayWithoutTypecastValue()
{
$renderer = new ColumnsRenderer();

$result = $renderer->render($this->formatter, [
SchemaInterface::COLUMNS => [
'id' => 'integer',
'title' => 'string(36)',
'description' => 'string',
],
], 'bar');

$this->assertSame(
<<<'OUT'
Fields:
(property -> db.field -> typecast)
id -> integer
title -> string(36)
description -> string
OUT
,
$result
);
}

public function testRenderArrayWithTypecastValue()
{
$renderer = new ColumnsRenderer();

$result = $renderer->render($this->formatter, [
SchemaInterface::COLUMNS => [
'id' => 'integer',
'title' => 'string',
'slug' => 'string',
'json' => 'json',
'description' => 'text',
],
SchemaInterface::TYPECAST => [
'id' => 'int',
'title' => null,
'slug' => '',
'json' => ['Json', 'cast'],
'description' => [],
],
], 'bar');

$this->assertSame(
<<<'OUT'
Fields:
(property -> db.field -> typecast)
id -> integer -> int
title -> string
slug -> string
json -> json -> Json::cast
description -> text
OUT
,
$result
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Cycle\Schema\Renderer\Tests\ConsoleRenderer\Renderer;

use Cycle\Schema\Renderer\ConsoleRenderer\Formatter;
use Cycle\Schema\Renderer\ConsoleRenderer\Formatter\PlainFormatter;
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\KeysRenderer;
use PHPUnit\Framework\TestCase;

class KeysRendererTest extends TestCase
{
private Formatter $formatter;

protected function setUp(): void
{
parent::setUp();

$this->formatter = new PlainFormatter();
}

public function testRenderNullNotRequiredValue()
{
$renderer = new KeysRenderer(1, 'Foo', false);

$result = $renderer->render($this->formatter, [1 => null], 'bar');

$this->assertNull($result);
}

public function testRenderNullRequiredValue()
{
$renderer = new KeysRenderer(1, 'Foo', true);

$result = $renderer->render($this->formatter, [1 => null], 'bar');

$this->assertSame(' Foo: not defined', $result);
}

public function testRenderStringValue()
{
$renderer = new KeysRenderer(1, 'Foo');

$result = $renderer->render($this->formatter, [1 => 'foo'], 'bar');

$this->assertSame(' Foo: foo', $result);
}

public function testRenderEmptyStringValue()
{
$renderer = new KeysRenderer(1, 'Foo');

$result = $renderer->render($this->formatter, [1 => ''], 'bar');

$this->assertSame(' Foo: not defined', $result);
}

public function testRenderEmptyArrayValue()
{
$renderer = new KeysRenderer(1, 'Foo');

$result = $renderer->render($this->formatter, [1 => []], 'bar');

$this->assertSame(' Foo: not defined', $result);
}

public function testRenderSingleItemArrayValue()
{
$renderer = new KeysRenderer(1, 'Foo');

$result = $renderer->render($this->formatter, [1 => ['hello']], 'bar');

$this->assertSame(' Foo: hello', $result);
}

public function testRenderArrayValue()
{
$renderer = new KeysRenderer(1, 'Foo');

$result = $renderer->render($this->formatter, [1 => ['hello', 'world']], 'bar');

$this->assertSame(' Foo: hello, world', $result);
}
}
Loading

0 comments on commit 46b8abb

Please sign in to comment.