‪TYPO3CMS  9.5
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 
17 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
18 
22 class ‪QueryResultTest extends UnitTestCase
23 {
27  protected ‪$queryResult;
28 
32  protected ‪$mockQuery;
33 
38 
42  protected ‪$mockDataMapper;
43 
47  protected ‪$sampleResult = [];
48 
52  protected function ‪setUp()
53  {
54  $this->mockPersistenceManager = $this->createMock(\‪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->createMock(\‪TYPO3\CMS\‪Extbase\Persistence\Generic\Mapper\DataMapper::class);
58  $this->mockQuery = $this->createMock(\‪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  $this->mockPersistenceManager->expects(self::once())->method('getObjectCountByQuery')->willReturn(2);
167  $firstCount = $this->queryResult->count();
168  $this->queryResult->offsetSet(3, new \stdClass());
169  $this->queryResult->offsetSet(4, new \stdClass());
170  $secondCount = $this->queryResult->count();
171  $this->queryResult->offsetUnset(1);
172  $thirdCount = $this->queryResult->count();
173 
174  self::assertSame(2, $firstCount);
175  self::assertSame(4, $secondCount);
176  self::assertSame(3, $thirdCount);
177  }
178 
183  {
184  $array1 = ['foo' => 'Foo1', 'bar' => 'Bar1'];
185  $array2 = ['foo' => 'Foo2', 'bar' => 'Bar2'];
186  $this->assertEquals($array1, $this->queryResult->current());
187  $this->assertTrue($this->queryResult->valid());
188  $this->queryResult->next();
189  $this->assertEquals($array2, $this->queryResult->current());
190  $this->assertTrue($this->queryResult->valid());
191  $this->assertEquals(1, $this->queryResult->key());
192  $this->queryResult->next();
193  $this->assertFalse($this->queryResult->current());
194  $this->assertFalse($this->queryResult->valid());
195  $this->assertNull($this->queryResult->key());
196  $this->queryResult->rewind();
197  $this->assertEquals(0, $this->queryResult->key());
198  $this->assertEquals($array1, $this->queryResult->current());
199  }
200 
205  {
207  ‪$queryResult = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Persistence\Generic\QueryResult::class, ['dummy'], [$this->mockQuery]);
208  ‪$queryResult->_set('persistenceManager', $this->mockPersistenceManager);
209  ‪$queryResult->_set('dataMapper', $this->mockDataMapper);
210  $this->mockPersistenceManager->expects($this->once())->method('getObjectDataByQuery')->with($this->mockQuery)->will($this->returnValue(['FAKERESULT']));
211  ‪$queryResult->_call('initialize');
212  }
213 
218  {
219  ‪$queryResult = new \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult($this->mockQuery);
220  $actualResult = current(‪$queryResult);
221  $this->assertNull($actualResult);
222  }
223 }
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\getQueryReturnsQueryObject
‪getQueryReturnsQueryObject()
Definition: QueryResultTest.php:64
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\$mockQuery
‪TYPO3 CMS Extbase Persistence QueryInterface $mockQuery
Definition: QueryResultTest.php:30
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\initializeExecutesQueryWithArrayFetchMode
‪initializeExecutesQueryWithArrayFetchMode()
Definition: QueryResultTest.php:199
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\$queryResult
‪TYPO3 CMS Extbase Persistence Generic QueryResult $queryResult
Definition: QueryResultTest.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\usingCurrentOnTheQueryResultReturnsNull
‪usingCurrentOnTheQueryResultReturnsNull()
Definition: QueryResultTest.php:212
‪TYPO3
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\countCallsGetObjectCountByQueryOnPersistenceManager
‪countCallsGetObjectCountByQueryOnPersistenceManager()
Definition: QueryResultTest.php:129
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\offsetGetWorksAsExpected
‪offsetGetWorksAsExpected()
Definition: QueryResultTest.php:90
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\offsetUnsetWorksAsExpected
‪offsetUnsetWorksAsExpected()
Definition: QueryResultTest.php:109
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\getQueryReturnsAClone
‪getQueryReturnsAClone()
Definition: QueryResultTest.php:72
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\offsetExistsWorksAsExpected
‪offsetExistsWorksAsExpected()
Definition: QueryResultTest.php:80
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\countCallsGetObjectCountByQueryIfOffsetChanges
‪countCallsGetObjectCountByQueryIfOffsetChanges()
Definition: QueryResultTest.php:159
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\offsetSetWorksAsExpected
‪offsetSetWorksAsExpected()
Definition: QueryResultTest.php:100
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic
Definition: BackendTest.php:3
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest
Definition: QueryResultTest.php:23
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\countDoesNotInitializeProxy
‪countDoesNotInitializeProxy()
Definition: QueryResultTest.php:118
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\setUp
‪setUp()
Definition: QueryResultTest.php:47
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\countCountsQueryResultDirectlyIfAlreadyInitialized
‪countCountsQueryResultDirectlyIfAlreadyInitialized()
Definition: QueryResultTest.php:139
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\iteratorMethodsAreCorrectlyImplemented
‪iteratorMethodsAreCorrectlyImplemented()
Definition: QueryResultTest.php:177
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\countOnlyCallsGetObjectCountByQueryOnPersistenceManagerOnce
‪countOnlyCallsGetObjectCountByQueryOnPersistenceManagerOnce()
Definition: QueryResultTest.php:149
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\$mockDataMapper
‪TYPO3 CMS Extbase Persistence Generic Mapper DataMapper $mockDataMapper
Definition: QueryResultTest.php:38
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\$sampleResult
‪array $sampleResult
Definition: QueryResultTest.php:42
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\$mockPersistenceManager
‪TYPO3 CMS Extbase Persistence PersistenceManagerInterface $mockPersistenceManager
Definition: QueryResultTest.php:34