TYPO3 CMS  TYPO3_6-2
QueryResultTest.php
Go to the documentation of this file.
1 <?php
3 
21 
25  protected $queryResult;
26 
30  protected $mockQuery;
31 
36 
40  protected $mockDataMapper;
41 
45  protected $sampleResult = array();
46 
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 
68  public function getQueryReturnsQueryObject() {
69  $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Persistence\\QueryInterface', $this->queryResult->getQuery());
70  }
71 
75  public function getQueryReturnsAClone() {
76  $this->assertNotSame($this->mockQuery, $this->queryResult->getQuery());
77  }
78 
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 
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 
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 
108  public function offsetUnsetWorksAsExpected() {
109  $this->queryResult->offsetUnset(0);
110  $this->assertFalse($this->queryResult->offsetExists(0));
111  }
112 
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 
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 
136  $this->mockPersistenceManager->expects($this->never())->method('getObjectCountByQuery');
137  $this->queryResult->toArray();
138  $this->assertEquals(2, $this->queryResult->count());
139  }
140 
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 
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 
174  public function initializeExecutesQueryWithArrayFetchMode() {
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 
187  $queryResult = new \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult($this->mockQuery);
188  $actualResult = current($queryResult);
189  $this->assertNull($actualResult);
190  }
191 }
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)