From 940ce4f208fc9a5b672b1a7fb351a1b43e2140f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6rner?= Date: Fri, 3 Jan 2025 12:46:31 +0100 Subject: [PATCH] working on testing --- src/EntityFinder/Finder.php | 12 ++++++-- tests/EntityFinder/FinderTest.php | 48 +++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 tests/EntityFinder/FinderTest.php diff --git a/src/EntityFinder/Finder.php b/src/EntityFinder/Finder.php index 742ec7ba..dfc3a507 100644 --- a/src/EntityFinder/Finder.php +++ b/src/EntityFinder/Finder.php @@ -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; @@ -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 @@ -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); } diff --git a/tests/EntityFinder/FinderTest.php b/tests/EntityFinder/FinderTest.php new file mode 100644 index 00000000..34547c10 --- /dev/null +++ b/tests/EntityFinder/FinderTest.php @@ -0,0 +1,48 @@ +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); + + } +} \ No newline at end of file