Skip to content

Commit b709ccc

Browse files
authored
Merge pull request #589 from wayofdev/feat/updates
2 parents bbd8897 + 92295cd commit b709ccc

File tree

7 files changed

+121
-9
lines changed

7 files changed

+121
-9
lines changed

.yamllint.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22

3-
extends: "default"
3+
extends: default
44

55
ignore: |
66
.build/
@@ -51,8 +51,6 @@ rules:
5151
require-starting-space: true
5252
min-spaces-from-content: 1
5353

54-
yaml-files:
55-
- "*.yaml"
56-
- "*.yml"
54+
line-length: disable
5755

5856
...

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ hooks: ## Install git hooks from pre-commit-config
152152
pre-commit autoupdate
153153
.PHONY: hooks
154154

155+
lint: lint-yaml lint-php lint-stan ## Runs all linting commands
156+
.PHONY: lint
157+
155158
lint-yaml: ## Lints yaml files inside project
156159
yamllint .
157160
.PHONY: lint-yaml

config/cycle.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
return [
1414
/*
1515
* Where ClassLocator should search for entities and embeddings.
16-
* Important: By default, Laravel's application skeleton has its Model classes in the app/Models folder.
16+
* Important: By default, Laravel application skeleton has its Model classes in the app/Models folder.
1717
* With Cycle you'll need to create a dedicated folder for your Entities and point your config/cycle.php
1818
* paths array to it. If you don't, Cycle will scan your whole app/ folder for files,
1919
* which will have a huge impact on performance!
@@ -228,9 +228,9 @@
228228

229229
'migrations' => [
230230
'directory' => database_path('migrations/cycle'),
231-
231+
'strategy' => Schema\Generator\Migrations\Strategy\SingleFileStrategy::class,
232+
'nameGenerator' => Schema\Generator\Migrations\NameBasedOnChangesGenerator::class,
232233
'table' => env('DB_MIGRATIONS_TABLE', 'cycle_migrations'),
233-
234234
'safe' => env('APP_ENV') !== 'production',
235235
],
236236

@@ -253,5 +253,7 @@
253253
* Enables support of SoftDelete and other features from cycle/entity-behavior package
254254
* @see https://github.com/cycle/entity-behavior
255255
*/
256-
'load_cycle_behavior' => true,
256+
'entityBehavior' => [
257+
'register' => true,
258+
],
257259
];

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
use Cycle\Migrations\FileRepository;
1010
use Cycle\Migrations\Migrator;
1111
use Cycle\Migrations\RepositoryInterface;
12+
use Cycle\Schema\Generator\Migrations\NameBasedOnChangesGenerator;
13+
use Cycle\Schema\Generator\Migrations\NameGeneratorInterface;
14+
use Cycle\Schema\Generator\Migrations\Strategy\GeneratorStrategyInterface;
15+
use Cycle\Schema\Generator\Migrations\Strategy\SingleFileStrategy;
1216
use Illuminate\Contracts\Foundation\Application;
1317

