Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"require-dev": {
"doctrine/annotations": "^2.0.0",
"doctrine/orm": "^2.12 || ^3.0",
"matthiasnoback/symfony-config-test": "^4.3 || ^5.2",
"matthiasnoback/symfony-dependency-injection-test": "^4.3 || ^5.0",
"nyholm/psr7": "^1.8.1",
"php-cs-fixer/shim": "^3.58.1",
Expand Down
163 changes: 3 additions & 160 deletions tests/BaseKernelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,8 @@
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\Client;
use Meilisearch\Exceptions\ApiException;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
Expand All @@ -44,156 +35,16 @@ protected function setUp(): void
$this->cleanUp();
}

protected function createPost(?int $id = null): Post
protected function createPost(): Post
{
$post = new Post();
$post->setTitle('Test Post');
$post->setContent('Test content post');

if (null !== $id) {
$post->setId($id);
}
$post = new Post('Test Post', 'Test content post');

$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 getPrefix(): string
{
return $this->searchService->getConfiguration()->get('prefix');
Expand All @@ -204,11 +55,6 @@ 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];
Expand All @@ -219,13 +65,10 @@ private function cleanUp(): void
{
(new Collection($this->searchService->getConfiguration()->get('indices')))
->each(function ($item): bool {
$this->cleanupIndex($this->getPrefix().$item['name']);
$this->cleanupIndex($item['prefixed_name']);

return true;
});

$this->cleanupIndex($this->getPrefix().'indexA');
$this->cleanupIndex($this->getPrefix().'indexB');
}

private function cleanupIndex(string $indexName): void
Expand Down
57 changes: 13 additions & 44 deletions tests/Entity/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Comment
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::INTEGER, nullable: true)]
#[ORM\Column(type: Types::INTEGER)]
#[Groups('searchable')]
private ?int $id = null;

Expand All @@ -39,88 +39,57 @@ class Comment
*/
#[ORM\ManyToOne(inversedBy: 'comments')]
#[ORM\JoinColumn(nullable: false)]
private ?Post $post = null;
private Post $post;

/**
* @var string
*
* @ORM\Column(type="text")
*
* @Groups({"searchable"})
*/
#[ORM\Column(type: Types::TEXT)]
#[Groups('searchable')]
private $content;
private string $content;

/**
* @var \DateTime
*
* @ORM\Column(type="datetime")
* @ORM\Column(type="datetime_immutable")
*
* @Groups({"searchable"})
*/
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
#[Groups('searchable')]
private $publishedAt;
private \DateTimeImmutable $publishedAt;

/**
* Comment constructor.
*
* @param array<string, mixed> $attributes
*/
public function __construct(array $attributes = [])
public function __construct(Post $post, string $content, ?\DateTimeImmutable $publishedAt = null)
{
$this->id = $attributes['id'] ?? null;
$this->content = $attributes['content'] ?? null;
$this->publishedAt = $attributes['publishedAt'] ?? new \DateTime();
$this->post = $attributes['post'] ?? null;
$this->post = $post;
$this->content = $content;
$this->publishedAt = $publishedAt ?? new \DateTimeImmutable();
}

public function getId(): ?int
{
return $this->id;
}

public function setId(?int $id): Comment
{
$this->id = $id;

return $this;
}

public function getContent(): ?string
public function getContent(): string
{
return $this->content;
}

public function setContent(?string $content): Comment
public function setContent(string $content): Comment
{
$this->content = $content;

return $this;
}

public function getPublishedAt(): \DateTime
public function getPublishedAt(): \DateTimeImmutable
{
return $this->publishedAt;
}

public function setPublishedAt(\DateTime $publishedAt): Comment
{
$this->publishedAt = $publishedAt;

return $this;
}

public function getPost(): ?Post
{
return $this->post;
}

public function setPost(Post $post): Comment
{
$this->post = $post;

return $this;
}
}
21 changes: 6 additions & 15 deletions tests/Entity/ContentItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,26 @@ abstract class ContentItem
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::INTEGER)]
private int $id;
private ?int $id = null;

/**
* @ORM\Column(type="string")
*/
#[ORM\Column(type: Types::STRING)]
private string $title = 'Title';
private string $title;

public function getId(): int
public function __construct(string $title = 'Title')
{
return $this->id;
$this->title = $title;
}

public function setId(int $id): self
public function getId(): ?int
{
$this->id = $id;

return $this;
return $this->id;
}

public function getTitle(): string
{
return $this->title;
}

public function setTitle(string $title): self
{
$this->title = $title;

return $this;
}
}
2 changes: 2 additions & 0 deletions tests/Entity/DummyCustomGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class DummyCustomGroups
*
* @ORM\Column(type="integer")
*
* @ORM\GeneratedValue("NONE")
*
* @Groups("public")
*/
#[ORM\Id]
Expand Down
2 changes: 2 additions & 0 deletions tests/Entity/DynamicSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class DynamicSettings
* @ORM\Id
*
* @ORM\Column(type="integer")
*
* @ORM\GeneratedValue("NONE")
*/
#[ORM\Id]
#[ORM\Column(type: Types::INTEGER)]
Expand Down
Loading