TYPO3 CMS  TYPO3_8-7
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 
20 class QueryResultTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
21 {
25  protected $queryResult;
26 
30  protected $mockQuery;
31 
36 
40  protected $mockDataMapper;
41 
45  protected $sampleResult = [];
46 
50  protected function setUp()
51  {
52  $this->mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
53  $this->mockPersistenceManager->expects($this->any())->method('getObjectDataByQuery')->will($this->returnValue(['one', 'two']));
54  $this->mockPersistenceManager->expects($this->any())->method('getObjectCountByQuery')->will($this->returnValue(2));
55  $this->mockDataMapper = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
56  $this->mockQuery = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
57  $this->queryResult = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\QueryResult::class, ['dummy'], [$this->mockQuery]);
58  $this->queryResult->_set('persistenceManager', $this->mockPersistenceManager);
59  $this->queryResult->_set('dataMapper', $this->mockDataMapper);
60  $this->sampleResult = [['foo' => 'Foo1', 'bar' => 'Bar1'], ['foo' => 'Foo2', 'bar' => 'Bar2']];
61  $this->mockDataMapper->expects($this->any())->method('map')->will($this->returnValue($this->sampleResult));
62  }
63 
67  public function getQueryReturnsQueryObject()
68  {
69  $this->assertInstanceOf(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class, $this->queryResult->getQuery());
70  }
71 
75  public function getQueryReturnsAClone()
76  {
77  $this->assertNotSame($this->mockQuery, $this->queryResult->getQuery());
78  }
79 
83  public function offsetExistsWorksAsExpected()
84  {
85  $this->assertTrue($this->queryResult->offsetExists(0));
86  $this->assertFalse($this->queryResult->offsetExists(2));
87  $this->assertFalse($this->queryResult->offsetExists('foo'));
88  }
89 
93  public function offsetGetWorksAsExpected()
94  {
95  $this->assertEquals(['foo' => 'Foo1', 'bar' => 'Bar1'], $this->queryResult->offsetGet(0));
96  $this->assertNull($this->queryResult->offsetGet(2));
97  $this->assertNull($this->queryResult->offsetGet('foo'));
98  }
99 
103  public function offsetSetWorksAsExpected()
104  {
105  $this->queryResult->offsetSet(0, ['foo' => 'FooOverridden', 'bar' => 'BarOverridden']);
106  $this->assertEquals(['foo' => 'FooOverridden', 'bar' => 'BarOverridden'], $this->queryResult->offsetGet(0));
107  }
108 
112  public function offsetUnsetWorksAsExpected()
113  {
114  $this->queryResult->offsetUnset(0);
115  $this->assertFalse($this->queryResult->offsetExists(0));
116  }
117 
121  public function countDoesNotInitializeProxy()
122  {
123  $queryResult = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\QueryResult::class, ['initialize'], [$this->mockQuery]);
124  $queryResult->_set('persistenceManager', $this->mockPersistenceManager);
125  $queryResult->expects($this->never())->method('initialize');
126  $queryResult->count();
127  }
128 
133  {
134  $queryResult = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\QueryResult::class, ['initialize'], [$this->mockQuery]);
135  $queryResult->_set('persistenceManager', $this->mockPersistenceManager);
136  $this->assertEquals(2, $queryResult->count());
137  }
138 
143  {
144  $this->mockPersistenceManager->expects($this->never())->method('getObjectCountByQuery');
145  $this->queryResult->toArray();
146  $this->assertEquals(2, $this->queryResult->count());
147  }
148 
153  {
154  $this->mockPersistenceManager->expects($this->once())->method('getObjectCountByQuery')->will($this->returnValue(2));
155  $this->queryResult->count();
156  $this->assertEquals(2, $this->queryResult->count());
157  }
158 
163  {
164  $array1 = ['foo' => 'Foo1', 'bar' => 'Bar1'];
165  $array2 = ['foo' => 'Foo2', 'bar' => 'Bar2'];
166  $this->assertEquals($array1, $this->queryResult->current());
167  $this->assertTrue($this->queryResult->valid());
168  $this->queryResult->next();
169  $this->assertEquals($array2, $this->queryResult->current());
170  $this->assertTrue($this->queryResult->valid());
171  $this->assertEquals(1, $this->queryResult->key());
172  $this->queryResult->next();
173  $this->assertFalse($this->queryResult->current());
174  $this->assertFalse($this->queryResult->valid());
175  $this->assertNull($this->queryResult->key());
176  $this->queryResult->rewind();
177  $this->assertEquals(0, $this->queryResult->key());
178  $this->assertEquals($array1, $this->queryResult->current());
179  }
180 
184  public function initializeExecutesQueryWithArrayFetchMode()
185  {
187  $queryResult = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\QueryResult::class, ['dummy'], [$this->mockQuery]);
188  $queryResult->_set('persistenceManager', $this->mockPersistenceManager);
189  $queryResult->_set('dataMapper', $this->mockDataMapper);
190  $this->mockPersistenceManager->expects($this->once())->method('getObjectDataByQuery')->with($this->mockQuery)->will($this->returnValue(['FAKERESULT']));
191  $queryResult->_call('initialize');
192  }
193 
198  {
199  $queryResult = new \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult($this->mockQuery);
200  $actualResult = current($queryResult);
201  $this->assertNull($actualResult);
202  }
203 }