Skip to content

Commit

Permalink
working on testing
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Jan 3, 2025
1 parent b785058 commit 940ce4f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/EntityFinder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Contao\ContentModel;
use Contao\Controller;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\DC_Table;
use Contao\FormFieldModel;
use Contao\FormModel;
Expand All @@ -19,14 +20,18 @@ class Finder
{
private EntityFinderHelper $helper;
private EventDispatcherInterface $eventDispatcher;
private ContaoFramework $framework;

public function __construct(
EntityFinderHelper $helper,
EventDispatcherInterface $eventDispatcher
EventDispatcherInterface $eventDispatcher,
ContaoFramework $framework

)
{
$this->helper = $helper;
$this->eventDispatcher = $eventDispatcher;
$this->framework = $framework;
}

public function find(string $table, int $id): ?Element
Expand Down Expand Up @@ -64,9 +69,10 @@ private function fallback(string $table, $idOrAlias): ?Element
'parents' => null,
];

Controller::loadDataContainer($table);
$this->framework->getAdapter(Controller::class)->loadDataContainer($table);

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

Expand Down
48 changes: 48 additions & 0 deletions tests/EntityFinder/FinderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace HeimrichHannot\UtilsBundle\Tests\EntityFinder;

use Contao\Controller;
use HeimrichHannot\UtilsBundle\EntityFinder\Element;
use HeimrichHannot\UtilsBundle\EntityFinder\EntityFinderHelper;
use HeimrichHannot\UtilsBundle\EntityFinder\Finder;
use HeimrichHannot\UtilsBundle\Tests\AbstractUtilsTestCase;
use PHPUnit\Framework\MockObject\MockBuilder;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class FinderTest extends AbstractUtilsTestCase
{

public function getTestInstance(array $parameters = [], ?MockBuilder $mockBuilder = null)
{
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->method('dispatch')->willReturnArgument(0);

$contaoFramework = $this->mockContaoFramework([
Controller::class => $this->mockAdapter(['loadDataContainer']),
]);

return new Finder(
$parameters['helper'] ?? $this->createMock(EntityFinderHelper::class),
$parameters['eventDispatcher'] ?? $eventDispatcher,
$parameters['framework'] ?? $contaoFramework
);
}

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

$element = new \stdClass();
$element->id = 1;

$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);

}
}

0 comments on commit 940ce4f

Please sign in to comment.