Skip to content

Commit f7ba12d

Browse files
committed
1 parent 64a9bfe commit f7ba12d

File tree

10 files changed

+77
-11
lines changed

10 files changed

+77
-11
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"cycle/schema-migrations-generator": "^2.2",
2626
"cycle/schema-renderer": "^1.2",
2727
"illuminate/console": "^10.45",
28+
"cycle/schema-builder": "^2.8.0",
2829
"illuminate/contracts": "^10.45",
2930
"illuminate/support": "^10.45",
3031
"laminas/laminas-hydrator": "^4.15",

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/cycle.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@
159159
SchemaInterface::MAPPER => Cycle\ORM\Mapper\Mapper::class,
160160
SchemaInterface::REPOSITORY => Cycle\ORM\Select\Repository::class,
161161
SchemaInterface::SCOPE => null,
162-
SchemaInterface::TYPECAST_HANDLER => [
163-
// \Cycle\ORM\Parser\Typecast::class, \App\Infrastructure\CycleORM\Typecaster\UuidTypecast::class,
164-
],
162+
SchemaInterface::TYPECAST_HANDLER => null,
165163
],
166164

167165
'collections' => [

src/Bridge/Laravel/Providers/Registrators/RegisterSchema.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace WayOfDev\Cycle\Bridge\Laravel\Providers\Registrators;
66

7+
use Cycle\Database\DatabaseProviderInterface;
78
use Cycle\ORM\SchemaInterface;
9+
use Cycle\Schema\Defaults;
810
use Cycle\Schema\Registry;
911
use Illuminate\Container\Container;
1012
use Illuminate\Contracts\Cache\Factory as CacheFactory;
@@ -37,6 +39,17 @@ public function __invoke(Application $app): void
3739
);
3840
});
3941

42+
$app->bind(Registry::class, static function (Application $app): Registry {
43+
$defaults = new Defaults();
44+
45+
/** @var SchemaConfig $config */
46+
$config = $app->get(SchemaConfig::class);
47+
48+
$defaults->merge($config->defaults());
49+
50+
return new Registry($app->get(DatabaseProviderInterface::class), $defaults);
51+
});
52+
4053
$app->bind(SchemaInterface::class, static function (Application $app): SchemaInterface {
4154
/** @var SchemaConfig $config */
4255
$config = $app->get(SchemaConfig::class);

src/Schema/Config/SchemaConfig.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,9 @@ public function cacheSchema(): bool
4545
{
4646
return $this->config['cache']['enabled'];
4747
}
48+
49+
public function defaults()
50+
{
51+
return $this->config['defaults'];
52+
}
4853
}

tests/src/Bridge/Laravel/Console/Commands/Migrations/MigrateCommandTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ public function it_runs_handle(): void
2323
$this->artisanCall('cycle:migrate:init');
2424
$this->artisanCall('cycle:orm:migrate', ['--force' => true]);
2525

26+
// @phpstan-ignore-next-line
2627
$this::assertCount(1, $database->getTables());
2728

2829
$this->artisanCall('cycle:migrate', ['--force' => true]);
29-
$this::assertCount(4, $database->getTables());
30+
$this::assertCount(5, $database->getTables());
3031
}
3132
}

tests/src/Bridge/Laravel/Console/Commands/Migrations/ReplayCommandTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ public function it_runs_handle(): void
2323
$this->assertConsoleCommandOutputContainsStrings('cycle:migrate:replay', ['--force' => true], 'No');
2424

2525
$this->artisanCall('cycle:orm:migrate', ['--force' => true]);
26+
// @phpstan-ignore-next-line
2627
$this::assertCount(1, $database->getTables());
2728

2829
$this->artisanCall('cycle:migrate', ['--force' => true]);
29-
$this::assertCount(4, $database->getTables());
30+
$this::assertCount(5, $database->getTables());
3031

3132
$this->artisanCall('cycle:migrate:replay', ['--force' => true]);
32-
$this::assertCount(4, $database->getTables());
33+
$this::assertCount(5, $database->getTables());
3334
}
3435
}

tests/src/Bridge/Laravel/Console/Commands/Migrations/RollbackCommandTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ public function it_runs_handle(): void
2424
$this::assertConsoleCommandOutputContainsStrings('cycle:migrate:rollback', ['--force' => true], 'No');
2525

2626
$this->artisanCall('cycle:orm:migrate', ['--force' => true]);
27+
// @phpstan-ignore-next-line
2728
$this::assertCount(1, $database->getTables());
2829

