[TASK] Remove deprecated methods/properties in Extbase
[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 * Copyright notice
6 *
7 * (c) 2010 Bastian Waidelich <bastian@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Test case
29 */
30 class QueryResultTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
31
32 /**
33 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult
34 */
35 protected $queryResult;
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Persistence\QueryInterface
39 */
40 protected $mockQuery;
41
42 /**
43 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
44 */
45 protected $mockPersistenceManager;
46
47 /**
48 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
49 */
50 protected $mockDataMapper;
51
52 /**
53 * @var array
54 */
55 protected $sampleResult = array();
56
57 /**
58 * Sets up this test case
59 *
60 * @return void
61 */
62 public function setUp() {
63 $this->mockPersistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
64 $this->mockPersistenceManager->expects($this->any())->method('getObjectDataByQuery')->will($this->returnValue(array('one', 'two')));
65 $this->mockPersistenceManager->expects($this->any())->method('getObjectCountByQuery')->will($this->returnValue(2));
66 $this->mockDataMapper = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper');
67 $this->mockQuery = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\QueryInterface');
68 $this->queryResult = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Generic\QueryResult', array('dummy'), array($this->mockQuery));
69 $this->queryResult->_set('persistenceManager', $this->mockPersistenceManager);
70 $this->queryResult->_set('dataMapper', $this->mockDataMapper);
71 $this->sampleResult = array(array('foo' => 'Foo1', 'bar' => 'Bar1'), array('foo' => 'Foo2', 'bar' => 'Bar2'));
72 $this->mockDataMapper->expects($this->any())->method('map')->will($this->returnValue($this->sampleResult));
73 }
74
75 /**
76 * @test
77 */
78 public function getQueryReturnsQueryObject() {
79 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Persistence\\QueryInterface', $this->queryResult->getQuery());
80 }
81
82 /**
83 * @test
84 */
85 public function getQueryReturnsAClone() {
86 $this->assertNotSame($this->mockQuery, $this->queryResult->getQuery());
87 }
88
89 /**
90 * @test
91 */
92 public function offsetExistsWorksAsExpected() {
93 $this->assertTrue($this->queryResult->offsetExists(0));
94 $this->assertFalse($this->queryResult->offsetExists(2));
95 $this->assertFalse($this->queryResult->offsetExists('foo'));
96 }
97
98 /**
99 * @test
100 */
101 public function offsetGetWorksAsExpected() {
102 $this->assertEquals(array('foo' => 'Foo1', 'bar' => 'Bar1'), $this->queryResult->offsetGet(0));
103 $this->assertNull($this->queryResult->offsetGet(2));
104 $this->assertNull($this->queryResult->offsetGet('foo'));
105 }
106
107 /**
108 * @test
109 */
110 public function offsetSetWorksAsExpected() {
111 $this->queryResult->offsetSet(0, array('foo' => 'FooOverridden', 'bar' => 'BarOverridden'));
112 $this->assertEquals(array('foo' => 'FooOverridden', 'bar' => 'BarOverridden'), $this->queryResult->offsetGet(0));
113 }
114
115 /**
116 * @test
117 */
118 public function offsetUnsetWorksAsExpected() {
119 $this->queryResult->offsetUnset(0);
120 $this->assertFalse($this->queryResult->offsetExists(0));
121 }
122
123 /**
124 * @test
125 */
126 public function countDoesNotInitializeProxy() {
127 $queryResult = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QueryResult', array('initialize'), array($this->mockQuery));
128 $queryResult->_set('persistenceManager', $this->mockPersistenceManager);
129 $queryResult->expects($this->never())->method('initialize');
130 $queryResult->count();
131 }
132
133 /**
134 * @test
135 */
136 public function countCallsGetObjectCountByQueryOnPersistenceManager() {
137 $queryResult = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QueryResult', array('initialize'), array($this->mockQuery));
138 $queryResult->_set('persistenceManager', $this->mockPersistenceManager);
139 $this->assertEquals(2, $queryResult->count());
140 }
141
142 /**
143 * @test
144 */
145 public function iteratorMethodsAreCorrectlyImplemented() {
146 $array1 = array('foo' => 'Foo1', 'bar' => 'Bar1');
147 $array2 = array('foo' => 'Foo2', 'bar' => 'Bar2');
148 $this->assertEquals($array1, $this->queryResult->current());
149 $this->assertTrue($this->queryResult->valid());
150 $this->queryResult->next();
151 $this->assertEquals($array2, $this->queryResult->current());
152 $this->assertTrue($this->queryResult->valid());
153 $this->assertEquals(1, $this->queryResult->key());
154 $this->queryResult->next();
155 $this->assertFalse($this->queryResult->current());
156 $this->assertFalse($this->queryResult->valid());
157 $this->assertNull($this->queryResult->key());
158 $this->queryResult->rewind();
159 $this->assertEquals(0, $this->queryResult->key());
160 $this->assertEquals($array1, $this->queryResult->current());
161 }
162
163 /**
164 * @test
165 */
166 public function initializeExecutesQueryWithArrayFetchMode() {
167 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
168 $queryResult = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QueryResult', array('dummy'), array($this->mockQuery));
169 $queryResult->_set('persistenceManager', $this->mockPersistenceManager);
170 $queryResult->_set('dataMapper', $this->mockDataMapper);
171 $this->mockPersistenceManager->expects($this->once())->method('getObjectDataByQuery')->with($this->mockQuery)->will($this->returnValue(array('FAKERESULT')));
172 $queryResult->_call('initialize');
173 }
174
175 /**
176 * @test
177 */
178 public function usingCurrentOnTheQueryResultReturnsNull() {
179 $queryResult = new \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult($this->mockQuery);
180 $actualResult = current($queryResult);
181 $this->assertNull($actualResult);
182 }
183 }