Skip to content

Commit

Permalink
more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Jan 6, 2025
1 parent 3aa6c2a commit 9e9d00b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/EntityFinder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,13 @@ private function listConfig(int $id): ?Element

return new Element(
$model->id,
$model->getTable(),
'tl_list_config',
'List config ' . $model->title. ' (ID: ' . $model->id . ')',
(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];
$modules = $this->framework->getAdapter(ModuleModel::class)->findBy(["$t.type=?", "$t.listConfig=?"], ['listConfig', $model->id]);
foreach ($modules as $module) {
yield ['table' => ModuleModel::getTable(), 'id' => $module->id];
}
})()
);
Expand All @@ -168,7 +169,7 @@ private function listConfigElement(int $id): ?Element

return new Element(
$model->id,
$model->getTable(),
'tl_list_config_element',
'List config element ' . $model->title. ' (ID: ' . $model->id . ')',
(function() use ($model): \Iterator {
yield ['table' => 'tl_list_config', 'id' => $model->pid];
Expand Down
58 changes: 58 additions & 0 deletions tests/EntityFinder/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Contao\Controller;
use Contao\DC_Table;
use Contao\Model\Collection;
use Contao\ModuleModel;
use HeimrichHannot\UtilsBundle\EntityFinder\Element;
use HeimrichHannot\UtilsBundle\EntityFinder\EntityFinderHelper;
use HeimrichHannot\UtilsBundle\EntityFinder\Finder;
Expand Down Expand Up @@ -146,4 +148,60 @@ public function testFindFormField()
$this->assertInstanceOf(\Generator::class, $entity->getParents());
$this->assertSame([['table' => 'tl_form', 'id' => 5]], iterator_to_array($entity->getParents()));
}

public function testFind()
{
$this->typicalFind('tl_list_config', 3, 'List config', function (Element $entity) {
$this->assertSame([['table' => 'tl_module', 'id' => 4]], iterator_to_array($entity->getParents()));
});
$this->typicalFind('tl_list_config_element', 4, 'List config element', function (Element $entity) {
$this->assertSame([['table' => 'tl_list_config', 'id' => 4]], iterator_to_array($entity->getParents()));
});

}

private function typicalFind(string $table, int $id, string $name = 'Default', ?callable $callback = null)
{
$moduleModel = $this->mockAdapter(['findBy']);
$moduleModel->method('findBy')->willReturnCallback(function ($columns, $values) {

return new Collection([
$this->mockModelObject(ModuleModel::class, [
'id' => 4
])
], 'tl_module');
});
$framework = $this->mockContaoFramework([
ModuleModel::class => $moduleModel,
]);

$finder = $this->getTestInstance([
'framework' => $framework
]);
$entity = $finder->find($table, $id);
$this->assertNull($entity);

$element = new \stdClass();
$element->id = $id;
$element->pid = 4;
$element->title = $name;
$element->name = $name;

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

$entity = $finder->find($table, $id);
$this->assertInstanceOf(Element::class, $entity);
$this->assertSame($table, $entity->getTable());
$this->assertSame($id, $entity->getId());
$this->assertInstanceOf(\Generator::class, $entity->getParents());

if ($callback) {
$callback($entity);
}
}
}

0 comments on commit 9e9d00b

Please sign in to comment.