2930
$this->artisanCall('cycle:migrate', ['--force' => true]);
30-
$this::assertCount(4, $database->getTables());
31+
$this::assertCount(5, $database->getTables());
3132

3233
$this->artisanCall('cycle:migrate:rollback', ['--force' => true]);
3334
$this::assertCount(1, $database->getTables());

tests/src/Bridge/Laravel/Console/Commands/Migrations/StatusCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function it_runs_handle(): void
3636
$this::assertStringContainsString('not executed yet', $output);
3737

3838
$this->artisanCall('cycle:migrate', ['--force' => true]);
39-
$this::assertCount(4, $database->getTables());
39+
$this::assertCount(5, $database->getTables());
4040

4141
$this->artisanCall('cycle:migrate:status');
4242
$output2 = Artisan::output();

tests/src/Bridge/Laravel/Providers/Registrators/RegisterSchemaTest.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
namespace WayOfDev\Tests\Bridge\Laravel\Providers\Registrators;
66

7+
use Cycle\ORM\Mapper\Mapper;
78
use Cycle\ORM\Schema;
89
use Cycle\ORM\SchemaInterface;
10+
use Cycle\ORM\Select\Repository;
11+
use Cycle\ORM\Select\Source;
12+
use Cycle\Schema\Registry;
913
use Illuminate\Contracts\Container\BindingResolutionException;
1014
use Psr\Container\ContainerExceptionInterface;
1115
use Psr\Container\NotFoundExceptionInterface;
@@ -49,6 +53,8 @@ public function it_registers_generator_loader_as_singleton(): void
4953
$this::fail($e->getMessage());
5054
}
5155

56+
$this::assertSame($class1, $class2);
57+
5258
$this::assertInstanceOf(GeneratorLoader::class, $class1);
5359
$this::assertInstanceOf(GeneratorQueue::class, $class1);
5460

@@ -62,19 +68,59 @@ public function it_registers_schema_interface(): void
6268
{
6369
try {
6470
$class1 = $this->app->get(SchemaInterface::class);
65-
// $class2 = $this->app->get(SchemaInterface::class);
71+
$class2 = $this->app->get(SchemaInterface::class);
6672
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
6773
$this::fail($e->getMessage());
6874
}
6975

7076
$this::assertInstanceOf(SchemaInterface::class, $class1);
7177
$this::assertInstanceOf(Schema::class, $class1);
7278

73-
// $this::assertSame($class1, $class2);
79+
$this::assertNotSame($class1, $class2);
7480

7581
$schema = $class1->toArray();
7682

7783
$this::assertArrayHasKey('user', $schema);
7884
$this::assertArrayHasKey('role', $schema);
7985
}
86+
87+
/**
88+
* @test
89+
*/
90+
public function it_registers_schema_registry_with_default_config(): void
91+
{
92+
try {
93+
$defaults = $this->app->get(Registry::class)->getDefaults();
94+
95+
$this::assertSame(Mapper::class, $defaults[SchemaInterface::MAPPER]);
96+
$this::assertSame(Repository::class, $defaults[SchemaInterface::REPOSITORY]);
97+
$this::assertSame(Source::class, $defaults[SchemaInterface::SOURCE]);
98+
$this::assertNull($defaults[SchemaInterface::SCOPE]);
99+
$this::assertNull($defaults[SchemaInterface::TYPECAST_HANDLER]);
100+
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
101+
$this::fail($e->getMessage());
102+
}
103+
}
104+
105+
/**
106+
* @test
107+
*/
108+
public function it_registers_schema_registry_with_custom_config(): void
109+
{
110+
try {
111+
config()->set('cycle.schema.defaults', [
112+
SchemaInterface::TYPECAST_HANDLER => ['foo', 'bar'],
113+
]);
114+
115+
$defaults = $this->app->get(Registry::class)->getDefaults();
116+
117+
$this::assertSame(Mapper::class, $defaults[SchemaInterface::MAPPER]);
118+
$this::assertSame(Repository::class, $defaults[SchemaInterface::REPOSITORY]);
119+
$this::assertSame(Source::class, $defaults[SchemaInterface::SOURCE]);
120+
$this::assertNull($defaults[SchemaInterface::SCOPE]);
121+
$this::assertSame(['foo', 'bar'], $defaults[SchemaInterface::TYPECAST_HANDLER]);
122+
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
123+
$this::fail($e->getMessage());
124+
}
125+
}
80126
}

0 commit comments

Comments
 (0)