[BUGFIX] Fix early return in addUnionStatement method
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Functional / Persistence / CountTest.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 CountTest extends \TYPO3\CMS\Components\TestingFramework\Core\FunctionalTestCase
20 {
21 /**
22 * @var int number of all records
23 */
24 protected $numberOfRecordsInFixture = 14;
25
26 /**
27 * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
28 */
29 protected $persistentManager;
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 * @var \TYPO3\CMS\Extbase\Persistence\Repository
48 */
49 protected $blogRepository;
50
51 /**
52 * @var \ExtbaseTeam\BlogExample\Domain\Repository\PostRepository
53 */
54 protected $postRepository;
55
56 /**
57 * Sets up this test suite.
58 */
59 protected function setUp()
60 {
61 parent::setUp();
62
63 $this->importDataSet(ORIGINAL_ROOT . 'components/testing_framework/core/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/post-post-mm.xml');
67 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/tags.xml');
68 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/tags-mm.xml');
69 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/post-tag-mm.xml');
70 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/persons.xml');
71
72 $this->objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
73 $this->persistentManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);
74 $this->postRepository = $this->objectManager->get(\ExtbaseTeam\BlogExample\Domain\Repository\PostRepository::class);
75 }
76
77 /**
78 * @test
79 */
80 public function simpleCountTest()
81 {
82 $query = $this->postRepository->createQuery();
83 $this->assertSame($this->numberOfRecordsInFixture, $query->count());
84 }
85
86 /**
87 * @test
88 */
89 public function offsetCountTest()
90 {
91 $query = $this->postRepository->createQuery();
92
93 $query->setLimit($this->numberOfRecordsInFixture+1);
94 $query->setOffset(6);
95
96 $this->assertSame(($this->numberOfRecordsInFixture - 6), $query->count());
97 }
98
99 /**
100 * @test
101 */
102 public function exceedingOffsetCountTest()
103 {
104 $query = $this->postRepository->createQuery();
105
106 $query->setLimit($this->numberOfRecordsInFixture+1);
107 $query->setOffset(($this->numberOfRecordsInFixture + 5));
108
109 $this->assertSame(0, $query->count());
110 }
111
112 /**
113 * @test
114 */
115 public function limitCountTest()
116 {
117 $query = $this->postRepository->createQuery();
118
119 $query->setLimit(4);
120
121 $this->assertSame(4, $query->count());
122 }
123
124 /**
125 * @test
126 */
127 public function limitAndOffsetCountTest()
128 {
129 $query = $this->postRepository->createQuery();
130
131 $query
132 ->setOffset(($this->numberOfRecordsInFixture - 3))
133 ->setLimit(4);
134
135 $this->assertSame(3, $query->count());
136 }
137
138 /**
139 * @test
140 */
141 public function inConstraintCountTest()
142 {
143 $query = $this->postRepository->createQuery();
144
145 $query->matching(
146 $query->in('uid', [1, 2, 3])
147 );
148
149 $this->assertSame(3, $query->count());
150 }
151
152 /**
153 * Test if count works with subproperties in subselects.
154 *
155 * @test
156 */
157 public function subpropertyJoinCountTest()
158 {
159 $query = $this->postRepository->createQuery();
160
161 $query->matching(
162 $query->equals('blog.title', 'Blog1')
163 );
164
165 $this->assertSame(10, $query->count());
166 }
167
168 /**
169 * Test if count works with subproperties in subselects that use the same table as the repository.
170 *
171 * @test
172 */
173 public function subpropertyJoinSameTableCountTest()
174 {
175 $query = $this->postRepository->createQuery();
176
177 $query->matching(
178 $query->equals('relatedPosts.title', 'Post2')
179 );
180
181 $this->assertSame(1, $query->count());
182 }
183
184 /**
185 * Test if count works with subproperties in multiple left join.
186 *
187 * @test
188 */
189 public function subpropertyInMultipleLeftJoinCountTest()
190 {
191 $query = $this->postRepository->createQuery();
192
193 $query->matching(
194 $query->logicalOr(
195 $query->equals('tags.uid', 1),
196 $query->equals('tags.uid', 2)
197 )
198 );
199
200 // QueryResult is lazy, so we have to run valid method to initialize
201 $result = $query->execute();
202 $result->valid();
203
204 $this->assertSame(10, $result->count());
205 }
206
207 /**
208 * @test
209 */
210 public function queryWithAndConditionsToTheSameTableReturnExpectedCount()
211 {
212 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\PersonRepository $personRepository */
213 $personRepository = $this->objectManager->get(\ExtbaseTeam\BlogExample\Domain\Repository\PersonRepository::class);
214 $query = $personRepository->createQuery();
215 $query->matching(
216 $query->logicalAnd(
217 $query->equals('tags.name', 'TagForAuthor1'),
218 $query->equals('tagsSpecial.name', 'SpecialTagForAuthor1')
219 )
220 );
221 $this->assertSame(1, $query->count());
222 }
223
224 /**
225 * @test
226 */
227 public function queryWithOrConditionsToTheSameTableReturnExpectedCount()
228 {
229 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\PersonRepository $personRepository */
230 $personRepository = $this->objectManager->get(\ExtbaseTeam\BlogExample\Domain\Repository\PersonRepository::class);
231 $query = $personRepository->createQuery();
232 $query->matching(
233 $query->logicalOr(
234 $query->equals('tags.name', 'TagForAuthor1'),
235 $query->equals('tagsSpecial.name', 'SpecialTagForAuthor1')
236 )
237 );
238 $this->assertSame(3, $query->count());
239 }
240 }