|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Cycle\ORM\Entity\Behavior\Identifier\Tests\Functional\Driver\Common; |
| 6 | + |
| 7 | +use Cycle\Annotated\Entities; |
| 8 | +use Cycle\Annotated\Locator\EntityLocatorInterface; |
| 9 | +use Cycle\Annotated\MergeColumns; |
| 10 | +use Cycle\Annotated\MergeIndexes; |
| 11 | +use Cycle\Database\Config\DatabaseConfig; |
| 12 | +use Cycle\Database\Database; |
| 13 | +use Cycle\Database\DatabaseManager; |
| 14 | +use Cycle\Database\Driver\DriverInterface; |
| 15 | +use Cycle\Database\Driver\Handler; |
| 16 | +use Cycle\ORM\Collection\ArrayCollectionFactory; |
| 17 | +use Cycle\ORM\Config\RelationConfig; |
| 18 | +use Cycle\ORM\Entity\Behavior\EventDrivenCommandGenerator; |
| 19 | +use Cycle\ORM\Entity\Behavior\Identifier\Tests\Traits\Loggable; |
| 20 | +use Cycle\ORM\Entity\Behavior\Identifier\Tests\Utils\SimpleContainer; |
| 21 | +use Cycle\ORM\EntityManager; |
| 22 | +use Cycle\ORM\Factory; |
| 23 | +use Cycle\ORM\SchemaInterface; |
| 24 | +use Cycle\ORM\ORM; |
| 25 | +use Cycle\Schema\Compiler; |
| 26 | +use Cycle\Schema\Generator\GenerateModifiers; |
| 27 | +use Cycle\Schema\Generator\GenerateRelations; |
| 28 | +use Cycle\Schema\Generator\GenerateTypecast; |
| 29 | +use Cycle\Schema\Generator\RenderModifiers; |
| 30 | +use Cycle\Schema\Generator\RenderRelations; |
| 31 | +use Cycle\Schema\Generator\RenderTables; |
| 32 | +use Cycle\Schema\Generator\ResetTables; |
| 33 | +use Cycle\Schema\Generator\ValidateEntities; |
| 34 | +use Cycle\Schema\Registry; |
| 35 | +use PHPUnit\Framework\TestCase; |
| 36 | +use Spiral\Attributes\AnnotationReader; |
| 37 | +use Spiral\Attributes\AttributeReader; |
| 38 | +use Spiral\Attributes\Composite\SelectiveReader; |
| 39 | +use Spiral\Attributes\ReaderInterface; |
| 40 | + |
| 41 | +abstract class BaseTest extends TestCase |
| 42 | +{ |
| 43 | + use Loggable; |
| 44 | + |
| 45 | + public const DRIVER = null; |
| 46 | + |
| 47 | + public static array $config; |
| 48 | + protected ?DatabaseManager $dbal = null; |
| 49 | + protected ?ORM $orm = null; |
| 50 | + protected ?DriverInterface $driver = null; |
| 51 | + private static array $driverCache = []; |
| 52 | + |
| 53 | + public static function readersDataProvider(): \Traversable |
| 54 | + { |
| 55 | + yield [new AnnotationReader()]; |
| 56 | + yield [new AttributeReader()]; |
| 57 | + yield [new SelectiveReader([new AttributeReader(), new AnnotationReader()])]; |
| 58 | + } |
| 59 | + |
| 60 | + public function getDriver(): DriverInterface |
| 61 | + { |
| 62 | + if (isset(static::$driverCache[static::DRIVER])) { |
| 63 | + return static::$driverCache[static::DRIVER]; |
| 64 | + } |
| 65 | + |
| 66 | + $config = self::$config[static::DRIVER]; |
| 67 | + if (!isset($this->driver)) { |
| 68 | + $this->driver = $config->driver::create($config); |
| 69 | + } |
| 70 | + |
| 71 | + return static::$driverCache[static::DRIVER] = $this->driver; |
| 72 | + } |
| 73 | + |
| 74 | + public function withSchema(SchemaInterface $schema): ORM |
| 75 | + { |
| 76 | + $this->orm = new ORM( |
| 77 | + new Factory( |
| 78 | + $this->dbal, |
| 79 | + RelationConfig::getDefault(), |
| 80 | + null, |
| 81 | + new ArrayCollectionFactory(), |
| 82 | + ), |
| 83 | + $schema, |
| 84 | + new EventDrivenCommandGenerator($schema, new SimpleContainer()), |
| 85 | + ); |
| 86 | + |
| 87 | + return $this->orm; |
| 88 | + } |
| 89 | + |
| 90 | + public function compileWithTokenizer(EntityLocatorInterface $tokenizer, ReaderInterface $reader): void |
| 91 | + { |
| 92 | + (new Compiler())->compile($this->registry = new Registry($this->dbal), [ |
| 93 | + new Entities($tokenizer, $reader), |
| 94 | + new ResetTables(), |
| 95 | + new MergeColumns($reader), |
| 96 | + new MergeIndexes($reader), |
| 97 | + new GenerateRelations(), |
| 98 | + new GenerateModifiers(), |
| 99 | + new ValidateEntities(), |
| 100 | + new RenderTables(), |
| 101 | + new RenderRelations(), |
| 102 | + new RenderModifiers(), |
| 103 | + new GenerateTypecast(), |
| 104 | + ]); |
| 105 | + } |
| 106 | + |
| 107 | + public function setUp(): void |
| 108 | + { |
| 109 | + $this->dbal = new DatabaseManager(new DatabaseConfig()); |
| 110 | + $this->dbal->addDatabase( |
| 111 | + new Database( |
| 112 | + 'default', |
| 113 | + '', |
| 114 | + $this->getDriver(), |
| 115 | + ), |
| 116 | + ); |
| 117 | + |
| 118 | + if (self::$config['debug'] ?? false) { |
| 119 | + $this->setUpLogger($this->getDriver()); |
| 120 | + $this->enableProfiling(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public function tearDown(): void |
| 125 | + { |
| 126 | + $this->dropDatabase($this->dbal->database('default')); |
| 127 | + |
| 128 | + $this->orm = null; |
| 129 | + $this->dbal = null; |
| 130 | + |
| 131 | + if (\function_exists('gc_collect_cycles')) { |
| 132 | + \gc_collect_cycles(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + protected function dropDatabase(?Database $database = null): void |
| 137 | + { |
| 138 | + if ($database === null) { |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + foreach ($database->getTables() as $table) { |
| 143 | + $schema = $table->getSchema(); |
| 144 | + |
| 145 | + foreach ($schema->getForeignKeys() as $foreign) { |
| 146 | + $schema->dropForeignKey($foreign->getColumns()); |
| 147 | + } |
| 148 | + |
| 149 | + $schema->save(Handler::DROP_FOREIGN_KEYS); |
| 150 | + } |
| 151 | + |
| 152 | + foreach ($database->getTables() as $table) { |
| 153 | + $schema = $table->getSchema(); |
| 154 | + $schema->declareDropped(); |
| 155 | + $schema->save(); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + protected function getDatabase(): Database |
| 160 | + { |
| 161 | + return $this->dbal->database('default'); |
| 162 | + } |
| 163 | + |
| 164 | + protected function save(object ...$entities): void |
| 165 | + { |
| 166 | + $em = new EntityManager($this->orm); |
| 167 | + foreach ($entities as $entity) { |
| 168 | + $em->persist($entity); |
| 169 | + } |
| 170 | + $em->run(); |
| 171 | + } |
| 172 | +} |
0 commit comments