[TASK] Use name-resolution instead of strings where possible: 3
[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::class);
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::class);
57 $this->mockQuery = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
58 $this->queryResult = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\QueryResult::class, 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::class, $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::class, 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::class, 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 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 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
158 $queryResult = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\QueryResult::class, array('dummy'), array($this->mockQuery));
159 $queryResult->_set('persistenceManager', $this->mockPersistenceManager);
160 $queryResult->_set('dataMapper', $this->mockDataMapper);
161 $this->mockPersistenceManager->expects($this->once())->method('getObjectDataByQuery')->with($this->mockQuery)->will($this->returnValue(array('FAKERESULT')));
162 $queryResult->_call('initialize');
163 }
164
165 /**
166 * @test
167 */
168 public function usingCurrentOnTheQueryResultReturnsNull() {
169 $queryResult = new \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult($this->mockQuery);
170 $actualResult = current($queryResult);
171 $this->assertNull($actualResult);
172 }
173 }