[TASK] Add functional tests for generic persistence
[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 * 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 CountTest extends \TYPO3\CMS\Core\Tests\FunctionalTestCase {
31
32 /**
33 * @var int number of all records
34 */
35 protected $numberOfRecordsInFixture = 11;
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
39 */
40 protected $persistentManager;
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 * @var \TYPO3\CMS\Extbase\Persistence\Repository
59 */
60 protected $blogRepository;
61
62 /**
63 * Sets up this test suite.
64 */
65 public function setUp() {
66 parent::setUp();
67
68 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/pages.xml');
69 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/blogs.xml');
70 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/posts.xml');
71 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/tags.xml');
72 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/post-tag-mm.xml');
73
74 $this->objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
75 $this->persistentManager = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager');
76 $this->postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
77 }
78
79 /**
80 * @test
81 */
82 public function simpleCountTest() {
83 $query = $this->postRepository->createQuery();
84 $this->assertSame($this->numberOfRecordsInFixture, $query->count());
85 }
86
87 /**
88 * @test
89 */
90 public function offsetCountTest() {
91 $query = $this->postRepository->createQuery();
92
93 $query->setOffset(6);
94
95 $this->assertSame(($this->numberOfRecordsInFixture - 6), $query->count());
96 }
97
98 /**
99 * @test
100 */
101 public function exceedingOffsetCountTest() {
102 $query = $this->postRepository->createQuery();
103
104 $query->setOffset(($this->numberOfRecordsInFixture + 5));
105
106 $this->assertSame(0, $query->count());
107 }
108
109 /**
110 * @test
111 */
112 public function limitCountTest() {
113 $query = $this->postRepository->createQuery();
114
115 $query->setLimit(4);
116
117 $this->assertSame(4, $query->count());
118 }
119
120 /**
121 * @test
122 */
123 public function limitAndOffsetCountTest() {
124 $query = $this->postRepository->createQuery();
125
126 $query
127 ->setOffset(($this->numberOfRecordsInFixture - 3))
128 ->setLimit(4);
129
130 $this->assertSame(3, $query->count());
131 }
132
133 /**
134 * @test
135 */
136 public function inConstraintCountTest() {
137 $query = $this->postRepository->createQuery();
138
139 $query->matching(
140 $query->in('uid', array(1,2,3))
141 );
142
143 $this->assertSame(3, $query->count());
144 }
145
146 /**
147 * Test if count works with subproperties in subselects.
148 *
149 * @test
150 */
151 public function subpropertyJoinCountTest() {
152 $query = $this->postRepository->createQuery();
153
154 $query->matching(
155 $query->equals('blog.title', 'Blog1')
156 );
157
158 $this->assertSame(10, $query->count());
159 }
160
161 /**
162 * Test if count works with subproperties in multiple left join.
163 *
164 * @test
165 */
166 public function subpropertyInMultipleLeftJoinCountTest() {
167 $query = $this->postRepository->createQuery();
168
169 $query->matching(
170 $query->logicalOr(
171 $query->equals('tags.uid', 1),
172 $query->equals('tags.uid', 2)
173 )
174 );
175
176 $this->assertSame(10, $query->count());
177 }
178 }