|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MsgPhp\Domain\Infra\Doctrine\Test; |
| 6 | + |
| 7 | +use Doctrine\Common\Annotations\AnnotationReader; |
| 8 | +use Doctrine\DBAL\Types\Type; |
| 9 | +use Doctrine\ORM\Configuration; |
| 10 | +use Doctrine\ORM\EntityManager; |
| 11 | +use Doctrine\ORM\Mapping\Driver\AnnotationDriver; |
| 12 | +use Doctrine\ORM\Tools\SchemaTool; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author Roland Franssen <franssen.roland@gmail.com> |
| 16 | + * |
| 17 | + * @internal |
| 18 | + */ |
| 19 | +trait EntityManagerTrait |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var EntityManager |
| 23 | + */ |
| 24 | + private static $em; |
| 25 | + |
| 26 | + public static function setUpBeforeClass(): void |
| 27 | + { |
| 28 | + foreach (self::getTypes() as $type) { |
| 29 | + if (Type::hasType($type::NAME)) { |
| 30 | + Type::overrideType($type::NAME, $type); |
| 31 | + } else { |
| 32 | + Type::addType($type::NAME, $type); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + $paths = self::getEntityPaths(); |
| 37 | + $config = new Configuration(); |
| 38 | + $config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader(), \is_array($paths) ? $paths : iterator_to_array($paths))); |
| 39 | + $config->setProxyDir(sys_get_temp_dir().'/msgphp_'.md5(static::class."\0".microtime())); |
| 40 | + $config->setProxyNamespace(static::class); |
| 41 | + |
| 42 | + self::$em = EntityManager::create(['driver' => 'pdo_sqlite', 'memory' => true], $config); |
| 43 | + } |
| 44 | + |
| 45 | + public static function tearDownAfterClass(): void |
| 46 | + { |
| 47 | + /** @psalm-suppress DocblockTypeContradiction */ |
| 48 | + if (null === self::$em) { |
| 49 | + throw new \LogicException('Entity manager not set.'); |
| 50 | + } |
| 51 | + |
| 52 | + $proxyDir = self::$em->getConfiguration()->getProxyDir(); |
| 53 | + |
| 54 | + if (null !== $proxyDir && is_dir($proxyDir)) { |
| 55 | + /** @var \SplFileInfo $file */ |
| 56 | + foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($proxyDir, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $file) { |
| 57 | + if ($file->isDir()) { |
| 58 | + @rmdir($file->getRealPath() ?: $file->getPath()); |
| 59 | + } else { |
| 60 | + @unlink($file->getRealPath() ?: $file->getPath()); |
| 61 | + } |
| 62 | + } |
| 63 | + @rmdir($proxyDir); |
| 64 | + } |
| 65 | + |
| 66 | + self::$em->close(); |
| 67 | + self::$em = null; |
| 68 | + } |
| 69 | + |
| 70 | + protected function setUp(): void |
| 71 | + { |
| 72 | + /** @psalm-suppress DocblockTypeContradiction */ |
| 73 | + if (null === self::$em) { |
| 74 | + throw new \LogicException('Entity manager not set.'); |
| 75 | + } |
| 76 | + |
| 77 | + if (!self::$em->isOpen()) { |
| 78 | + self::$em = self::$em::create(self::$em->getConnection(), self::$em->getConfiguration(), self::$em->getEventManager()); |
| 79 | + } |
| 80 | + |
| 81 | + if (self::createSchema()) { |
| 82 | + (new SchemaTool(self::$em))->createSchema(self::$em->getMetadataFactory()->getAllMetadata()); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + protected function tearDown(): void |
| 87 | + { |
| 88 | + /** @psalm-suppress DocblockTypeContradiction */ |
| 89 | + if (null === self::$em) { |
| 90 | + throw new \LogicException('Entity manager not set.'); |
| 91 | + } |
| 92 | + |
| 93 | + self::$em->clear(); |
| 94 | + |
| 95 | + if (self::createSchema()) { |
| 96 | + (new SchemaTool(self::$em))->dropDatabase(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + abstract protected static function createSchema(): bool; |
| 101 | + |
| 102 | + abstract protected static function getEntityPaths(): iterable; |
| 103 | + |
| 104 | + abstract protected static function getTypes(): iterable; |
| 105 | +} |
0 commit comments