‪TYPO3CMS  10.4
QueryResultTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
27 class ‪QueryResultTest extends UnitTestCase
28 {
32  protected ‪$queryResult;
33 
37  protected ‪$mockQuery;
38 
43 
47  protected ‪$mockDataMapper;
48 
52  protected ‪$sampleResult = [];
53 
57  protected function ‪setUp(): void
58  {
59  parent::setUp();
60  $this->mockPersistenceManager = $this->createMock(PersistenceManagerInterface::class);
61  $this->mockPersistenceManager->expects(self::any())->method('getObjectDataByQuery')->willReturn(['one', 'two']);
62  $this->mockPersistenceManager->expects(self::any())->method('getObjectCountByQuery')->willReturn(2);
63  $this->mockDataMapper = $this->createMock(DataMapper::class);
64  $this->mockQuery = $this->createMock(QueryInterface::class);
65  $this->queryResult = $this->getAccessibleMock(QueryResult::class, ['dummy'], [$this->mockQuery]);
66  $this->queryResult->_set('persistenceManager', $this->mockPersistenceManager);
67  $this->queryResult->_set('dataMapper', $this->mockDataMapper);
68  $this->sampleResult = [['foo' => 'Foo1', 'bar' => 'Bar1'], ['foo' => 'Foo2', 'bar' => 'Bar2']];
69  $this->mockDataMapper->expects(self::any())->method('map')->willReturn($this->sampleResult);
70  }
71 
75  public function ‪getQueryReturnsQueryObject()
76  {
77  self::assertInstanceOf(QueryInterface::class, $this->queryResult->getQuery());
78  }
79 
83  public function ‪getQueryReturnsAClone()
84  {
85  self::assertNotSame($this->mockQuery, $this->queryResult->getQuery());
86  }
87 
91  public function ‪offsetExistsWorksAsExpected()
92  {
93  self::assertTrue($this->queryResult->offsetExists(0));
94  self::assertFalse($this->queryResult->offsetExists(2));
95  self::assertFalse($this->queryResult->offsetExists('foo'));
96  }
97 
101  public function ‪offsetGetWorksAsExpected()
102  {
103  self::assertEquals(['foo' => 'Foo1', 'bar' => 'Bar1'], $this->queryResult->offsetGet(0));
104  self::assertNull($this->queryResult->offsetGet(2));
105  self::assertNull($this->queryResult->offsetGet('foo'));
106  }
107 
111  public function ‪offsetSetWorksAsExpected()
112  {
113  $this->queryResult->offsetSet(0, ['foo' => 'FooOverridden', 'bar' => 'BarOverridden']);
114  self::assertEquals(['foo' => 'FooOverridden', 'bar' => 'BarOverridden'], $this->queryResult->offsetGet(0));
115  }
116 
120  public function ‪offsetUnsetWorksAsExpected()
121  {
122  $this->queryResult->offsetUnset(0);
123  self::assertFalse($this->queryResult->offsetExists(0));
124  }
125 
129  public function ‪countDoesNotInitializeProxy()
130  {
131  ‪$queryResult = $this->getAccessibleMock(QueryResult::class, ['initialize'], [$this->mockQuery]);
132  ‪$queryResult->_set('persistenceManager', $this->mockPersistenceManager);
133  ‪$queryResult->expects(self::never())->method('initialize');
135  }
136 
141  {
142  ‪$queryResult = $this->getAccessibleMock(QueryResult::class, ['initialize'], [$this->mockQuery]);
143  ‪$queryResult->_set('persistenceManager', $this->mockPersistenceManager);
144  self::assertEquals(2, ‪$queryResult->‪count());
145  }
146 
151  {
152  $this->mockPersistenceManager->expects(self::never())->method('getObjectCountByQuery');
153  $this->queryResult->toArray();
154  self::assertEquals(2, $this->queryResult->count());
155  }
156 
161  {
162  $this->mockPersistenceManager->expects(self::once())->method('getObjectCountByQuery')->willReturn(2);
163  $this->queryResult->count();
164  self::assertEquals(2, $this->queryResult->count());
165  }
166 
171  {
172  $this->mockPersistenceManager->expects(self::once())->method('getObjectCountByQuery')->willReturn(2);
173  $firstCount = $this->queryResult->count();
174  $this->queryResult->offsetSet(3, new \stdClass());
175  $this->queryResult->offsetSet(4, new \stdClass());
176  $secondCount = $this->queryResult->count();
177  $this->queryResult->offsetUnset(1);
178  $thirdCount = $this->queryResult->count();
179 
180  self::assertSame(2, $firstCount);
181  self::assertSame(4, $secondCount);
182  self::assertSame(3, $thirdCount);
183  }
184 
189  {
190  $array1 = ['foo' => 'Foo1', 'bar' => 'Bar1'];
191  $array2 = ['foo' => 'Foo2', 'bar' => 'Bar2'];
192  self::assertEquals($array1, $this->queryResult->current());
193  self::assertTrue($this->queryResult->valid());
194  $this->queryResult->next();
195  self::assertEquals($array2, $this->queryResult->current());
196  self::assertTrue($this->queryResult->valid());
197  self::assertEquals(1, $this->queryResult->key());
198  $this->queryResult->next();
199  self::assertFalse($this->queryResult->current());
200  self::assertFalse($this->queryResult->valid());
201  self::assertNull($this->queryResult->key());
202  $this->queryResult->rewind();
203  self::assertEquals(0, $this->queryResult->key());
204  self::assertEquals($array1, $this->queryResult->current());
205  }
206 
211  {
213  ‪$queryResult = $this->getAccessibleMock(QueryResult::class, ['dummy'], [$this->mockQuery]);
214  ‪$queryResult->_set('persistenceManager', $this->mockPersistenceManager);
215  ‪$queryResult->_set('dataMapper', $this->mockDataMapper);
216  $this->mockPersistenceManager->expects(self::once())->method('getObjectDataByQuery')->with($this->mockQuery)->willReturn(['FAKERESULT']);
217  ‪$queryResult->_call('initialize');
218  }
219 
224  {
225  ‪$queryResult = new ‪QueryResult($this->mockQuery);
226  $actualResult = current(‪$queryResult);
227  self::assertNull($actualResult);
228  }
229 }
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\getQueryReturnsQueryObject
‪getQueryReturnsQueryObject()
Definition: QueryResultTest.php:70
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\$mockQuery
‪TYPO3 CMS Extbase Persistence QueryInterface $mockQuery
Definition: QueryResultTest.php:35
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:22
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\initializeExecutesQueryWithArrayFetchMode
‪initializeExecutesQueryWithArrayFetchMode()
Definition: QueryResultTest.php:205
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\$queryResult
‪TYPO3 CMS Extbase Persistence Generic QueryResult $queryResult
Definition: QueryResultTest.php:31
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\usingCurrentOnTheQueryResultReturnsNull
‪usingCurrentOnTheQueryResultReturnsNull()
Definition: QueryResultTest.php:218
‪TYPO3\CMS\Extbase\Persistence\QueryInterface
Definition: QueryInterface.php:29
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
Definition: DataMapper.php:52
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\countCallsGetObjectCountByQueryOnPersistenceManager
‪countCallsGetObjectCountByQueryOnPersistenceManager()
Definition: QueryResultTest.php:135
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\offsetGetWorksAsExpected
‪offsetGetWorksAsExpected()
Definition: QueryResultTest.php:96
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult\count
‪int count()
Definition: QueryResult.php:137
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\offsetUnsetWorksAsExpected
‪offsetUnsetWorksAsExpected()
Definition: QueryResultTest.php:115
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\getQueryReturnsAClone
‪getQueryReturnsAClone()
Definition: QueryResultTest.php:78
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\offsetExistsWorksAsExpected
‪offsetExistsWorksAsExpected()
Definition: QueryResultTest.php:86
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\countCallsGetObjectCountByQueryIfOffsetChanges
‪countCallsGetObjectCountByQueryIfOffsetChanges()
Definition: QueryResultTest.php:165
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\offsetSetWorksAsExpected
‪offsetSetWorksAsExpected()
Definition: QueryResultTest.php:106
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic
Definition: BackendTest.php:18
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult
Definition: QueryResult.php:30
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest
Definition: QueryResultTest.php:28
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\countDoesNotInitializeProxy
‪countDoesNotInitializeProxy()
Definition: QueryResultTest.php:124
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\setUp
‪setUp()
Definition: QueryResultTest.php:52
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\countCountsQueryResultDirectlyIfAlreadyInitialized
‪countCountsQueryResultDirectlyIfAlreadyInitialized()
Definition: QueryResultTest.php:145
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\iteratorMethodsAreCorrectlyImplemented
‪iteratorMethodsAreCorrectlyImplemented()
Definition: QueryResultTest.php:183
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\countOnlyCallsGetObjectCountByQueryOnPersistenceManagerOnce
‪countOnlyCallsGetObjectCountByQueryOnPersistenceManagerOnce()
Definition: QueryResultTest.php:155
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\$mockDataMapper
‪TYPO3 CMS Extbase Persistence Generic Mapper DataMapper $mockDataMapper
Definition: QueryResultTest.php:43
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\$sampleResult
‪array $sampleResult
Definition: QueryResultTest.php:47
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryResultTest\$mockPersistenceManager
‪TYPO3 CMS Extbase Persistence PersistenceManagerInterface $mockPersistenceManager
Definition: QueryResultTest.php:39