TYPO3 CMS  TYPO3_7-6
QueryResultTest.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 
21 {
25  protected $queryResult;
26 
30  protected $mockQuery;
31 
36 
40  protected $mockDataMapper;
41 
45  protected $sampleResult = [];
46 
52  protected function setUp()
53  {
54  $this->mockPersistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
55  $this->mockPersistenceManager->expects($this->any())->method('getObjectDataByQuery')->will($this->returnValue(['one', 'two']));
56  $this->mockPersistenceManager->expects($this->any())->method('getObjectCountByQuery')->will($this->returnValue(2));
57  $this->mockDataMapper = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
58  $this->mockQuery = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
59  $this->queryResult = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\QueryResult::class, ['dummy'], [$this->mockQuery]);
60  $this->queryResult->_set('persistenceManager', $this->mockPersistenceManager);
61  $this->queryResult->_set('dataMapper', $this->mockDataMapper);
62  $this->sampleResult = [['foo' => 'Foo1', 'bar' => 'Bar1'], ['foo' => 'Foo2', 'bar' => 'Bar2']];
63  $this->mockDataMapper->expects($this->any())->method('map')->will($this->returnValue($this->sampleResult));
64  }
65 
69  public function getQueryReturnsQueryObject()
70  {
71  $this->assertInstanceOf(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class, $this->queryResult->getQuery());
72  }
73 
77  public function getQueryReturnsAClone()
78  {
79  $this->assertNotSame($this->mockQuery, $this->queryResult->getQuery());
80  }
81 
85  public function offsetExistsWorksAsExpected()
86  {
87  $this->assertTrue($this->queryResult->offsetExists(0));
88  $this->assertFalse($this->queryResult->offsetExists(2));
89  $this->assertFalse($this->queryResult->offsetExists('foo'));
90  }
91 
95  public function offsetGetWorksAsExpected()
96  {
97  $this->assertEquals(['foo' => 'Foo1', 'bar' => 'Bar1'], $this->queryResult->offsetGet(0));
98  $this->assertNull($this->queryResult->offsetGet(2));
99  $this->assertNull($this->queryResult->offsetGet('foo'));
100  }
101 
105  public function offsetSetWorksAsExpected()
106  {
107  $this->queryResult->offsetSet(0, ['foo' => 'FooOverridden', 'bar' => 'BarOverridden']);
108  $this->assertEquals(['foo' => 'FooOverridden', 'bar' => 'BarOverridden'], $this->queryResult->offsetGet(0));
109  }
110 
114  public function offsetUnsetWorksAsExpected()
115  {
116  $this->queryResult->offsetUnset(0);
117  $this->assertFalse($this->queryResult->offsetExists(0));
118  }
119 
123  public function countDoesNotInitializeProxy()
124  {
125  $queryResult = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\QueryResult::class, ['initialize'], [$this->mockQuery]);
126  $queryResult->_set('persistenceManager', $this->mockPersistenceManager);
127  $queryResult->expects($this->never())->method('initialize');
128  $queryResult->count();
129  }
130 
135  {
136  $queryResult = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\QueryResult::class, ['initialize'], [$this->mockQuery]);
137  $queryResult->_set('persistenceManager', $this->mockPersistenceManager);
138  $this->assertEquals(2, $queryResult->count());
139  }
140 
145  {
146  $this->mockPersistenceManager->expects($this->never())->method('getObjectCountByQuery');
147  $this->queryResult->toArray();
148  $this->assertEquals(2, $this->queryResult->count());
149  }
150 
155  {
156  $this->mockPersistenceManager->expects($this->once())->method('getObjectCountByQuery')->will($this->returnValue(2));
157  $this->queryResult->count();
158  $this->assertEquals(2, $this->queryResult->count());
159  }
160 
165  {
166  $array1 = ['foo' => 'Foo1', 'bar' => 'Bar1'];
167  $array2 = ['foo' => 'Foo2', 'bar' => 'Bar2'];
168  $this->assertEquals($array1, $this->queryResult->current());
169  $this->assertTrue($this->queryResult->valid());
170  $this->queryResult->next();
171  $this->assertEquals($array2, $this->queryResult->current());
172  $this->assertTrue($this->queryResult->valid());
173  $this->assertEquals(1, $this->queryResult->key());
174  $this->queryResult->next();
175  $this->assertFalse($this->queryResult->current());
176  $this->assertFalse($this->queryResult->valid());
177  $this->assertNull($this->queryResult->key());
178  $this->queryResult->rewind();
179  $this->assertEquals(0, $this->queryResult->key());
180  $this->assertEquals($array1, $this->queryResult->current());
181  }
182 
186  public function initializeExecutesQueryWithArrayFetchMode()
187  {
189  $queryResult = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\QueryResult::class, ['dummy'], [$this->mockQuery]);
190  $queryResult->_set('persistenceManager', $this->mockPersistenceManager);
191  $queryResult->_set('dataMapper', $this->mockDataMapper);
192  $this->mockPersistenceManager->expects($this->once())->method('getObjectDataByQuery')->with($this->mockQuery)->will($this->returnValue(['FAKERESULT']));
193  $queryResult->_call('initialize');
194  }
195 
200  {
201  $queryResult = new \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult($this->mockQuery);
202  $actualResult = current($queryResult);
203  $this->assertNull($actualResult);
204  }
205 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)