TYPO3 CMS  TYPO3_8-7
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 
28 
32 class DatabaseTreeDataProviderTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
33 {
37  protected $subject;
38 
42  protected $treeData;
43 
47  protected $database;
48 
52  protected function setUp()
53  {
54  $this->treeData = new TreeNode();
55  }
56 
63  protected function setupDatabaseMock(int $instanceCount = 1)
64  {
65  // Prophecies and revelations for a lot of the database stack classes
66  $connectionPoolProphecy = $this->prophesize(ConnectionPool::class);
67  $queryRestrictionProphecy = $this->prophesize(QueryRestrictionContainerInterface::class);
68  $queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
69  $expressionBuilderProphecy = $this->prophesize(ExpressionBuilder::class);
70  $statementProphecy = $this->prophesize(Statement::class);
71 
72  $expressionBuilderProphecy->eq(Argument::cetera())->willReturn('1=1');
73 
74  // Simulate method call flow on database objects and verify correct query is built
75  $connectionPoolProphecy->getQueryBuilderForTable(Argument::cetera())
76  ->shouldBeCalled()
77  ->willReturn($queryBuilderProphecy->reveal());
78  $queryRestrictionProphecy->removeAll()
79  ->shouldBeCalled()
80  ->willReturn($queryRestrictionProphecy->reveal());
81  $queryBuilderProphecy->getRestrictions()
82  ->shouldBeCalled()
83  ->willReturn($queryRestrictionProphecy->reveal());
84  $queryBuilderProphecy->expr()
85  ->shouldBeCalled()
86  ->willReturn($expressionBuilderProphecy->reveal());
87  $queryBuilderProphecy->execute()
88  ->shouldBeCalled()
89  ->willReturn($statementProphecy->reveal());
90 
91  $queryBuilderProphecy->select(Argument::cetera())
92  ->shouldBeCalled()
93  ->willReturn($queryBuilderProphecy->reveal());
94  $queryBuilderProphecy->from(Argument::cetera())
95  ->shouldBeCalled()
96  ->willReturn($queryBuilderProphecy->reveal());
97  $queryBuilderProphecy->createNamedParameter(Argument::cetera())
98  ->shouldBeCalled()
99  ->willReturnArgument(0);
100  $queryBuilderProphecy->where(Argument::cetera())
101  ->shouldBeCalled()
102  ->willReturn($queryBuilderProphecy->reveal());
103  $queryBuilderProphecy->setMaxResults(Argument::cetera())
104  ->shouldBeCalled()
105  ->willReturn($queryBuilderProphecy->reveal());
106 
107  $statementProphecy->fetch()
108  ->shouldBeCalled()
109  ->willReturn(['uid' => 0, 'parent' => '']);
110 
111  // Register connection pool revelation in framework, this is the entry point used by system unter test
112  for ($i = 1; $i <= $instanceCount; $i++) {
113  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphecy->reveal());
114  }
115 
116  return $queryBuilderProphecy;
117  }
118 
122  protected function initializeSubjectMock(array $mockMethods)
123  {
124  $this->subject = $this->getAccessibleMock(DatabaseTreeDataProvider::class, $mockMethods, [], '', false);
125  $this->subject->expects($this->any())->method('getRootUid')->will($this->returnValue(0));
126  $this->subject->_set('treeData', $this->treeData);
127  }
128 
133  {
134  $this->initializeSubjectMock(['getRelatedRecords', 'getRootUid', 'getChildrenOf']);
135  $this->subject->_set('levelMaximum', 0);
136  $this->subject->expects($this->never())->method('getChildrenOf');
137  $this->subject->_call('loadTreeData');
138  }
139 
144  {
145  $this->initializeSubjectMock(['getRelatedRecords', 'getRootUid', 'getChildrenOf']);
146  $this->subject->_set('levelMaximum', 1);
147  $this->subject->expects($this->once())->method('getChildrenOf')->with($this->treeData, 1);
148  $this->subject->_call('loadTreeData');
149  }
150 
155  {
156  $this->setupDatabaseMock();
157 
158  $expectedTreeNode = new TreeNode();
159  $expectedTreeNode->setId(1);
160  $expectedStorage = new TreeNodeCollection();
161  $expectedStorage->append($expectedTreeNode);
162 
163  $this->initializeSubjectMock(['getRelatedRecords', 'getRootUid']);
164  $this->subject->_set('levelMaximum', 1);
165  $this->subject->expects($this->once())->method('getRelatedRecords')->will($this->returnValue([1]));
166  $storage = $this->subject->_call('getChildrenOf', $this->treeData, 1);
167 
168  $this->assertEquals($expectedStorage, $storage);
169  }
170 
175  {
176  $this->setupDatabaseMock(2);
177 
178  $expectedStorage = new TreeNodeCollection();
179 
180  $expectedFirstLevelTreeNode = new TreeNode();
181  $expectedFirstLevelTreeNode->setId(1);
182 
183  $expectedSecondLevelTreeNode = new TreeNode();
184  $expectedSecondLevelTreeNode->setId(2);
185 
186  $expectedStorageOfSecondLevelChildren = new TreeNodeCollection();
187  $expectedStorageOfSecondLevelChildren->append($expectedSecondLevelTreeNode);
188 
189  $expectedFirstLevelTreeNode->setChildNodes($expectedStorageOfSecondLevelChildren);
190  $expectedStorage->append($expectedFirstLevelTreeNode);
191 
192  $this->initializeSubjectMock(['getRelatedRecords', 'getRootUid']);
193  $this->subject->_set('levelMaximum', 2);
194  $this->subject->expects($this->at(0))->method('getRelatedRecords')->will($this->returnValue([1]));
195  $this->subject->expects($this->at(1))->method('getRelatedRecords')->will($this->returnValue([2]));
196  $storage = $this->subject->_call('getChildrenOf', $this->treeData, 1);
197 
198  $this->assertEquals($expectedStorage, $storage);
199  }
200 }
static addInstance($className, $instance)