Skip to content

Commit

Permalink
Support for Symfony 6 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cenadros authored Oct 30, 2024
2 parents 0128e1b + 8f11c03 commit 6d05d42
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 92 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ jobs:
dependencies: highest
symfony-require: 5.4.*
elasticsearch: 5.6-alpine
- php: '8.3'
dependencies: highest
symfony-require: 6.4.*
elasticsearch: 5.6-alpine
env:
SYMFONY_REQUIRE: ${{matrix.symfony-require}}
services:
Expand All @@ -54,4 +58,4 @@ jobs:
- uses: ramsey/composer-install@v2
with:
dependency-versions: ${{ matrix.dependencies }}
- run: vendor/bin/phpunit
- run: vendor/bin/phpunit
29 changes: 15 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,32 @@
],
"require": {
"php": "^7.4 || ^8.1",
"symfony/framework-bundle": "^4.4|^5.4",
"symfony/console": "^4.4|^5.4",
"symfony/dependency-injection": "^4.4|^5.4",
"symfony/property-access": "^4.4|^5.4",
"symfony/framework-bundle": "^4.4|^5.4|^6.4",
"symfony/console": "^4.4|^5.4|^6.4",
"symfony/dependency-injection": "^4.4|^5.4|^6.4",
"symfony/property-access": "^4.4|^5.4|^6.4",
"pagerfanta/pagerfanta": "^2.4|^3.0|^4.0",
"psr/log": "^1.0",
"psr/log": "^1.0|^2.0|^3.0",
"ruflin/elastica": "^5.3.5|^6.1.2"
},
"require-dev": {
"doctrine/annotations": "^1.1|^2.0",
"doctrine/orm": "^2.7",
"doctrine/doctrine-bundle": "^2.2",
"doctrine/persistence": "^2.5",
"doctrine/persistence": "^2.5|^3.1",
"doctrine/phpcr-bundle": "^2.0",
"doctrine/phpcr-odm": "^1.5",
"jackalope/jackalope-doctrine-dbal": "^1.3",
"jms/serializer-bundle": "^2.4|^3.5",
"doctrine/phpcr-odm": "^1.5|^2.0",
"jackalope/jackalope-doctrine-dbal": "^1.3|^2.0",
"jms/serializer-bundle": "^2.4|^3.5|^4.0|^5.0",
"phpunit/phpunit": "^9.6",
"phpspec/prophecy-phpunit": "^2.2",
"knplabs/knp-components": "^3.0|^4.0",
"symfony/expression-language" : "^4.4|^5.4",
"symfony/twig-bundle": "^4.4|^5.4",
"symfony/serializer": "^4.4|^5.4",
"symfony/yaml": "^4.4|^5.4",
"symfony/expression-language" : "^4.4|^5.4|^6.4",
"symfony/twig-bundle": "^4.4|^5.4|^6.4",
"symfony/serializer": "^4.4|^5.4|^6.4",
"symfony/yaml": "^4.4|^5.4|^6.4",
"friendsofphp/php-cs-fixer": "^3.4",
"symfony/web-profiler-bundle": "^4.4|^5.4"
"symfony/web-profiler-bundle": "^4.4|^5.4|^6.4"
},
"suggest": {
"enqueue/elastica-bundle": "The bundle adds extra features to FOSElasticaBundle bundle. Aimed to improve performance."
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct($debug)
*
* @return TreeBuilder
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('fos_elastica');

Expand Down
9 changes: 7 additions & 2 deletions src/Doctrine/RepositoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@ public function getRepository($entityName)
{
$realEntityName = $entityName;
if (false !== strpos($entityName, ':')) {
list($namespaceAlias, $simpleClassName) = explode(':', $entityName);
$realEntityName = $this->managerRegistry->getAliasNamespace($namespaceAlias).'\\'.$simpleClassName;
[$namespaceAlias, $simpleClassName] = explode(':', $entityName);
// @link https://github.com/doctrine/persistence/pull/204
if (\method_exists($this->managerRegistry, 'getAliasNamespace')) {
$realEntityName = $this->managerRegistry->getAliasNamespace($namespaceAlias).'\\'.$simpleClassName;
} else {
$realEntityName = $simpleClassName.'::class';
}
}

if (isset($this->entities[$realEntityName])) {
Expand Down
13 changes: 8 additions & 5 deletions tests/Functional/ProfilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@ public function setUp(): void
$this->twig = new Environment($twigLoaderFilesystem, ['debug' => true, 'strict_variables' => true]);

$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
$urlGenerator->method('generate')->willReturn('');
$fragmentHandler = $this->createMock(FragmentHandler::class);
$fragmentHandler->method('render')->willReturn('');
$loader = $this->getMockBuilder(RuntimeLoaderInterface::class)->getMock();

$this->twig->addExtension(new CodeExtension('', '', ''));
if (\class_exists('Symfony\Bridge\Twig\Extension\CodeExtension')) {
$this->twig->addExtension(new CodeExtension('', '', ''));
}
$this->twig->addExtension(new RoutingExtension($urlGenerator));
$this->twig->addExtension(new HttpKernelExtension($fragmentHandler));

$loader = $this->getMockBuilder(RuntimeLoaderInterface::class)->getMock();

$urlGenerator->method('generate')->willReturn('');
$fragmentHandler->method('render')->willReturn('');
$loader->method('load')->willReturn(new HttpKernelRuntime($fragmentHandler));

$this->twig->addRuntimeLoader($loader);
}

Expand All @@ -81,6 +83,7 @@ public function testRender($query)
'request' => $request,
'collector' => $this->collector,
'queries' => $this->logger->getQueries(),
'profile_type' => 'request',
]);

