Skip to content

Commit

Permalink
Refactors tests (#7)
Browse files Browse the repository at this point in the history
* Refactors tests
 - Adds tests for typecast handler
 - Adds tests for macros
 - Adds tests for relations
 - Adds tests for STI
 - Adds tests for JTI

* Apply fixes from StyleCI

* Fixes CS

* Added tests for cycle/orm v2

* Added schema constants via ConstantsInterface

* Fixes tests for CycleORM v1 and v2.

* Adds missed test

* Renames discriminator column property name

* Fixes tests

Co-authored-by: Aleksei Gagarin <[email protected]>
  • Loading branch information
butschster and roxblnfk authored Dec 9, 2021
1 parent 46b8abb commit 2d14ab2
Show file tree
Hide file tree
Showing 37 changed files with 1,386 additions and 128 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/main-v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: build-v2

on:
push:
pull_request:
schedule:
- cron: '0 0 * * *'

jobs:
test:
name: Build (${{matrix.php}}, ${{ matrix.os }}, ${{ matrix.stability }})
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
php: [ '8.0', '8.1' ]
os: [ ubuntu-latest ]
stability: [ prefer-lowest, prefer-stable ]

steps:
- name: Set Git To Use LF
run: |
git config --global core.autocrlf false
git config --global core.eol lf
- name: Checkout
uses: actions/checkout@v2

# Install PHP Dependencies
- name: Setup PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
# PHP Extras
extensions: pdo, pdo_mysql, pdo_sqlite, sockets
coverage: pcov
tools: pecl
ini-values: "memory_limit=-1"
- name: Validate Composer
run: composer validate
- name: Get Composer Cache Directory
# Docs: <https://github.com/actions/cache/blob/master/examples.md#php---composer>
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Restore Composer Cache
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.stability }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-${{ matrix.php }}-composer-

- name: Install dependencies with composer
if: matrix.php-version != '8.1'
run: composer update --with cycle/orm:2.0.x-dev --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Install dependencies with composer php 8.1
if: matrix.php-version == '8.1'
run: composer update --with cycle/orm:2.0.x-dev --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi --ignore-platform-reqs

# Execution
- name: Execute Tests
run: vendor/bin/phpunit --coverage-clover=coverage.xml
- name: Upload Coverage To Codecov
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
fail_ci_if_error: false
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: build
name: build-v1

on:
push:
Expand Down Expand Up @@ -46,7 +46,7 @@ jobs:
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
key: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.stability }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-${{ matrix.php }}-composer-

- name: Install dependencies with composer
Expand Down
2 changes: 2 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ risky: true
version: 7

finder:
exclude:
- "tests"
not-name:
- "*.stub.txt"

Expand Down
10 changes: 5 additions & 5 deletions src/ConsoleRenderer/Renderer/ColumnsRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ public function render(Formatter $formatter, array $schema, string $role): ?stri
$rows = [];

$columns = $schema[SchemaInterface::COLUMNS] ?? [];
$title = sprintf('%s:', $formatter->title('Fields'));
$title = \sprintf('%s:', $formatter->title('Fields'));

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

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

$rows[] = $title;

