[BUGFIX] 1st level cache for ``QueryResult::count()``
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / Generic / QueryResultTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic;
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 /**
18 * Test case
19 */
20 class QueryResultTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
21
22 /**
23 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult
24 */
25 protected $queryResult;
26
27 /**
28 * @var \TYPO3\CMS\Extbase\Persistence\QueryInterface
29 */
30 protected $mockQuery;
31
32 /**
33 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
34 */
35 protected $mockPersistenceManager;
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
39 */
40 protected $mockDataMapper;
41
42 /**
43 * @var array
44 */
45 protected $sampleResult = array();
46
47 /**
48 * Sets up this test case
49 *
50 * @return void
51 */
52 public function setUp() {
53 $this->mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
54 $this->mockPersistenceManager->expects($this->any())->method('getObjectDataByQuery')->will($this->returnValue(array('one', 'two')));
55 $this->mockPersistenceManager->expects($this->any())->method('getObjectCountByQuery')->will($this->returnValue(2));
56 $this->mockDataMapper = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper');
57 $this->mockQuery = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\QueryInterface');
58 $this->queryResult = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Generic\QueryResult', array('dummy'), array($this->mockQuery));
59 $this->queryResult->_set('persistenceManager', $this->mockPersistenceManager);
60 $this->queryResult->_set('dataMapper', $this->mockDataMapper);
61 $this->sampleResult = array(array('foo' => 'Foo1', 'bar' => 'Bar1'), array('foo' => 'Foo2', 'bar' => 'Bar2'));
62 $this->mockDataMapper->expects($this->any())->method('map')->will($this->returnValue($this->sampleResult));
63 }
64
65 /**
66 * @test
67 */
68 public function getQueryReturnsQueryObject() {
69 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Persistence\\QueryInterface', $this->queryResult->getQuery());
70 }
71
72 /**
73 * @test
74 */
75 public function getQueryReturnsAClone() {
76 $this->assertNotSame($this->mockQuery, $this->queryResult->getQuery());
77 }
78
79 /**
80 * @test
81 */
82 public function offsetExistsWorksAsExpected() {
83 $this->assertTrue($this->queryResult->offsetExists(0));
84 $this->assertFalse($this->queryResult->offsetExists(2));
85 $this->assertFalse($this->queryResult->offsetExists('foo'));
86 }
87
88 /**
89 * @test
90 */
91 public function offsetGetWorksAsExpected() {
92 $this->assertEquals(array('foo' => 'Foo1', 'bar' => 'Bar1'), $this->queryResult->offsetGet(0));
93 $this->assertNull($this->queryResult->offsetGet(2));
94 $this->assertNull($this->queryResult->offsetGet('foo'));
95 }
96
97 /**
98 * @test
99 */
100 public function offsetSetWorksAsExpected() {
101 $this->queryResult->offsetSet(0, array('foo' => 'FooOverridden', 'bar' => 'BarOverridden'));
102 $this->assertEquals(array('foo' => 'FooOverridden', 'bar' => 'BarOverridden'), $this->queryResult->offsetGet(0));
103 }
104
105 /**
106 * @test
107 */
108 public function offsetUnsetWorksAsExpected() {
109 $this->queryResult->offsetUnset(0);
110 $this->assertFalse($this->queryResult->offsetExists(0));
111 }
112
113 /**
114 * @test
115 */
116 public function countDoesNotInitializeProxy() {
117 $queryResult = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QueryResult', array('initialize'), array($this->mockQuery));
118 $queryResult->_set('persistenceManager', $this->mockPersistenceManager);
119 $queryResult->expects($this->never())->method('initialize');
120 $queryResult->count();
121 }
122
123 /**
124 * @test
125 */
126 public function countCallsGetObjectCountByQueryOnPersistenceManager() {
127 $queryResult = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QueryResult', array('initialize'), array($this->mockQuery));
128 $queryResult->_set('persistenceManager', $this->mockPersistenceManager);
129 $this->assertEquals(2, $queryResult->count());
130 }
131
132 /**
133 * @test
134 */
135 public function countCountsQueryResultDirectlyIfAlreadyInitialized() {
136 $this->mockPersistenceManager->expects($this->never())->method('getObjectCountByQuery');
137 $this->queryResult->toArray();
138 $this->assertEquals(2, $this->queryResult->count());
139 }
140
141 /**
142 * @test
143 */
144 public function countOnlyCallsGetObjectCountByQueryOnPersistenceManagerOnce() {
145 $this->mockPersistenceManager->expects($this->once())->method('getObjectCountByQuery')->will($this->returnValue(2));
146 $this->queryResult->count();
147 $this->assertEquals(2, $this->queryResult->count());
148 }
149
150 /**
151 * @test
152 */
153 public function iteratorMethodsAreCorrectlyImplemented() {
154 $array1 = array('foo' => 'Foo1', 'bar' => 'Bar1');
155 $array2 = array('foo' => 'Foo2', 'bar' => 'Bar2');
156 $this->assertEquals($array1, $this->queryResult->current());
157 $this->assertTrue($this->queryResult->valid());
158 $this->queryResult->next();
159 $this->assertEquals($array2, $this->queryResult->current());
160 $this->assertTrue($this->queryResult->valid());
161 $this->assertEquals(1, $this->queryResult->key());
162 $this->queryResult->next();
163 $this->assertFalse($this->queryResult->current());
164 $this->assertFalse($this->queryResult->valid());
165 $this->assertNull($this->queryResult->key());
166 $this->queryResult->rewind();
167 $this->assertEquals(0, $this->queryResult->key());
168 $this->assertEquals($array1, $this->queryResult->current());
169 }
170
171 /**
172 * @test
173 */
174 public function initializeExecutesQueryWithArrayFetchMode() {
175 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
176 $queryResult = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QueryResult', array('dummy'), array($this->mockQuery));
177 $queryResult->_set('persistenceManager', $this->mockPersistenceManager);
178 $queryResult->_set('dataMapper', $this->mockDataMapper);
179 $this->mockPersistenceManager->expects($this->once())->method('getObjectDataByQuery')->with($this->mockQuery)->will($this->returnValue(array('FAKERESULT')));
180 $queryResult->_call('initialize');
181 }
182
183 /**
184 * @test
185 */
186 public function usingCurrentOnTheQueryResultReturnsNull() {
187 $queryResult = new \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult($this->mockQuery);
188 $actualResult = current($queryResult);
189 $this->assertNull($actualResult);
190 }
191 }