$output = str_replace(""", '"', $output);
Expand Down
13 changes: 7 additions & 6 deletions tests/Functional/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
use FOS\ElasticaBundle\Tests\Functional\app\AppKernel;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase as BaseKernelTestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\KernelInterface;

/*
* Based on https://github.com/symfony/symfony/blob/2.7/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/WebTestCase.php
*/
class WebTestCase extends BaseKernelTestCase
{
protected static function getKernelClass()
protected static function getKernelClass(): string
{
require_once __DIR__.'/app/AppKernel.php';

Expand All @@ -55,7 +56,7 @@ protected static function deleteTmpDir()
$fs->remove($dir);
}

protected static function createKernel(array $options = [])
protected static function createKernel(array $options = []): KernelInterface
{
$class = self::getKernelClass();

Expand All @@ -66,14 +67,14 @@ protected static function createKernel(array $options = [])
return new $class(
static::getVarDir(),
$options['test_case'],
isset($options['root_config']) ? $options['root_config'] : 'config.yml',
isset($options['environment']) ? $options['environment'] : strtolower(static::getVarDir().$options['test_case']),
isset($options['debug']) ? $options['debug'] : true
$options['root_config'] ?? 'config.yml',
$options['environment'] ?? strtolower(static::getVarDir().$options['test_case']),
$options['debug'] ?? true
);
}

protected static function getVarDir()
{
return substr(strrchr(get_called_class(), '\\'), 1);
return substr(strrchr(static::class, '\\'), 1);
}
}
112 changes: 53 additions & 59 deletions tests/Unit/DependencyInjection/ConfigSourcePassTest.php
Original file line number Diff line number Diff line change
@@ -1,90 +1,84 @@
<?php

/*
* This file is part of the FOSElasticaBundle package.
*
* (c) FriendsOfSymfony <https://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\ElasticaBundle\Tests\Unit\DependencyInjection;

use FOS\ElasticaBundle\Configuration\ConfigManager;
use FOS\ElasticaBundle\Configuration\Source\ContainerSource;
use FOS\ElasticaBundle\DependencyInjection\Compiler\ConfigSourcePass;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

/**
* @internal
*/
class ConfigSourcePassTest extends TestCase
{
use ProphecyTrait;

/** @var ContainerBuilder */
private $container;

protected function setUp(): void
{
$this->container = $this->prophesize(ContainerBuilder::class);
$this->container = new ContainerBuilder();
}

public function testProcessWithoutConfigManager()
{
$this->container
->hasDefinition('fos_elastica.config_manager')
->shouldBeCalled()
->willReturn(false);
$configManagerDefinition = new Definition(ConfigManager::class);
$configManagerDefinition->addArgument([]);
$this->container->setDefinition('fos_elastica.config_manager', $configManagerDefinition);

$configManagerIndexTemplatesDefinition = new Definition(ConfigManager::class);
$configManagerIndexTemplatesDefinition->addArgument([]);
$this->container->setDefinition('fos_elastica.config_manager.index_templates', $configManagerIndexTemplatesDefinition);

$pass = new ConfigSourcePass();
$pass->process($this->container->reveal());
$pass->process($this->container);

$this->container->getDefinition('fos_elastica.config_manager')->shouldNotBeCalled();
$this->container->getDefinition('fos_elastica.config_manager.index_templates')->shouldNotBeCalled();
$this->assertSame([], $this->container->getDefinition('fos_elastica.config_manager')->getArgument(0));
$this->assertSame([], $this->container->getDefinition('fos_elastica.config_manager.index_templates')->getArgument(0));
}

public function testProcessWithConfigManager()
{
$this->container
->hasDefinition('fos_elastica.config_manager')
->shouldBeCalled()
->willReturn(true);

$this->container
->findTaggedServiceIds('fos_elastica.config_source')
->shouldBeCalled()
->willReturn(
[
'index_definition_id' => null,
'index_template_definition_id' => null,
]
);

$indexDefinition = $this->prophesize(Definition::class);
$indexDefinition->getTag('fos_elastica.config_source')
->shouldBeCalled()
->willReturn([]);
$this->container
->findDefinition('index_definition_id')
->shouldBeCalled()
->willReturn($indexDefinition->reveal());


$indexTemplateDefinition = $this->prophesize(Definition::class);
$indexTemplateDefinition->getTag('fos_elastica.config_source')
->shouldBeCalled()
->willReturn([]);
$this->container
->findDefinition('index_template_definition_id')
->shouldBeCalled()
->willReturn($indexTemplateDefinition->reveal());

$configManagerDefinition = $this->prophesize(Definition::class);
$configManagerDefinition->replaceArgument(0, ['index_definition_id']);
$this->container
->getDefinition('fos_elastica.config_manager')
->shouldBeCalled()
->willReturn($configManagerDefinition);

$templateConfigManagerDefinition = $this->prophesize(Definition::class);
$templateConfigManagerDefinition->replaceArgument(0, ['index_template_definition_id']);
$this->container
->getDefinition('fos_elastica.config_manager.index_templates')
->shouldBeCalled()
->willReturn($templateConfigManagerDefinition);
$configManagerDefinition = new Definition(ConfigManager::class);
$configManagerDefinition->addArgument([]);
$this->container->setDefinition('fos_elastica.config_manager', $configManagerDefinition);

$configManagerIndexTemplatesDefinition = new Definition(ConfigManager::class);
$configManagerIndexTemplatesDefinition->addArgument([]);
$this->container->setDefinition('fos_elastica.config_manager.index_templates', $configManagerIndexTemplatesDefinition);

$indexDefinition = new Definition(ContainerSource::class);
$indexDefinition->addTag('fos_elastica.config_source');

$this->container->setDefinition('index_definition_id', $indexDefinition);

$indexTemplateDefinition = new Definition(ContainerSource::class);
$indexTemplateDefinition->addTag('fos_elastica.config_source');

$this->container->setDefinition('index_template_definition_id', $indexTemplateDefinition);

$pass = new ConfigSourcePass();
$pass->process($this->container->reveal());
$pass->process($this->container);

$argument = $configManagerDefinition->getArgument(0);

$this->assertIsArray($argument);
$this->assertCount(2, $argument);
$this->assertInstanceOf(Reference::class, $argument[0]);
$this->assertSame('index_definition_id', $argument[0]->__toString());
$this->assertInstanceOf(Reference::class, $argument[1]);
$this->assertSame('index_template_definition_id', $argument[1]->__toString());
}
}
17 changes: 13 additions & 4 deletions tests/Unit/Doctrine/RepositoryManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,26 @@ public function testGetRepositoryShouldResolveEntityShortName()
$registryMock = $this->createMock(ManagerRegistry::class);
$mainManager = $this->createMock(RepositoryManagerInterface::class);

$registryMock->method('getAliasNamespace')
->with($this->equalTo('FOSElasticaBundle'))
->willReturn((new \ReflectionClass(NamespacedEntity::class))->getNamespaceName());
// @link https://github.com/doctrine/persistence/pull/204
if (\method_exists(ManagerRegistry::class, 'getAliasNamespace')) {
$registryMock->method('getAliasNamespace')
->with($this->equalTo('FOSElasticaBundle'))
->willReturn((new \ReflectionClass(NamespacedEntity::class))->getNamespaceName());
}

$mainManager->method('getRepository')
->with($this->equalTo('index/type'))
->willReturn(new Repository($finderMock));

$manager = new RepositoryManager($registryMock, $mainManager);
$manager->addEntity(NamespacedEntity::class, 'index/type');
$repository = $manager->getRepository('FOSElasticaBundle:NamespacedEntity');

if (\method_exists(ManagerRegistry::class, 'getAliasNamespace')) {
$repository = $manager->getRepository('FOSElasticaBundle:NamespacedEntity');
} else {
$repository = $manager->getRepository(NamespacedEntity::class);
}

$this->assertInstanceOf(Repository::class, $repository);
}
}

0 comments on commit 6d05d42

Please sign in to comment.