$rows[] = sprintf(
$rows[] = \sprintf(
'%s(%s -> %s -> %s)',
$padding,
$formatter->property('property'),
Expand All @@ -37,15 +37,15 @@ public function render(Formatter $formatter, array $schema, string $role): ?stri

foreach ($columns as $property => $field) {
$typecast = $types[$property] ?? $types[$field] ?? null;
$row = sprintf(
$row = \sprintf(
'%s%s -> %s',
$padding,
$formatter->property((string)$property),
$formatter->column($field)
);

if ($typecast !== null && $typecast !== [] && $typecast !== '') {
$row .= sprintf(
$row .= \sprintf(
' -> %s',
$formatter->typecast(\implode('::', (array)$typecast))
);
Expand Down
29 changes: 23 additions & 6 deletions src/ConsoleRenderer/Renderer/CustomPropertiesRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CustomPropertiesRenderer implements Renderer
private array $exclude;

/**
* @param array<int, int> $exclude Values list that should be excluded
* @param array<int, int> $exclude Values list that should be excluded
*/
public function __construct(array $exclude)
{
Expand All @@ -21,26 +21,43 @@ public function __construct(array $exclude)

public function render(Formatter $formatter, array $schema, string $role): ?string
{
$customProperties = array_diff(array_keys($schema), $this->exclude);
$customProperties = \array_diff(\array_keys($schema), $this->exclude);

if ($customProperties === []) {
return null;
}

$rows = [
sprintf('%s:', $formatter->title('Custom props')),
\sprintf('%s:', $formatter->title('Custom props')),
];

foreach ($customProperties as $property) {
$data = $schema[$property] ?? null;

$rows[] = sprintf(
' %s: %s',
$rows[] = \sprintf(
'%s%s: %s',
$formatter->title(' '),
$property,
$formatter->typecast(print_r($data, true))
$formatter->typecast($this->printValue($data, $formatter))
);
}

return \implode($formatter::LINE_SEPARATOR, $rows);
}

/**
* @param mixed $value
*/
private function printValue($value, Formatter $formatter): string
{
$data = \trim(\var_export($value, true), '\'');
$data = \array_map(
static fn (string $row): string => $formatter->title(' ') . $row,
\explode("\n", $data)
);

return \ltrim(
\implode("\n", $data)
);
}
}
2 changes: 1 addition & 1 deletion src/ConsoleRenderer/Renderer/KeysRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function render(Formatter $formatter, array $schema, string $role): ?stri
{
$keys = $schema[$this->key] ?? null;

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

if ($keys === null || $keys === '' || $keys === []) {
return $this->required ? $row . $formatter->error('not defined') : null;
Expand Down
45 changes: 30 additions & 15 deletions src/ConsoleRenderer/Renderer/MacrosRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,34 @@

namespace Cycle\Schema\Renderer\ConsoleRenderer\Renderer;

use Cycle\ORM\SchemaInterface;
use Cycle\Schema\Renderer\ConsoleRenderer\Formatter;
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer;

class MacrosRenderer implements Renderer
{
private int $property;
private string $title;

public function __construct(int $property, string $title)
{
$this->property = $property;
$this->title = $title;
}

public function render(Formatter $formatter, array $schema, string $role): ?string
{
if (! \defined(SchemaInterface::class . '::MACROS')) {
if (! isset($schema[$this->property])) {
return null;
}
$macrosList = (array)($schema[SchemaInterface::MACROS] ?? []);

$macrosList = (array)($schema[$this->property] ?? []);

if ($macrosList === []) {
return null;
}

$rows = [
sprintf('%s:', $formatter->title('Entity macros')),
\sprintf('%s:', $formatter->title($this->title)),
];

foreach ($macrosList as $definition) {
Expand All @@ -32,39 +41,45 @@ public function render(Formatter $formatter, array $schema, string $role): ?stri
$class = \is_string($data[0] ?? null)
? $formatter->typecast($data[0])
: $formatter->error('undefined');
$rows[] = sprintf(' %s', $class);
$rows[] = \sprintf('%s%s', $formatter->title(' '), $class);

// macros params
$params = $data[1] ?? null;
if ($params === null) {
continue;
}
if (is_array($params)) {
if (\is_array($params)) {
foreach ($params as $key => $value) {
$row = ' ';
$row = $formatter->title(' ') . ' - ';
if (\is_string($key)) {
$row .= $formatter->property($key) . ' : ';
}
$row .= $formatter->info($this->printValue($formatter, $value));
$row .= $formatter->info($this->printValue($value, $formatter));
$rows[] = $row;
}
} elseif (is_string($params)) {
$rows[] = $formatter->title(' ') . ' - ' . $formatter->info($this->printValue($params, $formatter));
} else {
$rows[] = $formatter->typecast($this->printValue($formatter, $data));
$rows[] = $formatter->typecast($this->printValue($data, $formatter));
}
}

return \implode($formatter::LINE_SEPARATOR, $rows);
}

/**
* @param mixed $value
* @param mixed $value
*/
private function printValue(Formatter $formatter, $value): string
private function printValue($value, Formatter $formatter): string
{
return str_replace(
["\r\n", "\n"],
$formatter::LINE_SEPARATOR . ' ',
print_r($value, true)
$data = \trim(\var_export($value, true), '\'');
$data = \array_map(
static fn (string $row): string => $formatter->title(' ') . $row,
\explode("\n", $data)
);

return \ltrim(
\implode("\n", $data)
);
}
}
14 changes: 7 additions & 7 deletions src/ConsoleRenderer/Renderer/PropertyRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ public function __construct(int $property, string $title, bool $required = false

public function render(Formatter $formatter, array $schema, string $role): ?string
{
$row = sprintf('%s: ', $formatter->title($this->title));
$row = \sprintf('%s: ', $formatter->title($this->title));

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

$propertyValue = $schema[$this->property];

if (is_array($propertyValue)) {
if (count($propertyValue) >= 1) {
if (\is_array($propertyValue)) {
if (\count($propertyValue) >= 1) {
return $row . $this->convertArrayToString($formatter, $propertyValue);
}
$propertyValue = '[]';
}

return sprintf(
return \sprintf(
'%s%s',
$row,
$formatter->typecast($propertyValue)
Expand All @@ -46,15 +46,15 @@ public function render(Formatter $formatter, array $schema, string $role): ?stri

private function convertArrayToString(Formatter $formatter, array $values): string
{
$string = implode(
$string = \implode(
"\n",
array_map(static fn ($property) => sprintf(
\array_map(static fn ($property) => \sprintf(
' %s%s',
$formatter->title(' '),
$formatter->typecast($property)
), $values)
);

return ltrim($string);
return \ltrim($string);
}
}
Loading

0 comments on commit 2d14ab2

Please sign in to comment.