‪TYPO3CMS  ‪main
TreeDataProviderFactoryTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
22 use Psr\EventDispatcher\EventDispatcherInterface;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
31 final class ‪TreeDataProviderFactoryTest extends UnitTestCase
32 {
33  protected function ‪setUp(): void
34  {
35  parent::setUp();
36  ‪$GLOBALS['TCA'] = [];
37  ‪$GLOBALS['TCA']['foo'] = [];
38  ‪$GLOBALS['TCA']['foo']['ctrl'] = [];
39  ‪$GLOBALS['TCA']['foo']['ctrl']['label'] = 'labelFoo';
40  ‪$GLOBALS['TCA']['foo']['columns'] = [];
41  }
42 
43  public static function ‪invalidConfigurationDataProvider(): array
44  {
45  return [
46  'Empty Configuration' => [[], 1288215890],
47  'Unknown Type' => [
48  [
49  'type' => 'folder',
50  'treeConfig' => [],
51  ],
52  1288215892,
53  ],
54  'No foreign table' => [
55  [
56  'type' => 'group',
57  'treeConfig' => [],
58  ],
59  1288215888,
60  ],
61  'No tree configuration' => [
62  [
63  'type' => 'group',
64  'foreign_table' => 'foo',
65  ],
66  1288215890,
67  ],
68  'Tree configuration not array' => [
69  [
70  'type' => 'group',
71  'foreign_table' => 'foo',
72  'treeConfig' => 'bar',
73  ],
74  1288215890,
75  ],
76  'Tree configuration missing children and parent field' => [
77  [
78  'type' => 'group',
79  'foreign_table' => 'foo',
80  'treeConfig' => [],
81  ],
82  1288215889,
83  ],
84  ];
85  }
86 
87  #[DataProvider('invalidConfigurationDataProvider')]
88  #[Test]
89  public function ‪factoryThrowsExceptionIfInvalidConfigurationIsGiven(array $tcaConfiguration, int $expectedExceptionCode): void
90  {
91  if (isset($tcaConfiguration['type']) && $tcaConfiguration['type'] !== 'folder' && is_array($tcaConfiguration['treeConfig'] ?? null)) {
92  $treeDataProvider = $this->createMock(DatabaseTreeDataProvider::class);
93  GeneralUtility::addInstance(DatabaseTreeDataProvider::class, $treeDataProvider);
94  }
95 
96  $this->expectException(\InvalidArgumentException::class);
97  $this->expectExceptionCode($expectedExceptionCode);
98 
99  ‪TreeDataProviderFactory::getDataProvider($tcaConfiguration, 'foo', 'bar', ['uid' => 1]);
100  }
101 
102  #[Test]
104  {
105  $dataProviderMockClassName = TreeDataProviderFixture::class;
106  GeneralUtility::addInstance(EventDispatcherInterface::class, new ‪NoopEventDispatcher());
107 
108  $tcaConfiguration = [
109  'treeConfig' => ['dataProvider' => $dataProviderMockClassName],
110  'type' => 'folder',
111  ];
112  $dataProvider = ‪TreeDataProviderFactory::getDataProvider($tcaConfiguration, 'foo', 'bar', ['uid' => 1]);
113 
114  self::assertInstanceOf($dataProviderMockClassName, $dataProvider);
115  }
116 
117  #[Test]
119  {
120  $dataProviderMockClassName = TreeDataProviderWithConfigurationFixture::class;
121  GeneralUtility::addInstance(EventDispatcherInterface::class, new ‪NoopEventDispatcher());
122 
123  $tcaConfiguration = [
124  'treeConfig' => [
125  'dataProvider' => $dataProviderMockClassName,
126  ],
127  ];
128  $this->expectException(\RuntimeException::class);
129  $this->expectExceptionCode(1438875249);
130  ‪TreeDataProviderFactory::getDataProvider($tcaConfiguration, 'foo', 'bar', ['uid' => 1]);
131  }
132 }
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration
Definition: DatabaseTreeDataProviderTest.php:18
‪TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider
Definition: DatabaseTreeDataProvider.php:39
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\TreeDataProviderFactoryTest\configuredDataProviderClassIsInstantiated
‪configuredDataProviderClassIsInstantiated()
Definition: TreeDataProviderFactoryTest.php:103
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\TreeDataProviderFactoryTest\setUp
‪setUp()
Definition: TreeDataProviderFactoryTest.php:33
‪TYPO3\CMS\Core\Tree\TableConfiguration\TreeDataProviderFactory\getDataProvider
‪static DatabaseTreeDataProvider getDataProvider(array $tcaConfiguration, $table, $field, $currentValue)
Definition: TreeDataProviderFactory.php:36
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\Fixtures\TreeDataProviderFixture
Definition: TreeDataProviderFixture.php:24
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\Fixtures\TreeDataProviderWithConfigurationFixture
Definition: TreeDataProviderWithConfigurationFixture.php:24
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\TreeDataProviderFactoryTest\factoryThrowsExceptionIfInvalidConfigurationIsGiven
‪factoryThrowsExceptionIfInvalidConfigurationIsGiven(array $tcaConfiguration, int $expectedExceptionCode)
Definition: TreeDataProviderFactoryTest.php:89
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\TreeDataProviderFactoryTest
Definition: TreeDataProviderFactoryTest.php:32
‪TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher
Definition: NoopEventDispatcher.php:29
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tree\TableConfiguration\TreeDataProviderFactory
Definition: TreeDataProviderFactory.php:26
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\TreeDataProviderFactoryTest\invalidConfigurationDataProvider
‪static invalidConfigurationDataProvider()
Definition: TreeDataProviderFactoryTest.php:43
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\TreeDataProviderFactoryTest\configuredDataProviderClassIsInstantiatedWithTcaConfigurationInConstructor
‪configuredDataProviderClassIsInstantiatedWithTcaConfigurationInConstructor()
Definition: TreeDataProviderFactoryTest.php:118