Skip to content

Commit

Permalink
Deprecate FormColumnType, add tests for built-in column types
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu committed Jan 6, 2025
1 parent da1e2dd commit 9cd2936
Show file tree
Hide file tree
Showing 15 changed files with 822 additions and 162 deletions.
8 changes: 6 additions & 2 deletions src/Column/Type/EnumColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
final class EnumColumnType extends AbstractColumnType
{
public function __construct(
private ?TranslatorInterface $translator,
private ?TranslatorInterface $translator = null,
) {
}

Expand All @@ -22,7 +22,11 @@ public function configureOptions(OptionsResolver $resolver): void

protected function format(\UnitEnum $enum): string
{
return $enum instanceof TranslatableInterface ? $enum->trans($this->translator) : $enum->name;
if ($enum instanceof TranslatableInterface && null !== $this->translator) {
return $enum->trans($this->translator);
}

return $enum->name;
}

public function getParent(): ?string
Expand Down
3 changes: 3 additions & 0 deletions src/Column/Type/FormColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @deprecated
*/
final class FormColumnType extends AbstractColumnType
{
public function buildValueView(ColumnValueView $view, ColumnInterface $column, array $options): void
Expand Down
57 changes: 56 additions & 1 deletion src/Test/Column/Type/ColumnTypeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
namespace Kreyu\Bundle\DataTableBundle\Test\Column\Type;

use Kreyu\Bundle\DataTableBundle\Column\ColumnFactoryInterface;
use Kreyu\Bundle\DataTableBundle\Column\ColumnHeaderView;
use Kreyu\Bundle\DataTableBundle\Column\ColumnInterface;
use Kreyu\Bundle\DataTableBundle\Column\ColumnRegistry;
use Kreyu\Bundle\DataTableBundle\Column\ColumnRegistryInterface;
use Kreyu\Bundle\DataTableBundle\Column\ColumnValueView;
use Kreyu\Bundle\DataTableBundle\Column\Type\ColumnType;
use Kreyu\Bundle\DataTableBundle\Column\Type\ColumnTypeInterface;
use Kreyu\Bundle\DataTableBundle\Column\Type\ResolvedColumnTypeFactory;
use Kreyu\Bundle\DataTableBundle\Column\Type\ResolvedColumnTypeFactoryInterface;
Expand All @@ -16,11 +19,14 @@
use Kreyu\Bundle\DataTableBundle\DataTableInterface;
use Kreyu\Bundle\DataTableBundle\DataTableRegistry;
use Kreyu\Bundle\DataTableBundle\DataTableRegistryInterface;
use Kreyu\Bundle\DataTableBundle\DataTableView;
use Kreyu\Bundle\DataTableBundle\HeaderRowView;
use Kreyu\Bundle\DataTableBundle\Query\ArrayProxyQuery;
use Kreyu\Bundle\DataTableBundle\Tests\Fixtures\Column\TestColumnFactory;
use Kreyu\Bundle\DataTableBundle\Type\DataTableType;
use Kreyu\Bundle\DataTableBundle\Type\ResolvedDataTableTypeFactory;
use Kreyu\Bundle\DataTableBundle\Type\ResolvedDataTableTypeFactoryInterface;
use Kreyu\Bundle\DataTableBundle\ValueRowView;
use PHPUnit\Framework\TestCase;

abstract class ColumnTypeTestCase extends TestCase
Expand All @@ -35,6 +41,16 @@ abstract class ColumnTypeTestCase extends TestCase

abstract protected function getTestedColumnType(): ColumnTypeInterface;

/**
* @return ColumnTypeInterface[]
*/
protected function getAdditionalColumnTypes(): array
{
return [
new ColumnType(),
];
}

protected function createColumn(array $options = []): ColumnInterface
{
return $this->getColumnFactory()->create($this->getTestedColumnType()::class, $options);
Expand Down Expand Up @@ -66,7 +82,10 @@ protected function getColumnRegistry(): ColumnRegistryInterface
protected function createColumnRegistry(): ColumnRegistryInterface
{
return new ColumnRegistry(
types: [$this->getTestedColumnType()],
types: [
$this->getTestedColumnType(),
...$this->getAdditionalColumnTypes(),
],
typeExtensions: [],
resolvedTypeFactory: $this->getResolvedColumnTypeFactory(),
);
Expand Down Expand Up @@ -126,4 +145,40 @@ protected function createDataTable(): DataTableInterface
{
return $this->getDataTableFactory()->create(DataTableType::class, new ArrayProxyQuery([]));
}

protected function createHeaderRowView(?DataTableView $dataTableView = null): HeaderRowView
{
return new HeaderRowView($dataTableView ?? new DataTableView());
}

protected function createValueRowView(?DataTableView $dataTableView = null, int $index = 0, mixed $data = null): ValueRowView
{
return new ValueRowView($dataTableView ?? new DataTableView(), $index, $data);
}

protected function createColumnHeaderView(ColumnInterface $column, ?HeaderRowView $headerRowView = null): ColumnHeaderView
{
return $column->createHeaderView($headerRowView ?? $this->createHeaderRowView());
}

protected function createExportColumnHeaderView(ColumnInterface $column, ?HeaderRowView $headerRowView = null): ColumnHeaderView
{
return $column->createExportHeaderView($headerRowView ?? $this->createHeaderRowView());
}

protected function createColumnValueView(ColumnInterface $column, ?ValueRowView $valueRowView = null, mixed $data = null, mixed $rowData = null): ColumnValueView
{
$columnValueView = $column->createValueView($valueRowView ?? $this->createValueRowView(data: $rowData));
$columnValueView->data = $data;

return $columnValueView;
}

protected function createExportColumnValueView(ColumnInterface $column, ?ValueRowView $valueRowView = null, mixed $data = null, mixed $rowData = null): ColumnValueView
{
$columnValueView = $column->createExportValueView($valueRowView ?? $this->createValueRowView(data: $rowData));
$columnValueView->data = $data;

return $columnValueView;
}
}
22 changes: 22 additions & 0 deletions tests/Fixtures/Enum/TranslatableEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\Tests\Fixtures\Enum;

use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

enum TranslatableEnum implements TranslatableInterface
{
case Foo;
case Bar;

public function trans(TranslatorInterface $translator, ?string $locale = null): string
{
return match ($this) {
self::Foo => 'Translated foo',
self::Bar => 'Translated bar',
};
}
}
11 changes: 11 additions & 0 deletions tests/Fixtures/Enum/UnitEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\Tests\Fixtures\Enum;

enum UnitEnum
{
case Foo;
case Bar;
}
39 changes: 39 additions & 0 deletions tests/Unit/Column/Type/BooleanColumnTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\Tests\Unit\Column\Type;

use Kreyu\Bundle\DataTableBundle\Column\Type\BooleanColumnType;
use Kreyu\Bundle\DataTableBundle\Column\Type\ColumnTypeInterface;
use Kreyu\Bundle\DataTableBundle\Test\Column\Type\ColumnTypeTestCase;

class BooleanColumnTypeTest extends ColumnTypeTestCase
{
protected function getTestedColumnType(): ColumnTypeInterface
{
return new BooleanColumnType();
}

public function testLabelTrueOption(): void
{
$column = $this->createColumn([
'label_true' => 'True',
]);

$valueView = $this->createColumnValueView($column);

$this->assertEquals('True', $valueView->vars['label_true']);
}

public function testLabelFalseOption(): void
{
$column = $this->createColumn([
'label_false' => 'False',
]);

$valueView = $this->createColumnValueView($column);

$this->assertEquals('False', $valueView->vars['label_false']);
}
}
30 changes: 30 additions & 0 deletions tests/Unit/Column/Type/CheckboxColumnTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\Tests\Unit\Column\Type;

use Kreyu\Bundle\DataTableBundle\Column\Type\CheckboxColumnType;
use Kreyu\Bundle\DataTableBundle\Column\Type\ColumnTypeInterface;
use Kreyu\Bundle\DataTableBundle\Test\Column\Type\ColumnTypeTestCase;

class CheckboxColumnTypeTest extends ColumnTypeTestCase
{
protected function getTestedColumnType(): ColumnTypeInterface
{
return new CheckboxColumnType();
}

public function testPassingIdentifierNameOption(): void
{
$column = $this->createColumn([
'identifier_name' => 'uuid',
]);

$columnHeaderView = $this->createColumnHeaderView($column);
$columnValueView = $this->createColumnValueView($column);

$this->assertEquals('uuid', $columnHeaderView->vars['identifier_name']);
$this->assertEquals('uuid', $columnValueView->vars['identifier_name']);
}
}
87 changes: 87 additions & 0 deletions tests/Unit/Column/Type/CollectionColumnTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\Tests\Unit\Column\Type;

use Kreyu\Bundle\DataTableBundle\Column\ColumnValueView;
use Kreyu\Bundle\DataTableBundle\Column\Type\CollectionColumnType;
use Kreyu\Bundle\DataTableBundle\Column\Type\ColumnType;
use Kreyu\Bundle\DataTableBundle\Column\Type\ColumnTypeInterface;
use Kreyu\Bundle\DataTableBundle\Column\Type\NumberColumnType;
use Kreyu\Bundle\DataTableBundle\Column\Type\TextColumnType;
use Kreyu\Bundle\DataTableBundle\Test\Column\Type\ColumnTypeTestCase;

class CollectionColumnTypeTest extends ColumnTypeTestCase
{
protected function getTestedColumnType(): ColumnTypeInterface
{
return new CollectionColumnType();
}

protected function getAdditionalColumnTypes(): array
{
return [
new NumberColumnType(),
new TextColumnType(),
new ColumnType(),
];
}

public function testPassingSeparatorOptionAsString(): void
{
$column = $this->createColumn([
'separator' => '|',
]);

$valueView = $this->createColumnValueView($column);

$this->assertEquals('|', $valueView->vars['separator']);
}

public function testPassingSeparatorOptionAsNull(): void
{
$column = $this->createColumn([
'separator' => null,
]);

$valueView = $this->createColumnValueView($column);

$this->assertNull($valueView->vars['separator']);
}

public function testCreatesChildren(): void
{
$column = $this->createColumn([
'entry_type' => NumberColumnType::class,
'entry_options' => [
'use_intl_formatter' => false,
],
]);

$data = new class {
public array $collection = [1, 2, 3];
};

$valueRowView = $this->createValueRowView(data: $data);
$columnValueView = $this->createColumnValueView($column, $valueRowView);

$this->assertCount(3, $columnValueView->vars['children']);
$this->assertContainsOnlyInstancesOf(ColumnValueView::class, $columnValueView->vars['children']);

for ($i = 0; $i <= 2; ++$i) {
/** @var ColumnValueView $child */
$child = $columnValueView->vars['children'][$i];

$expectedValueRowView = clone $valueRowView;
$expectedValueRowView->origin = $valueRowView;
$expectedValueRowView->index = $i;
$expectedValueRowView->data = $data->collection[$i];

$this->assertEquals((string) $i, $child->vars['name']);
$this->assertEquals($expectedValueRowView, $child->vars['row']);
$this->assertFalse($child->vars['use_intl_formatter']);
$this->assertEquals($child->getDataTable(), $columnValueView->getDataTable());
}
}
}
Loading

0 comments on commit 9cd2936

Please sign in to comment.