-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from kafkiansky/feature/orm-bootloader-improve…
…ments Improve CycleOrmBootloader bindings
- Loading branch information
Showing
5 changed files
with
114 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |