Skip to content

Commit

Permalink
fix error, raise test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Jan 6, 2025
1 parent 940ce4f commit 3aa6c2a
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 39 deletions.
11 changes: 4 additions & 7 deletions src/EntityFinder/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ class Element
private int $id;
private string $table;
private ?string $description;
private ?\Closure $parents;
private ?iterable $parents;

/**
* @param int $id
* @param string $table
* @param string|null $description
* @param \Closure(string $table, int $id):\Iterator|null $parents A closure that returns an iterator of parent elements
* @param \Traversable|null $parents A closure that returns an iterator of parent elements
*/
public function __construct(int $id, string $table, string $description = null, \Closure $parents = null)
public function __construct(int $id, string $table, ?string $description = null, ?iterable $parents = null)
{
$this->id = $id;
$this->table = $table;
Expand All @@ -38,10 +38,7 @@ public function getDescription(): ?string
return $this->description;
}

/**
* @return \Closure(string $table, int $id):\Iterator|null
*/
public function getParents(): ?\Closure
public function getParents(): ?iterable
{
return $this->parents;
}
Expand Down
64 changes: 33 additions & 31 deletions src/EntityFinder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,39 +72,40 @@ private function fallback(string $table, $idOrAlias): ?Element
$this->framework->getAdapter(Controller::class)->loadDataContainer($table);

$dca = &$GLOBALS['TL_DCA'][$table];
if (empty($dca['config']['dataContainer']) ||!in_array($dca['config']['dataContainer'], ['Table', DC_Table::class])) {
return new Element(...$elementData);
}

if (isset($dca['config']['ptable'])) {
$elementData['parents'] = function() use ($model, $dca): \Iterator {
yield ['table' => $dca['config']['ptable'], 'id' => $model->pid];
};

return new Element(...$elementData);
if (!empty($model->pid)) {
if (isset($dca['config']['ptable'])) {
$elementData['parents'] = (function () use ($model, $dca): \Iterator {
yield ['table' => $dca['config']['ptable'], 'id' => $model->pid];
})();
} elseif (isset($dca['config']['dynamicPtable']) && isset($dca['fields']['pid']) && $model->ptable) {
$elementData['parents'] = (function () use ($model, $dca): \Iterator {
yield ['table' => $model->ptable, 'id' => $model->pid];
})();
}
}

if (isset($dca['config']['dynamicPtable']) && isset($dca['fields']['pid'])) {
$elementData['parents'] = function() use ($model, $dca): \Iterator {
yield ['table' => $model->ptable, 'id' => $model->pid];
};

return new Element(...$elementData);
}
return new Element(
$elementData['id'],
$elementData['table'],
$elementData['description'],
$elementData['parents']
);
}

private function form(int $id): ?Element
{
$model = FormModel::findByPk($id);
if ($model === null) {
$model = $this->helper->fetchModelOrData('tl_form', $id);

if (null === $model) {
return null;
}

return new Element(
$model->id,
$model->getTable(),
'tl_form',
'Form ' . $model->title. ' (ID: ' . $model->id . ')',
function() use ($model): \Iterator {
(function() use ($model): \Iterator {
foreach (ModuleModel::findByForm($model->id) as $model) {
yield ['table' => $model::getTable(), 'id' => $model->id];
}
Expand All @@ -117,23 +118,24 @@ function() use ($model): \Iterator {
foreach ($this->helper->findContentElementByInserttag('html', 'html', 'insert_form', $model->id) as $model) {
yield ['table' => $model::getTable(), 'id' => $model->id];
}
}
})()
);
}

private function formField(int $id): ?Element
{
$model = FormFieldModel::findByPk($id);
if ($model === null) {
$model = $this->helper->fetchModelOrData('tl_form_field', $id);

if (null === $model) {
return null;
}
return new Element(
$model->id,
$model->getTable(),
'Form field ' . $model->name. ' (ID: ' . $model->id . ')',
function() use ($model): \Iterator {
'tl_form_field',
'Form field ' . $model->name . ' (ID: ' . $model->id . ')',
(function () use ($model): \Generator {
yield ['table' => FormModel::getTable(), 'id' => $model->pid];
}
})()
);
}

Expand All @@ -148,12 +150,12 @@ private function listConfig(int $id): ?Element
$model->id,
$model->getTable(),
'List config ' . $model->title. ' (ID: ' . $model->id . ')',
function() use ($model): \Iterator {
(function() use ($model): \Iterator {
$t = ModuleModel::getTable();
foreach (ModuleModel::findBy(["$t.type=?", "$t.listConfig=?"], ['listConfig', $model->id]) as $module) {
yield ['table' => $module::getTable(), 'id' => $module->id];
}
}
})()
);
}

Expand All @@ -168,9 +170,9 @@ private function listConfigElement(int $id): ?Element
$model->id,
$model->getTable(),
'List config element ' . $model->title. ' (ID: ' . $model->id . ')',
function() use ($model): \Iterator {
(function() use ($model): \Iterator {
yield ['table' => 'tl_list_config', 'id' => $model->pid];
}
})()
);
}
}
103 changes: 102 additions & 1 deletion tests/EntityFinder/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace HeimrichHannot\UtilsBundle\Tests\EntityFinder;

use Contao\Controller;
use Contao\DC_Table;
use HeimrichHannot\UtilsBundle\EntityFinder\Element;
use HeimrichHannot\UtilsBundle\EntityFinder\EntityFinderHelper;
use HeimrichHannot\UtilsBundle\EntityFinder\Finder;
Expand All @@ -29,20 +30,120 @@ public function getTestInstance(array $parameters = [], ?MockBuilder $mockBuilde
);
}

public function testFind()
public function testFindEmpty()
{
$finder = $this->getTestInstance();
$this->assertNull($finder->find('find', 1));
$this->assertNull($finder->find('tl_custom', 1));
}

public function testFindFallback()
{
$element = new \stdClass();
$element->id = 1;
$element->pid = 3;

$helper = $this->createMock(EntityFinderHelper::class);
$helper->method('fetchModelOrData')->willReturn($element);
$finder = $this->getTestInstance(['helper' => $helper]);
$entity = $finder->find('tl_custom', 1);
$this->assertInstanceOf(Element::class, $entity);
$this->assertSame('tl_custom', $entity->getTable());
$this->assertSame(1, $entity->getId());
$this->assertNull($entity->getParents());
$this->assertNull($entity->getDescription());

$GLOBALS['TL_DCA']['tl_custom'] = [
'config' => [
'dataContainer' => DC_Table::class,
'ptable' => 'tl_parent',
],
];

$entity = $finder->find('tl_custom', 1);
$this->assertInstanceOf(Element::class, $entity);
$this->assertSame('tl_custom', $entity->getTable());
$this->assertSame(1, $entity->getId());
$this->assertInstanceOf(\Generator::class, $entity->getParents());
$this->assertNull($entity->getDescription());
$this->assertSame([['table' => 'tl_parent', 'id' => 3]], iterator_to_array($entity->getParents()));
}

public function testFindFallbackDynamicPtable()
{
$GLOBALS['TL_DCA']['tl_custom'] = [
'config' => [
'dataContainer' => DC_Table::class,
'dynamicPtable' => true,
],
'fields' => [
'pid' => [],
'ptable' => [],
]
];

$element = new \stdClass();
$element->id = 1;
$element->pid = 4;
$element->ptable = 'tl_other_parent';

$helper = $this->createMock(EntityFinderHelper::class);
$helper->method('fetchModelOrData')->willReturn($element);
$finder = $this->getTestInstance(['helper' => $helper]);

$entity = $finder->find('tl_custom', 1);
$this->assertInstanceOf(Element::class, $entity);
$this->assertSame('tl_custom', $entity->getTable());
$this->assertSame(1, $entity->getId());
$this->assertInstanceOf(\Generator::class, $entity->getParents());
$this->assertNull($entity->getDescription());
$this->assertSame([['table' => 'tl_other_parent', 'id' => 4]], iterator_to_array($entity->getParents()));
}

public function testFindForm()
{
$finder = $this->getTestInstance();
$entity = $finder->find('tl_form', 1);
$this->assertNull($entity);

$element = new \stdClass();
$element->id = 1;
$element->pid = 4;
$element->title = 'Test';

$helper = $this->createMock(EntityFinderHelper::class);
$helper->method('fetchModelOrData')->willReturn($element);
$finder = $this->getTestInstance(['helper' => $helper]);

$entity = $finder->find('tl_form', 1);
$this->assertInstanceOf(Element::class, $entity);
$this->assertSame('tl_form', $entity->getTable());
$this->assertSame(1, $entity->getId());
$this->assertSame('Form Test (ID: 1)', $entity->getDescription());
$this->assertInstanceOf(\Generator::class, $entity->getParents());
}

public function testFindFormField()
{
$finder = $this->getTestInstance();
$entity = $finder->find('tl_form_field', 1);
$this->assertNull($entity);

$element = new \stdClass();
$element->id = 2;
$element->pid = 5;
$element->name = 'Field';

$helper = $this->createMock(EntityFinderHelper::class);
$helper->method('fetchModelOrData')->willReturn($element);
$finder = $this->getTestInstance(['helper' => $helper]);

$entity = $finder->find('tl_form_field', 1);
$this->assertInstanceOf(Element::class, $entity);
$this->assertSame('tl_form_field', $entity->getTable());
$this->assertSame(2, $entity->getId());
$this->assertSame('Form field Field (ID: 2)', $entity->getDescription());
$this->assertInstanceOf(\Generator::class, $entity->getParents());
$this->assertSame([['table' => 'tl_form', 'id' => 5]], iterator_to_array($entity->getParents()));
}
}

0 comments on commit 3aa6c2a

Please sign in to comment.