diff --git a/composer.json b/composer.json index cda8a4c..5ac3460 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,7 @@ "cycle/schema-migrations-generator": "^2.1", "cycle/schema-renderer": "^1.2", "cycle/schema-builder": "^2.7", + "cycle/schema-provider": "^1.0", "doctrine/inflector": "^1.4 || ^2.0", "spiral/attributes": "^2.10 || ^3.0", "spiral/reactor": "^3.0", diff --git a/src/Bootloader/AnnotatedBootloader.php b/src/Bootloader/AnnotatedBootloader.php index f22d332..3411b2a 100644 --- a/src/Bootloader/AnnotatedBootloader.php +++ b/src/Bootloader/AnnotatedBootloader.php @@ -5,72 +5,78 @@ namespace Spiral\Cycle\Bootloader; use Cycle\Annotated; +use Cycle\Annotated\Locator\EmbeddingLocatorInterface; +use Cycle\Annotated\Locator\EntityLocatorInterface; use Spiral\Attributes\ReaderInterface; use Spiral\Boot\Bootloader\Bootloader; use Spiral\Bootloader\Attributes\AttributesBootloader; +use Spiral\Core\FactoryInterface; use Spiral\Cycle\Annotated\Locator\ListenerEmbeddingsLocator; use Spiral\Cycle\Annotated\Locator\ListenerEntityLocator; +use Spiral\Cycle\Config\CycleConfig; +use Spiral\Cycle\Schema\Provider\AnnotatedSchemaProvider; use Spiral\Tokenizer\Bootloader\TokenizerListenerBootloader; final class AnnotatedBootloader extends Bootloader { - protected const DEPENDENCIES = [ - SchemaBootloader::class, - TokenizerListenerBootloader::class, - AttributesBootloader::class, - ]; - - protected const BINDINGS = [ - Annotated\Embeddings::class => [self::class, 'initEmbeddings'], - Annotated\Entities::class => [self::class, 'initEntities'], - Annotated\MergeColumns::class => [self::class, 'initMergeColumns'], - Annotated\TableInheritance::class => [self::class, 'initTableInheritance'], - Annotated\MergeIndexes::class => [self::class, 'initMergeIndexes'], - ]; + public function defineDependencies(): array + { + return [ + SchemaBootloader::class, + TokenizerListenerBootloader::class, + AttributesBootloader::class, + ]; + } - protected const SINGLETONS = [ - ListenerEntityLocator::class => ListenerEntityLocator::class, - ListenerEmbeddingsLocator::class => ListenerEmbeddingsLocator::class, - ]; + public function defineSingletons(): array + { + return [ + ListenerEntityLocator::class => ListenerEntityLocator::class, + ListenerEmbeddingsLocator::class => ListenerEmbeddingsLocator::class, + EmbeddingLocatorInterface::class => ListenerEmbeddingsLocator::class, + EntityLocatorInterface::class => ListenerEntityLocator::class, + AnnotatedSchemaProvider::class => static function (FactoryInterface $factory, SchemaBootloader $schema, CycleConfig $config) { + return $factory->make(AnnotatedSchemaProvider::class, ['generators' => $schema->getGenerators($config)]); + }, + ]; + } public function init( - SchemaBootloader $schema, TokenizerListenerBootloader $tokenizer, ListenerEntityLocator $entityLocator, ListenerEmbeddingsLocator $embeddingsLocator ): void { $tokenizer->addListener($entityLocator); $tokenizer->addListener($embeddingsLocator); - - $schema->addGenerator(SchemaBootloader::GROUP_INDEX, Annotated\Embeddings::class); - $schema->addGenerator(SchemaBootloader::GROUP_INDEX, Annotated\Entities::class); - $schema->addGenerator(SchemaBootloader::GROUP_INDEX, Annotated\TableInheritance::class); - $schema->addGenerator(SchemaBootloader::GROUP_INDEX, Annotated\MergeColumns::class); - $schema->addGenerator(SchemaBootloader::GROUP_RENDER, Annotated\MergeIndexes::class); - } - - private function initEmbeddings( - ReaderInterface $reader, - ListenerEmbeddingsLocator $embeddingsLocator - ): Annotated\Embeddings { - return new Annotated\Embeddings($embeddingsLocator, $reader); } + /** + * @deprecated since v2.10.0. Will be removed in v3.0.0. + */ public function initEntities(ReaderInterface $reader, ListenerEntityLocator $entityLocator): Annotated\Entities { return new Annotated\Entities($entityLocator, $reader); } + /** + * @deprecated since v2.10.0. Will be removed in v3.0.0. + */ public function initMergeColumns(ReaderInterface $reader): Annotated\MergeColumns { return new Annotated\MergeColumns($reader); } + /** + * @deprecated since v2.10.0. Will be removed in v3.0.0. + */ public function initTableInheritance(ReaderInterface $reader): Annotated\TableInheritance { return new Annotated\TableInheritance($reader); } + /** + * @deprecated since v2.10.0. Will be removed in v3.0.0. + */ public function initMergeIndexes(ReaderInterface $reader): Annotated\MergeIndexes { return new Annotated\MergeIndexes($reader); diff --git a/src/Bootloader/CycleOrmBootloader.php b/src/Bootloader/CycleOrmBootloader.php index 90f7c04..4f2085d 100644 --- a/src/Bootloader/CycleOrmBootloader.php +++ b/src/Bootloader/CycleOrmBootloader.php @@ -13,15 +13,18 @@ use Cycle\ORM\ORM; use Cycle\ORM\ORMInterface; use Cycle\ORM\RepositoryInterface; +use Cycle\Schema\Provider\PhpFileSchemaProvider; use Psr\Container\ContainerInterface; use Spiral\Boot\AbstractKernel; use Spiral\Boot\Bootloader\Bootloader; +use Spiral\Boot\DirectoriesInterface; use Spiral\Boot\EnvironmentInterface; use Spiral\Boot\FinalizerInterface; use Spiral\Config\ConfiguratorInterface; use Spiral\Core\Container; use Spiral\Cycle\Config\CycleConfig; use Spiral\Cycle\Injector\RepositoryInjector; +use Spiral\Cycle\Schema\Provider\AnnotatedSchemaProvider; final class CycleOrmBootloader extends Bootloader { @@ -39,7 +42,8 @@ final class CycleOrmBootloader extends Bootloader public function __construct( private readonly ConfiguratorInterface $config, - private readonly EnvironmentInterface $env + private readonly EnvironmentInterface $env, + private readonly DirectoriesInterface $dirs, ) { } @@ -113,6 +117,13 @@ private function initOrmConfig(): void 'collections' => [], ], 'warmup' => $this->env->get('CYCLE_SCHEMA_WARMUP', false), + 'schemaProviders' => [ + PhpFileSchemaProvider::class => PhpFileSchemaProvider::config( + file: $this->dirs->get('runtime') . '/cycle.php', + mode: PhpFileSchemaProvider::MODE_READ_AND_WRITE + ), + AnnotatedSchemaProvider::class + ], ] ); } diff --git a/src/Bootloader/SchemaBootloader.php b/src/Bootloader/SchemaBootloader.php index 027762b..810b28e 100644 --- a/src/Bootloader/SchemaBootloader.php +++ b/src/Bootloader/SchemaBootloader.php @@ -4,17 +4,19 @@ namespace Spiral\Cycle\Bootloader; +use Cycle\ORM\Schema; use Cycle\ORM\SchemaInterface; use Cycle\Schema\Defaults; use Cycle\Schema\Generator; use Cycle\Schema\GeneratorInterface; +use Cycle\Schema\Provider\SchemaProviderInterface; +use Cycle\Schema\Provider\Support\SchemaProviderPipeline; use Cycle\Schema\Registry; +use Psr\Container\ContainerInterface; use Spiral\Boot\Bootloader\Bootloader; -use Spiral\Boot\MemoryInterface; use Spiral\Core\Container; use Spiral\Core\FactoryInterface; use Spiral\Cycle\Config\CycleConfig; -use Spiral\Cycle\Schema\Compiler; use Spiral\Tokenizer\Bootloader\TokenizerBootloader; final class SchemaBootloader extends Bootloader implements Container\SingletonInterface @@ -23,16 +25,6 @@ final class SchemaBootloader extends Bootloader implements Container\SingletonIn public const GROUP_RENDER = 'render'; public const GROUP_POSTPROCESS = 'postprocess'; - protected const DEPENDENCIES = [ - TokenizerBootloader::class, - CycleOrmBootloader::class, - ]; - - protected const BINDINGS = [ - SchemaInterface::class => [self::class, 'schema'], - Registry::class => [self::class, 'initRegistry'] - ]; - /** @var string[][]|GeneratorInterface[][] */ private array $defaultGenerators; @@ -61,7 +53,31 @@ public function __construct( ]; } - public function addGenerator(string $group, string $generator): void + public function defineDependencies(): array + { + return [ + TokenizerBootloader::class, + CycleOrmBootloader::class, + ]; + } + + public function defineBindings(): array + { + return [ + Registry::class => [self::class, 'initRegistry'], + SchemaInterface::class => static fn (SchemaProviderInterface $provider): SchemaInterface => new Schema( + $provider->read() ?? [] + ), + SchemaProviderInterface::class => static function (ContainerInterface $container): SchemaProviderInterface { + /** @var CycleConfig $config */ + $config = $container->get(CycleConfig::class); + + return (new SchemaProviderPipeline($container))->withConfig($config->getSchemaProviders()); + }, + ]; + } + + public function addGenerator(string $group, string|GeneratorInterface $generator): void { $this->defaultGenerators[$group][] = $generator; } @@ -93,26 +109,6 @@ public function getGenerators(CycleConfig $config): array return $result; } - /** - * @throws \Throwable - */ - protected function schema(MemoryInterface $memory, CycleConfig $config): SchemaInterface - { - $schemaCompiler = Compiler::fromMemory($memory); - - if ($schemaCompiler->isEmpty() || ! $config->cacheSchema()) { - $schemaCompiler = Compiler::compile( - $this->container->get(Registry::class), - $this->getGenerators($config), - $config->getSchemaDefaults() - ); - - $schemaCompiler->toMemory($memory); - } - - return $schemaCompiler->toSchema(); - } - private function initRegistry(FactoryInterface $factory, CycleConfig $config): Registry { $defaults = new Defaults(); diff --git a/src/Config/CycleConfig.php b/src/Config/CycleConfig.php index 78fec74..d6c6780 100644 --- a/src/Config/CycleConfig.php +++ b/src/Config/CycleConfig.php @@ -59,4 +59,9 @@ public function warmup(): bool { return (bool)($this->config['warmup'] ?? false); } + + public function getSchemaProviders(): array + { + return (array)($this->config['schemaProviders'] ?? []); + } } diff --git a/src/Console/Command/CycleOrm/Generator/ShowChanges.php b/src/Console/Command/CycleOrm/Generator/ShowChanges.php index 2c29159..b64599a 100644 --- a/src/Console/Command/CycleOrm/Generator/ShowChanges.php +++ b/src/Console/Command/CycleOrm/Generator/ShowChanges.php @@ -12,6 +12,7 @@ /** * @deprecated. Use {@see \Cycle\Schema\Generator\PrintChanges} instead. Will be removed in v3.0. + * @codeCoverageIgnore */ final class ShowChanges implements GeneratorInterface { diff --git a/src/Console/Command/CycleOrm/MigrateCommand.php b/src/Console/Command/CycleOrm/MigrateCommand.php index 96445cf..7807108 100644 --- a/src/Console/Command/CycleOrm/MigrateCommand.php +++ b/src/Console/Command/CycleOrm/MigrateCommand.php @@ -4,6 +4,7 @@ namespace Spiral\Cycle\Console\Command\CycleOrm; +use Cycle\Schema\Compiler; use Cycle\Schema\Generator\Migrations\Strategy\GeneratorStrategyInterface; use Cycle\Schema\Generator\Migrations\Strategy\MultipleFilesStrategy; use Cycle\Schema\Generator\PrintChanges; @@ -13,11 +14,10 @@ use Spiral\Cycle\Console\Command\Migrate\AbstractCommand; use Cycle\Migrations\State; use Cycle\Schema\Generator\Migrations\GenerateMigrations; -use Spiral\Cycle\Schema\Compiler; use Cycle\Schema\Registry; -use Spiral\Boot\MemoryInterface; use Spiral\Console\Console; use Cycle\Migrations\Migrator; +use Spiral\Cycle\Schema\Provider\AnnotatedSchemaProvider; use Symfony\Component\Console\Input\InputOption; final class MigrateCommand extends AbstractCommand @@ -33,9 +33,9 @@ public function perform( SchemaBootloader $bootloader, CycleConfig $config, Registry $registry, - MemoryInterface $memory, Migrator $migrator, Console $console, + AnnotatedSchemaProvider $provider, ): int { $migrator->configure(); @@ -54,15 +54,10 @@ public function perform( $this->comment('Detecting schema changes...'); - $schemaCompiler = Compiler::compile( - $registry, - \array_merge($bootloader->getGenerators($config), [ - $print = new PrintChanges($this->output), - ]), - $config->getSchemaDefaults(), - ); - - $schemaCompiler->toMemory($memory); + $provider = $provider->withGenerators(\array_merge($bootloader->getGenerators($config), [ + $print = new PrintChanges($this->output) + ])); + $provider->read(); if ($print->hasChanges()) { if ($this->option('split')) { @@ -72,7 +67,7 @@ public function perform( $migrations = $this->container->get(GenerateMigrations::class); - (new \Cycle\Schema\Compiler())->compile($registry, [$migrations]); + (new Compiler())->compile($registry, \array_merge($provider->getGenerators(), [$migrations])); if ($this->option('run')) { $console->run('migrate', [], $this->output); diff --git a/src/Console/Command/CycleOrm/SyncCommand.php b/src/Console/Command/CycleOrm/SyncCommand.php index 7a307bf..017a970 100644 --- a/src/Console/Command/CycleOrm/SyncCommand.php +++ b/src/Console/Command/CycleOrm/SyncCommand.php @@ -7,11 +7,9 @@ use Spiral\Cycle\Bootloader\SchemaBootloader; use Cycle\Schema\Generator\PrintChanges; use Cycle\Schema\Generator\SyncTables; -use Cycle\Schema\Registry; -use Spiral\Boot\MemoryInterface; use Spiral\Cycle\Config\CycleConfig; use Spiral\Cycle\Console\Command\Migrate\AbstractCommand; -use Spiral\Cycle\Schema\Compiler; +use Spiral\Cycle\Schema\Provider\AnnotatedSchemaProvider; final class SyncCommand extends AbstractCommand { @@ -21,8 +19,7 @@ final class SyncCommand extends AbstractCommand public function perform( SchemaBootloader $bootloader, CycleConfig $config, - Registry $registry, - MemoryInterface $memory, + AnnotatedSchemaProvider $provider, ): int { if (!$this->verifyEnvironment(message: 'This operation is not recommended for production environment.')) { return self::FAILURE; @@ -30,13 +27,10 @@ public function perform( $print = new PrintChanges($this->output); - $schemaCompiler = Compiler::compile( - $registry, - \array_merge($bootloader->getGenerators($config), [$print, new SyncTables()]), - $config->getSchemaDefaults(), + $provider = $provider->withGenerators( + \array_merge($bootloader->getGenerators($config), [$print, new SyncTables()]) ); - - $schemaCompiler->toMemory($memory); + $provider->read(); if ($print->hasChanges()) { $this->info('ORM Schema has been synchronized with database.'); diff --git a/src/Console/Command/CycleOrm/UpdateCommand.php b/src/Console/Command/CycleOrm/UpdateCommand.php index ef83522..af7669b 100644 --- a/src/Console/Command/CycleOrm/UpdateCommand.php +++ b/src/Console/Command/CycleOrm/UpdateCommand.php @@ -4,31 +4,19 @@ namespace Spiral\Cycle\Console\Command\CycleOrm; -use Cycle\Schema\Registry; -use Spiral\Boot\MemoryInterface; +use Cycle\Schema\Provider\SchemaProviderInterface; use Spiral\Console\Command; -use Spiral\Cycle\Bootloader\SchemaBootloader; -use Spiral\Cycle\Config\CycleConfig; -use Spiral\Cycle\Schema\Compiler; final class UpdateCommand extends Command { protected const NAME = 'cycle'; protected const DESCRIPTION = 'Update (init) cycle schema from database and annotated classes'; - public function perform( - SchemaBootloader $bootloader, - CycleConfig $config, - Registry $registry, - MemoryInterface $memory, - ): int { + public function perform(SchemaProviderInterface $schemaProvider): int + { $this->info('Updating ORM schema... '); - Compiler::compile( - $registry, - $bootloader->getGenerators($config), - $config->getSchemaDefaults(), - )->toMemory($memory); + $schemaProvider->read(); $this->info('Schema has been updated.'); diff --git a/src/Schema/Compiler.php b/src/Schema/Compiler.php index 451ae12..503706a 100644 --- a/src/Schema/Compiler.php +++ b/src/Schema/Compiler.php @@ -9,6 +9,10 @@ use Cycle\Schema\Registry; use Spiral\Boot\MemoryInterface; +/** + * @deprecated since v2.10.0. Will be removed in v3.0.0. Use {@see \Cycle\Schema\Compiler} instead. + * @codeCoverageIgnore + */ final class Compiler { private const MEMORY_SECTION = 'cycle'; diff --git a/src/Schema/Provider/AnnotatedSchemaProvider.php b/src/Schema/Provider/AnnotatedSchemaProvider.php new file mode 100644 index 0000000..f9ab9a5 --- /dev/null +++ b/src/Schema/Provider/AnnotatedSchemaProvider.php @@ -0,0 +1,110 @@ + + */ + private array $generators = []; + + public function __construct( + array $generators, + private readonly ReaderInterface $reader, + private readonly EntityLocatorInterface $entityLocator, + private readonly EmbeddingLocatorInterface $embeddingLocator, + private readonly ContainerInterface $container, + ) { + $this->generators = \array_merge($this->getAnnotatedGenerators(), $generators); + } + + /** + * Create a configuration array for the {@see self::withConfig()} method. + */ + public static function config(int $tableNaming): array + { + return [ + 'tableNaming' => $tableNaming, + ]; + } + + public function withConfig(array $config): self + { + if (\array_key_exists('tableNaming', $config) && !\is_int($config['tableNaming'])) { + throw new ConfigurationException('The `tableNaming` parameter must be an integer.'); + } + + $new = clone $this; + $new->tableNaming = $config['tableNaming'] ?? $this->tableNaming; + + return $new; + } + + /** + * @param array $generators + */ + public function withGenerators(array $generators): self + { + $new = clone $this; + $new->generators = \array_merge($this->getAnnotatedGenerators(), $generators); + + return $new; + } + + public function read(?SchemaProviderInterface $nextProvider = null): ?array + { + $defaults = new Defaults(); + $defaults->merge($this->container->get(CycleConfig::class)->getSchemaDefaults()); + + $schema = (new Compiler())->compile( + new Registry($this->container->get(DatabaseProviderInterface::class), $defaults), + $this->generators + ); + + return \count($schema) !== 0 || $nextProvider === null ? $schema : $nextProvider->read(); + } + + public function getGenerators(): array + { + return $this->generators; + } + + public function clear(): bool + { + return false; + } + + private function getAnnotatedGenerators(): array + { + return [ + new Embeddings($this->embeddingLocator, $this->reader), + new Entities($this->entityLocator, $this->reader, $this->tableNaming), + new TableInheritance($this->reader), + new MergeColumns($this->reader), + new MergeIndexes($this->reader), + ]; + } +} diff --git a/tests/app/Bootloader/AppBootloader.php b/tests/app/Bootloader/AppBootloader.php index cb2b963..fc2dfa3 100644 --- a/tests/app/Bootloader/AppBootloader.php +++ b/tests/app/Bootloader/AppBootloader.php @@ -4,6 +4,7 @@ namespace Spiral\App\Bootloader; +use Doctrine\Common\Annotations\AnnotationReader; use Spiral\App\Repositories\RoleRepository; use Spiral\App\Repositories\RoleRepositoryInterface; use Spiral\Bootloader\DomainBootloader; @@ -28,6 +29,8 @@ final class AppBootloader extends DomainBootloader public function init(CasterRegistryInterface $casterRegistry, EntityCaster $caster): void { + AnnotationReader::addGlobalIgnoredName('note'); + $casterRegistry->register($caster); } } diff --git a/tests/src/Bootloader/AnnotatedBootloaderTest.php b/tests/src/Bootloader/AnnotatedBootloaderTest.php index 81cf550..45769c4 100644 --- a/tests/src/Bootloader/AnnotatedBootloaderTest.php +++ b/tests/src/Bootloader/AnnotatedBootloaderTest.php @@ -4,8 +4,6 @@ namespace Spiral\Tests\Bootloader; -use Cycle\Annotated; -use Cycle\Schema\GeneratorInterface; use Spiral\Attributes\ReaderInterface; use Spiral\Cycle\Annotated\Locator\ListenerEmbeddingsLocator; use Spiral\Cycle\Annotated\Locator\ListenerEntityLocator; @@ -18,31 +16,6 @@ public function testGetsReader(): void $this->assertContainerBound(ReaderInterface::class); } - public function testGetsAnnotatedEmbeddings(): void - { - $this->assertContainerBound(Annotated\Embeddings::class, GeneratorInterface::class); - } - - public function testGetsAnnotatedEntities(): void - { - $this->assertContainerBound(Annotated\Entities::class, GeneratorInterface::class); - } - - public function testGetsAnnotatedMergeColumns(): void - { - $this->assertContainerBound(Annotated\MergeColumns::class, GeneratorInterface::class); - } - - public function testGetsAnnotatedTableInheritance(): void - { - $this->assertContainerBound(Annotated\TableInheritance::class, GeneratorInterface::class); - } - - public function testGetsAnnotatedMergeIndexes(): void - { - $this->assertContainerBound(Annotated\MergeIndexes::class, GeneratorInterface::class); - } - public function testGetsListenerEntityLocator(): void { $this->assertContainerBoundAsSingleton(ListenerEntityLocator::class, ListenerEntityLocator::class); diff --git a/tests/src/Bootloader/SchemaBootloaderTest.php b/tests/src/Bootloader/SchemaBootloaderTest.php index 821fadf..fee89c5 100644 --- a/tests/src/Bootloader/SchemaBootloaderTest.php +++ b/tests/src/Bootloader/SchemaBootloaderTest.php @@ -35,7 +35,7 @@ public function testGetsDefaultSchemaGenerators(): void { $generators = $this->bootloader->getGenerators($this->getContainer()->get(CycleConfig::class)); - $this->assertCount(15, $generators); + $this->assertCount(10, $generators); $this->assertContainsOnlyInstancesOf(GeneratorInterface::class, $generators); } diff --git a/tests/src/Console/Command/CycleOrm/MigrateCommandTest.php b/tests/src/Console/Command/CycleOrm/MigrateCommandTest.php index c53767a..1703885 100644 --- a/tests/src/Console/Command/CycleOrm/MigrateCommandTest.php +++ b/tests/src/Console/Command/CycleOrm/MigrateCommandTest.php @@ -5,7 +5,6 @@ namespace Spiral\Tests\Console\Command\CycleOrm; use Cycle\ORM\SchemaInterface; -use Spiral\Boot\MemoryInterface; use Spiral\Cycle\Annotated\Locator\ListenerEntityLocator; use Spiral\Cycle\Config\CycleConfig; use Spiral\Files\Files; @@ -110,27 +109,16 @@ class Tag public function testSchemaDefaultsShouldBePassedToCompiler(): void { + $config = $this->getContainer()->get(CycleConfig::class)->toArray(); $config['schema']['defaults'][SchemaInterface::TYPECAST_HANDLER][] = 'foo'; - $memory = new class implements MemoryInterface { - private mixed $data; - - public function loadData(string $section): mixed - { - return $this->data[$section]; - } - - public function saveData(string $section, mixed $data): void - { - $this->data[$section] = $data; - } - }; - $this->getContainer()->bind(CycleConfig::class, new CycleConfig($config)); - $this->getContainer()->bindSingleton(MemoryInterface::class, $memory); $this->runCommand('cycle:migrate'); - $this->assertSame(['foo'], $memory->loadData('cycle')['role'][SchemaInterface::TYPECAST_HANDLER]); + $this->assertSame( + ['foo'], + $this->getContainer()->get(SchemaInterface::class)->define('role', SchemaInterface::TYPECAST_HANDLER) + ); } } diff --git a/tests/src/Console/Command/CycleOrm/SyncCommandTest.php b/tests/src/Console/Command/CycleOrm/SyncCommandTest.php index 2ee137c..f5aaae1 100644 --- a/tests/src/Console/Command/CycleOrm/SyncCommandTest.php +++ b/tests/src/Console/Command/CycleOrm/SyncCommandTest.php @@ -6,7 +6,6 @@ use Cycle\ORM\SchemaInterface; use Spiral\App\Entities\User; -use Spiral\Boot\MemoryInterface; use Spiral\Cycle\Config\CycleConfig; use Spiral\Testing\Attribute\Env; use Spiral\Tests\ConsoleTest; @@ -59,27 +58,16 @@ public function testSyncDebug(): void #[Env('SAFE_MIGRATIONS', 'true')] public function testSchemaDefaultsShouldBePassedToCompiler(): void { + $config = $this->getContainer()->get(CycleConfig::class)->toArray(); $config['schema']['defaults'][SchemaInterface::TYPECAST_HANDLER][] = 'foo'; - $memory = new class implements MemoryInterface { - private mixed $data; - - public function loadData(string $section): mixed - { - return $this->data[$section]; - } - - public function saveData(string $section, mixed $data): void - { - $this->data[$section] = $data; - } - }; - $this->getContainer()->bind(CycleConfig::class, new CycleConfig($config)); - $this->getContainer()->bindSingleton(MemoryInterface::class, $memory); $this->runCommand('cycle:sync'); - $this->assertSame(['foo'], $memory->loadData('cycle')['role'][SchemaInterface::TYPECAST_HANDLER]); + $this->assertSame( + ['foo'], + $this->getContainer()->get(SchemaInterface::class)->define('role', SchemaInterface::TYPECAST_HANDLER) + ); } } diff --git a/tests/src/Console/Command/CycleOrm/UpdateCommandTest.php b/tests/src/Console/Command/CycleOrm/UpdateCommandTest.php index 9ec752d..519ce3e 100644 --- a/tests/src/Console/Command/CycleOrm/UpdateCommandTest.php +++ b/tests/src/Console/Command/CycleOrm/UpdateCommandTest.php @@ -6,8 +6,6 @@ use Cycle\ORM\Schema; use Cycle\ORM\SchemaInterface; -use Mockery as m; -use Spiral\Boot\MemoryInterface; use Spiral\Cycle\Config\CycleConfig; use Spiral\Tests\ConsoleTest; @@ -34,45 +32,18 @@ public function testGetSchema(): void ); } - public function testGetSchemaFromMemory(): void - { - $memory = m::mock(MemoryInterface::class); - $this->getContainer()->bind(MemoryInterface::class, $memory); - - $memory->shouldReceive('saveData')->once(); - $memory->shouldReceive('loadData')->once()->andReturn(new Schema([])); - $this->runCommand('cycle'); - - /** @var SchemaInterface $schema */ - $schema = $this->getContainer()->get(SchemaInterface::class); - - $this->assertFalse($schema->defines('user')); - } - public function testSchemaDefaultsShouldBePassedToCompiler(): void { + $config = $this->getContainer()->get(CycleConfig::class)->toArray(); $config['schema']['defaults'][SchemaInterface::TYPECAST_HANDLER][] = 'foo'; - $memory = new class implements MemoryInterface - { - private mixed $data; - - public function loadData(string $section): mixed - { - return $this->data[$section]; - } - - public function saveData(string $section, mixed $data): void - { - $this->data[$section] = $data; - } - }; - $this->getContainer()->bind(CycleConfig::class, new CycleConfig($config)); - $this->getContainer()->bindSingleton(MemoryInterface::class, $memory); $this->runCommand('cycle'); - $this->assertSame(['foo'], $memory->loadData('cycle')['role'][SchemaInterface::TYPECAST_HANDLER]); + $this->assertSame( + ['foo'], + $this->getContainer()->get(SchemaInterface::class)->define('role', SchemaInterface::TYPECAST_HANDLER) + ); } } diff --git a/tests/src/Console/Command/Migrate/StatusCommandTest.php b/tests/src/Console/Command/Migrate/StatusCommandTest.php index 2c72111..21e976b 100644 --- a/tests/src/Console/Command/Migrate/StatusCommandTest.php +++ b/tests/src/Console/Command/Migrate/StatusCommandTest.php @@ -15,7 +15,7 @@ final class StatusCommandTest extends ConsoleTest 'USE_MIGRATIONS' => true, ]; - public function testCheckMigratinoStatus(): void + public function testCheckMigrationStatus(): void { /** @var Database $db */ $db = $this->getContainer()->get(DatabaseInterface::class);