Skip to content

Commit

Permalink
Update Doctrine components and add Symfony 5.2 support (#355)
Browse files Browse the repository at this point in the history
* Update Doctrine components and add Symfony 5.2 support

- Add Symfony 5.2 support (requires `symfony/doctrine-bridge:5.2`-> `doctrine/persistence:^2`)
- Migrate from `doctrine/common` (deprecated) to `doctrine/*` components
- Dropping support for PHP < 7.1 (`doctrine/event-manager` and `doctrine/persistence`)

* Remove unused symfony/doctrine-bridge

* Remove PHP < 7.1 support

* Remove unused condition on CircleCI coverage test

Exact same command on PHP >= and < 7.2

* Add tests for ClassInfo util class

* Bump PHP minimum version to 7.2

* Bump PHPUnit version to >=8.5 to support PHP >= 7.2

* Restriction of cleaning to used items

Close #355 (comment)

* Adding tests for (deprecated) Doctrine Proxy Manager

Close #355 (comment)
  • Loading branch information
guillaume-ro-fr authored Mar 18, 2021
1 parent a26bf6e commit ffd296f
Show file tree
Hide file tree
Showing 28 changed files with 377 additions and 242 deletions.
6 changes: 2 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,12 @@ jobs:
- run:
name: Check coverage
command: |
if (( $(echo "<<parameters.version>> >= 7.2" |bc -l) )); then php coverage-checker.php clover.xml 100; fi
if (( $(echo "<<parameters.version>> < 7.2" |bc -l) )); then php coverage-checker.php clover.xml 100; fi
command: php coverage-checker.php clover.xml 100

workflows:
workflow:
jobs:
- test:
matrix:
parameters:
version: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4']
version: ['7.2', '7.3', '7.4', '8.0']
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
],
"prefer-stable": true,
"require": {
"php": "^5.6 || ^7.0",
"php": "^7.2 || ^8.0",
"algolia/algoliasearch-client-php": "^2.5",
"doctrine/common": "^2.5",
"doctrine/event-manager": "^1.1",
"doctrine/persistence": "^2.1",
"symfony/filesystem": "^3.4 || ^4.0 || ^5.0",
"symfony/property-access": "^3.4 || ^4.0 || ^5.0",
"symfony/serializer": "^3.4 || ^4.0 || ^5.0"
Expand All @@ -31,12 +32,12 @@
}
},
"require-dev": {
"ext-json": "*",
"doctrine/doctrine-bundle": "^1.8 || ^2.0",
"doctrine/orm": "^2.5",
"jms/serializer-bundle": "^2.3 || ^3.0",
"jms/serializer-bundle": "^3.0",
"ocramius/proxy-manager": "*",
"phpunit/phpunit": "^5.7.21 || ^6.4 || ^7.0",
"symfony/doctrine-bridge": "^3.4 || ^4.0 || ^5.0",
"phpunit/phpunit": "^8.5 || ^9.0",
"symfony/framework-bundle": "^3.4 || ^4.0 || ^5.0",
"symfony/phpunit-bridge": "^3.4 || ^4.0 || ^5.0",
"symfony/proxy-manager-bridge": "*",
Expand Down
2 changes: 1 addition & 1 deletion src/Command/SearchImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Algolia\AlgoliaSearch\SearchClient;
use Algolia\SearchBundle\Entity\Aggregator;
use Algolia\SearchBundle\SearchService;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/SearchIndexerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Algolia\SearchBundle\SearchService;
use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\Persistence\Event\LifecycleEventArgs;

/**
* @internal
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Aggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Algolia\SearchBundle\Exception\EntityNotFoundInObjectID;
use Algolia\SearchBundle\Exception\InvalidEntityForAggregator;
use Doctrine\Common\Util\ClassUtils;
use Algolia\SearchBundle\Util\ClassInfo;
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

Expand Down Expand Up @@ -39,7 +39,7 @@ public function __construct($entity, array $entityIdentifierValues)
throw new InvalidEntityForAggregator("Aggregators don't support more than one primary key.");
}

$this->objectID = ClassUtils::getClass($this->entity) . '::' . reset($entityIdentifierValues);
$this->objectID = ClassInfo::getClass($this->entity) . '::' . reset($entityIdentifierValues);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Algolia\SearchBundle;

use Algolia\AlgoliaSearch\RequestOptions\RequestOptions;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectManager;

interface SearchService
{
Expand Down
12 changes: 6 additions & 6 deletions src/Services/AlgoliaSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Algolia\SearchBundle\Responses\SearchServiceResponse;
use Algolia\SearchBundle\SearchableEntity;
use Algolia\SearchBundle\SearchService;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Util\ClassUtils;
use Algolia\SearchBundle\Util\ClassInfo;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\PropertyAccess\PropertyAccess;

Expand Down Expand Up @@ -91,7 +91,7 @@ public function __construct($normalizer, Engine $engine, array $configuration)
public function isSearchable($className)
{
if (is_object($className)) {
$className = ClassUtils::getClass($className);
$className = ClassInfo::getClass($className);
}

return in_array($className, $this->searchableEntities, true);
Expand Down Expand Up @@ -277,7 +277,7 @@ public function count($className, $query = '', $requestOptions = [])
*/
public function shouldBeIndexed($entity)
{
$className = ClassUtils::getClass($entity);
$className = ClassInfo::getClass($entity);
$propertyPath = $this->indexIfMapping[$className];

if ($propertyPath !== null) {
Expand Down Expand Up @@ -404,7 +404,7 @@ private function makeSearchServiceResponseFrom(ObjectManager $objectManager, arr
foreach (array_chunk($entities, $this->configuration['batchSize']) as $chunk) {
$searchableEntitiesChunk = [];
foreach ($chunk as $entity) {
$entityClassName = ClassUtils::getClass($entity);
$entityClassName = ClassInfo::getClass($entity);

$searchableEntitiesChunk[] = new SearchableEntity(
$this->searchableAs($entityClassName),
Expand Down Expand Up @@ -433,7 +433,7 @@ private function getAggregatorsFromEntities(ObjectManager $objectManager, array
$aggregators = [];

foreach ($entities as $entity) {
$entityClassName = ClassUtils::getClass($entity);
$entityClassName = ClassInfo::getClass($entity);
if (array_key_exists($entityClassName, $this->entitiesAggregators)) {
foreach ($this->entitiesAggregators[$entityClassName] as $aggregator) {
$aggregators[] = new $aggregator($entity, $objectManager->getClassMetadata($entityClassName)->getIdentifierValues($entity));
Expand Down
2 changes: 1 addition & 1 deletion src/Services/NullSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Algolia\AlgoliaSearch\RequestOptions\RequestOptions;
use Algolia\AlgoliaSearch\Response\NullResponse;
use Algolia\SearchBundle\SearchService;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectManager;

/**
* This class aims to be used in dev or testing environments. It may
Expand Down
61 changes: 61 additions & 0 deletions src/Util/ClassInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Algolia\SearchBundle\Util;

/**
* Retrieves information about a class.
*
* @internal
*/
final class ClassInfo
{
/**
* Get class name of the given object.
*
* @param object $object
*
* @return string
*/
public static function getClass($object)
{
return self::getRealClassName(get_class($object));
}

/**
* Get the real class name of a class name that could be a proxy.
*
* @param string $className
*
* @return string
*/
public static function getRealClassName($className)
{
// Define variable for static analysis
$positionPm = false;
// __CG__: Doctrine Common Marker for Proxy (ODM < 2.0 and ORM < 3.0)
// __PM__: Ocramius Proxy Manager (ODM >= 2.0)
if ((false === $positionCg = strrpos($className, '\\__CG__\\')) &&
(false === $positionPm = strrpos($className, '\\__PM__\\'))) {
return $className;
}

if (false !== $positionCg) {
return substr($className, $positionCg + 8);
}

$className = ltrim($className, '\\');

return substr(
$className,
8 + $positionPm,
strrpos($className, '\\') - ($positionPm + 8)
);
}

/**
* @codeCoverageIgnore
*/
private function __construct()
{
}
}
50 changes: 20 additions & 30 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,26 @@

class BaseTest extends KernelTestCase
{
public static function setUpBeforeClass()
public function setUp(): 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['ALGOLIA_PREFIX'] = getenv('ALGOLIA_PREFIX');
$_ENV['CIRCLE_BUILD_NUM'] = getenv('CIRCLE_BUILD_NUM');
}
}

public function setUp()
{
$this->bootKernel();
self::bootKernel();
}

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

if (!is_null($id)) {
$post->setId($id);
}

return $post;
}

protected function createSearchablePost()
protected function createSearchablePost(): SearchableEntity
{
$post = $this->createPost(rand(100, 300));
$post = $this->createPost(random_int(100, 300));

return new SearchableEntity(
$this->getPrefix() . 'posts',
Expand All @@ -55,7 +40,7 @@ protected function createSearchablePost()
);
}

protected function createComment($id = null)
protected function createComment($id = null): Comment
{
$comment = new Comment();
$comment->setContent('Comment content');
Expand All @@ -68,7 +53,7 @@ protected function createComment($id = null)
return $comment;
}

protected function createImage($id = null)
protected function createImage($id = null): Image
{
$image = new Image();

Expand All @@ -79,9 +64,9 @@ protected function createImage($id = null)
return $image;
}

protected function createSearchableImage()
protected function createSearchableImage(): SearchableEntity
{
$image = $this->createImage(rand(100, 300));
$image = $this->createImage(random_int(100, 300));

return new SearchableEntity(
$this->getPrefix() . 'image',
Expand All @@ -91,17 +76,17 @@ protected function createSearchableImage()
);
}

protected function getPrefix()
protected function getPrefix(): ?string
{
return $this->get('search.service')->getConfiguration()['prefix'];
}

protected function get($id)
protected function get($id): ?object
{
return self::$kernel->getContainer()->get($id);
}

protected function refreshDb($application)
protected function refreshDb($application): void
{
$inputs = [
new ArrayInput([
Expand All @@ -122,12 +107,17 @@ protected function refreshDb($application)
}
}

protected function getFileName($indexName, $type)
protected function getFileName($indexName, $type): string
{
return sprintf('%s/%s-%s.json', $this->get('search.service')->getConfiguration()['settingsDirectory'], $indexName, $type);
return sprintf(
'%s/%s-%s.json',
$this->get('search.service')->getConfiguration()['settingsDirectory'],
$indexName,
$type
);
}

protected function getDefaultConfig()
protected function getDefaultConfig(): array
{
return [
'hitsPerPage' => 20,
Expand Down
4 changes: 2 additions & 2 deletions tests/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Kernel extends HttpKernel
*
* @return BundleInterface[] An array of bundle instances
*/
public function registerBundles()
public function registerBundles(): array
{
return [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
Expand All @@ -28,7 +28,7 @@ public function registerBundles()
*
* @param LoaderInterface $loader A LoaderInterface instance
*/
public function registerContainerConfiguration(LoaderInterface $loader)
public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(__DIR__ . '/config/config.yml');

Expand Down
4 changes: 2 additions & 2 deletions tests/Normalizer/CommentNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

class CommentNormalizer implements NormalizerInterface
{
public function normalize($object, $format = null, array $context = [])
public function normalize($object, $format = null, array $context = []): array
{
return [
'content' => $object->getContent(),
'post_title' => $object->getPost()->getTitle(),
];
}

public function supportsNormalization($data, $format = null)
public function supportsNormalization($data, $format = null): bool
{
return $data instanceof Comment;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/TestApp/Entity/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ class Comment

public function __construct(array $attributes = [])
{
$this->id = isset($attributes['id']) ? $attributes['id'] : null;
$this->content = isset($attributes['content']) ? $attributes['content'] : null;
$this->publishedAt = isset($attributes['publishedAt']) ? $attributes['publishedAt'] : new \DateTime();
$this->post = isset($attributes['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()
Expand Down
4 changes: 2 additions & 2 deletions tests/TestApp/Entity/ContentAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
class ContentAggregator extends Aggregator
{
public function getIsVisible()
public function getIsVisible(): bool
{
if ($this->entity instanceof Post) {
return $this->entity->getTitle() !== 'Foo';
Expand All @@ -19,7 +19,7 @@ public function getIsVisible()
return true;
}

public static function getEntities()
public static function getEntities(): array
{
return [
Post::class,
Expand Down
Loading

0 comments on commit ffd296f

Please sign in to comment.