Skip to content

Commit

Permalink
Merge pull request #88 from heimrichhannot/feature/dca-options-extension
Browse files Browse the repository at this point in the history
Add DCA eval config to DcaFieldConfiguration
  • Loading branch information
ericges authored Nov 27, 2024
2 parents 2d64083 + fc3ba22 commit 49d1940
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 13 deletions.
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
<directory>./tests/</directory>
<exclude>tests/Arrays</exclude>
<exclude>tests/Choice</exclude>
<exclude>tests/Dca</exclude>
<exclude>tests/Driver</exclude>
<exclude>tests/File</exclude>
<exclude>tests/Form</exclude>
Expand Down
22 changes: 18 additions & 4 deletions src/Dca/DcaFieldConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ class DcaFieldConfiguration
protected bool $search = false;
protected bool $filter = false;
protected bool $sorting = false;
protected array $eval = [];

/**
* @param string $table
*/
public function __construct(private string $table)
{

}
public function __construct(private readonly string $table) {}

public function getTable(): string
{
Expand Down Expand Up @@ -77,4 +75,20 @@ public function setFilter(bool $filter): DcaFieldConfiguration
$this->filter = $filter;
return $this;
}

public function setEvalValue(string $key, mixed $value): DcaFieldConfiguration
{
$this->eval[$key] = $value;
return $this;
}

public function getEvalValue(string $key): mixed
{
return $this->eval[$key] ?? null;
}

public function getEval(): array
{
return $this->eval;
}
}
9 changes: 8 additions & 1 deletion src/EventListener/DcaField/AbstractDcaFieldListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected function getModelInstance(string $table, int $id): ?Model
return $framework->getAdapter($modelClass)->findByPk($id);
}

protected function applyDefaultFieldAdjustments(array &$field, DcaFieldConfiguration $configuration)
protected function applyDefaultFieldAdjustments(array &$field, DcaFieldConfiguration $configuration): void
{
if ($configuration->isFilter()) {
$field['filter'] = true;
Expand All @@ -42,8 +42,15 @@ protected function applyDefaultFieldAdjustments(array &$field, DcaFieldConfigura
if ($configuration->getFlag() !== null) {
$field['flag'] = $configuration->getFlag();
}

if (!empty($configuration->getEval())) {
$field['eval'] = \array_merge($field['eval'] ?? [], $configuration->getEval());
}
}

/**
* @codeCoverageIgnore Can't be tested due to nature of the method
*/
public static function getSubscribedServices(): array
{
return [
Expand Down
40 changes: 39 additions & 1 deletion tests/Dca/DcaFieldOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,47 @@

class DcaFieldOptionsTest extends TestCase
{
public function testGetTable()
public function testAllOptions()
{
$dcaFieldOptions = new DcaFieldConfiguration('test_table');
$this->assertEquals('test_table', $dcaFieldOptions->getTable());

$dcaFieldOptions
->setFlag(69)
->setExclude(true)
->setSearch(true)
->setFilter(true)
->setSorting(true)
->setEvalValue('test_eval_1', 'test_value_1')
->setEvalValue('test_eval_2', 'test_value_2')
;
$this->assertEquals(69, $dcaFieldOptions->getFlag());
$this->assertTrue($dcaFieldOptions->isExclude());
$this->assertTrue($dcaFieldOptions->isSearch());
$this->assertTrue($dcaFieldOptions->isFilter());
$this->assertTrue($dcaFieldOptions->isSorting());
$this->assertEquals(
[
'test_eval_1' => 'test_value_1',
'test_eval_2' => 'test_value_2',
],
$dcaFieldOptions->getEval()
);

$dcaFieldOptions->setEvalValue('test_eval_2', 'test_value_new2');
$this->assertEquals(
[
'test_eval_1' => 'test_value_1',
'test_eval_2' => 'test_value_new2',
],
$dcaFieldOptions->getEval()
);

$dcaFieldOptions->setEvalValue('test_eval_1', 'test_value_single1');
$dcaFieldOptions->setEvalValue('test_eval_2', 'test_value_single2');
$dcaFieldOptions->setEvalValue('test_eval_3', 'test_value_single3');
$this->assertEquals('test_value_single1', $dcaFieldOptions->getEvalValue('test_eval_1'));
$this->assertEquals('test_value_single2', $dcaFieldOptions->getEvalValue('test_eval_2'));
$this->assertEquals('test_value_single3', $dcaFieldOptions->getEvalValue('test_eval_3'));
}
}
25 changes: 19 additions & 6 deletions tests/EventListener/DcaField/DateAddedFieldListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,22 @@ public function testOnLoadDataContainer()
public function dateAddedConfig():array
{
return [
[null, false, false, false, false],
[1, true, false, false, false],
[2, false, true, false, false],
[null, false, false, true, false],
[null, false, false, false, true],
[null, false, false, false, false, null],
[1, true, false, false, false, []],
[2, false, true, false, false, ['testProp' => 'testValue']],
[null, false, false, true, false, ['a' => 'x', 'b' => 'y', 'c' => 99]],
[null, false, false, false, true, ['noSubmissionField' => true]],
];
}

/**
* @dataProvider dateAddedConfig
* @runInSeparateProcess
*/
public function testConfig(?int $flag, bool $sorting, bool $exclude, bool $filter, bool $search)
public function testConfig(?int $flag, bool $sorting, bool $exclude, bool $filter, bool $search, ?array $eval)
{
$eval ??= [];

$container = $this->createMock(ContainerInterface::class);

$listener = new DateAddedFieldListener($container);
Expand All @@ -79,6 +81,9 @@ public function testConfig(?int $flag, bool $sorting, bool $exclude, bool $filte
$config->setExclude($exclude);
$config->setFilter($filter);
$config->setSearch($search);
foreach ($eval as $key => $value) {
$config->setEvalValue($key, $value);
}

$listener->onLoadDataContainer($table);

Expand All @@ -104,6 +109,14 @@ public function testConfig(?int $flag, bool $sorting, bool $exclude, bool $filte
$this->assertArrayHasKey('search', $field);
$this->assertTrue($field['search']);
}
if (!empty($eval)) {
$this->assertArrayHasKey('eval', $field);
$this->assertIsArray($field['eval']);
foreach ($eval as $key => $value) {
$this->assertArrayHasKey($key, $field['eval']);
$this->assertEquals($value, $field['eval'][$key]);
}
}
}

public function testOnLoadCallback()
Expand Down

0 comments on commit 49d1940

Please sign in to comment.