From 2644f95ad8a5f4c6a067020d5150930d1f355230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Tue, 27 Apr 2021 13:08:38 +0200 Subject: [PATCH 01/12] General refactoring - Php version 7.4 or 8.0 - Use strict types - Set type hints - Remove unnecessary comments - Remove not needed service injections for tests --- .php_cs.dist | 15 ++- composer.json | 8 +- src/Command/IndexCommand.php | 13 +-- src/Command/MeiliSearchClearCommand.php | 6 +- src/Command/MeiliSearchImportCommand.php | 47 +-------- src/DependencyInjection/Configuration.php | 7 +- .../MeiliSearchExtension.php | 4 +- src/Document/Aggregator.php | 2 + src/Engine.php | 38 +------ src/Entity/Aggregator.php | 2 + .../MeiliSearchIndexerSubscriber.php | 19 ++-- src/Exception/EntityNotFoundInObjectID.php | 2 + src/Exception/InvalidEntityForAggregator.php | 2 + src/Exception/InvalidSettingName.php | 2 + src/Exception/SearchHitsNotFoundException.php | 2 + src/Exception/UpdateException.php | 2 + src/MeiliSearchBundle.php | 2 + src/Model/Aggregator.php | 30 ++---- src/SearchService.php | 60 ----------- src/Searchable.php | 2 + src/SearchableEntity.php | 16 +-- src/Services/MeiliSearchService.php | 99 +++---------------- src/Services/NullSearchService.php | 26 +---- tests/BaseTest.php | 40 ++------ tests/Kernel.php | 22 ++--- tests/Normalizer/CommentNormalizer.php | 15 +-- tests/config/services.yaml | 17 ---- 27 files changed, 105 insertions(+), 395 deletions(-) delete mode 100644 tests/config/services.yaml diff --git a/.php_cs.dist b/.php_cs.dist index ed824215..96d74539 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -4,9 +4,9 @@ if (!file_exists(__DIR__.'/src')) { exit(0); } -return PhpCsFixer\Config::create() - ->setRules( - [ +$finder = PhpCsFixer\Finder::create()->in(__DIR__.'/src')->append([__FILE__]); +$config = new PhpCsFixer\Config(); +$config->setRules([ '@Symfony' => true, '@Symfony:risky' => true, '@PHPUnit75Migration:risky' => true, @@ -26,8 +26,7 @@ return PhpCsFixer\Config::create() ] ) ->setRiskyAllowed(true) - ->setFinder( - PhpCsFixer\Finder::create() - ->in(__DIR__.'/src') - ->append([__FILE__]) - ); + ->setFinder($finder) +; + +return $config; 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/src/Command/IndexCommand.php b/src/Command/IndexCommand.php index 0e2ecffa..1c6fe187 100644 --- a/src/Command/IndexCommand.php +++ b/src/Command/IndexCommand.php @@ -1,5 +1,7 @@ searchService = $searchService; parent::__construct($name); diff --git a/src/Command/MeiliSearchClearCommand.php b/src/Command/MeiliSearchClearCommand.php index 7d8bcdbd..d28cefaa 100644 --- a/src/Command/MeiliSearchClearCommand.php +++ b/src/Command/MeiliSearchClearCommand.php @@ -1,5 +1,7 @@ searchClient = $searchClient; } - /** - * {@inheritdoc} - */ protected function configure() { $this @@ -72,12 +45,6 @@ protected function configure() ); } - /** - * {@inheritdoc} - * - * @throws TimeOutException - * @throws ApiException - */ protected function execute(InputInterface $input, OutputInterface $output): int { $entitiesToIndex = $this->getEntitiesFromArgs($input, $output); @@ -165,11 +132,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..7dfc4047 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..bc394523 100644 --- a/src/DependencyInjection/MeiliSearchExtension.php +++ b/src/DependencyInjection/MeiliSearchExtension.php @@ -1,5 +1,7 @@ load('services.xml'); diff --git a/src/Document/Aggregator.php b/src/Document/Aggregator.php index d4c797a8..3cb61f1f 100644 --- a/src/Document/Aggregator.php +++ b/src/Document/Aggregator.php @@ -1,5 +1,7 @@ client = $client; @@ -35,10 +28,7 @@ public function __construct(Client $client) * * @param array|SearchableEntity $searchableEntities * - * @return array - * * @throws ApiException - * @throws ExceptionInterface */ public function index($searchableEntities): array { @@ -78,9 +68,6 @@ public function index($searchableEntities): array * * @param array|SearchableEntity $searchableEntities * - * @return array - * - * @throws ExceptionInterface */ public function remove($searchableEntities): array { @@ -117,10 +104,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 $indexName - * - * @return array - * * @throws ApiException */ public function clear(string $indexName): array @@ -131,13 +114,6 @@ public function clear(string $indexName): array return $index->getUpdateStatus($return['updateId']); } - /** - * Delete an index and it's content. - * - * @param string $indexName - * - * @return array|null - */ public function delete(string $indexName): ?array { return $this->client->deleteIndex($indexName); @@ -145,12 +121,6 @@ public function delete(string $indexName): ?array /** * Method used for querying an index. - * - * @param string $query - * @param string $indexName - * @param array $searchParams - * - * @return array */ public function search(string $query, string $indexName, array $searchParams): array { @@ -163,12 +133,6 @@ public function search(string $query, string $indexName, array $searchParams): a /** * Search the index and returns the number of results. - * - * @param string $query - * @param string $indexName - * @param array $requestOptions - * - * @return int */ public function count(string $query, string $indexName, array $requestOptions): int { diff --git a/src/Entity/Aggregator.php b/src/Entity/Aggregator.php index c26138f2..f6fb4163 100644 --- a/src/Entity/Aggregator.php +++ b/src/Entity/Aggregator.php @@ -1,5 +1,7 @@ searchService = $searchService; @@ -48,6 +41,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..92e5ba2b 100644 --- a/src/Exception/EntityNotFoundInObjectID.php +++ b/src/Exception/EntityNotFoundInObjectID.php @@ -1,5 +1,7 @@ objectID = reset($entityIdentifierValues); + $this->objectID = (string) reset($entityIdentifierValues); } /** @@ -61,40 +59,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 b2c0f68a..0fb94dc5 100644 --- a/src/SearchService.php +++ b/src/SearchService.php @@ -13,68 +13,21 @@ interface SearchService { /** * @param string|object $className - * - * @return bool */ public function isSearchable($className): bool; - /** - * @return array - */ public function getSearchable(): array; - - /** - * @return array - */ public function getConfiguration(): array; /** * Get the index name for the given `$className`. - * - * @param string $className - * - * @return string */ public function searchableAs(string $className): string; - /** - * @param ObjectManager $objectManager - * @param $searchable - * - * @return array - */ public function index(ObjectManager $objectManager, $searchable): array; - - /** - * @param ObjectManager $objectManager - * @param $searchable - * - * @return array - */ public function remove(ObjectManager $objectManager, $searchable): array; - - /** - * @param string $className - * - * @return array - */ public function clear(string $className): array; - - /** - * @param string $className - * - * @return array|null - */ public function delete(string $className): ?array; - - /** - * @param ObjectManager $objectManager - * @param string $className - * @param string $query - * @param array $requestOptions - * - * @return array - */ public function search( ObjectManager $objectManager, string $className, @@ -86,12 +39,6 @@ public function search( * Get the raw search result. * * @see https://docs.meilisearch.com/reference/api/search.html#response - * - * @param string $className - * @param string $query - * @param array $searchParams - * - * @return array */ public function rawSearch( string $className, @@ -99,12 +46,5 @@ public function rawSearch( array $searchParams = [] ): array; - /** - * @param string $className - * @param string $query - * @param array $requestOptions - * - * @return int - */ public function count(string $className, string $query = '', array $requestOptions = []): int; } diff --git a/src/Searchable.php b/src/Searchable.php index 704da763..b6ae0048 100644 --- a/src/Searchable.php +++ b/src/Searchable.php @@ -1,5 +1,7 @@ entityMetadata->getIdentifierValues($this->entity); diff --git a/src/Services/MeiliSearchService.php b/src/Services/MeiliSearchService.php index c74daab1..33b1d4c6 100644 --- a/src/Services/MeiliSearchService.php +++ b/src/Services/MeiliSearchService.php @@ -1,5 +1,7 @@ normalizer = $normalizer; @@ -98,17 +62,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 +80,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 +113,6 @@ function ($chunk) { ); } - /** - * {@inheritdoc} - */ public function remove(ObjectManager $objectManager, $searchable): array { $searchable = is_array($searchable) ? $searchable : [$searchable]; @@ -182,9 +134,6 @@ function ($chunk) { ); } - /** - * {@inheritdoc} - */ public function clear(string $className): array { $this->assertIsSearchable($className); @@ -192,9 +141,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 +148,6 @@ public function delete(string $className): ?array return $this->engine->delete($this->searchableAs($className)); } - /** - * {@inheritdoc} - */ public function search( ObjectManager $objectManager, string $className, @@ -223,8 +166,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 +197,6 @@ public function rawSearch( return $this->engine->search($query, $this->searchableAs($className), $searchParams); } - /** - * {@inheritdoc} - */ public function count(string $className, string $query = '', array $requestOptions = []): int { $this->assertIsSearchable($className); @@ -266,8 +206,6 @@ public function count(string $className, string $query = '', array $requestOptio /** * @param object $entity - * - * @return bool */ public function shouldBeIndexed($entity): bool { @@ -299,7 +237,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])) { @@ -413,11 +351,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 919b3daf..a2108cfa 100644 --- a/src/Services/NullSearchService.php +++ b/src/Services/NullSearchService.php @@ -1,5 +1,7 @@ 200]; @@ -45,41 +41,26 @@ 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, @@ -100,9 +81,6 @@ public function rawSearch( return []; } - /** - * {@inheritdoc} - */ public function count(string $className, string $query = '', array $requestOptions = []): int { return 0; diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 9f83427c..4807b83e 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -12,34 +12,14 @@ use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\ConsoleOutput; -use function class_exists; -use function getenv; -use function is_null; -use function rand; -use function sprintf; - /** - * Class BaseTest + * Class BaseTest. * * @package MeiliSearch\Bundle */ class BaseTest extends KernelTestCase { - - public static function setUpBeforeClass(): void - { - /* - * Older version of PHPUnit (<6.0) load - * env variables differently, we override them - * here to make sure they're coming from the - * env rather than the XML config - */ - if (class_exists('\PHPUnit_Runner_Version')) { - $_ENV['MEILISEARCH_PREFIX'] = getenv('MEILISEARCH_PREFIX'); - } - } - - public function setUp(): void + protected function setUp(): void { $this->bootKernel(); } @@ -55,7 +35,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 +47,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') @@ -85,7 +65,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); } @@ -101,7 +81,7 @@ protected function createImage($id = null): Image { $image = new Image(); - if (!is_null($id)) { + if (null !== $id) { $image->setId($id); } @@ -113,7 +93,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 @@ -140,10 +120,10 @@ 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/Kernel.php b/tests/Kernel.php index e044db09..cd0a3978 100644 --- a/tests/Kernel.php +++ b/tests/Kernel.php @@ -2,40 +2,30 @@ namespace MeiliSearch\Bundle\Test; -use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; use MeiliSearch\Bundle\MeiliSearchBundle; use Symfony\Bundle\FrameworkBundle\FrameworkBundle; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\HttpKernel\Kernel as HttpKernel; /** - * Class Kernel + * Class Kernel. * * @package MeiliSearch\Bundle */ class Kernel extends HttpKernel { - - /** - * @inheritDoc - */ - public function registerBundles() + public function registerBundles(): array { return [ new FrameworkBundle(), - new DoctrineBundle(), new MeiliSearchBundle(), ]; } - /** - * @inheritDoc - */ - public function registerContainerConfiguration(LoaderInterface $loader) + public function registerContainerConfiguration(LoaderInterface $loader): void { - $loader->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..62d96b7d 100644 --- a/tests/Normalizer/CommentNormalizer.php +++ b/tests/Normalizer/CommentNormalizer.php @@ -6,28 +6,21 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface; /** - * Class CommentNormalizer + * Class CommentNormalizer. * * @package MeiliSearch\Bundle\Test\Normalizer */ class CommentNormalizer implements NormalizerInterface { - - /** - * @inheritDoc - */ - public function normalize($object, $format = null, array $context = []) + public function normalize($object, $format = null, array $context = []): array { return [ - 'content' => $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/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 From 4764a2fee1d994c0a19c5608a41e8bdec24d4402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Tue, 27 Apr 2021 17:27:31 +0200 Subject: [PATCH 02/12] Code style to me more inline with Symphony and meilisearch-php package --- .php_cs.dist | 19 ++++-------- src/Command/IndexCommand.php | 11 ++----- src/Command/MeiliSearchClearCommand.php | 2 -- src/Command/MeiliSearchImportCommand.php | 2 -- src/DependencyInjection/Configuration.php | 2 -- .../MeiliSearchExtension.php | 8 ++--- src/Document/Aggregator.php | 2 -- src/Engine.php | 14 ++++----- src/Entity/Aggregator.php | 2 -- .../MeiliSearchIndexerSubscriber.php | 6 ++-- src/Exception/EntityNotFoundInObjectID.php | 6 +--- src/Exception/InvalidEntityForAggregator.php | 6 +--- src/Exception/InvalidSettingName.php | 6 +--- src/Exception/SearchHitsNotFoundException.php | 6 +--- src/Exception/UpdateException.php | 6 +--- src/MeiliSearchBundle.php | 2 -- src/Model/Aggregator.php | 3 -- src/SearchService.php | 9 ++++-- src/Searchable.php | 2 -- src/SearchableEntity.php | 31 +++++++------------ src/Services/MeiliSearchService.php | 27 ++++++---------- src/Services/NullSearchService.php | 5 +-- 22 files changed, 54 insertions(+), 123 deletions(-) diff --git a/.php_cs.dist b/.php_cs.dist index 96d74539..3c8814b7 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -1,5 +1,7 @@ in(__DIR__.'/src')->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) diff --git a/src/Command/IndexCommand.php b/src/Command/IndexCommand.php index 1c6fe187..2b899cd7 100644 --- a/src/Command/IndexCommand.php +++ b/src/Command/IndexCommand.php @@ -8,14 +8,9 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use function array_keys; -use function count; -use function explode; /** * Class IndexCommand. - * - * @package MeiliSearch\Bundle\Command */ abstract class IndexCommand extends Command { @@ -33,13 +28,13 @@ protected function getEntitiesFromArgs(InputInterface $input, OutputInterface $o $indexNames = []; if ($indexList = $input->getOption('indices')) { - $indexNames = explode(',', $indexList); + $indexNames = \explode(',', $indexList); } $config = $this->searchService->getConfiguration(); - if (0 === count($indexNames)) { - $indexNames = array_keys($config['indices']); + if (0 === \count($indexNames)) { + $indexNames = \array_keys($config['indices']); } foreach ($indexNames as $name) { diff --git a/src/Command/MeiliSearchClearCommand.php b/src/Command/MeiliSearchClearCommand.php index d28cefaa..979eba86 100644 --- a/src/Command/MeiliSearchClearCommand.php +++ b/src/Command/MeiliSearchClearCommand.php @@ -10,8 +10,6 @@ /** * Class MeiliSearchClearCommand. - * - * @package MeiliSearch\Bundle\Command */ final class MeiliSearchClearCommand extends IndexCommand { diff --git a/src/Command/MeiliSearchImportCommand.php b/src/Command/MeiliSearchImportCommand.php index 2e96b3f1..09bfde99 100644 --- a/src/Command/MeiliSearchImportCommand.php +++ b/src/Command/MeiliSearchImportCommand.php @@ -16,8 +16,6 @@ /** * Class MeiliSearchImportCommand. - * - * @package MeiliSearch\Bundle\Command */ final class MeiliSearchImportCommand extends IndexCommand { diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 7dfc4047..ce7d1f16 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -9,8 +9,6 @@ /** * Class Configuration. - * - * @package MeiliSearch\Bundle\DependencyInjection */ final class Configuration implements ConfigurationInterface { diff --git a/src/DependencyInjection/MeiliSearchExtension.php b/src/DependencyInjection/MeiliSearchExtension.php index bc394523..b68f860c 100644 --- a/src/DependencyInjection/MeiliSearchExtension.php +++ b/src/DependencyInjection/MeiliSearchExtension.php @@ -12,13 +12,9 @@ use Symfony\Component\DependencyInjection\Loader; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpKernel\DependencyInjection\Extension; -use function count; -use function dirname; /** * Class MeiliSearchExtension. - * - * @package MeiliSearch\Bundle\DependencyInjection */ final class MeiliSearchExtension extends Extension { @@ -27,7 +23,7 @@ final class MeiliSearchExtension extends Extension */ public function load(array $configs, ContainerBuilder $container): void { - $loader = new Loader\XmlFileLoader($container, new FileLocator(dirname(__DIR__).'/Resources/config')); + $loader = new Loader\XmlFileLoader($container, new FileLocator(\dirname(__DIR__).'/Resources/config')); $loader->load('services.xml'); $configuration = new Configuration(); @@ -37,7 +33,7 @@ public function load(array $configs, ContainerBuilder $container): void $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 3cb61f1f..f75b613b 100644 --- a/src/Document/Aggregator.php +++ b/src/Document/Aggregator.php @@ -8,8 +8,6 @@ /** * Class Aggregator. - * - * @package MeiliSearch\Bundle\Document */ abstract class Aggregator extends BaseAggregator { diff --git a/src/Engine.php b/src/Engine.php index bdc797f7..12705de6 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -1,16 +1,15 @@ getSearchableArray(); - if (null === $searchableArray || 0 === count($searchableArray)) { + if (null === $searchableArray || 0 === \count($searchableArray)) { continue; } @@ -67,7 +66,6 @@ public function index($searchableEntities): array * This method enables you to remove one or more objects from an index. * * @param array|SearchableEntity $searchableEntities - * */ public function remove($searchableEntities): array { @@ -78,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; } $indexName = $entity->getIndexName(); @@ -94,7 +92,7 @@ public function remove($searchableEntities): array foreach ($data as $indexName => $objects) { $result[$indexName] = $this->client ->index($indexName) - ->deleteDocument(reset($objects)); + ->deleteDocument(\reset($objects)); } return $result; diff --git a/src/Entity/Aggregator.php b/src/Entity/Aggregator.php index f6fb4163..73b0313b 100644 --- a/src/Entity/Aggregator.php +++ b/src/Entity/Aggregator.php @@ -8,8 +8,6 @@ /** * Class Aggregator. - * - * @package MeiliSearch\Bundle\Entity */ abstract class Aggregator extends BaseAggregator { diff --git a/src/EventListener/MeiliSearchIndexerSubscriber.php b/src/EventListener/MeiliSearchIndexerSubscriber.php index 0390c144..52e275b3 100644 --- a/src/EventListener/MeiliSearchIndexerSubscriber.php +++ b/src/EventListener/MeiliSearchIndexerSubscriber.php @@ -10,13 +10,11 @@ /** * Class MeiliSearchIndexerSubscriber. - * - * @package MeiliSearch\Bundle\EventListener */ final class MeiliSearchIndexerSubscriber implements EventSubscriber { - protected SearchService $searchService; - protected array $subscribedEvents; + private SearchService $searchService; + private array $subscribedEvents; public function __construct(SearchService $searchService, array $subscribedEvents) { diff --git a/src/Exception/EntityNotFoundInObjectID.php b/src/Exception/EntityNotFoundInObjectID.php index 92e5ba2b..fe8d4d07 100644 --- a/src/Exception/EntityNotFoundInObjectID.php +++ b/src/Exception/EntityNotFoundInObjectID.php @@ -4,13 +4,9 @@ namespace MeiliSearch\Bundle\Exception; -use LogicException; - /** * Class EntityNotFoundInObjectID. - * - * @package MeiliSearch\Bundle\Exception */ -final class EntityNotFoundInObjectID extends LogicException +final class EntityNotFoundInObjectID extends \LogicException { } diff --git a/src/Exception/InvalidEntityForAggregator.php b/src/Exception/InvalidEntityForAggregator.php index 4444a5ab..8870ae5c 100644 --- a/src/Exception/InvalidEntityForAggregator.php +++ b/src/Exception/InvalidEntityForAggregator.php @@ -4,13 +4,9 @@ namespace MeiliSearch\Bundle\Exception; -use LogicException; - /** * Class InvalidEntityForAggregator. - * - * @package MeiliSearch\Bundle\Exception */ -final class InvalidEntityForAggregator extends LogicException +final class InvalidEntityForAggregator extends \LogicException { } diff --git a/src/Exception/InvalidSettingName.php b/src/Exception/InvalidSettingName.php index 6afe0179..15e7b67b 100644 --- a/src/Exception/InvalidSettingName.php +++ b/src/Exception/InvalidSettingName.php @@ -4,13 +4,9 @@ namespace MeiliSearch\Bundle\Exception; -use LogicException; - /** * Class InvalidSettingName. - * - * @package MeiliSearch\Bundle\Exception */ -final class InvalidSettingName extends LogicException +final class InvalidSettingName extends \LogicException { } diff --git a/src/Exception/SearchHitsNotFoundException.php b/src/Exception/SearchHitsNotFoundException.php index 730a470a..a0a760cf 100644 --- a/src/Exception/SearchHitsNotFoundException.php +++ b/src/Exception/SearchHitsNotFoundException.php @@ -4,13 +4,9 @@ namespace MeiliSearch\Bundle\Exception; -use LogicException; - /** * Class SearchHitsNotFoundException. - * - * @package MeiliSearch\Bundle\Exception */ -final class SearchHitsNotFoundException extends LogicException +final class SearchHitsNotFoundException extends \LogicException { } diff --git a/src/Exception/UpdateException.php b/src/Exception/UpdateException.php index b543c3ba..ef887f69 100644 --- a/src/Exception/UpdateException.php +++ b/src/Exception/UpdateException.php @@ -4,13 +4,9 @@ namespace MeiliSearch\Bundle\Exception; -use LogicException; - /** * Class UpdateException. - * - * @package MeiliSearch\Bundle\Exception */ -final class UpdateException extends LogicException +final class UpdateException extends \LogicException { } diff --git a/src/MeiliSearchBundle.php b/src/MeiliSearchBundle.php index 45dd16ff..0d1db942 100644 --- a/src/MeiliSearchBundle.php +++ b/src/MeiliSearchBundle.php @@ -8,8 +8,6 @@ /** * Class MeiliSearchBundle. - * - * @package MeiliSearch */ final class MeiliSearchBundle extends Bundle { diff --git a/src/Model/Aggregator.php b/src/Model/Aggregator.php index 30ccbcbd..ab090dd9 100644 --- a/src/Model/Aggregator.php +++ b/src/Model/Aggregator.php @@ -11,8 +11,6 @@ /** * Class Aggregator. - * - * @package MeiliSearch\Bundle\Model */ abstract class Aggregator implements NormalizableInterface { @@ -34,7 +32,6 @@ abstract class Aggregator implements NormalizableInterface * Aggregator constructor. * * @param object $entity - * @param array $entityIdentifierValues */ public function __construct($entity, array $entityIdentifierValues) { diff --git a/src/SearchService.php b/src/SearchService.php index 0fb94dc5..13781aa3 100644 --- a/src/SearchService.php +++ b/src/SearchService.php @@ -1,13 +1,13 @@ setId(); } - /** - * @return string - */ public function getIndexName(): string { return $this->indexName; @@ -88,11 +81,11 @@ 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 33b1d4c6..f3157c06 100644 --- a/src/Services/MeiliSearchService.php +++ b/src/Services/MeiliSearchService.php @@ -18,22 +18,20 @@ /** * Class MeiliSearchService. - * - * @package MeiliSearch\Bundle\Services */ final class MeiliSearchService implements SearchService { - protected SerializerInterface $normalizer; - protected Engine $engine; - protected array $configuration; + private SerializerInterface $normalizer; + private Engine $engine; + private array $configuration; private PropertyAccessor $propertyAccessor; - protected array $searchableEntities; - protected array $entitiesAggregators; - protected array $aggregators; - protected array $classToIndexMapping; - protected array $classToSerializerGroupMapping; - protected array $indexIfMapping; - protected array $settingsMapping; + private array $searchableEntities; + private array $entitiesAggregators; + private array $aggregators; + private array $classToIndexMapping; + private array $classToSerializerGroupMapping; + private array $indexIfMapping; + private array $settingsMapping; public function __construct(SerializerInterface $normalizer, Engine $engine, array $configuration) { @@ -292,7 +290,6 @@ private function setSettingsMapping(): void /** * Returns the aggregators instances of the provided entities. * - * @param ObjectManager $objectManager * @param array $entities * * @return array @@ -319,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, diff --git a/src/Services/NullSearchService.php b/src/Services/NullSearchService.php index a2108cfa..33ae0b97 100644 --- a/src/Services/NullSearchService.php +++ b/src/Services/NullSearchService.php @@ -6,12 +6,9 @@ use Doctrine\Persistence\ObjectManager; use MeiliSearch\Bundle\SearchService; -use stdClass; /** * Class NullSearchService. - * - * @package MeiliSearch\Bundle\Services */ class NullSearchService implements SearchService { @@ -67,7 +64,7 @@ public function search( string $query = '', array $requestOptions = [] ): array { - return [new stdClass()]; + return [new \stdClass()]; } /** From 5f4ff12e08f8dac33c167f5efbc59b937dcb4dab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Tue, 27 Apr 2021 17:29:06 +0200 Subject: [PATCH 03/12] Remove php 7.2 and 7.3 from CI --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From e3b5a6c057027a386fdb8a7280125260905f489a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Tue, 27 Apr 2021 23:50:18 +0200 Subject: [PATCH 04/12] Use php 7.4 for Docker image as minimum php version is bumped --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 \ From e1b4d2fd5f77f836af782bf4c16e58af7196d70b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Wed, 28 Apr 2021 12:10:03 +0200 Subject: [PATCH 05/12] Fixes linter --- src/SearchableEntity.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/SearchableEntity.php b/src/SearchableEntity.php index 74cc78a5..15383c0c 100644 --- a/src/SearchableEntity.php +++ b/src/SearchableEntity.php @@ -15,7 +15,7 @@ */ final class SearchableEntity { - protected string $indexUid; + private string $indexUid; /** @var object */ private $entity; @@ -33,11 +33,8 @@ final class SearchableEntity /** * SearchableEntity constructor. * - * @param string $indexUid - * @param object $entity - * @param ClassMetadata $entityMetadata - * @param object|null $normalizer - * @param array $extra + * @param object $entity + * @param object|null $normalizer */ public function __construct( string $indexUid, From e74bde894ec632103eb56b5132e673c7a57e0b4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Wed, 28 Apr 2021 12:19:11 +0200 Subject: [PATCH 06/12] Add missing doctrine bundle for tests --- tests/Kernel.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Kernel.php b/tests/Kernel.php index cd0a3978..2a5f8bce 100644 --- a/tests/Kernel.php +++ b/tests/Kernel.php @@ -2,6 +2,7 @@ namespace MeiliSearch\Bundle\Test; +use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; use MeiliSearch\Bundle\MeiliSearchBundle; use Symfony\Bundle\FrameworkBundle\FrameworkBundle; use Symfony\Component\Config\Loader\LoaderInterface; @@ -18,6 +19,7 @@ public function registerBundles(): array { return [ new FrameworkBundle(), + new DoctrineBundle(), new MeiliSearchBundle(), ]; } From afb46cdda2b38827d4baf920462a3976c792c9ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Wed, 28 Apr 2021 12:20:55 +0200 Subject: [PATCH 07/12] Fix wrong path --- tests/Kernel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Kernel.php b/tests/Kernel.php index 2a5f8bce..ba7f20e1 100644 --- a/tests/Kernel.php +++ b/tests/Kernel.php @@ -27,7 +27,7 @@ public function registerBundles(): array public function registerContainerConfiguration(LoaderInterface $loader): void { $loader->load(__DIR__.'/config/config.yaml'); - $loader->load(__DIR__.'/src/Resources/config/services.xml'); + $loader->load(__DIR__.'/../src/Resources/config/services.xml'); $loader->load(__DIR__.'/config/meili_search.yaml'); } } From 6eee9214c26298c54c403421b2e559d91331c0c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Wed, 28 Apr 2021 13:40:32 +0200 Subject: [PATCH 08/12] Code style for tests folder --- .php_cs.dist | 5 +- tests/BaseTest.php | 15 +---- tests/Entity/Comment.php | 27 ++++---- tests/Entity/ContentAggregator.php | 5 +- tests/Entity/EmptyAggregator.php | 3 +- tests/Entity/Image.php | 15 ++--- tests/Entity/Link.php | 24 ++++--- tests/Entity/Post.php | 36 +++++------ tests/Entity/Tag.php | 36 +++++------ tests/Kernel.php | 4 +- tests/Normalizer/CommentNormalizer.php | 4 +- tests/TestCase/AggregatorTest.php | 13 ++-- tests/TestCase/CommandsTest.php | 82 ++++++++++-------------- tests/TestCase/ConfigurationTest.php | 69 ++++++++++---------- tests/TestCase/EngineTest.php | 16 ++--- tests/TestCase/SearchTest.php | 88 +++++++++++--------------- tests/TestCase/SerializationTest.php | 30 ++++----- tests/TestCase/SettingsTest.php | 42 +++++------- 18 files changed, 223 insertions(+), 291 deletions(-) diff --git a/.php_cs.dist b/.php_cs.dist index 3c8814b7..bc649fc7 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -6,7 +6,10 @@ if (!file_exists(__DIR__.'/src')) { exit(0); } -$finder = PhpCsFixer\Finder::create()->in(__DIR__.'/src')->append([__FILE__]); +$finder = PhpCsFixer\Finder::create() + ->in(__DIR__.'/src') + ->in(__DIR__.'/tests') + ->append([__FILE__]); $config = new PhpCsFixer\Config(); $config->setRules([ '@Symfony' => true, diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 4807b83e..f9342bc2 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -1,8 +1,9 @@ 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 +78,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..33aa95fb 100644 --- a/tests/Entity/Post.php +++ b/tests/Entity/Post.php @@ -1,8 +1,9 @@ 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,12 +105,12 @@ 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; 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 ba7f20e1..d7b07f06 100644 --- a/tests/Kernel.php +++ b/tests/Kernel.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..9d2a1f14 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, ] ) ); @@ -58,13 +56,13 @@ public function testSimpleEntityToSearchableArray() ); $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', + 'content' => 'a great comment', 'post_title' => 'a simple post', ], ], 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']); } - } From be1942571c9f578d3b664c726d6095d02aee12ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Wed, 28 Apr 2021 13:52:59 +0200 Subject: [PATCH 09/12] Explicitly define the properties to be searchable Circular reference occurring out of nowhere :( --- tests/Entity/Comment.php | 5 ++++- tests/Entity/Post.php | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/Entity/Comment.php b/tests/Entity/Comment.php index 434c2d82..06be7f0c 100644 --- a/tests/Entity/Comment.php +++ b/tests/Entity/Comment.php @@ -4,8 +4,8 @@ namespace MeiliSearch\Bundle\Test\Entity; -use DateTime; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Serializer\Annotation\Groups; /** * @ORM\Entity @@ -17,6 +17,7 @@ class Comment * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer", nullable=true) + * @Groups({"searchable"}) */ private ?int $id = null; @@ -34,12 +35,14 @@ class Comment * max=10000, * maxMessage="comment.too_long" * ) + * @Groups({"searchable"}) */ private $content; /** * @var \DateTime * @ORM\Column(type="datetime") + * @Groups({"searchable"}) */ private $publishedAt; diff --git a/tests/Entity/Post.php b/tests/Entity/Post.php index 33aa95fb..abac6fee 100644 --- a/tests/Entity/Post.php +++ b/tests/Entity/Post.php @@ -32,11 +32,13 @@ class Post /** * @ORM\Column(type="text", nullable=true) + * @Groups({"searchable"}) */ private ?string $content = null; /** * @ORM\Column(type="datetime") + * @Groups({"searchable"}) */ private ?\DateTime $publishedAt = null; From 7cf6a7891d6fdc1881fc88a6c4b6717e509d81b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Wed, 28 Apr 2021 14:07:57 +0200 Subject: [PATCH 10/12] Remove deprecated option for composer --- docker-entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From b259a8e8db3823abb35a6b03f9c72253a42dab9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Wed, 28 Apr 2021 15:32:15 +0200 Subject: [PATCH 11/12] Fix serialization tests --- tests/Entity/Post.php | 6 ++++-- tests/TestCase/SerializationTest.php | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/Entity/Post.php b/tests/Entity/Post.php index abac6fee..422342ab 100644 --- a/tests/Entity/Post.php +++ b/tests/Entity/Post.php @@ -5,6 +5,7 @@ namespace MeiliSearch\Bundle\Test\Entity; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation\Groups; @@ -43,13 +44,14 @@ class Post private ?\DateTime $publishedAt = null; /** - * @var Comment[]|ArrayCollection + * @var Comment[]|Collection * @ORM\OneToMany( * targetEntity="Comment", * mappedBy="post", * orphanRemoval=true * ) * @ORM\OrderBy({"publishedAt": "DESC"}) + * @Groups({"searchable"}) */ private $comments; @@ -119,7 +121,7 @@ public function setPublishedAt(?\DateTime $publishedAt): Post return $this; } - public function getComments(): ?ArrayCollection + public function getComments(): ?Collection { return $this->comments; } diff --git a/tests/TestCase/SerializationTest.php b/tests/TestCase/SerializationTest.php index 9d2a1f14..edfb67d3 100644 --- a/tests/TestCase/SerializationTest.php +++ b/tests/TestCase/SerializationTest.php @@ -52,7 +52,8 @@ public function testSimpleEntityToSearchableArray() 'posts', $post, $postMeta, - $this->get('serializer') + $this->get('serializer'), + ['useSerializerGroup' => true] ); $expected = [ @@ -62,8 +63,9 @@ public function testSimpleEntityToSearchableArray() 'publishedAt' => $serializedDateTime, 'comments' => [ [ + 'id' => null, 'content' => 'a great comment', - 'post_title' => 'a simple post', + 'publishedAt' => $serializedDateTime, ], ], ]; From 69c67453344836b750c077eea9555847be8799a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Tue, 11 May 2021 20:28:37 +0200 Subject: [PATCH 12/12] Remove integration tests --- bors.toml | 2 -- 1 file changed, 2 deletions(-) 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'