-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathBaseKernelTestCase.php
251 lines (199 loc) · 6.82 KB
/
BaseKernelTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
declare(strict_types=1);
namespace Meilisearch\Bundle\Tests;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use Meilisearch\Bundle\Collection;
use Meilisearch\Bundle\SearchableEntity;
use Meilisearch\Bundle\SearchService;
use Meilisearch\Bundle\Tests\Entity\Article;
use Meilisearch\Bundle\Tests\Entity\Comment;
use Meilisearch\Bundle\Tests\Entity\Image;
use Meilisearch\Bundle\Tests\Entity\Link;
use Meilisearch\Bundle\Tests\Entity\ObjectId\DummyObjectId;
use Meilisearch\Bundle\Tests\Entity\Page;
use Meilisearch\Bundle\Tests\Entity\Podcast;
use Meilisearch\Bundle\Tests\Entity\Post;
use Meilisearch\Bundle\Tests\Entity\Tag;
use Meilisearch\Bundle\Tests\Entity\Ticket;
use Meilisearch\Client;
use Meilisearch\Exceptions\ApiException;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
abstract class BaseKernelTestCase extends KernelTestCase
{
protected EntityManagerInterface $entityManager;
protected Client $client;
protected SearchService $searchService;
protected function setUp(): void
{
self::bootKernel();
$this->entityManager = $this->get('doctrine.orm.entity_manager');
$this->client = $this->get('meilisearch.client');
$this->searchService = $this->get('meilisearch.service');
$metaData = $this->entityManager->getMetadataFactory()->getAllMetadata();
$tool = new SchemaTool($this->entityManager);
$tool->dropSchema($metaData);
$tool->createSchema($metaData);
$this->cleanUp();
}
protected function createPost(?int $id = null): Post
{
$post = new Post();
$post->setTitle('Test Post');
$post->setContent('Test content post');
if (null !== $id) {
$post->setId($id);
}
$this->entityManager->persist($post);
$this->entityManager->flush();
return $post;
}
protected function createPage(int $id): Page
{
$page = new Page();
$page->setTitle('Test Page');
$page->setContent('Test content page');
$page->setId(new DummyObjectId($id));
$this->entityManager->persist($page);
$this->entityManager->flush();
return $page;
}
protected function createSearchablePost(): SearchableEntity
{
$post = $this->createPost(random_int(100, 300));
return new SearchableEntity(
$this->getPrefix().'posts',
$post,
$this->get('doctrine')->getManager()->getClassMetadata(Post::class),
$this->get('serializer')
);
}
protected function createComment(?int $id = null): Comment
{
$post = new Post(['title' => 'What a post!']);
$comment = new Comment();
$comment->setContent('Comment content');
$comment->setPost($post);
if (null !== $id) {
$comment->setId($id);
}
$this->entityManager->persist($post);
$this->entityManager->persist($comment);
$this->entityManager->flush();
return $comment;
}
protected function createImage(?int $id = null): Image
{
$image = new Image();
$image->setUrl('https://docs.meilisearch.com/logo.png');
if (null !== $id) {
$image->setId($id);
}
$this->entityManager->persist($image);
$this->entityManager->flush();
return $image;
}
protected function createArticle(?int $id = null): Article
{
$article = new Article();
$article->setTitle('Test Article');
if (null !== $id) {
$article->setId($id);
}
$this->entityManager->persist($article);
$this->entityManager->flush();
return $article;
}
protected function createPodcast(?int $id = null): Podcast
{
$podcast = new Podcast();
$podcast->setTitle('Test Podcast');
if (null !== $id) {
$podcast->setId($id);
}
$this->entityManager->persist($podcast);
$this->entityManager->flush();
return $podcast;
}
protected function createSearchableImage(): SearchableEntity
{
$image = $this->createImage(random_int(100, 300));
return new SearchableEntity(
$this->getPrefix().'image',
$image,
$this->get('doctrine')->getManager()->getClassMetadata(Image::class),
null
);
}
protected function createTag(array $properties = []): Tag
{
$tag = new Tag();
$tag->setName('Meilisearch Test Tag');
if (\count($properties) > 0) {
foreach ($properties as $key => $value) {
$method = 'set'.ucfirst($key);
$tag->$method($value);
}
}
$this->entityManager->persist($tag);
$this->entityManager->flush();
return $tag;
}
protected function createLink(array $properties = []): Link
{
$link = new Link();
$link->setName('Meilisearch Test Link');
if (\count($properties) > 0) {
foreach ($properties as $key => $value) {
$method = 'set'.ucfirst($key);
$link->$method($value);
}
}
$this->entityManager->persist($link);
$this->entityManager->flush();
return $link;
}
protected function createTicket(int $id, bool $sold): Ticket
{
$ticket = new Ticket($id, str_pad((string) random_int(0, 1000000), 6, '0', STR_PAD_LEFT), $sold);
$this->entityManager->persist($ticket);
$this->entityManager->flush();
return $ticket;
}
protected function getPrefix(): string
{
return $this->searchService->getConfiguration()->get('prefix');
}
protected function get(string $id): ?object
{
return self::getContainer()->get($id);
}
protected function getFileName(string $indexName, string $type): string
{
return \sprintf('%s/%s.json', $indexName, $type);
}
protected function waitForAllTasks(): void
{
$firstTask = $this->client->getTasks()->getResults()[0];
$this->client->waitForTask($firstTask['uid']);
}
private function cleanUp(): void
{
(new Collection($this->searchService->getConfiguration()->get('indices')))
->each(function ($item): bool {
$this->cleanupIndex($this->getPrefix().$item['name']);
return true;
});
$this->cleanupIndex($this->getPrefix().'indexA');
$this->cleanupIndex($this->getPrefix().'indexB');
}
private function cleanupIndex(string $indexName): void
{
try {
$this->searchService->deleteByIndexName($indexName);
} catch (ApiException $e) {
// Don't assert undefined indexes.
// Just plainly delete all existing indexes to get a clean state.
}
}
}