Skip to content

Commit

Permalink
Pass schema defaults to the Registry (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz authored May 19, 2023
1 parent 81de82d commit 0048710
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"cycle/orm": "^2.0.2",
"cycle/schema-migrations-generator": "^2.1",
"cycle/schema-renderer": "^1.2",
"cycle/schema-builder": "^2.5",
"doctrine/inflector": "^1.4 || ^2.0",
"spiral/attributes": "^2.10 || ^3.0",
"spiral/framework": "^3.3",
Expand Down
11 changes: 11 additions & 0 deletions src/Bootloader/SchemaBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
namespace Spiral\Cycle\Bootloader;

use Cycle\ORM\SchemaInterface;
use Cycle\Schema\Defaults;
use Cycle\Schema\Generator;
use Cycle\Schema\GeneratorInterface;
use Cycle\Schema\Registry;
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;
Expand All @@ -28,6 +30,7 @@ final class SchemaBootloader extends Bootloader implements Container\SingletonIn

protected const BINDINGS = [
SchemaInterface::class => [self::class, 'schema'],
Registry::class => [self::class, 'initRegistry']
];

/** @var string[][]|GeneratorInterface[][] */
Expand Down Expand Up @@ -108,5 +111,13 @@ protected function schema(MemoryInterface $memory, CycleConfig $config): SchemaI

return $schemaCompiler->toSchema();
}

private function initRegistry(FactoryInterface $factory, CycleConfig $config): Registry
{
$defaults = new Defaults();
$defaults->merge($config->getSchemaDefaults());

return $factory->make(Registry::class, ['defaults' => $defaults]);
}
}

27 changes: 27 additions & 0 deletions tests/src/Bootloader/SchemaBootloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
namespace Spiral\Tests\Bootloader;

use Cycle\Annotated\Entities;
use Cycle\ORM\Mapper\Mapper;
use Cycle\ORM\SchemaInterface;
use Cycle\ORM\Select\Repository;
use Cycle\ORM\Select\Source;
use Cycle\Schema\GeneratorInterface;
use Cycle\Schema\Registry;
use Spiral\Cycle\Bootloader\SchemaBootloader;
use Spiral\Cycle\Config\CycleConfig;
use Spiral\Tests\BaseTest;
Expand Down Expand Up @@ -51,4 +55,27 @@ public function testGetsSchemaGeneratorsOverrideByConfigWithEmptyArray(): void

$this->assertCount(0, $generators);
}

public function testRegistryWithDefaultConfig(): void
{
$defaults = $this->getContainer()->get(Registry::class)->getDefaults();

$this->assertSame(Mapper::class, $defaults[SchemaInterface::MAPPER]);
$this->assertSame(Repository::class, $defaults[SchemaInterface::REPOSITORY]);
$this->assertSame(Source::class, $defaults[SchemaInterface::SOURCE]);
$this->assertNull($defaults[SchemaInterface::SCOPE]);
$this->assertNull($defaults[SchemaInterface::TYPECAST_HANDLER]);
}

#[ConfigAttribute(path: 'cycle.schema.defaults', value: [SchemaInterface::TYPECAST_HANDLER => ['foo', 'bar']])]
public function testRegistryWithModifiedConfig(): void
{
$defaults = $this->getContainer()->get(Registry::class)->getDefaults();

$this->assertSame(Mapper::class, $defaults[SchemaInterface::MAPPER]);
$this->assertSame(Repository::class, $defaults[SchemaInterface::REPOSITORY]);
$this->assertSame(Source::class, $defaults[SchemaInterface::SOURCE]);
$this->assertNull($defaults[SchemaInterface::SCOPE]);
$this->assertSame(['foo', 'bar'], $defaults[SchemaInterface::TYPECAST_HANDLER]);
}
}
1 change: 1 addition & 0 deletions tests/src/LoggerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
final class LoggerFactoryTest extends BaseTest
{
protected ConfigsInterface $config;
private LogsInterface $logger;

protected function setUp(): void
{
Expand Down

0 comments on commit 0048710

Please sign in to comment.