d512ebdef876baa886f422fe1be43d30eb852d25
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / QueryResultTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Bastian Waidelich <bastian@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 class Tx_Extbase_Tests_Unit_Persistence_QueryResultTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
26
27 /**
28 * @var Tx_Extbase_Persistence_QueryResult
29 */
30 protected $queryResult;
31
32 /**
33 * @var Tx_Extbase_Persistence_QueryInterface
34 */
35 protected $query;
36
37 /**
38 * @var Tx_Extbase_Persistence_ManagerInterface
39 */
40 protected $persistenceManager;
41
42 /**
43 * @var Tx_Extbase_Persistence_DataMapper
44 */
45 protected $dataMapper;
46
47 /**
48 * Sets up this test case
49 *
50 * @return void
51 */
52 public function setUp() {
53 $this->persistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
54 $this->persistenceManager->expects($this->any())->method('getObjectDataByQuery')->will($this->returnValue(array('one', 'two')));
55 $this->persistenceManager->expects($this->any())->method('getObjectCountByQuery')->will($this->returnValue(2));
56 $this->dataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper');
57 $this->query = $this->getMock('Tx_Extbase_Persistence_QueryInterface');
58 $this->queryResult = new Tx_Extbase_Persistence_QueryResult($this->query);
59 $this->queryResult->injectPersistenceManager($this->persistenceManager);
60 $this->queryResult->injectDataMapper($this->dataMapper);
61 $this->sampleResult = array(array('foo' => 'Foo1', 'bar' => 'Bar1'), array('foo' => 'Foo2', 'bar' => 'Bar2'));
62 $this->dataMapper->expects($this->any())->method('map')->will($this->returnValue($this->sampleResult));
63 }
64
65 /**
66 * @test
67 */
68 public function getQueryReturnsQueryObject() {
69 $this->assertType('Tx_Extbase_Persistence_QueryInterface', $this->queryResult->getQuery());
70 }
71
72 /**
73 * @test
74 */
75 public function getQueryReturnsAClone() {
76 $this->assertNotSame($this->query, $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->getMock('Tx_Extbase_Persistence_QueryResult', array('initialize'), array($this->query));
118 $queryResult->injectPersistenceManager($this->persistenceManager);
119 $queryResult->expects($this->never())->method('initialize');
120 $queryResult->count();
121 }
122
123 /**
124 * @test
125 */
126 public function countCallsGetObjectCountByQueryOnPersistenceManager() {
127 $queryResult = $this->getMock('Tx_Extbase_Persistence_QueryResult', array('initialize'), array($this->query));
128 $queryResult->injectPersistenceManager($this->persistenceManager);
129 $this->assertEquals(2, $queryResult->count());
130 }
131
132 /**
133 * @test
134 */
135 public function iteratorMethodsAreCorrectlyImplemented() {
136 $array1 = array('foo' => 'Foo1', 'bar' => 'Bar1');
137 $array2 = array('foo' => 'Foo2', 'bar' => 'Bar2');
138 $this->assertEquals($array1, $this->queryResult->current());
139 $this->assertTrue($this->queryResult->valid());
140 $this->queryResult->next();
141 $this->assertEquals($array2, $this->queryResult->current());
142 $this->assertTrue($this->queryResult->valid());
143 $this->assertEquals(1, $this->queryResult->key());
144 $this->queryResult->next();
145 $this->assertFalse($this->queryResult->current());
146 $this->assertFalse($this->queryResult->valid());
147 $this->assertNull($this->queryResult->key());
148 $this->queryResult->rewind();
149 $this->assertEquals(0, $this->queryResult->key());
150 $this->assertEquals($array1, $this->queryResult->current());
151 }
152
153 /**
154 * @test
155 */
156 public function initializeExecutesQueryWithArrayFetchMode() {
157 $queryResult = $this->getAccessibleMock('Tx_Extbase_Persistence_QueryResult', array('dummy'), array($this->query));
158 $queryResult->injectPersistenceManager($this->persistenceManager);
159 $queryResult->injectDataMapper($this->dataMapper);
160 $this->persistenceManager->expects($this->once())->method('getObjectDataByQuery')->with($this->query)->will($this->returnValue(array('FAKERESULT')));
161 $queryResult->_call('initialize');
162 }
163
164 }
165 ?>