Skip to content

Commit

Permalink
Merge pull request #33 from kafkiansky/feature/orm-bootloader-improve…
Browse files Browse the repository at this point in the history
…ments

Improve CycleOrmBootloader bindings
  • Loading branch information
butschster authored Feb 7, 2023
2 parents a018fba + cf533fb commit dfa3d8a
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/Bootloader/CycleOrmBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,29 @@ final class CycleOrmBootloader extends Bootloader

protected const BINDINGS = [
TransactionInterface::class => Transaction::class,
EntityManagerInterface::class => EntityManager::class,
];

protected const SINGLETONS = [
ORMInterface::class => ORM::class,
EntityManagerInterface::class => EntityManager::class,
FactoryInterface::class => [self::class, 'factory'],
];

public function __construct(
private ConfiguratorInterface $config,
private EnvironmentInterface $env
private ConfiguratorInterface $config
) {
}

public function boot(
Container $container,
FinalizerInterface $finalizer,
EnvironmentInterface $env
EnvironmentInterface $env,
): void {
$finalizer->addFinalizer(
static function () use ($container): void {
if ($container->hasInstance(EntityManagerInterface::class)) {
$container->get(EntityManagerInterface::class)->clean();
}
if ($container->hasInstance(ORMInterface::class)) {
$container->get(ORMInterface::class)->getHeap()->clean();
}
Expand All @@ -64,7 +66,7 @@ static function () use ($container): void {

$container->bindInjector(RepositoryInterface::class, RepositoryInjector::class);

$this->initOrmConfig();
$this->initOrmConfig($env);
}

public function start(AbstractKernel $kernel): void
Expand Down Expand Up @@ -102,18 +104,18 @@ private function factory(
return $factory;
}

private function initOrmConfig(): void
private function initOrmConfig(EnvironmentInterface $env): void
{
$this->config->setDefaults(
CycleConfig::CONFIG,
[
'schema' => [
'cache' => $this->env->get('CYCLE_SCHEMA_CACHE', false),
'cache' => $env->get('CYCLE_SCHEMA_CACHE', false),
'generators' => null,
'defaults' => [],
'collections' => [],
],
'warmup' => $this->env->get('CYCLE_SCHEMA_WARMUP', false),
'warmup' => $env->get('CYCLE_SCHEMA_WARMUP', false),
]
);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/src/Bootloader/CycleOrmBootloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Spiral\Tests\Bootloader;

use Cycle\ORM\EntityManager;
use Cycle\ORM\EntityManagerInterface;
use Cycle\ORM\FactoryInterface;
use Cycle\ORM\ORM;
Expand Down Expand Up @@ -34,7 +35,7 @@ public function testGetsTransaction(): void

public function testGetsEntityManager(): void
{
$this->assertContainerBound(EntityManagerInterface::class);
$this->assertContainerBoundAsSingleton(EntityManagerInterface::class, EntityManager::class);
}

#[ConfigAttribute(path: 'cycle.schema.collections', value: ['default' => 'test'])]
Expand Down
8 changes: 8 additions & 0 deletions tests/src/Bootloader/CycleOrmWarmedUpBootloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Spiral\Tests\Bootloader;

use Cycle\ORM\EntityManagerInterface;
use Cycle\ORM\Heap\HeapInterface;
use Cycle\ORM\ORM;
use Cycle\ORM\ORMInterface;
Expand Down Expand Up @@ -43,6 +44,13 @@ protected function setUp(): void
$container->bindSingleton(ORM::class, $orm);
});

$em = m::mock(EntityManagerInterface::class);
$em->shouldReceive('clean');

$this->beforeStarting(static function (Container $container) use ($em) {
$container->bindSingleton(EntityManagerInterface::class, $em);
});

parent::setUp();
}

Expand Down
71 changes: 71 additions & 0 deletions tests/src/Cycle/EntityManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Spiral\Tests\Cycle;

use Cycle\Database\DatabaseInterface;
use Cycle\ORM\EntityManagerInterface;
use Cycle\ORM\Mapper\Mapper;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\Schema;
use Cycle\ORM\SchemaInterface;
use Spiral\Boot\FinalizerInterface;
use Spiral\Tests\BaseTest;
use Spiral\Tests\Cycle\Fixture\Bar;

final class EntityManagerTest extends BaseTest
{
protected function setUp(): void
{
parent::setUp();

$db = $this->getContainer()->get(DatabaseInterface::class);

$bars = $db->table('bars')->getSchema();
$bars->primary('id');
$bars->string('name');
$bars->save();
}

public function testEntityManagerStateCanBeSharedAndFinalizeAtOnce(): void
{
$orm = $this->getOrm()->with(
new Schema([
Bar::class => [
SchemaInterface::ROLE => 'bar',
SchemaInterface::MAPPER => Mapper::class,
SchemaInterface::DATABASE => 'default',
SchemaInterface::TABLE => 'bars',
SchemaInterface::PRIMARY_KEY => 'id',
SchemaInterface::COLUMNS => ['id', 'name'],
SchemaInterface::SCHEMA => [],
SchemaInterface::RELATIONS => [],
],
])
);

$this->getContainer()->bindSingleton(ORMInterface::class, fn(): ORMInterface => $orm);

$em = $this->getEntityManager();
$em->persist(new Bar('foo'));
$em->run();
$bar = $this->getRepository(Bar::class)->findOne(['name' => 'foo']);
self::assertEquals('foo', $bar->name);
$bar->name = 'baz';
$em->persist($bar);
$this->getContainer()->get(FinalizerInterface::class)->finalize();
$em->run();
self::assertNull($this->getRepository(Bar::class)->findOne(['name' => 'baz']));
}

public function testMethodCleanCalled(): void
{
$em = $this->createMock(EntityManagerInterface::class);
$em->expects($this->once())->method('clean');

$this->getContainer()->bindSingleton(EntityManagerInterface::class, $em);
$this->getContainer()->get(EntityManagerInterface::class);
$this->getContainer()->get(FinalizerInterface::class)->finalize();
}
}
23 changes: 23 additions & 0 deletions tests/src/Cycle/Fixture/Bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Spiral\Tests\Cycle\Fixture;

use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;

#[Entity]
class Bar
{
#[Column(type: 'primary')]
public $id;

#[Column(type: 'string')]
public $name;

public function __construct(string $name)
{
$this->name = $name;
}
}

0 comments on commit dfa3d8a

Please sign in to comment.