‪TYPO3CMS  ‪main
RepositoryTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Group;
22 use PHPUnit\Framework\Attributes\Test;
24 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
27 
28 final class ‪RepositoryTest extends FunctionalTestCase
29 {
30  protected array ‪$testExtensionsToLoad = [
31  'typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example',
32  ];
33 
34  protected function ‪setUp(): void
35  {
36  parent::setUp();
37  $this->importCSVDataSet(__DIR__ . '/Fixtures/RepositoryTestImport.csv');
38  }
39 
40  public static function ‪findByRespectsSingleCriteriaDataProvider(): \Generator
41  {
42  yield 'findBy(["blog" => 1]) => 10' => [
43  ['blog' => 1],
44  10,
45  ];
46  yield 'findBy(["blog" => 1]) => 1' => [
47  ['blog' => 2],
48  1,
49  ];
50  yield 'findBy(["blog" => 1]) => 3' => [
51  ['blog' => 3],
52  3,
53  ];
54  }
55 
56  #[DataProvider('findByRespectsSingleCriteriaDataProvider')]
57  #[Test]
58  public function ‪findByRespectsSingleCriteria(array $criteria, int $expectedCount): void
59  {
60  self::assertCount($expectedCount, $this->get(PostRepository::class)->findBy($criteria));
61  }
62 
63  #[Test]
64  public function ‪findByRespectsMultipleCriteria(): void
65  {
66  self::assertCount(6, $this->get(PostRepository::class)->findBy(['blog' => 1, 'author' => 1]));
67  }
68 
69  #[Test]
70  public function ‪findByRespectsSingleOrderBy(): void
71  {
72  $posts = $this->get(PostRepository::class)->findBy(
73  ['blog' => 1, 'author' => 1],
75  )->toArray();
76  $titles = array_map(
77  static fn(‪Post $post): string => $post->‪getTitle(),
78  $posts
79  );
80  self::assertSame([
81  'Post9',
82  'Post8',
83  'Post7',
84  'Post5',
85  'Post4',
86  'Post10',
87  ], $titles);
88  }
89 
90  #[Test]
91  public function ‪findByRespectsMultipleOrderBy(): void
92  {
93  $posts = $this->get(PostRepository::class)->findBy(
94  [],
96  )->toArray();
97  self::assertSame(
98  [
99  [
100  'blog.uid' => 1,
101  'post.title' => 'Post9',
102  ],
103  [
104  'blog.uid' => 1,
105  'post.title' => 'Post8',
106  ],
107  [
108  'blog.uid' => 1,
109  'post.title' => 'Post7',
110  ],
111  [
112  'blog.uid' => 1,
113  'post.title' => 'Post6',
114  ],
115  [
116  'blog.uid' => 1,
117  'post.title' => 'Post5',
118  ],
119  [
120  'blog.uid' => 1,
121  'post.title' => 'Post4',
122  ],
123  [
124  'blog.uid' => 1,
125  'post.title' => 'Post3',
126  ],
127  [
128  'blog.uid' => 1,
129  'post.title' => 'Post2',
130  ],
131  [
132  'blog.uid' => 1,
133  'post.title' => 'Post10',
134  ],
135  [
136  'blog.uid' => 1,
137  'post.title' => 'Post1',
138  ],
139  [
140  'blog.uid' => 2,
141  'post.title' => 'post1',
142  ],
143  [
144  'blog.uid' => 3,
145  'post.title' => 'post with tagged author',
146  ],
147  [
148  'blog.uid' => 3,
149  'post.title' => 'post with tag and tagged author',
150  ],
151  [
152  'blog.uid' => 3,
153  'post.title' => 'post with tag',
154  ],
155  ],
156  array_map(
157  static fn(‪Post $post): array => ['blog.uid' => $post->‪getBlog()->getUid(), 'post.title' => $post->‪getTitle()],
158  $posts
159  )
160  );
161  }
162 
163  #[Test]
164  public function ‪findByRespectsLimit(): void
165  {
166  $posts = $this->get(PostRepository::class)->findBy(
167  ['author' => 1],
169  3
170  )->toArray();
171  $titles = array_map(
172  static fn(‪Post $post): array => ['uid' => $post->‪getUid(), 'title' => $post->‪getTitle()],
173  $posts
174  );
175  self::assertSame([
176  [
177  'uid' => 14,
178  'title' => 'post with tag and tagged author',
179  ],
180  [
181  'uid' => 13,
182  'title' => 'post with tagged author',
183  ],
184  [
185  'uid' => 10,
186  'title' => 'Post10',
187  ],
188  ], $titles);
189  }
190 
191  #[Test]
192  public function ‪findByRespectsOffset(): void
193  {
194  $posts = $this->get(PostRepository::class)->findBy(
195  ['author' => 1],
197  3,
198  1
199  )->toArray();
200  $titles = array_map(
201  static fn(‪Post $post): array => ['uid' => $post->‪getUid(), 'title' => $post->‪getTitle()],
202  $posts
203  );
204  self::assertSame([
205  [
206  'uid' => 13,
207  'title' => 'post with tagged author',
208  ],
209  [
210  'uid' => 10,
211  'title' => 'Post10',
212  ],
213  [
214  'uid' => 9,
215  'title' => 'Post9',
216  ],
217  ], $titles);
218  }
219 
220  public static function ‪findOneByRespectsSingleCriteriaDataProvider(): \Generator
221  {
222  yield 'findOneBy(["blog" => 1]) => "Post4"' => [
223  ['uid' => 1],
224  1,
225  ];
226  yield 'findOneBy(["blog" => 100]) => null' => [
227  ['uid' => 100],
228  null,
229  ];
230  }
231 
232  #[DataProvider('findOneByRespectsSingleCriteriaDataProvider')]
233  #[Test]
234  public function ‪findOneByRespectsSingleCriteria(array $criteria, int|null $expectedUid): void
235  {
237  $post = $this->get(PostRepository::class)->findOneBy($criteria);
238  self::assertSame($expectedUid, $post?->getUid());
239  }
240 
241  #[Group('not-postgres')]
242  #[Test]
243  public function ‪findOneByRespectsMultipleCriteria(): void
244  {
245  $post = $this->get(PostRepository::class)->findOneBy(['blog' => 1, 'author' => 1]);
246  self::assertSame('Post4', $post?->getTitle());
247  }
248 
249  #[Test]
250  public function ‪findOneByRespectsOrderBy(): void
251  {
252  $post = $this->get(PostRepository::class)->findOneBy(
253  ['blog' => 1, 'author' => 1],
255  );
256  self::assertSame('Post9', $post?->getTitle());
257  }
258 
259  #[Test]
260  public function ‪countRespectsSingleCriteria(): void
261  {
262  self::assertSame(
263  10,
264  $this->get(PostRepository::class)->count(
265  ['blog' => 1],
266  )
267  );
268  }
269 
270  #[Test]
271  public function ‪countRespectsMultipleCriteria(): void
272  {
273  self::assertSame(
274  1,
275  $this->get(PostRepository::class)->count(
276  ['blog' => 1, 'author' => 3],
277  )
278  );
279  }
280 }
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\RepositoryTest\setUp
‪setUp()
Definition: RepositoryTest.php:34
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\RepositoryTest\findOneByRespectsSingleCriteriaDataProvider
‪static findOneByRespectsSingleCriteriaDataProvider()
Definition: RepositoryTest.php:220
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\RepositoryTest\findByRespectsSingleCriteria
‪findByRespectsSingleCriteria(array $criteria, int $expectedCount)
Definition: RepositoryTest.php:58
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\RepositoryTest\findByRespectsMultipleCriteria
‪findByRespectsMultipleCriteria()
Definition: RepositoryTest.php:64
‪TYPO3\CMS\Extbase\Persistence\QueryInterface
Definition: QueryInterface.php:30
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\ORDER_DESCENDING
‪const ORDER_DESCENDING
Definition: QueryInterface.php:100
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\RepositoryTest\findOneByRespectsMultipleCriteria
‪findOneByRespectsMultipleCriteria()
Definition: RepositoryTest.php:243
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\RepositoryTest\findByRespectsOffset
‪findByRespectsOffset()
Definition: RepositoryTest.php:192
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\RepositoryTest\findByRespectsLimit
‪findByRespectsLimit()
Definition: RepositoryTest.php:164
‪TYPO3Tests\BlogExample\Domain\Model\Post\getTitle
‪getTitle()
Definition: Post.php:120
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\ORDER_ASCENDING
‪const ORDER_ASCENDING
Definition: QueryInterface.php:99
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\RepositoryTest
Definition: RepositoryTest.php:29
‪TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface\getUid
‪getUid()
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\RepositoryTest\countRespectsMultipleCriteria
‪countRespectsMultipleCriteria()
Definition: RepositoryTest.php:271
‪TYPO3Tests\BlogExample\Domain\Model\Post
Definition: Post.php:28
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence
Definition: AddTest.php:18
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\RepositoryTest\countRespectsSingleCriteria
‪countRespectsSingleCriteria()
Definition: RepositoryTest.php:260
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\RepositoryTest\findByRespectsSingleOrderBy
‪findByRespectsSingleOrderBy()
Definition: RepositoryTest.php:70
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\RepositoryTest\findOneByRespectsSingleCriteria
‪findOneByRespectsSingleCriteria(array $criteria, int|null $expectedUid)
Definition: RepositoryTest.php:234
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\RepositoryTest\$testExtensionsToLoad
‪array $testExtensionsToLoad
Definition: RepositoryTest.php:30
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\RepositoryTest\findByRespectsMultipleOrderBy
‪findByRespectsMultipleOrderBy()
Definition: RepositoryTest.php:91
‪TYPO3Tests\BlogExample\Domain\Repository\PostRepository
Definition: PostRepository.php:32
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\RepositoryTest\findOneByRespectsOrderBy
‪findOneByRespectsOrderBy()
Definition: RepositoryTest.php:250
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\RepositoryTest\findByRespectsSingleCriteriaDataProvider
‪static findByRespectsSingleCriteriaDataProvider()
Definition: RepositoryTest.php:40
‪TYPO3Tests\BlogExample\Domain\Model\Post\getBlog
‪getBlog()
Definition: Post.php:110