diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 39643806..a92c4956 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -14,7 +14,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['7.2', '7.3', '7.4', '8.0'] + php-versions: ['7.4', '8.0'] name: integration-tests (PHP ${{ matrix.php-versions }}) steps: - uses: actions/checkout@v2 diff --git a/.php_cs.dist b/.php_cs.dist index ed824215..bc649fc7 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -1,33 +1,28 @@ setRules( - [ +$finder = PhpCsFixer\Finder::create() + ->in(__DIR__.'/src') + ->in(__DIR__.'/tests') + ->append([__FILE__]); +$config = new PhpCsFixer\Config(); +$config->setRules([ '@Symfony' => true, - '@Symfony:risky' => true, - '@PHPUnit75Migration:risky' => true, - 'php_unit_dedicate_assert' => ['target' => 'newest'], - 'array_syntax' => ['syntax' => 'short'], - 'fopen_flags' => false, - 'protected_to_private' => false, - 'combine_nested_dirname' => true, + 'declare_strict_types' => true, 'global_namespace_import' => [ - 'import_classes' => true, - 'import_constants' => true, - 'import_functions' => true, + 'import_classes' => false, + 'import_functions' => false, + 'import_constants' => false, ], - 'phpdoc_no_package' => false, - 'no_superfluous_phpdoc_tags' => false, - 'ordered_imports' => ['sortAlgorithm' => 'none'], ] ) ->setRiskyAllowed(true) - ->setFinder( - PhpCsFixer\Finder::create() - ->in(__DIR__.'/src') - ->append([__FILE__]) - ); + ->setFinder($finder) +; + +return $config; diff --git a/Dockerfile b/Dockerfile index 78be152e..30d7154a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM php:7.2.0-fpm-alpine as php +FROM php:7.4-fpm-alpine as php # persistent / runtime deps RUN apk add --no-cache \ diff --git a/bors.toml b/bors.toml index 7a464242..dd3f648c 100644 --- a/bors.toml +++ b/bors.toml @@ -1,6 +1,4 @@ status = [ - 'integration-tests (PHP 7.2)', - 'integration-tests (PHP 7.3)', 'integration-tests (PHP 7.4)', 'integration-tests (PHP 8.0)', 'linter-check' diff --git a/composer.json b/composer.json index a1f03c4d..dadebf56 100644 --- a/composer.json +++ b/composer.json @@ -18,17 +18,17 @@ } ], "require": { - "php": ">=7.2", + "php": "^7.4|^8.0", "ext-json": "*", + "doctrine/orm": "^2.8", "meilisearch/meilisearch-php": "^0.17", "symfony/filesystem": "^4.0 || ^5.0", "symfony/property-access": "^4.0 || ^5.0", "symfony/serializer": "^4.0 || ^5.0" }, "require-dev": { - "doctrine/doctrine-bundle": "^2.0", - "doctrine/orm": "^2.5", - "friendsofphp/php-cs-fixer": "^2.16", + "doctrine/doctrine-bundle": "^2.3", + "friendsofphp/php-cs-fixer": "^2.18", "jms/serializer-bundle": "^3.0", "nyholm/psr7": "^1.3", "phpunit/phpunit": "^8.5", diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 91ed7c74..cdb576e5 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -12,7 +12,7 @@ if [ "$1" = 'php-fpm' ] || [ "$1" = 'php' ] || [ "$1" = 'bin/console' ]; then setfacl -dR -m u:www-data:rwX -m u:"$(whoami)":rwX var if [ "$APP_ENV" != 'prod' ]; then - composer install --prefer-dist --no-progress --no-suggest --no-interaction + composer install --prefer-dist --no-progress --no-interaction fi fi diff --git a/src/Command/IndexCommand.php b/src/Command/IndexCommand.php index 665a11fc..b3a9aeef 100644 --- a/src/Command/IndexCommand.php +++ b/src/Command/IndexCommand.php @@ -1,32 +1,22 @@ searchService = $searchService; parent::__construct($name); @@ -38,7 +28,7 @@ protected function getEntitiesFromArgs(InputInterface $input, OutputInterface $o $indexNames = []; if ($indexList = $input->getOption('indices')) { - $indexNames = explode(',', $indexList); + $indexNames = \explode(',', $indexList); } $config = $this->searchService->getConfiguration(); diff --git a/src/Command/MeiliSearchClearCommand.php b/src/Command/MeiliSearchClearCommand.php index 7d8bcdbd..979eba86 100644 --- a/src/Command/MeiliSearchClearCommand.php +++ b/src/Command/MeiliSearchClearCommand.php @@ -1,5 +1,7 @@ searchClient = $searchClient; } - /** - * {@inheritdoc} - */ protected function configure() { $this @@ -72,12 +43,6 @@ protected function configure() ); } - /** - * {@inheritdoc} - * - * @throws TimeOutException - * @throws ApiException - */ protected function execute(InputInterface $input, OutputInterface $output): int { $entitiesToIndex = $this->getEntitiesFromArgs($input, $output); @@ -164,11 +129,7 @@ function ($entity) { return 0; } - /** - * @param array $batch - * - * @return array - * + /* * @throws TimeOutException */ private function formatIndexingResponse(array $batch): array diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 4b209241..ce7d1f16 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -1,5 +1,7 @@ getRootNode(); diff --git a/src/DependencyInjection/MeiliSearchExtension.php b/src/DependencyInjection/MeiliSearchExtension.php index d94754e6..b68f860c 100644 --- a/src/DependencyInjection/MeiliSearchExtension.php +++ b/src/DependencyInjection/MeiliSearchExtension.php @@ -1,5 +1,7 @@ load('services.xml'); $configuration = new Configuration(); @@ -35,7 +33,7 @@ public function load(array $configs, ContainerBuilder $container) $config['prefix'] = $container->getParameter('kernel.environment').'_'; } - if (count($doctrineSubscribedEvents = $config['doctrineSubscribedEvents']) > 0) { + if (\count($doctrineSubscribedEvents = $config['doctrineSubscribedEvents']) > 0) { $container->getDefinition('search.search_indexer_subscriber')->setArgument(1, $doctrineSubscribedEvents); } else { $container->removeDefinition('search.search_indexer_subscriber'); diff --git a/src/Document/Aggregator.php b/src/Document/Aggregator.php index d4c797a8..f75b613b 100644 --- a/src/Document/Aggregator.php +++ b/src/Document/Aggregator.php @@ -1,13 +1,13 @@ client = $client; @@ -35,10 +27,7 @@ public function __construct(Client $client) * * @param array|SearchableEntity $searchableEntities * - * @return array - * * @throws ApiException - * @throws ExceptionInterface */ public function index($searchableEntities): array { @@ -49,7 +38,7 @@ public function index($searchableEntities): array $data = []; foreach ($searchableEntities as $entity) { $searchableArray = $entity->getSearchableArray(); - if (null === $searchableArray || 0 === count($searchableArray)) { + if (null === $searchableArray || 0 === \count($searchableArray)) { continue; } @@ -77,10 +66,6 @@ public function index($searchableEntities): array * This method enables you to remove one or more objects from an index. * * @param array|SearchableEntity $searchableEntities - * - * @return array - * - * @throws ExceptionInterface */ public function remove($searchableEntities): array { @@ -91,7 +76,7 @@ public function remove($searchableEntities): array $data = []; foreach ($searchableEntities as $entity) { $searchableArray = $entity->getSearchableArray(); - if (null === $searchableArray || 0 === count($searchableArray)) { + if (null === $searchableArray || 0 === \count($searchableArray)) { continue; } $indexUid = $entity->getIndexName(); @@ -117,10 +102,6 @@ public function remove($searchableEntities): array * Clear the records of an index. * This method enables you to delete an index’s contents (records). * - * @param string $indexUid - * - * @return array - * * @throws ApiException */ public function clear(string $indexUid): array @@ -133,10 +114,6 @@ public function clear(string $indexUid): array /** * Delete an index and it's content. - * - * @param string $indexUid - * - * @return array|null */ public function delete(string $indexUid): ?array { @@ -145,12 +122,6 @@ public function delete(string $indexUid): ?array /** * Method used for querying an index. - * - * @param string $query - * @param string $indexUid - * @param array $searchParams - * - * @return array */ public function search(string $query, string $indexUid, array $searchParams): array { diff --git a/src/Entity/Aggregator.php b/src/Entity/Aggregator.php index c26138f2..73b0313b 100644 --- a/src/Entity/Aggregator.php +++ b/src/Entity/Aggregator.php @@ -1,13 +1,13 @@ searchService = $searchService; @@ -48,6 +39,6 @@ public function postPersist(LifecycleEventArgs $args): void public function preRemove(LifecycleEventArgs $args): void { - $this->searchService->remove($args->getObjectManager(), $object = $args->getObject()); + $this->searchService->remove($args->getObjectManager(), $args->getObject()); } } diff --git a/src/Exception/EntityNotFoundInObjectID.php b/src/Exception/EntityNotFoundInObjectID.php index 9d82fdf3..fe8d4d07 100644 --- a/src/Exception/EntityNotFoundInObjectID.php +++ b/src/Exception/EntityNotFoundInObjectID.php @@ -1,14 +1,12 @@ objectID = reset($entityIdentifierValues); + $this->objectID = (string) reset($entityIdentifierValues); } /** @@ -61,40 +56,32 @@ public static function getEntities(): array /** * Returns an entity id from the provided object id. - * - * @param string $objectID - * - * @return string */ - public static function getEntityIdFromObjectID(string $objectID): string + public static function getEntityIdFromObjectId(string $objectId): string { - return $objectID; + return $objectId; } /** * Returns an entity class name from the provided object id. * - * @param string $objectID - * - * @return string - * * @throws EntityNotFoundInObjectID */ - public static function getEntityClassFromObjectID(string $objectID) + public static function getEntityClassFromObjectID(string $objectId): string { - $type = explode('::', $objectID)[0]; + $type = explode('::', $objectId)[0]; if (in_array($type, static::getEntities(), true)) { return $type; } - throw new EntityNotFoundInObjectID("Entity class from ObjectID {$objectID} not found."); + throw new EntityNotFoundInObjectID("Entity class from ObjectID $objectId not found."); } /** * {@inheritdoc} */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) + public function normalize(NormalizerInterface $normalizer, ?string $format = null, array $context = []): array { return array_merge(['objectID' => $this->objectID], $normalizer->normalize($this->entity, $format, $context)); } diff --git a/src/SearchService.php b/src/SearchService.php index 5de7b33e..3cce5bf5 100644 --- a/src/SearchService.php +++ b/src/SearchService.php @@ -1,80 +1,38 @@ setId(); } - /** - * @return string - */ public function getindexUid(): string { return $this->indexUid; } /** - * @return array - * * @throws ExceptionInterface */ public function getSearchableArray(): array @@ -89,18 +77,15 @@ public function getSearchableArray(): array return []; } - /** - * @return void - */ - private function setId() + private function setId(): void { $ids = $this->entityMetadata->getIdentifierValues($this->entity); - if (0 === count($ids)) { + if (0 === \count($ids)) { throw new Exception('Entity has no primary key'); } - if (1 == count($ids)) { + if (1 == \count($ids)) { $this->id = reset($ids); } else { $objectID = ''; diff --git a/src/Services/MeiliSearchService.php b/src/Services/MeiliSearchService.php index 400e08c7..1c21a338 100644 --- a/src/Services/MeiliSearchService.php +++ b/src/Services/MeiliSearchService.php @@ -1,5 +1,7 @@ normalizer = $normalizer; @@ -98,17 +60,11 @@ public function isSearchable($className): bool return in_array($className, $this->searchableEntities, true); } - /** - * {@inheritdoc} - */ public function getSearchable(): array { return $this->searchableEntities; } - /** - * {@inheritdoc} - */ public function getConfiguration(): array { return $this->configuration; @@ -122,9 +78,6 @@ public function searchableAs(string $className): string return $this->configuration['prefix'].$this->classToIndexMapping[$className]; } - /** - * {@inheritdoc} - */ public function index(ObjectManager $objectManager, $searchable): array { $searchable = is_array($searchable) ? $searchable : [$searchable]; @@ -158,9 +111,6 @@ function ($chunk) { ); } - /** - * {@inheritdoc} - */ public function remove(ObjectManager $objectManager, $searchable): array { $searchable = is_array($searchable) ? $searchable : [$searchable]; @@ -182,9 +132,6 @@ function ($chunk) { ); } - /** - * {@inheritdoc} - */ public function clear(string $className): array { $this->assertIsSearchable($className); @@ -192,9 +139,6 @@ public function clear(string $className): array return $this->engine->clear($this->searchableAs($className)); } - /** - * {@inheritdoc} - */ public function delete(string $className): ?array { $this->assertIsSearchable($className); @@ -202,9 +146,6 @@ public function delete(string $className): ?array return $this->engine->delete($this->searchableAs($className)); } - /** - * {@inheritdoc} - */ public function search( ObjectManager $objectManager, string $className, @@ -223,8 +164,8 @@ public function search( foreach ($ids['hits'] as $objectID) { if (in_array($className, $this->aggregators, true)) { - $entityClass = $className::getEntityClassFromObjectID($objectID); - $id = $className::getEntityIdFromObjectID($objectID); + $entityClass = $className::getEntityClassFromObjectId($objectID); + $id = $className::getEntityIdFromObjectId($objectID); } else { $id = $objectID; $entityClass = $className; @@ -254,9 +195,6 @@ public function rawSearch( return $this->engine->search($query, $this->searchableAs($className), $searchParams); } - /** - * {@inheritdoc} - */ public function count(string $className, string $query = '', array $searchParams = []): int { $this->assertIsSearchable($className); @@ -266,8 +204,6 @@ public function count(string $className, string $query = '', array $searchParams /** * @param object $entity - * - * @return bool */ public function shouldBeIndexed($entity): bool { @@ -299,7 +235,7 @@ private function setAggregatorsAndEntitiesAggregators(): void $this->entitiesAggregators = []; $this->aggregators = []; - foreach ($this->configuration['indices'] as $name => $index) { + foreach ($this->configuration['indices'] as $index) { if (is_subclass_of($index['class'], Aggregator::class)) { foreach ($index['class']::getEntities() as $entityClass) { if (!isset($this->entitiesAggregators[$entityClass])) { @@ -354,7 +290,6 @@ private function setSettingsMapping(): void /** * Returns the aggregators instances of the provided entities. * - * @param ObjectManager $objectManager * @param array $entities * * @return array @@ -381,11 +316,7 @@ private function getAggregatorsFromEntities(ObjectManager $objectManager, array /** * For each chunk performs the provided operation. * - * @param ObjectManager $objectManager * @param array $entities - * @param callable $operation - * - * @return array */ private function makeSearchServiceResponseFrom( ObjectManager $objectManager, @@ -413,11 +344,6 @@ private function makeSearchServiceResponseFrom( return $batch; } - /** - * @param string $className - * - * @return bool - */ private function canUseSerializerGroup(string $className): bool { return $this->classToSerializerGroupMapping[$className]; diff --git a/src/Services/NullSearchService.php b/src/Services/NullSearchService.php index ac2e6b21..6d939082 100644 --- a/src/Services/NullSearchService.php +++ b/src/Services/NullSearchService.php @@ -1,15 +1,14 @@ 200]; @@ -45,48 +38,33 @@ public function searchableAs(string $className): string return ''; } - /** - * {@inheritdoc} - */ public function index(ObjectManager $objectManager, $searchable): array { return []; } - /** - * {@inheritdoc} - */ public function remove(ObjectManager $objectManager, $searchable): array { return []; } - /** - * {@inheritdoc} - */ public function clear(string $className): array { return []; } - /** - * {@inheritdoc} - */ public function delete(string $className): ?array { return []; } - /** - * {@inheritdoc} - */ public function search( ObjectManager $objectManager, string $className, string $query = '', array $searchParams = [] ): array { - return [new stdClass()]; + return [new \stdClass()]; } /** @@ -100,9 +78,6 @@ public function rawSearch( return []; } - /** - * {@inheritdoc} - */ public function count(string $className, string $query = '', array $searchParams = []): int { return 0; diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 9f83427c..f9342bc2 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -1,8 +1,9 @@ bootKernel(); } /** * @param int|string|null $id - * - * @return Post */ protected function createPost($id = null): Post { @@ -55,7 +32,7 @@ protected function createPost($id = null): Post $post->setTitle('Test'); $post->setContent('Test content'); - if (!is_null($id)) { + if (null !== $id) { $post->setId($id); } @@ -67,7 +44,7 @@ protected function createSearchablePost(): SearchableEntity $post = $this->createPost(rand(100, 300)); return new SearchableEntity( - $this->getPrefix() . 'posts', + $this->getPrefix().'posts', $post, $this->get('doctrine')->getManager()->getClassMetadata(Post::class), $this->get('serializer') @@ -76,8 +53,6 @@ protected function createSearchablePost(): SearchableEntity /** * @param int|string|null $id - * - * @return Comment */ protected function createComment($id = null): Comment { @@ -85,7 +60,7 @@ protected function createComment($id = null): Comment $comment->setContent('Comment content'); $comment->setPost(new Post(['title' => 'What a post!'])); - if (!is_null($id)) { + if (null !== $id) { $comment->setId($id); } @@ -94,14 +69,12 @@ protected function createComment($id = null): Comment /** * @param int|string|null $id - * - * @return Image */ protected function createImage($id = null): Image { $image = new Image(); - if (!is_null($id)) { + if (null !== $id) { $image->setId($id); } @@ -113,7 +86,7 @@ protected function createSearchableImage(): SearchableEntity $image = $this->createImage(rand(100, 300)); return new SearchableEntity( - $this->getPrefix() . 'image', + $this->getPrefix().'image', $image, $this->get('doctrine')->getManager()->getClassMetadata(Image::class), null @@ -131,19 +104,17 @@ protected function get(string $id): ?object } /** - * @param Application $application - * - * @throws Exception + * @throws \Exception */ protected function refreshDb(Application $application): void { $inputs = [ new ArrayInput( [ - 'command' => 'doctrine:schema:drop', + 'command' => 'doctrine:schema:drop', '--full-database' => true, - '--force' => true, - '--quiet' => true, + '--force' => true, + '--quiet' => true, ] ), new ArrayInput( diff --git a/tests/Entity/Comment.php b/tests/Entity/Comment.php index c31533a1..06be7f0c 100644 --- a/tests/Entity/Comment.php +++ b/tests/Entity/Comment.php @@ -1,9 +1,11 @@ id = $attributes['id'] ?? null; - $this->content = $attributes['content'] ?? null; - $this->publishedAt = $attributes['publishedAt'] ?? new DateTime(); - $this->post = $attributes['post'] ?? null; + $this->id = $attributes['id'] ?? null; + $this->content = $attributes['content'] ?? null; + $this->publishedAt = $attributes['publishedAt'] ?? new \DateTime(); + $this->post = $attributes['post'] ?? null; } public function getId(): ?int @@ -81,12 +81,12 @@ public function setContent(?string $content): Comment return $this; } - public function getPublishedAt(): DateTime + public function getPublishedAt(): \DateTime { return $this->publishedAt; } - public function setPublishedAt(DateTime $publishedAt): Comment + public function setPublishedAt(\DateTime $publishedAt): Comment { $this->publishedAt = $publishedAt; diff --git a/tests/Entity/ContentAggregator.php b/tests/Entity/ContentAggregator.php index 751142c3..b88819ae 100644 --- a/tests/Entity/ContentAggregator.php +++ b/tests/Entity/ContentAggregator.php @@ -1,5 +1,7 @@ entity instanceof Post) { - return $this->entity->getTitle() !== 'Foo'; + return 'Foo' !== $this->entity->getTitle(); } return true; diff --git a/tests/Entity/EmptyAggregator.php b/tests/Entity/EmptyAggregator.php index 0675d962..404d657a 100644 --- a/tests/Entity/EmptyAggregator.php +++ b/tests/Entity/EmptyAggregator.php @@ -1,5 +1,7 @@ id = $attributes['id'] ?? null; + $this->id = $attributes['id'] ?? null; $this->url = $attributes['url'] ?? 'https://docs.meilisearch.com/logo.png'; } diff --git a/tests/Entity/Link.php b/tests/Entity/Link.php index bc9573d2..2288f5c5 100644 --- a/tests/Entity/Link.php +++ b/tests/Entity/Link.php @@ -1,5 +1,7 @@ id = $attributes['id'] ?? null; + $this->id = $attributes['id'] ?? null; $this->name = $attributes['name'] ?? 'This is a tag'; - $this->url = $attributes['url'] ?? null; + $this->url = $attributes['url'] ?? null; } private function isSponsored(): bool @@ -49,15 +47,15 @@ private function isSponsored(): bool } /** - * @inheritDoc + * {@inheritDoc} */ public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) { if (Searchable::NORMALIZATION_FORMAT === $format) { return [ - 'id' => $this->id, + 'id' => $this->id, 'name' => 'this test is correct', - 'url' => 'https://www.meilisearch.com', + 'url' => 'https://www.meilisearch.com', ]; } diff --git a/tests/Entity/Post.php b/tests/Entity/Post.php index 3a4fdd41..422342ab 100644 --- a/tests/Entity/Post.php +++ b/tests/Entity/Post.php @@ -1,9 +1,11 @@ id = $attributes['id'] ?? null; - $this->title = $attributes['title'] ?? null; - $this->content = $attributes['content'] ?? null; - $this->publishedAt = $attributes['publishedAt'] ?? new DateTime(); - $this->comments = new ArrayCollection($attributes['comments'] ?? []); + $this->id = $attributes['id'] ?? null; + $this->title = $attributes['title'] ?? null; + $this->content = $attributes['content'] ?? null; + $this->publishedAt = $attributes['publishedAt'] ?? new \DateTime(); + $this->comments = new ArrayCollection($attributes['comments'] ?? []); } public function getId(): ?int @@ -111,19 +109,19 @@ public function setContent(?string $content): Post /** * @Groups({"searchable"}) */ - public function getPublishedAt(): ?DateTime + public function getPublishedAt(): ?\DateTime { return $this->publishedAt; } - public function setPublishedAt(?DateTime $publishedAt): Post + public function setPublishedAt(?\DateTime $publishedAt): Post { $this->publishedAt = $publishedAt; return $this; } - public function getComments(): ?ArrayCollection + public function getComments(): ?Collection { return $this->comments; } diff --git a/tests/Entity/Tag.php b/tests/Entity/Tag.php index 59dff4de..3461c9f5 100644 --- a/tests/Entity/Tag.php +++ b/tests/Entity/Tag.php @@ -1,8 +1,9 @@ id = $attributes['id'] ?? null; - $this->name = $attributes['name'] ?? 'This is a tag'; - $this->count = $attributes['count'] ?? 0; - $this->public = $attributes['public'] ?? true; - $this->publishedAt = $attributes['publishedAt'] ?? new DateTime(); + $this->id = $attributes['id'] ?? null; + $this->name = $attributes['name'] ?? 'This is a tag'; + $this->count = $attributes['count'] ?? 0; + $this->public = $attributes['public'] ?? true; + $this->publishedAt = $attributes['publishedAt'] ?? new \DateTime(); } public function isPublic(): bool @@ -62,16 +59,17 @@ public function setPublic(bool $public): Tag } /** - * @inheritDoc + * {@inheritDoc} + * * @throws ExceptionInterface */ public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) { if (Searchable::NORMALIZATION_FORMAT === $format) { return [ - 'id' => $this->id, - 'name' => 'this test is correct', - 'count' => 10, + 'id' => $this->id, + 'name' => 'this test is correct', + 'count' => 10, 'publishedAt' => $normalizer->normalize($this->publishedAt), ]; } diff --git a/tests/Kernel.php b/tests/Kernel.php index e044db09..d7b07f06 100644 --- a/tests/Kernel.php +++ b/tests/Kernel.php @@ -1,5 +1,7 @@ load(__DIR__ . '/config/config.yaml'); - $loader->load(dirname(__DIR__) . '/src/Resources/config/services.xml'); - $loader->load(__DIR__ . '/config/meili_search.yaml'); - $loader->load(__DIR__ . '/config/services.yaml'); + $loader->load(__DIR__.'/config/config.yaml'); + $loader->load(__DIR__.'/../src/Resources/config/services.xml'); + $loader->load(__DIR__.'/config/meili_search.yaml'); } } diff --git a/tests/Normalizer/CommentNormalizer.php b/tests/Normalizer/CommentNormalizer.php index d01fbca8..a91ac814 100644 --- a/tests/Normalizer/CommentNormalizer.php +++ b/tests/Normalizer/CommentNormalizer.php @@ -1,33 +1,26 @@ $object->getContent(), + 'content' => $object->getContent(), 'post_title' => $object->getPost()->getTitle(), ]; } - /** - * @inheritDoc - */ - public function supportsNormalization($data, $format = null) + public function supportsNormalization($data, $format = null): bool { return $data instanceof Comment; } diff --git a/tests/TestCase/AggregatorTest.php b/tests/TestCase/AggregatorTest.php index 2d7e3c3c..f0ca435a 100644 --- a/tests/TestCase/AggregatorTest.php +++ b/tests/TestCase/AggregatorTest.php @@ -1,5 +1,7 @@ 1, - 'title' => 'Test', + 'id' => 1, + 'title' => 'Test', 'content' => 'Test content', ] ); @@ -69,7 +68,7 @@ public function testAggregatorProxyClass() $postMetadata = $this->entityManager->getClassMetadata(Post::class); $this->entityManager->getProxyFactory()->generateProxyClasses([$postMetadata], null); - $proxy = $this->entityManager->getProxyFactory()->getProxy($postMetadata->getName(), ['id' => 1]); + $proxy = $this->entityManager->getProxyFactory()->getProxy($postMetadata->getName(), ['id' => 1]); $contentAggregator = new ContentAggregator($proxy, ['objectId']); $serializer = $this->get('serializer'); diff --git a/tests/TestCase/CommandsTest.php b/tests/TestCase/CommandsTest.php index 88543215..f3efdca8 100644 --- a/tests/TestCase/CommandsTest.php +++ b/tests/TestCase/CommandsTest.php @@ -1,12 +1,12 @@ searchService = $this->get('search.service'); - $this->client = $this->get('search.client'); + $this->client = $this->get('search.client'); $this->objectManager = $this->get('doctrine')->getManager(); - $this->connection = $this->get('doctrine')->getConnection(); - $this->platform = $this->connection->getDatabasePlatform(); - $this->indexName = 'posts'; - $this->index = $this->client->getOrCreateIndex($this->getPrefix() . $this->indexName); + $this->connection = $this->get('doctrine')->getConnection(); + $this->platform = $this->connection->getDatabasePlatform(); + $this->indexName = 'posts'; + $this->index = $this->client->getOrCreateIndex($this->getPrefix().$this->indexName); $this->application = new Application(self::$kernel); $this->refreshDb($this->application); @@ -86,55 +67,58 @@ public function testSearchClearUnknownIndex() { $unknownIndexName = 'test'; - $command = $this->application->find('meili:clear'); + $command = $this->application->find('meili:clear'); $commandTester = new CommandTester($command); $commandTester->execute( [ - 'command' => $command->getName(), + 'command' => $command->getName(), '--indices' => $unknownIndexName, ] ); // Checks output and ensure it failed $output = $commandTester->getDisplay(); - $this->assertStringContainsString('No index named ' . $unknownIndexName, $output); + $this->assertStringContainsString('No index named '.$unknownIndexName, $output); $this->cleanUp(); } + /** + * @throws \Doctrine\DBAL\Exception + */ public function testSearchImportAggregator() { $now = new \DateTime(); $this->connection->insert( $this->indexName, [ - 'title' => 'Test', - 'content' => 'Test content', + 'title' => 'Test', + 'content' => 'Test content', 'published_at' => $now->format('Y-m-d H:i:s'), ] ); $this->connection->insert( $this->indexName, [ - 'title' => 'Test2', - 'content' => 'Test content2', + 'title' => 'Test2', + 'content' => 'Test content2', 'published_at' => $now->format('Y-m-d H:i:s'), ] ); $this->connection->insert( $this->indexName, [ - 'title' => 'Test3', - 'content' => 'Test content3', + 'title' => 'Test3', + 'content' => 'Test content3', 'published_at' => $now->format('Y-m-d H:i:s'), ] ); - $command = $this->application->find('meili:import'); + $command = $this->application->find('meili:import'); $commandTester = new CommandTester($command); $commandTester->execute( [ - 'command' => $command->getName(), + 'command' => $command->getName(), '--indices' => 'contents', ] ); @@ -144,7 +128,7 @@ public function testSearchImportAggregator() $this->assertStringContainsString('Done!', $output); // clearup table - $this->connection->executeUpdate($this->platform->getTruncateTableSQL($this->indexName, true)); + $this->connection->executeStatement($this->platform->getTruncateTableSQL($this->indexName, true)); $this->cleanUp(); } } diff --git a/tests/TestCase/ConfigurationTest.php b/tests/TestCase/ConfigurationTest.php index 77da75e7..fd0bc936 100644 --- a/tests/TestCase/ConfigurationTest.php +++ b/tests/TestCase/ConfigurationTest.php @@ -1,18 +1,17 @@ getConfigTreeBuilder()->buildTree(); + $node = $configuration->getConfigTreeBuilder()->buildTree(); $normalizedConfig = $node->normalize($inputConfig); - $finalizedConfig = $node->finalize($normalizedConfig); + $finalizedConfig = $node->finalize($normalizedConfig); $this->assertEquals($expectedConfig, $finalizedConfig); } @@ -36,60 +35,60 @@ public function dataTestConfigurationTree(): array 'test empty config for default value' => [ [], [ - 'prefix' => null, - 'nbResults' => 20, - 'batchSize' => 500, - 'serializer' => 'serializer', + 'prefix' => null, + 'nbResults' => 20, + 'batchSize' => 500, + 'serializer' => 'serializer', 'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'], - 'indices' => [], + 'indices' => [], ], ], - 'Simple config' => [ + 'Simple config' => [ [ - 'prefix' => 'sf_', + 'prefix' => 'sf_', 'nbResults' => 40, 'batchSize' => 100, ], [ - 'prefix' => 'sf_', - 'nbResults' => 40, - 'batchSize' => 100, - 'serializer' => 'serializer', + 'prefix' => 'sf_', + 'nbResults' => 40, + 'batchSize' => 100, + 'serializer' => 'serializer', 'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'], - 'indices' => [], + 'indices' => [], ], ], - 'Index config' => [ + 'Index config' => [ [ - 'prefix' => 'sf_', + 'prefix' => 'sf_', 'indices' => [ ['name' => 'posts', 'class' => 'App\Entity\Post', 'index_if' => null], [ - 'name' => 'tags', - 'class' => 'App\Entity\Tag', + 'name' => 'tags', + 'class' => 'App\Entity\Tag', 'enable_serializer_groups' => true, - 'index_if' => null + 'index_if' => null, ], ], ], [ - 'prefix' => 'sf_', - 'nbResults' => 20, - 'batchSize' => 500, - 'serializer' => 'serializer', + 'prefix' => 'sf_', + 'nbResults' => 20, + 'batchSize' => 500, + 'serializer' => 'serializer', 'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'], - 'indices' => [ + 'indices' => [ 'posts' => [ - 'class' => 'App\Entity\Post', + 'class' => 'App\Entity\Post', 'enable_serializer_groups' => false, - 'index_if' => null, - 'settings' => [] + 'index_if' => null, + 'settings' => [], ], - 'tags' => [ - 'class' => 'App\Entity\Tag', + 'tags' => [ + 'class' => 'App\Entity\Tag', 'enable_serializer_groups' => true, - 'index_if' => null, - 'settings' => [] + 'index_if' => null, + 'settings' => [], ], ], ], diff --git a/tests/TestCase/EngineTest.php b/tests/TestCase/EngineTest.php index ff96d274..34bc0510 100644 --- a/tests/TestCase/EngineTest.php +++ b/tests/TestCase/EngineTest.php @@ -1,30 +1,30 @@ engine = new Engine($this->get('search.client')); } + /** + * @throws ApiException + */ public function testIndexingEmptyEntity() { $searchableImage = $this->createSearchableImage(); @@ -44,7 +44,7 @@ public function testIndexingEmptyEntity() // Search try { $this->engine->search('query', $searchableImage->getIndexUid(), []); - } catch (Exception $e) { + } catch (\Exception $e) { $this->assertInstanceOf(ApiException::class, $e); } } diff --git a/tests/TestCase/SearchTest.php b/tests/TestCase/SearchTest.php index 93a8036e..9f33ac8b 100644 --- a/tests/TestCase/SearchTest.php +++ b/tests/TestCase/SearchTest.php @@ -1,12 +1,12 @@ searchService = $this->get('search.service'); - $this->client = $this->get('search.client'); + $this->client = $this->get('search.client'); $this->objectManager = $this->get('doctrine')->getManager(); - $this->connection = $this->get('doctrine')->getConnection(); - $this->platform = $this->connection->getDatabasePlatform(); - $this->indexName = 'posts'; - $this->index = $this->client->getOrCreateIndex($this->getPrefix() . $this->indexName); + $this->connection = $this->get('doctrine')->getConnection(); + $this->platform = $this->connection->getDatabasePlatform(); + $this->indexName = 'posts'; + $this->index = $this->client->getOrCreateIndex($this->getPrefix().$this->indexName); $this->application = new Application(self::$kernel); $this->refreshDb($this->application); @@ -82,6 +64,9 @@ public function cleanUp() } } + /** + * @throws \Doctrine\DBAL\Exception + */ public function testSearchImportAggregator() { $nbEntityIndexed = 0; @@ -90,39 +75,38 @@ public function testSearchImportAggregator() $this->connection->insert( $this->indexName, [ - 'title' => 'Test', - 'content' => 'Test content', + 'title' => 'Test', + 'content' => 'Test content', 'published_at' => $now->format('Y-m-d H:i:s'), ] ); - $nbEntityIndexed++; + ++$nbEntityIndexed; $this->connection->insert( $this->indexName, [ - 'title' => 'Test2', - 'content' => 'Test content2', + 'title' => 'Test2', + 'content' => 'Test content2', 'published_at' => $now->format('Y-m-d H:i:s'), ] ); - $nbEntityIndexed++; + ++$nbEntityIndexed; $this->connection->insert( $this->indexName, [ - 'title' => 'Test3', - 'content' => 'Test content3', + 'title' => 'Test3', + 'content' => 'Test content3', 'published_at' => $now->format('Y-m-d H:i:s'), ] ); - $nbEntityIndexed++; - + ++$nbEntityIndexed; - $command = $this->application->find('meili:import'); + $command = $this->application->find('meili:import'); $commandTester = new CommandTester($command); $commandTester->execute( [ - 'command' => $command->getName(), + 'command' => $command->getName(), '--indices' => 'contents', ] ); @@ -135,13 +119,13 @@ public function testSearchImportAggregator() // Test searchService $searchTerm = 'test'; $results = $this->searchService->search($this->objectManager, Post::class, $searchTerm); - $this->assertCount($nbEntityIndexed , $results); + $this->assertCount($nbEntityIndexed, $results); $results = $this->searchService->rawSearch(Post::class, $searchTerm); - $this->assertCount($nbEntityIndexed , $results['hits']); + $this->assertCount($nbEntityIndexed, $results['hits']); // clearup table - $this->connection->executeUpdate($this->platform->getTruncateTableSQL($this->indexName, true)); + $this->connection->executeStatement($this->platform->getTruncateTableSQL($this->indexName, true)); $this->cleanUp(); } } diff --git a/tests/TestCase/SerializationTest.php b/tests/TestCase/SerializationTest.php index 4925e6d8..edfb67d3 100644 --- a/tests/TestCase/SerializationTest.php +++ b/tests/TestCase/SerializationTest.php @@ -1,8 +1,9 @@ 12, - 'title' => 'a simple post', - 'content' => 'some text', + 'id' => 12, + 'title' => 'a simple post', + 'content' => 'some text', 'publishedAt' => $datetime, ] ); $post->addComment( new Comment( [ - 'content' => 'a great comment', + 'content' => 'a great comment', 'publishedAt' => $datetime, - 'post' => $post, + 'post' => $post, ] ) ); @@ -54,18 +52,20 @@ public function testSimpleEntityToSearchableArray() 'posts', $post, $postMeta, - $this->get('serializer') + $this->get('serializer'), + ['useSerializerGroup' => true] ); $expected = [ - 'id' => 12, - 'title' => 'a simple post', - 'content' => 'some text', + 'id' => 12, + 'title' => 'a simple post', + 'content' => 'some text', 'publishedAt' => $serializedDateTime, - 'comments' => [ + 'comments' => [ [ - 'content' => 'a great comment', - 'post_title' => 'a simple post', + 'id' => null, + 'content' => 'a great comment', + 'publishedAt' => $serializedDateTime, ], ], ]; diff --git a/tests/TestCase/SettingsTest.php b/tests/TestCase/SettingsTest.php index a6230a8c..c43a8963 100644 --- a/tests/TestCase/SettingsTest.php +++ b/tests/TestCase/SettingsTest.php @@ -1,8 +1,9 @@ searchService = $this->get('search.service'); - $this->client = $this->get('search.client'); - $this->indexName = 'posts'; + $this->client = $this->get('search.client'); + $this->indexName = 'posts'; $this->application = new Application(self::$kernel); $this->refreshDb($this->application); @@ -57,8 +48,8 @@ public function setUp(): void public function testGetDefaultSettings() { $primaryKey = 'ObjectID'; - $settingA = $this->client->getOrCreateIndex('indexA')->getSettings(); - $settingB = $this->client->getOrCreateIndex('indexB', ['primaryKey' => $primaryKey])->getSettings(); + $settingA = $this->client->getOrCreateIndex('indexA')->getSettings(); + $settingB = $this->client->getOrCreateIndex('indexB', ['primaryKey' => $primaryKey])->getSettings(); $this->assertEquals(self::DEFAULT_RANKING_RULES, $settingA['rankingRules']); $this->assertNull($settingA['distinctAttribute']); @@ -83,13 +74,13 @@ public function testGetDefaultSettings() public function testUpdateSettings() { - $command = $this->application->find('meili:import'); + $command = $this->application->find('meili:import'); $commandTester = new CommandTester($command); $commandTester->execute( [ - 'command' => $command->getName(), - '--indices' => 'posts', - '--update-settings' => true + 'command' => $command->getName(), + '--indices' => 'posts', + '--update-settings' => true, ] ); @@ -101,5 +92,4 @@ public function testUpdateSettings() $this->assertNotEmpty($settings['attributesForFaceting']); $this->assertEquals(['title', 'publishedAt'], $settings['attributesForFaceting']); } - } diff --git a/tests/config/services.yaml b/tests/config/services.yaml deleted file mode 100644 index ba863daf..00000000 --- a/tests/config/services.yaml +++ /dev/null @@ -1,17 +0,0 @@ -services: - search.search_indexer_subscriber: - class: MeiliSearch\Bundle\EventListener\MeiliSearchIndexerSubscriber - public: true - arguments: - - '@search.service' - # For some reason this parameter is not passed in the - # test environment (look at MeiliSearchExtension.php) - # So the service is redefined here - - ['postPersist', 'postUpdate', 'preRemove'] - tags: - - name: "doctrine.event_subscriber" - - name: "doctrine_mongodb.odm.event_subscriber" - - serializer.commentnormalizer: - class: MeiliSearch\Bundle\Test\Normalizer\CommentNormalizer - tags: [serializer.normalizer] \ No newline at end of file