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