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