‪TYPO3CMS  10.4
DatabaseTreeDataProviderTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Doctrine\DBAL\Driver\Statement;
19 use Prophecy\Argument;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
33 class ‪DatabaseTreeDataProviderTest extends UnitTestCase
34 {
38  protected ‪$subject;
39 
43  protected ‪$treeData;
44 
48  protected function ‪setUp(): void
49  {
50  parent::setUp();
51  $this->treeData = new ‪TreeNode();
52  }
53 
60  protected function ‪setupDatabaseMock(int $instanceCount = 1)
61  {
62  // Prophecies and revelations for a lot of the database stack classes
63  $connectionPoolProphecy = $this->prophesize(ConnectionPool::class);
64  $queryRestrictionProphecy = $this->prophesize(QueryRestrictionContainerInterface::class);
65  $queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
66  $expressionBuilderProphecy = $this->prophesize(ExpressionBuilder::class);
67  $statementProphecy = $this->prophesize(Statement::class);
68 
69  $expressionBuilderProphecy->eq(Argument::cetera())->willReturn('1=1');
70 
71  // Simulate method call flow on database objects and verify correct query is built
72  $connectionPoolProphecy->getQueryBuilderForTable(Argument::cetera())
73  ->shouldBeCalled()
74  ->willReturn($queryBuilderProphecy->reveal());
75  $queryRestrictionProphecy->removeAll()
76  ->shouldBeCalled()
77  ->willReturn($queryRestrictionProphecy->reveal());
78  $queryBuilderProphecy->getRestrictions()
79  ->shouldBeCalled()
80  ->willReturn($queryRestrictionProphecy->reveal());
81  $queryBuilderProphecy->expr()
82  ->shouldBeCalled()
83  ->willReturn($expressionBuilderProphecy->reveal());
84  $queryBuilderProphecy->execute()
85  ->shouldBeCalled()
86  ->willReturn($statementProphecy->reveal());
87 
88  $queryBuilderProphecy->select(Argument::cetera())
89  ->shouldBeCalled()
90  ->willReturn($queryBuilderProphecy->reveal());
91  $queryBuilderProphecy->from(Argument::cetera())
92  ->shouldBeCalled()
93  ->willReturn($queryBuilderProphecy->reveal());
94  $queryBuilderProphecy->createNamedParameter(Argument::cetera())
95  ->shouldBeCalled()
96  ->willReturnArgument(0);
97  $queryBuilderProphecy->where(Argument::cetera())
98  ->shouldBeCalled()
99  ->willReturn($queryBuilderProphecy->reveal());
100  $queryBuilderProphecy->setMaxResults(Argument::cetera())
101  ->shouldBeCalled()
102  ->willReturn($queryBuilderProphecy->reveal());
103 
104  $statementProphecy->fetch()
105  ->shouldBeCalled()
106  ->willReturn(['uid' => 0, 'parent' => '']);
107 
108  // Register connection pool revelation in framework, this is the entry point used by system under test
109  for ($i = 1; $i <= $instanceCount; $i++) {
110  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphecy->reveal());
111  }
112 
113  return $queryBuilderProphecy;
114  }
115 
119  protected function ‪initializeSubjectMock(array $mockMethods)
120  {
121  $this->subject = $this->getAccessibleMock(DatabaseTreeDataProvider::class, $mockMethods, [], '', false);
122  $this->subject->expects(self::any())->method('getRootUid')->willReturn(0);
123  $this->subject->_set('treeData', $this->treeData);
124  }
125 
130  {
131  $this->‪initializeSubjectMock(['getRelatedRecords', 'getRootUid', 'getChildrenOf']);
132  $this->subject->_set('levelMaximum', 0);
133  $this->subject->expects(self::never())->method('getChildrenOf');
134  $this->subject->_call('loadTreeData');
135  }
136 
141  {
142  $this->‪initializeSubjectMock(['getRelatedRecords', 'getRootUid', 'getChildrenOf']);
143  $this->subject->_set('levelMaximum', 1);
144  $this->subject->expects(self::once())->method('getChildrenOf')->with($this->treeData, 1);
145  $this->subject->_call('loadTreeData');
146  }
147 
152  {
153  $this->‪setupDatabaseMock();
154 
155  $expectedTreeNode = new ‪TreeNode();
156  $expectedTreeNode->setId(1);
157  $expectedStorage = new ‪TreeNodeCollection();
158  $expectedStorage->append($expectedTreeNode);
159 
160  $this->‪initializeSubjectMock(['getRelatedRecords', 'getRootUid']);
161  $this->subject->_set('levelMaximum', 1);
162  $this->subject->expects(self::once())->method('getRelatedRecords')->willReturn([1]);
163  $storage = $this->subject->_call('getChildrenOf', $this->treeData, 1);
164 
165  self::assertEquals($expectedStorage, $storage);
166  }
167 
172  {
173  $this->‪setupDatabaseMock(2);
174 
175  $expectedStorage = new ‪TreeNodeCollection();
176 
177  $expectedFirstLevelTreeNode = new ‪TreeNode();
178  $expectedFirstLevelTreeNode->setId(1);
179 
180  $expectedSecondLevelTreeNode = new ‪TreeNode();
181  $expectedSecondLevelTreeNode->setId(2);
182 
183  $expectedStorageOfSecondLevelChildren = new ‪TreeNodeCollection();
184  $expectedStorageOfSecondLevelChildren->append($expectedSecondLevelTreeNode);
185 
186  $expectedFirstLevelTreeNode->setChildNodes($expectedStorageOfSecondLevelChildren);
187  $expectedStorage->append($expectedFirstLevelTreeNode);
188 
189  $this->‪initializeSubjectMock(['getRelatedRecords', 'getRootUid']);
190  $this->subject->_set('levelMaximum', 2);
191  $this->subject->expects(self::exactly(2))->method('getRelatedRecords')->willReturnOnConsecutiveCalls([1], [2]);
192  $storage = $this->subject->_call('getChildrenOf', $this->treeData, 1);
193 
194  self::assertEquals($expectedStorage, $storage);
195  }
196 }
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\DatabaseTreeDataProviderTest\setupDatabaseMock
‪Prophecy Prophecy ObjectProphecy TYPO3 CMS Core Database Query QueryBuilder setupDatabaseMock(int $instanceCount=1)
Definition: DatabaseTreeDataProviderTest.php:58
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration
Definition: DatabaseTreeDataProviderTest.php:16
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:35
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\DatabaseTreeDataProviderTest\getChildrenOfLevelMaximumSetToTwoWorks
‪getChildrenOfLevelMaximumSetToTwoWorks()
Definition: DatabaseTreeDataProviderTest.php:169
‪TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider
Definition: DatabaseTreeDataProvider.php:36
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\DatabaseTreeDataProviderTest\getChildrenOfLevelMaximumSetToOneWorks
‪getChildrenOfLevelMaximumSetToOneWorks()
Definition: DatabaseTreeDataProviderTest.php:149
‪TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionContainerInterface
Definition: QueryRestrictionContainerInterface.php:25
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\DatabaseTreeDataProviderTest\$subject
‪PHPUnit Framework MockObject MockObject DatabaseTreeDataProvider TYPO3 TestingFramework Core AccessibleObjectInterface $subject
Definition: DatabaseTreeDataProviderTest.php:37
‪TYPO3\CMS\Backend\Tree\TreeNodeCollection
Definition: TreeNodeCollection.php:25
‪TYPO3\CMS\Core\Database\Query\QueryBuilder
Definition: QueryBuilder.php:52
‪TYPO3\CMS\Backend\Tree\TreeNode
Definition: TreeNode.php:25
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\DatabaseTreeDataProviderTest\initializeSubjectMock
‪initializeSubjectMock(array $mockMethods)
Definition: DatabaseTreeDataProviderTest.php:117
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\DatabaseTreeDataProviderTest\loadTreeDataLevelMaximumSetToOneWorks
‪loadTreeDataLevelMaximumSetToOneWorks()
Definition: DatabaseTreeDataProviderTest.php:138
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\DatabaseTreeDataProviderTest
Definition: DatabaseTreeDataProviderTest.php:34
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\DatabaseTreeDataProviderTest\setUp
‪setUp()
Definition: DatabaseTreeDataProviderTest.php:46
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\DatabaseTreeDataProviderTest\loadTreeDataLevelMaximumSetToZeroWorks
‪loadTreeDataLevelMaximumSetToZeroWorks()
Definition: DatabaseTreeDataProviderTest.php:127
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration\DatabaseTreeDataProviderTest\$treeData
‪TreeNode $treeData
Definition: DatabaseTreeDataProviderTest.php:41