1418
/**
@@ -26,6 +30,20 @@ public function __invoke(Application $app): void
2630
);
2731
});
2832

33+
$app->singleton(NameGeneratorInterface::class, static function (Application $app): NameGeneratorInterface {
34+
$config = $app->get(MigrationConfig::class);
35+
$nameGenerator = $config->toArray()['nameGenerator'] ?? NameBasedOnChangesGenerator::class;
36+
37+
return $app->get($nameGenerator);
38+
});
39+
40+
$app->singleton(GeneratorStrategyInterface::class, static function (Application $app): GeneratorStrategyInterface {
41+
$config = $app->get(MigrationConfig::class);
42+
$strategy = $config->toArray()['strategy'] ?? SingleFileStrategy::class;
43+
44+
return $app->get($strategy);
45+
});
46+
2947
$app->singleton(Migrator::class, static function (Application $app): Migrator {
3048
return new Migrator(
3149
config: $app->get(MigrationConfig::class),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __invoke(Application $app): void
4141

4242
$app->singleton(ORMInterface::class, function (Application $app): ORMInterface {
4343
$commandGenerator = null;
44-
$loadEntityBehavior = config('cycle.load_entity_behavior', true);
44+
$loadEntityBehavior = config('cycle.entityBehavior.register', true);
4545

4646
if (true === $loadEntityBehavior) {
4747
$commandGenerator = new EventDrivenCommandGenerator($app->get(SchemaInterface::class), $app);

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44

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

7+
use Cycle\Migrations\Config\MigrationConfig;
78
use Cycle\Migrations\Migrator;
89
use Cycle\Migrations\RepositoryInterface;
10+
use Cycle\Schema\Generator\Migrations\NameBasedOnChangesGenerator;
11+
use Cycle\Schema\Generator\Migrations\NameGeneratorInterface;
12+
use Cycle\Schema\Generator\Migrations\Strategy\GeneratorStrategyInterface;
13+
use Cycle\Schema\Generator\Migrations\Strategy\MultipleFilesStrategy;
14+
use Cycle\Schema\Generator\Migrations\Strategy\SingleFileStrategy;
915
use Psr\Container\ContainerExceptionInterface;
1016
use Psr\Container\NotFoundExceptionInterface;
1117
use WayOfDev\Tests\TestCase;
@@ -45,4 +51,56 @@ public function it_registers_migrator_as_singleton(): void
4551

4652
$this::assertSame($class1, $class2);
4753
}
54+
55+
/**
56+
* @test
57+
*/
58+
public function it_registers_name_generator_interface_as_expected(): void
59+
{
60+
$this->app->instance(MigrationConfig::class, new MigrationConfig([
61+
'nameGenerator' => NameBasedOnChangesGenerator::class,
62+
]));
63+
64+
try {
65+
$nameGenerator = $this->app->get(NameGeneratorInterface::class);
66+
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
67+
$this::fail($e->getMessage());
68+
}
69+
70+
$this::assertInstanceOf(NameBasedOnChangesGenerator::class, $nameGenerator);
71+
}
72+
73+
/**
74+
* @test
75+
*/
76+
public function it_registers_generator_strategy_interface_as_expected(): void
77+
{
78+
$this->app->instance(MigrationConfig::class, new MigrationConfig([
79+
'strategy' => SingleFileStrategy::class,
80+
]));
81+
82+
try {
83+
$strategy = $this->app->get(GeneratorStrategyInterface::class);
84+
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
85+
$this::fail($e->getMessage());
86+
}
87+
88+
$this::assertInstanceOf(SingleFileStrategy::class, $strategy);
89+
}
90+
91+
/**
92+
* @test
93+
*/
94+
public function it_registers_generator_strategy_with_multiple_files_strategy_in_config(): void
95+
{
96+
config()->set('cycle.migrations.strategy', MultipleFilesStrategy::class);
97+
98+
try {
99+
$strategy = $this->app->get(GeneratorStrategyInterface::class);
100+
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
101+
$this::fail($e->getMessage());
102+
}
103+
104+
$this::assertInstanceOf(MultipleFilesStrategy::class, $strategy);
105+
}
48106
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

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

7+
use Cycle\ORM\Entity\Behavior\EventDrivenCommandGenerator;
78
use Cycle\ORM\Factory;
89
use Cycle\ORM\FactoryInterface;
910
use Cycle\ORM\ORM;
1011
use Cycle\ORM\ORMInterface;
12+
use Cycle\ORM\Transaction\CommandGenerator;
1113
use Psr\Container\ContainerExceptionInterface;
1214
use Psr\Container\NotFoundExceptionInterface;
1315
use WayOfDev\Tests\TestCase;
@@ -46,4 +48,35 @@ public function it_registers_orm_as_singleton(): void
4648

4749
$this::assertSame($class1, $class2);
4850
}
51+
52+
/**
53+
* @test
54+
*/
55+
public function it_registers_entity_behavior_by_default(): void
56+
{
57+
try {
58+
$class = $this->app->get(ORMInterface::class);
59+
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
60+
$this::fail($e->getMessage());
61+
}
62+
63+
$this::assertInstanceOf(EventDrivenCommandGenerator::class, $class->getCommandGenerator());
64+
}
65+
66+
/**
67+
* @test
68+
*/
69+
public function it_disables_entity_behavior_by_default(): void
70+
{
71+
config()->set('cycle.entityBehavior.register', false);
72+
73+
try {
74+
/** @var ORM $class */
75+
$class = $this->app->get(ORMInterface::class);
76+
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
77+
$this::fail($e->getMessage());
78+
}
79+
80+
$this::assertInstanceOf(CommandGenerator::class, $class->getCommandGenerator());
81+
}
4982
}

0 commit comments

Comments
 (0)