1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM;
use Doctrine\Common\Annotations\SimpleAnnotationReader;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\Psr6\CacheAdapter;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use Doctrine\ORM\Cache\CacheConfiguration;
use Doctrine\ORM\Cache\Exception\MetadataCacheNotConfigured;
use Doctrine\ORM\Cache\Exception\MetadataCacheUsesNonPersistentCache;
use Doctrine\ORM\Cache\Exception\QueryCacheNotConfigured;
use Doctrine\ORM\Cache\Exception\QueryCacheUsesNonPersistentCache;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Exception\InvalidEntityRepository;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\Exception\ProxyClassesAlwaysRegenerating;
use Doctrine\ORM\Mapping as AnnotationNamespace;
use Doctrine\ORM\Mapping\DefaultTypedFieldMapper;
use Doctrine\ORM\Mapping\EntityListenerResolver;
use Doctrine\ORM\Mapping\NamingStrategy;
use Doctrine\ORM\Mapping\PrePersist;
use Doctrine\ORM\Mapping\QuoteStrategy;
use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\ObjectRepository;
use Doctrine\Tests\DoctrineTestCase;
use Doctrine\Tests\Models\DDC753\DDC753CustomRepository;
use Psr\Cache\CacheItemPoolInterface;
use ReflectionClass;
use stdClass;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use function class_exists;
/**
* Tests for the Configuration object
*/
class ConfigurationTest extends DoctrineTestCase
{
use VerifyDeprecations;
/** @var Configuration */
private $configuration;
protected function setUp(): void
{
parent::setUp();
$this->configuration = new Configuration();
}
public function testSetGetProxyDir(): void
{
self::assertNull($this->configuration->getProxyDir()); // defaults
$this->configuration->setProxyDir(__DIR__);
self::assertSame(__DIR__, $this->configuration->getProxyDir());
}
public function testSetGetAutoGenerateProxyClasses(): void
{
self::assertSame(ProxyFactory::AUTOGENERATE_ALWAYS, $this->configuration->getAutoGenerateProxyClasses()); // defaults
$this->configuration->setAutoGenerateProxyClasses(false);
self::assertSame(ProxyFactory::AUTOGENERATE_NEVER, $this->configuration->getAutoGenerateProxyClasses());
$this->configuration->setAutoGenerateProxyClasses(true);
self::assertSame(ProxyFactory::AUTOGENERATE_ALWAYS, $this->configuration->getAutoGenerateProxyClasses());
$this->configuration->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS);
self::assertSame(ProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS, $this->configuration->getAutoGenerateProxyClasses());
}
public function testSetGetProxyNamespace(): void
{
self::assertNull($this->configuration->getProxyNamespace()); // defaults
$this->configuration->setProxyNamespace(__NAMESPACE__);
self::assertSame(__NAMESPACE__, $this->configuration->getProxyNamespace());
}
public function testSetGetMetadataDriverImpl(): void
{
self::assertNull($this->configuration->getMetadataDriverImpl()); // defaults
$metadataDriver = $this->createMock(MappingDriver::class);
$this->configuration->setMetadataDriverImpl($metadataDriver);
self::assertSame($metadataDriver, $this->configuration->getMetadataDriverImpl());
}
public function testNewDefaultAnnotationDriver(): void
{
$paths = [__DIR__];
$reflectionClass = new ReflectionClass(ConfigurationTestAnnotationReaderChecker::class);
$annotationDriver = $this->configuration->newDefaultAnnotationDriver($paths, false);
$reader = $annotationDriver->getReader();
$annotation = $reader->getMethodAnnotation(
$reflectionClass->getMethod('namespacedAnnotationMethod'),
AnnotationNamespace\PrePersist::class
);
self::assertInstanceOf(AnnotationNamespace\PrePersist::class, $annotation);
}
public function testNewDefaultAnnotationDriverWithSimpleAnnotationReader(): void
{
if (! class_exists(SimpleAnnotationReader::class)) {
self::markTestSkipped('Requires doctrine/annotations 1.x');
}
$paths = [__DIR__];
$reflectionClass = new ReflectionClass(ConfigurationTestAnnotationReaderChecker::class);
$annotationDriver = $this->configuration->newDefaultAnnotationDriver($paths);
$reader = $annotationDriver->getReader();
$annotation = $reader->getMethodAnnotation(
$reflectionClass->getMethod('simpleAnnotationMethod'),
AnnotationNamespace\PrePersist::class
);
self::assertInstanceOf(AnnotationNamespace\PrePersist::class, $annotation);
}
public function testSetGetEntityNamespace(): void
{
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8818');
$this->configuration->addEntityNamespace('TestNamespace', __NAMESPACE__);
self::assertSame(__NAMESPACE__, $this->configuration->getEntityNamespace('TestNamespace'));
$namespaces = ['OtherNamespace' => __NAMESPACE__];
$this->configuration->setEntityNamespaces($namespaces);
self::assertSame($namespaces, $this->configuration->getEntityNamespaces());
$this->expectException(ORMException::class);
$this->configuration->getEntityNamespace('NonExistingNamespace');
}
public function testSetGetQueryCacheImpl(): void
{
self::assertNull($this->configuration->getQueryCacheImpl()); // defaults
self::assertNull($this->configuration->getQueryCache()); // defaults
$queryCacheImpl = $this->createMock(Cache::class);
$this->configuration->setQueryCacheImpl($queryCacheImpl);
self::assertSame($queryCacheImpl, $this->configuration->getQueryCacheImpl());
self::assertNotNull($this->configuration->getQueryCache());
}
public function testSetGetQueryCache(): void
{
self::assertNull($this->configuration->getQueryCache()); // defaults
$queryCache = $this->createMock(CacheItemPoolInterface::class);
$this->configuration->setQueryCache($queryCache);
self::assertSame($queryCache, $this->configuration->getQueryCache());
self::assertSame($queryCache, CacheAdapter::wrap($this->configuration->getQueryCacheImpl()));
}
public function testSetGetHydrationCacheImpl(): void
{
self::assertNull($this->configuration->getHydrationCacheImpl()); // defaults
$hydrationCacheImpl = $this->createMock(Cache::class);
$this->configuration->setHydrationCacheImpl($hydrationCacheImpl);
self::assertSame($hydrationCacheImpl, $this->configuration->getHydrationCacheImpl());
self::assertNotNull($this->configuration->getHydrationCache());
}
public function testSetGetHydrationCache(): void
{
self::assertNull($this->configuration->getHydrationCache()); // defaults
$hydrationCache = $this->createStub(CacheItemPoolInterface::class);
$this->configuration->setHydrationCache($hydrationCache);
self::assertSame($hydrationCache, $this->configuration->getHydrationCache());
self::assertSame($hydrationCache, CacheAdapter::wrap($this->configuration->getHydrationCacheImpl()));
}
public function testSetGetMetadataCacheImpl(): void
{
self::assertNull($this->configuration->getMetadataCacheImpl()); // defaults
$queryCacheImpl = $this->createMock(Cache::class);
$this->configuration->setMetadataCacheImpl($queryCacheImpl);
self::assertSame($queryCacheImpl, $this->configuration->getMetadataCacheImpl());
self::assertNotNull($this->configuration->getMetadataCache());
}
public function testSetGetMetadataCache(): void
{
self::assertNull($this->configuration->getMetadataCache());
$cache = $this->createStub(CacheItemPoolInterface::class);
$this->configuration->setMetadataCache($cache);
self::assertSame($cache, $this->configuration->getMetadataCache());
self::assertSame($cache, CacheAdapter::wrap($this->configuration->getMetadataCacheImpl()));
}
public function testAddGetNamedQuery(): void
{
$dql = 'SELECT u FROM User u';
$this->configuration->addNamedQuery('QueryName', $dql);
self::assertSame($dql, $this->configuration->getNamedQuery('QueryName'));
$this->expectException(ORMException::class);
$this->expectExceptionMessage('a named query');
$this->configuration->getNamedQuery('NonExistingQuery');
}
public function testAddGetNamedNativeQuery(): void
{
$sql = 'SELECT * FROM user';
$rsm = $this->createMock(ResultSetMapping::class);
$this->configuration->addNamedNativeQuery('QueryName', $sql, $rsm);
$fetched = $this->configuration->getNamedNativeQuery('QueryName');
self::assertSame($sql, $fetched[0]);
self::assertSame($rsm, $fetched[1]);
$this->expectException(ORMException::class);
$this->expectExceptionMessage('a named native query');
$this->configuration->getNamedNativeQuery('NonExistingQuery');
}
/**
* Configures $this->configuration to use production settings.
*
* @param string|null $skipCache Do not configure a cache of this type, either "query" or "metadata".
*/
protected function setProductionSettings(?string $skipCache = null): void
{
$this->configuration->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_NEVER);
$cache = $this->createMock(Cache::class);
if ($skipCache !== 'query') {
$this->configuration->setQueryCacheImpl($cache);
}
if ($skipCache !== 'metadata') {
$this->configuration->setMetadataCacheImpl($cache);
}
}
public function testEnsureProductionSettings(): void
{
$this->setProductionSettings();
$this->configuration->ensureProductionSettings();
$this->addToAssertionCount(1);
}
public function testEnsureProductionSettingsWithNewMetadataCache(): void
{
$this->setProductionSettings('metadata');
$this->configuration->setMetadataCache(new ArrayAdapter());
$this->configuration->ensureProductionSettings();
$this->addToAssertionCount(1);
}
public function testEnsureProductionSettingsMissingQueryCache(): void
{
$this->setProductionSettings('query');
$this->expectException(QueryCacheNotConfigured::class);
$this->expectExceptionMessage('Query Cache is not configured.');
$this->configuration->ensureProductionSettings();
}
public function testEnsureProductionSettingsMissingMetadataCache(): void
{
$this->setProductionSettings('metadata');
$this->expectException(MetadataCacheNotConfigured::class);
$this->expectExceptionMessage('Metadata Cache is not configured.');
$this->configuration->ensureProductionSettings();
}
public function testEnsureProductionSettingsQueryArrayCache(): void
{
if (! class_exists(ArrayCache::class)) {
self::markTestSkipped('Test only applies with doctrine/cache 1.x');
}
$this->setProductionSettings();
$this->configuration->setQueryCacheImpl(new ArrayCache());
$this->expectException(QueryCacheUsesNonPersistentCache::class);
$this->expectExceptionMessage('Query Cache uses a non-persistent cache driver, Doctrine\Common\Cache\ArrayCache.');
$this->configuration->ensureProductionSettings();
}
public function testEnsureProductionSettingsLegacyMetadataArrayCache(): void
{
if (! class_exists(ArrayCache::class)) {
self::markTestSkipped('Test only applies with doctrine/cache 1.x');
}
$this->setProductionSettings();
$this->configuration->setMetadataCacheImpl(new ArrayCache());
$this->expectException(MetadataCacheUsesNonPersistentCache::class);
$this->expectExceptionMessage('Metadata Cache uses a non-persistent cache driver, Doctrine\Common\Cache\ArrayCache.');
$this->configuration->ensureProductionSettings();
}
public function testEnsureProductionSettingsAutoGenerateProxyClassesAlways(): void
{
$this->setProductionSettings();
$this->configuration->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_ALWAYS);
$this->expectException(ProxyClassesAlwaysRegenerating::class);
$this->expectExceptionMessage('Proxy Classes are always regenerating.');
$this->configuration->ensureProductionSettings();
}
public function testEnsureProductionSettingsAutoGenerateProxyClassesFileNotExists(): void
{
$this->setProductionSettings();
$this->configuration->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS);
$this->expectException(ORMException::class);
$this->expectExceptionMessage('Proxy Classes are always regenerating.');
$this->configuration->ensureProductionSettings();
}
public function testEnsureProductionSettingsAutoGenerateProxyClassesEval(): void
{
$this->setProductionSettings();
$this->configuration->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_EVAL);
$this->expectException(ORMException::class);
$this->expectExceptionMessage('Proxy Classes are always regenerating.');
$this->configuration->ensureProductionSettings();
}
public function testAddGetCustomStringFunction(): void
{
$this->configuration->addCustomStringFunction('FunctionName', self::class);
self::assertSame(self::class, $this->configuration->getCustomStringFunction('FunctionName'));
self::assertNull($this->configuration->getCustomStringFunction('NonExistingFunction'));
$this->configuration->setCustomStringFunctions(['OtherFunctionName' => self::class]);
self::assertSame(self::class, $this->configuration->getCustomStringFunction('OtherFunctionName'));
}
public function testAddGetCustomNumericFunction(): void
{
$this->configuration->addCustomNumericFunction('FunctionName', self::class);
self::assertSame(self::class, $this->configuration->getCustomNumericFunction('FunctionName'));
self::assertNull($this->configuration->getCustomNumericFunction('NonExistingFunction'));
$this->configuration->setCustomNumericFunctions(['OtherFunctionName' => self::class]);
self::assertSame(self::class, $this->configuration->getCustomNumericFunction('OtherFunctionName'));
}
public function testAddGetCustomDatetimeFunction(): void
{
$this->configuration->addCustomDatetimeFunction('FunctionName', self::class);
self::assertSame(self::class, $this->configuration->getCustomDatetimeFunction('FunctionName'));
self::assertNull($this->configuration->getCustomDatetimeFunction('NonExistingFunction'));
$this->configuration->setCustomDatetimeFunctions(['OtherFunctionName' => self::class]);
self::assertSame(self::class, $this->configuration->getCustomDatetimeFunction('OtherFunctionName'));
}
public function testAddGetCustomHydrationMode(): void
{
self::assertNull($this->configuration->getCustomHydrationMode('NonExisting'));
$this->configuration->addCustomHydrationMode('HydrationModeName', self::class);
self::assertSame(self::class, $this->configuration->getCustomHydrationMode('HydrationModeName'));
}
public function testSetCustomHydrationModes(): void
{
$this->configuration->addCustomHydrationMode('HydrationModeName', self::class);
self::assertSame(self::class, $this->configuration->getCustomHydrationMode('HydrationModeName'));
$this->configuration->setCustomHydrationModes(
['AnotherHydrationModeName' => self::class]
);
self::assertNull($this->configuration->getCustomHydrationMode('HydrationModeName'));
self::assertSame(self::class, $this->configuration->getCustomHydrationMode('AnotherHydrationModeName'));
}
public function testSetGetClassMetadataFactoryName(): void
{
self::assertSame(AnnotationNamespace\ClassMetadataFactory::class, $this->configuration->getClassMetadataFactoryName());
$this->configuration->setClassMetadataFactoryName(self::class);
self::assertSame(self::class, $this->configuration->getClassMetadataFactoryName());
}
public function testAddGetFilters(): void
{
self::assertNull($this->configuration->getFilterClassName('NonExistingFilter'));
$this->configuration->addFilter('FilterName', self::class);
self::assertSame(self::class, $this->configuration->getFilterClassName('FilterName'));
}
public function setDefaultRepositoryClassName(): void
{
self::assertSame(EntityRepository::class, $this->configuration->getDefaultRepositoryClassName());
$this->configuration->setDefaultRepositoryClassName(DDC753CustomRepository::class);
self::assertSame(DDC753CustomRepository::class, $this->configuration->getDefaultRepositoryClassName());
$this->expectException(InvalidEntityRepository::class);
$this->expectExceptionMessage('Invalid repository class \'Doctrine\Tests\ORM\ConfigurationTest\'. It must be a Doctrine\ORM\EntityRepository.');
$this->configuration->setDefaultRepositoryClassName(self::class);
}
public function testSetDeprecatedDefaultRepositoryClassName(): void
{
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/9533');
$this->configuration->setDefaultRepositoryClassName(DeprecatedRepository::class);
self::assertSame(DeprecatedRepository::class, $this->configuration->getDefaultRepositoryClassName());
}
public function testSetGetNamingStrategy(): void
{
self::assertInstanceOf(NamingStrategy::class, $this->configuration->getNamingStrategy());
$namingStrategy = $this->createMock(NamingStrategy::class);
$this->configuration->setNamingStrategy($namingStrategy);
self::assertSame($namingStrategy, $this->configuration->getNamingStrategy());
}
public function testSetGetQuoteStrategy(): void
{
self::assertInstanceOf(QuoteStrategy::class, $this->configuration->getQuoteStrategy());
$quoteStrategy = $this->createMock(QuoteStrategy::class);
$this->configuration->setQuoteStrategy($quoteStrategy);
self::assertSame($quoteStrategy, $this->configuration->getQuoteStrategy());
}
/** @group DDC-1955 */
public function testSetGetEntityListenerResolver(): void
{
self::assertInstanceOf(EntityListenerResolver::class, $this->configuration->getEntityListenerResolver());
self::assertInstanceOf(AnnotationNamespace\DefaultEntityListenerResolver::class, $this->configuration->getEntityListenerResolver());
$resolver = $this->createMock(EntityListenerResolver::class);
$this->configuration->setEntityListenerResolver($resolver);
self::assertSame($resolver, $this->configuration->getEntityListenerResolver());
}
/** @group DDC-2183 */
public function testSetGetSecondLevelCacheConfig(): void
{
$mockClass = $this->createMock(CacheConfiguration::class);
self::assertNull($this->configuration->getSecondLevelCacheConfiguration());
$this->configuration->setSecondLevelCacheConfiguration($mockClass);
self::assertEquals($mockClass, $this->configuration->getSecondLevelCacheConfiguration());
}
/** @group GH10313 */
public function testSetGetTypedFieldMapper(): void
{
self::assertEmpty($this->configuration->getTypedFieldMapper());
$defaultTypedFieldMapper = new DefaultTypedFieldMapper();
$this->configuration->setTypedFieldMapper($defaultTypedFieldMapper);
self::assertSame($defaultTypedFieldMapper, $this->configuration->getTypedFieldMapper());
}
}
class ConfigurationTestAnnotationReaderChecker
{
/** @PrePersist */
public function simpleAnnotationMethod(): void
{
}
/** @AnnotationNamespace\PrePersist */
public function namespacedAnnotationMethod(): void
{
}
}
class DeprecatedRepository implements ObjectRepository
{
/**
* {@inheritdoc}
*/
public function find($id)
{
return null;
}
public function findAll(): array
{
return [];
}
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array
{
return [];
}
/**
* {@inheritdoc}
*/
public function findOneBy(array $criteria)
{
return null;
}
public function getClassName(): string
{
return stdClass::class;
}
}
|