Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Listener/OptimisticLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Cycle\ORM\Entity\Behavior\Listener;

use Cycle\ORM\Command\ScopeCarrierInterface;
use Cycle\ORM\Command\Special\WrappedCommand;
use Cycle\ORM\Command\Special\WrappedStoreCommand;
use Cycle\ORM\Command\StoreCommandInterface;
use Cycle\ORM\Entity\Behavior\Attribute\Listen;
use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnCreate;
Expand Down Expand Up @@ -76,7 +76,7 @@ public function __invoke(OnDelete|OnUpdate $event): void
$event->command = $this->lock($event->node, $event->state, $event->command);
}

private function lock(Node $node, State $state, ScopeCarrierInterface $command): WrappedCommand
private function lock(Node $node, State $state, ScopeCarrierInterface $command): WrappedStoreCommand
{
$nodeValue = $node->getData()[$this->field] ?? null;
if ($nodeValue === null) {
Expand All @@ -100,7 +100,7 @@ private function lock(Node $node, State $state, ScopeCarrierInterface $command):

$command->setScope($this->field, $nodeValue);

return WrappedCommand::wrapCommand($command)
return WrappedStoreCommand::wrapCommand($command)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be tested when an entity deletion (with and without SoftDelete behavior)

->withAfterExecution(static function (ScopeCarrierInterface $command) use ($node): void {
if ($command->getAffectedRows() === 0) {
throw new RecordIsLockedException($node);
Expand Down
23 changes: 23 additions & 0 deletions tests/Behavior/Fixtures/OptimisticLock/Author.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Entity\Behavior\Tests\Fixtures\OptimisticLock;

use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Embeddable;

#[Embeddable]
class Author
{
public function __construct(
#[Column(type: 'string', name: 'author_first_name')]
public string $firstName,

#[Column(type: 'string', name: 'author_last_name')]
public string $lastName,
)
{

}
}
9 changes: 9 additions & 0 deletions tests/Behavior/Fixtures/OptimisticLock/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Relation\Embedded;
use Cycle\ORM\Entity\Behavior\OptimisticLock;

#[Entity]
Expand All @@ -15,10 +16,18 @@ class Comment
#[Column(type: 'primary')]
public int $id;

#[Embedded(target: Author::class)]
public Author $author;

public ?string $content = null;
public ?int $versionInt = null;
public ?string $versionStr = null;
public ?\DateTimeImmutable $versionDatetime = null;
public ?string $versionMicrotime = null;
public ?int $versionCustom = null;

public function __construct()
{
$this->author = new Author(firstName: 'First', lastName: 'Last');
}
}
6 changes: 5 additions & 1 deletion tests/Behavior/Functional/Driver/Common/BaseSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common;

use Cycle\Annotated\Entities;
use Cycle\Annotated\Embeddings;
use Cycle\Annotated\MergeColumns;
use Cycle\Annotated\MergeIndexes;
use Cycle\ORM\Schema;
Expand All @@ -31,8 +32,11 @@ public function compileWithTokenizer(Tokenizer $tokenizer): void
{
$reader = new AttributeReader();

$classLocator = $tokenizer->classLocator();

$this->schema = new Schema((new Compiler())->compile($this->registry = new Registry($this->dbal), [
new Entities($tokenizer->classLocator(), $reader),
new Embeddings($classLocator, $reader),
new Entities($classLocator, $reader),
new ResetTables(),
new MergeColumns($reader),
new MergeIndexes($reader),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
use Cycle\ORM\Entity\Behavior\Exception\OptimisticLock\RecordIsLockedException;
use Cycle\ORM\Entity\Behavior\Listener\OptimisticLock;
use Cycle\ORM\Entity\Behavior\Tests\Fixtures\OptimisticLock\Comment;
use Cycle\ORM\Entity\Behavior\Tests\Fixtures\OptimisticLock\Author;
use Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\BaseListenerTest;
use Cycle\ORM\Entity\Behavior\Tests\Traits\TableTrait;
use Cycle\ORM\Heap\Heap;
use Cycle\ORM\Schema;
use Cycle\ORM\SchemaInterface;
use Cycle\ORM\Relation;
use Cycle\ORM\Select;
use Cycle\ORM\Transaction;

Expand All @@ -34,6 +36,8 @@ public function setUp(): void
'version_microtime' => 'string',
'version_custom' => 'int,nullable',
'content' => 'string,nullable',
'author_first_name' => 'string',
'author_last_name' => 'string',
]
);

Expand Down Expand Up @@ -81,8 +85,25 @@ public function setUp(): void
'versionDatetime' => 'datetime'
],
SchemaInterface::SCHEMA => [],
SchemaInterface::RELATIONS => [],
SchemaInterface::RELATIONS => [
'author' => [
Relation::TYPE => Relation::EMBEDDED,
Relation::TARGET => Author::class,
Relation::LOAD => Relation::LOAD_EAGER,
Relation::SCHEMA => [],
]
],
],
Author::class => [
SchemaInterface::ENTITY => Author::class,
SchemaInterface::DATABASE => 'default',
SchemaInterface::TABLE => 'comments',
SchemaInterface::PRIMARY_KEY => ['id'],
SchemaInterface::COLUMNS => [
'first_name' => 'author_first_name',
'last_name' => 'author_last_name',
],
]
]));
}

Expand All @@ -95,7 +116,7 @@ public function testAddVersionOnCreate()

$this->orm = $this->orm->with(heap: new Heap());
$select = new Select($this->orm, Comment::class);

$comment = $select->fetchOne();

$this->assertSame(1, $comment->versionInt);
Expand Down