[BUGFIX] Fix operator "in" for cached queries
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Functional / Persistence / InTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Functional\Persistence;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2014 Felix Oertel <typo3@foertel.com>
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 use TYPO3\CMS\Core\Utility\GeneralUtility;
28 use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
29
30 class InTest extends \TYPO3\CMS\Core\Tests\FunctionalTestCase {
31
32 /**
33 * @var \ExtbaseTeam\BlogExample\Domain\Repository\BlogRepository
34 */
35 protected $blogRepository;
36
37 /**
38 * @var \ExtbaseTeam\BlogExample\Domain\Repository\PostRepository
39 */
40 protected $postRepository;
41
42 /**
43 * @var array
44 */
45 protected $testExtensionsToLoad = array('typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example');
46
47 /**
48 * @var array
49 */
50 protected $coreExtensionsToLoad = array('extbase', 'fluid');
51
52 /**
53 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface The object manager
54 */
55 protected $objectManager;
56
57 /**
58 * Sets up this test suite.
59 */
60 public function setUp() {
61 parent::setUp();
62
63 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/pages.xml');
64 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/blogs.xml');
65 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/posts.xml');
66 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/tags.xml');
67 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/post-tag-mm.xml');
68
69 $this->objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
70 $this->blogRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\BlogRepository');
71 $this->postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
72
73 }
74
75 /**
76 * @test
77 */
78 public function inConditionWorksWithArrayOfObjects() {
79 $blog1 = $this->blogRepository->findByUid(1);
80 $blog2 = $this->blogRepository->findByUid(2);
81
82 $inQuery = $this->postRepository->createQuery();
83
84 $inQuery->matching(
85 $inQuery->in('blog', array($blog1, $blog2))
86 );
87
88 $this->assertSame(11, $inQuery->count());
89 }
90
91 /**
92 * @test
93 */
94 public function inConditionWorksWithArrayOfObjectsOnSecondCall() {
95 $blog1 = $this->blogRepository->findByUid(1);
96 $blog2 = $this->blogRepository->findByUid(2);
97
98 $inQuery = $this->postRepository->createQuery();
99
100 $inQuery->matching(
101 $inQuery->in('blog', array($blog1, $blog2))
102 );
103
104 $this->assertSame(11, $inQuery->count());
105
106 $newInQuery = $this->postRepository->createQuery();
107
108 $newInQuery->matching(
109 $newInQuery->in('blog', array($blog1))
110 );
111
112 $this->assertSame(10, $newInQuery->count());
113 }
114
115 /**
116 * @test
117 */
118 public function inConditionWorksWithObjectStorage() {
119 $blog1 = $this->blogRepository->findByUid(1);
120 $blog2 = $this->blogRepository->findByUid(2);
121
122 $objectStorage = $this->objectManager->create('TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage');
123 $objectStorage->attach($blog1);
124 $objectStorage->attach($blog2);
125
126 $inQuery = $this->postRepository->createQuery();
127
128 $inQuery->matching(
129 $inQuery->in('blog', $objectStorage)
130 );
131
132 $this->assertSame(11, $inQuery->count());
133 }
134
135 /**
136 * @test
137 */
138 public function inConditionWorksWithObjectStorageOnSecondCall() {
139 $blog1 = $this->blogRepository->findByUid(1);
140 $blog2 = $this->blogRepository->findByUid(2);
141
142 $objectStorage = $this->objectManager->create('TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage');
143 $objectStorage->attach($blog1);
144 $objectStorage->attach($blog2);
145
146 $inQuery = $this->postRepository->createQuery();
147
148 $inQuery->matching(
149 $inQuery->in('blog', $objectStorage)
150 );
151
152 $this->assertSame(11, $inQuery->count());
153
154 $newObjectStorage = $this->objectManager->create('TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage');
155 $newObjectStorage->attach($blog1);
156
157 $newInQuery = $this->postRepository->createQuery();
158
159 $newInQuery->matching(
160 $newInQuery->in('blog', $newObjectStorage)
161 );
162
163 $this->assertSame(10, $newInQuery->count());
164 }
165
166 /**
167 * @test
168 */
169 public function inConditionWorksWithQueryResult() {
170 $queryResult = $this->blogRepository->findAll();
171
172 $inQuery = $this->postRepository->createQuery();
173
174 $inQuery->matching(
175 $inQuery->in('blog', $queryResult)
176 );
177
178 $this->assertSame(11, $inQuery->count());
179 }
180
181
182 /**
183 * @test
184 */
185 public function inConditionWorksWithQueryResultOnSecondCall() {
186 $queryResult = $this->blogRepository->findAll();
187
188 $inQuery = $this->postRepository->createQuery();
189
190 $inQuery->matching(
191 $inQuery->in('blog', $queryResult)
192 );
193
194 $this->assertSame(11, $inQuery->count());
195
196 $newInQuery = $this->postRepository->createQuery();
197
198 $newInQuery->matching(
199 $newInQuery->in('blog', $queryResult)
200 );
201
202 $this->assertSame(11, $newInQuery->count());
203 }
204
205 /**
206 * @test
207 */
208 public function inConditionWorksWithLazyObjectStorage() {
209 $blog = $this->blogRepository->findByUid(1);
210
211 $this->assertInstanceOf(
212 '\TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage',
213 $blog->getPosts()
214 );
215
216 $inQuery = $this->postRepository->createQuery();
217
218 $inQuery->matching(
219 $inQuery->in('uid', $blog->getPosts())
220 );
221
222 $this->assertSame(10, $inQuery->count());
223 }
224
225 /**
226 * @test
227 */
228 public function inConditionWorksWithLazyObjectStorageOnSecondCall() {
229 $blog = $this->blogRepository->findByUid(1);
230
231 $this->assertInstanceOf(
232 '\TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage',
233 $blog->getPosts()
234 );
235
236 $inQuery = $this->postRepository->createQuery();
237
238 $inQuery->matching(
239 $inQuery->in('uid', $blog->getPosts())
240 );
241
242 $this->assertSame(10, $inQuery->count());
243
244
245 $newInQuery = $this->postRepository->createQuery();
246
247 $newInQuery->matching(
248 $newInQuery->in('uid', $blog->getPosts())
249 );
250
251 $this->assertSame(10, $newInQuery->count());
252 }
253 }