Skip to content

Commit

Permalink
Merge branch '2.14.x' into 3.0.x
Browse files Browse the repository at this point in the history
* 2.14.x:
  Control proxy implementation via env (doctrine#10282)
  • Loading branch information
derrabus committed Dec 10, 2022
2 parents 38c476f + 3ea8550 commit a9ba9fc
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 53 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ jobs:
run: "composer require doctrine/dbal ^${{ matrix.dbal-version }} --no-update"
if: "${{ matrix.dbal-version != 'default' }}"

- name: "Require specific lazy-proxy implementation"
run: "composer require symfony/var-exporter ^6.2 doctrine/persistence ^3.1 --no-update"
if: "${{ matrix.proxy == 'lazy-ghost' }}"

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v2"
with:
Expand All @@ -87,11 +83,13 @@ jobs:
run: "vendor/bin/phpunit -c ci/github/phpunit/${{ matrix.extension }}.xml --coverage-clover=coverage-no-cache.xml"
env:
ENABLE_SECOND_LEVEL_CACHE: 0
ORM_PROXY_IMPLEMENTATION: "${{ matrix.proxy }}"

- name: "Run PHPUnit with Second Level Cache"
run: "vendor/bin/phpunit -c ci/github/phpunit/${{ matrix.extension }}.xml --exclude-group performance,non-cacheable,locking_functional --coverage-clover=coverage-cache.xml"
env:
ENABLE_SECOND_LEVEL_CACHE: 1
ORM_PROXY_IMPLEMENTATION: "${{ matrix.proxy }}"

- name: "Upload coverage file"
uses: "actions/upload-artifact@v3"
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"psr/log": "^1 || ^2 || ^3",
"squizlabs/php_codesniffer": "3.7.1",
"symfony/cache": "^5.4 || ^6.0",
"symfony/var-exporter": "^5.4 || ^6.2",
"vimeo/psalm": "5.1.0"
},
"suggest": {
Expand Down
13 changes: 3 additions & 10 deletions tests/Doctrine/Performance/EntityManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,18 @@
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Persistence\Reflection\RuntimeReflectionProperty;
use Symfony\Component\VarExporter\LazyGhostTrait;
use Doctrine\Tests\TestUtil;

use function array_map;
use function class_exists;
use function realpath;
use function trait_exists;

final class EntityManagerFactory
{
public static function getEntityManager(array $schemaClassNames): EntityManagerInterface
{
$config = new Configuration();

$config->setLazyGhostObjectEnabled(trait_exists(LazyGhostTrait::class) && class_exists(RuntimeReflectionProperty::class));
$config->setProxyDir(__DIR__ . '/../Tests/Proxies');
$config->setProxyNamespace('Doctrine\Tests\Proxies');
TestUtil::configureProxies($config);
$config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_EVAL);
$config->setMetadataDriverImpl(new AttributeDriver([
realpath(__DIR__ . '/Models/Cache'),
Expand All @@ -58,9 +53,7 @@ public static function makeEntityManagerWithNoResultsConnection(): EntityManager
{
$config = new Configuration();

$config->setLazyGhostObjectEnabled(trait_exists(LazyGhostTrait::class) && class_exists(RuntimeReflectionProperty::class));
$config->setProxyDir(__DIR__ . '/../Tests/Proxies');
$config->setProxyNamespace('Doctrine\Tests\Proxies');
TestUtil::configureProxies($config);
$config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_EVAL);
$config->setMetadataDriverImpl(new AttributeDriver([
realpath(__DIR__ . '/Models/Cache'),
Expand Down
10 changes: 2 additions & 8 deletions tests/Doctrine/Tests/Mocks/EntityManagerMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\ORM\UnitOfWork;
use Doctrine\Persistence\Reflection\RuntimeReflectionProperty;
use Symfony\Component\VarExporter\LazyGhostTrait;

use function class_exists;
use function trait_exists;
use Doctrine\Tests\TestUtil;

/**
* Special EntityManager mock used for testing purposes.
Expand All @@ -29,9 +25,7 @@ public function __construct(Connection $conn, Configuration|null $config = null,
{
if ($config === null) {
$config = new Configuration();
$config->setLazyGhostObjectEnabled(trait_exists(LazyGhostTrait::class) && class_exists(RuntimeReflectionProperty::class));
$config->setProxyDir(__DIR__ . '/../Proxies');
$config->setProxyNamespace('Doctrine\Tests\Proxies');
TestUtil::configureProxies($config);
$config->setMetadataDriverImpl(new AttributeDriver([]));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\Reflection\RuntimeReflectionProperty;
use Doctrine\Tests\ORM\Functional\Locking\Doctrine\ORM\Query;
use Doctrine\Tests\TestUtil;
use GearmanWorker;
use InvalidArgumentException;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\VarExporter\LazyGhostTrait;

use function assert;
use function class_exists;
use function is_array;
use function microtime;
use function sleep;
use function trait_exists;
use function unserialize;

class LockAgentWorker
Expand Down Expand Up @@ -116,9 +113,7 @@ protected function processWorkload($job): array
protected function createEntityManager(Connection $conn): EntityManagerInterface
{
$config = new Configuration();
$config->setLazyGhostObjectEnabled(trait_exists(LazyGhostTrait::class) && class_exists(RuntimeReflectionProperty::class));
$config->setProxyDir(__DIR__ . '/../../../Proxies');
$config->setProxyNamespace('MyProject\Proxies');
TestUtil::configureProxies($config);
$config->setAutoGenerateProxyClasses(true);

$annotDriver = new AttributeDriver([__DIR__ . '/../../../Models/']);
Expand Down
9 changes: 2 additions & 7 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\Mapping\RuntimeReflectionService;
use Doctrine\Persistence\Reflection\RuntimeReflectionProperty;
use Doctrine\Tests\Mocks\EntityManagerMock;
use Doctrine\Tests\Mocks\MetadataDriverMock;
use Doctrine\Tests\Models\CMS\CmsArticle;
Expand All @@ -42,19 +41,17 @@
use Doctrine\Tests\Models\Quote\Phone;
use Doctrine\Tests\Models\Quote\User;
use Doctrine\Tests\OrmTestCase;
use Doctrine\Tests\TestUtil;
use Exception;
use InvalidArgumentException;
use PHPUnit\Framework\Assert;
use ReflectionClass;
use Symfony\Component\VarExporter\LazyGhostTrait;

use function array_search;
use function assert;
use function class_exists;
use function count;
use function method_exists;
use function sprintf;
use function trait_exists;

class ClassMetadataFactoryTest extends OrmTestCase
{
Expand Down Expand Up @@ -241,9 +238,7 @@ public function testGetAllMetadataWorksWithBadConnection(): void
protected function createEntityManager(MappingDriver $metadataDriver, $conn = null): EntityManagerMock
{
$config = new Configuration();
$config->setLazyGhostObjectEnabled(trait_exists(LazyGhostTrait::class) && class_exists(RuntimeReflectionProperty::class));
$config->setProxyDir(__DIR__ . '/../../Proxies');
$config->setProxyNamespace('Doctrine\Tests\Proxies');
TestUtil::configureProxies($config);
$eventManager = new EventManager();
if (! $conn) {
$platform = $this->createMock(AbstractPlatform::class);
Expand Down
6 changes: 5 additions & 1 deletion tests/Doctrine/Tests/ORM/Proxy/ProxyFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Common\EventManager;
use Doctrine\Common\Proxy\Proxy as CommonProxy;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\ORM\EntityNotFoundException;
use Doctrine\ORM\Mapping\ClassMetadata;
Expand Down Expand Up @@ -149,7 +150,10 @@ public function testFailedProxyCloningDoesNotMarkTheProxyAsInitialized(): void
{
$persister = $this->getMockBuilder(BasicEntityPersister::class)
->onlyMethods(['load'])
->disableOriginalConstructor()
->setConstructorArgs([
new EntityManagerMock(DriverManager::getConnection(['driver' => 'sqlite3', 'memory' => true])),
$this->createStub(ClassMetadata::class),
])
->getMock();
$this->uowMock->setEntityPersister(ECommerceFeature::class, $persister);

Expand Down
8 changes: 1 addition & 7 deletions tests/Doctrine/Tests/OrmFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\ToolsException;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\Reflection\RuntimeReflectionProperty;
use Doctrine\Tests\DbalExtensions\Connection;
use Doctrine\Tests\DbalExtensions\QueryLog;
use Doctrine\Tests\DbalTypes\Rot13Type;
Expand Down Expand Up @@ -175,15 +174,13 @@
use Psr\Cache\CacheItemPoolInterface;
use RuntimeException;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\VarExporter\LazyGhostTrait;
use Throwable;

use function array_map;
use function array_pop;
use function array_reverse;
use function array_slice;
use function assert;
use function class_exists;
use function explode;
use function get_debug_type;
use function getenv;
Expand All @@ -194,7 +191,6 @@
use function sprintf;
use function str_contains;
use function strtolower;
use function trait_exists;
use function var_export;

use const PHP_EOL;
Expand Down Expand Up @@ -906,11 +902,9 @@ protected function getEntityManager(
//FIXME: two different configs! $conn and the created entity manager have
// different configs.
$config = new Configuration();
$config->setLazyGhostObjectEnabled(trait_exists(LazyGhostTrait::class) && class_exists(RuntimeReflectionProperty::class));
TestUtil::configureProxies($config);
$config->setMetadataCache(self::$metadataCache);
$config->setQueryCache(self::$queryCache);
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Doctrine\Tests\Proxies');

if ($this->resultCache !== null) {
$config->setResultCache($this->resultCache);
Expand Down
8 changes: 1 addition & 7 deletions tests/Doctrine/Tests/OrmTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@
use Doctrine\ORM\Cache\Logging\StatisticsCacheLogger;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\Persistence\Reflection\RuntimeReflectionProperty;
use Doctrine\Tests\Mocks\EntityManagerMock;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\VarExporter\LazyGhostTrait;

use function class_exists;
use function method_exists;
use function realpath;
use function sprintf;
use function trait_exists;

/**
* Base testcase class for all ORM testcases.
Expand Down Expand Up @@ -95,11 +91,9 @@ private function buildTestEntityManagerWithPlatform(Connection $connection): Ent

$config = new Configuration();

$config->setLazyGhostObjectEnabled(trait_exists(LazyGhostTrait::class) && class_exists(RuntimeReflectionProperty::class));
TestUtil::configureProxies($config);
$config->setMetadataCache($metadataCache);
$config->setQueryCache(self::getSharedQueryCache());
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Doctrine\Tests\Proxies');
$config->setMetadataDriverImpl(new AttributeDriver([
realpath(__DIR__ . '/Models/Cache'),
]));
Expand Down
32 changes: 30 additions & 2 deletions tests/Doctrine/Tests/TestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@
namespace Doctrine\Tests;

use Doctrine\Common\EventSubscriber;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Configuration as DbalConfiguration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\ORM\Configuration;
use LogicException;
use Symfony\Component\VarExporter\LazyGhostTrait;
use UnexpectedValueException;

use function assert;
use function explode;
use function fwrite;
use function get_debug_type;
use function getenv;
use function sprintf;
use function str_starts_with;
use function strlen;
use function substr;
use function trait_exists;

use const STDERR;

Expand Down Expand Up @@ -50,7 +55,7 @@ class TestUtil
* 1) Each invocation of this method returns a NEW database connection.
* 2) The database is dropped and recreated to ensure it's clean.
*/
public static function getConnection(Configuration|null $config = null): DbalExtensions\Connection
public static function getConnection(DbalConfiguration|null $config = null): DbalExtensions\Connection
{
if (! self::$initialized) {
self::initializeDatabase();
Expand All @@ -73,6 +78,29 @@ public static function getPrivilegedConnection(): DbalExtensions\Connection
return $connection;
}

public static function configureProxies(Configuration $configuration): void
{
$configuration->setProxyDir(__DIR__ . '/Proxies');
$configuration->setProxyNamespace('Doctrine\Tests\Proxies');

$proxyImplementation = getenv('ORM_PROXY_IMPLEMENTATION')
?: (trait_exists(LazyGhostTrait::class) ? 'lazy-ghost' : 'common');

switch ($proxyImplementation) {
case 'lazy-ghost':
$configuration->setLazyGhostObjectEnabled(true);

return;

case 'common':
$configuration->setLazyGhostObjectEnabled(false);

return;
}

throw new LogicException(sprintf('Unknown proxy implementation: %s.', $proxyImplementation));
}

private static function initializeDatabase(): void
{
$testConnParams = self::getTestConnectionParameters();
Expand Down

0 comments on commit a9ba9fc

Please sign in to comment.