Skip to content

Commit

Permalink
Add compiler pass tests
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Sep 1, 2021
1 parent 63262f4 commit c439111
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
69 changes: 69 additions & 0 deletions Tests/DependencyInjection/Compiler/CacheCompatibilityPassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection\Compiler;

use Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection\Fixtures\TestKernel;
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

class CacheCompatibilityPassTest extends TestCase
{
use ExpectDeprecationTrait;

/**
* @group legacy
* @doesNotPerformAssertions
*/
public function testMetadataCacheConfigUsingPsr6ServiceDefinedByApplication(): void
{
(new class (false) extends TestKernel {
public function registerContainerConfiguration(LoaderInterface $loader): void
{
parent::registerContainerConfiguration($loader);
$loader->load(static function (ContainerBuilder $containerBuilder): void {
$containerBuilder->loadFromExtension(
'doctrine_mongodb',
['document_managers' => ['default' => ['metadata_cache_driver' => ['type' => 'service', 'id' => 'custom_cache_service']]]]
);
$containerBuilder->setDefinition(
'custom_cache_service',
new Definition(ArrayAdapter::class)
);
});
}
})->boot();
}

/**
* @group legacy
*/
public function testMetadataCacheConfigUsingNonPsr6ServiceDefinedByApplication(): void
{
$this->expectDeprecation('Since doctrine/mongodb-odm-bundle 4.4: Configuring doctrine/cache is deprecated. Please update the cache service "custom_cache_service" to use a PSR-6 cache.');
(new class (false) extends TestKernel {
public function registerContainerConfiguration(LoaderInterface $loader): void
{
parent::registerContainerConfiguration($loader);
$loader->load(static function (ContainerBuilder $containerBuilder): void {
$containerBuilder->loadFromExtension(
'doctrine_mongodb',
['document_managers' => ['default' => ['metadata_cache_driver' => ['type' => 'service', 'id' => 'custom_cache_service']]]]
);
$containerBuilder->setDefinition(
'custom_cache_service',
(new Definition(DoctrineProvider::class))
->setArguments([new Definition(ArrayAdapter::class)])
->setFactory([DoctrineProvider::class, 'wrap'])
);
});
}
})->boot();
}
}
76 changes: 76 additions & 0 deletions Tests/DependencyInjection/Fixtures/TestKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection\Fixtures;

use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Kernel;

use function md5;
use function mt_rand;
use function sys_get_temp_dir;

class TestKernel extends Kernel
{
/** @var string|null */
private $projectDir;

public function __construct(bool $debug = true)
{
parent::__construct('test', $debug);
}

/** @return iterable<Bundle> */
public function registerBundles(): iterable
{
return [
new FrameworkBundle(),
new DoctrineMongoDBBundle(),
];
}

public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(static function (ContainerBuilder $container): void {
$container->loadFromExtension('framework', ['secret' => 'F00']);

$container->loadFromExtension('doctrine_mongodb', [
'connections' => ['default' => []],
'document_managers' => [
'default' => [
'mappings' => [
'RepositoryServiceBundle' => [
'type' => 'annotation',
'dir' => __DIR__ . '/Bundles/RepositoryServiceBundle/Document',
'prefix' => 'Fixtures\Bundles\RepositoryServiceBundle\Document',
],
],
],
],
]);

// Register a NullLogger to avoid getting the stderr default logger of FrameworkBundle
$container->register('logger', NullLogger::class);
});
}

public function getProjectDir(): string
{
if ($this->projectDir === null) {
$this->projectDir = sys_get_temp_dir() . '/sf_kernel_' . md5((string) mt_rand());
}

return $this->projectDir;
}

public function getRootDir(): string
{
return $this->getProjectDir();
}
}

0 comments on commit c439111

Please sign in to comment.