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
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\Persistence\Mapping;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\Driver\FileDriver;
use Doctrine\Persistence\Mapping\Driver\FileLocator;
use Doctrine\Tests\DoctrineTestCase;
use Doctrine\Tests\Persistence\Mapping\Fixtures\AnotherGlobalClass;
use Doctrine\Tests\Persistence\Mapping\Fixtures\GlobalClass;
use Doctrine\Tests\Persistence\Mapping\Fixtures\NotLoadedClass;
use Doctrine\Tests\Persistence\Mapping\Fixtures\TestClassMetadata;
use PHPUnit\Framework\MockObject\MockObject;
use stdClass;
use function str_contains;
class FileDriverTest extends DoctrineTestCase
{
public function testGlobalBasename(): void
{
$driver = $this->createTestFileDriver([]);
self::assertSame('', $driver->getGlobalBasename());
$driver->setGlobalBasename('global');
self::assertSame('global', $driver->getGlobalBasename());
}
public function testGetElementFromGlobalFile(): void
{
$driver = $this->createTestFileDriver($this->newLocator());
$driver->setGlobalBasename('global');
$element = $driver->getElement(GlobalClass::class);
self::assertSame(GlobalClass::class, $element->getName());
}
public function testGetElementFromFile(): void
{
$locator = $this->newLocator();
$locator->expects(self::once())
->method('findMappingFile')
->with(self::equalTo(stdClass::class))
->willReturn(__DIR__ . '/_files/stdClass.yml');
$driver = $this->createTestFileDriver($locator);
self::assertSame(stdClass::class, $driver->getElement(stdClass::class)->getName());
}
public function testGetElementUpdatesClassCache(): void
{
$locator = $this->newLocator();
// findMappingFile should only be called once
$locator->expects(self::once())
->method('findMappingFile')
->with(self::equalTo(stdClass::class))
->willReturn(__DIR__ . '/_files/stdClass.yml');
$driver = $this->createTestFileDriver($locator);
// not cached
self::assertSame(stdClass::class, $driver->getElement(stdClass::class)->getName());
// cached call
self::assertSame(stdClass::class, $driver->getElement(stdClass::class)->getName());
}
public function testGetAllClassNamesGlobalBasename(): void
{
$locator = $this->newLocator();
$locator->expects(self::any())->method('getAllClassNames')->with('global')->willReturn([
GlobalClass::class,
AnotherGlobalClass::class,
]);
$driver = $this->createTestFileDriver($locator);
$driver->setGlobalBasename('global');
$classNames = $driver->getAllClassNames();
self::assertSame([GlobalClass::class, AnotherGlobalClass::class], $classNames);
}
public function testGetAllClassNamesFromMappingFile(): void
{
$locator = $this->newLocator();
$locator->expects(self::any())
->method('getAllClassNames')
->with(self::equalTo(null))
->willReturn([stdClass::class]);
$driver = new TestFileDriver($locator);
$classNames = $driver->getAllClassNames();
self::assertSame([stdClass::class], $classNames);
}
public function testGetAllClassNamesBothSources(): void
{
$locator = $this->newLocator();
$locator->expects(self::any())
->method('getAllClassNames')
->with(self::equalTo('global'))
->willReturn([stdClass::class]);
$driver = new TestFileDriver($locator);
$driver->setGlobalBasename('global');
$classNames = $driver->getAllClassNames();
self::assertSame([GlobalClass::class, AnotherGlobalClass::class, stdClass::class], $classNames);
}
public function testGetAllClassNamesBothSourcesNoDupes(): void
{
$locator = $this->newLocator();
$locator->expects(self::once())
->method('getAllClassNames')
->with(self::equalTo('global'))
->willReturn([stdClass::class]);
$locator->expects(self::once())
->method('findMappingFile')
->with(self::equalTo(stdClass::class))
->willReturn(__DIR__ . '/_files/stdClass.yml');
$driver = new TestFileDriver($locator);
$driver->setGlobalBasename('global');
$driver->getElement(stdClass::class);
$classNames = $driver->getAllClassNames();
self::assertSame([GlobalClass::class, AnotherGlobalClass::class, stdClass::class], $classNames);
}
public function testIsNotTransient(): void
{
$locator = $this->newLocator();
$locator->expects(self::once())
->method('fileExists')
->with(self::equalTo(stdClass::class))
->willReturn(true);
$driver = $this->createTestFileDriver($locator);
$driver->setGlobalBasename('global');
self::assertFalse($driver->isTransient(stdClass::class));
self::assertFalse($driver->isTransient(GlobalClass::class));
self::assertFalse($driver->isTransient(AnotherGlobalClass::class));
}
public function testIsTransient(): void
{
$locator = $this->newLocator();
$locator->expects(self::once())
->method('fileExists')
->with(self::equalTo(NotLoadedClass::class))
->willReturn(false);
$driver = $this->createTestFileDriver($locator);
self::assertTrue($driver->isTransient(NotLoadedClass::class));
}
public function testNonLocatorFallback(): void
{
$driver = new TestFileDriver(__DIR__ . '/_files', '.yml');
self::assertTrue($driver->isTransient(NotLoadedClass::class));
self::assertFalse($driver->isTransient(stdClass::class));
}
/** @return FileLocator&MockObject */
private function newLocator(): MockObject
{
$locator = $this->createMock(FileLocator::class);
$locator->expects(self::any())->method('getFileExtension')->willReturn('.yml');
$locator->expects(self::any())->method('getPaths')->willReturn([__DIR__ . '/_files']);
return $locator;
}
/** @param string|array<int, string>|FileLocator $locator */
private function createTestFileDriver(string|array|FileLocator $locator, string|null $fileExtension = null): TestFileDriver
{
$driver = new TestFileDriver($locator, $fileExtension);
$driver->stdClass = $this->createMock(ClassMetadata::class);
$driver->stdGlobal = $this->createMock(ClassMetadata::class);
$driver->stdGlobal2 = $this->createMock(ClassMetadata::class);
return $driver;
}
}
/** @template-extends FileDriver<TestClassMetadata<object>> */
class TestFileDriver extends FileDriver
{
/** @var ClassMetadata<object> */
public ClassMetadata $stdGlobal;
/** @var ClassMetadata<object> */
public ClassMetadata $stdGlobal2;
/** @var ClassMetadata<object> */
public ClassMetadata $stdClass;
/**
* {@inheritDoc}
*/
protected function loadMappingFile(string $file): array
{
if (str_contains($file, 'global.yml')) {
return [
GlobalClass::class => new TestClassMetadata(GlobalClass::class),
AnotherGlobalClass::class => new TestClassMetadata(AnotherGlobalClass::class),
];
}
return [stdClass::class => new TestClassMetadata(stdClass::class)];
}
/** @param ClassMetadata<object> $metadata */
public function loadMetadataForClass(string $className, ClassMetadata $metadata): void
{
}
}
|