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