Skip to content

Commit 60cc56f

Browse files
authored
Type error fixes for optimistic lock with embeddable entity (#24)
1 parent 5002a53 commit 60cc56f

File tree

5 files changed

+63
-6
lines changed

5 files changed

+63
-6
lines changed

src/Listener/OptimisticLock.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Cycle\ORM\Entity\Behavior\Listener;
66

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

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

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

103-
return WrappedCommand::wrapCommand($command)
103+
return WrappedStoreCommand::wrapCommand($command)
104104
->withAfterExecution(static function (ScopeCarrierInterface $command) use ($node): void {
105105
if ($command->getAffectedRows() === 0) {
106106
throw new RecordIsLockedException($node);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Entity\Behavior\Tests\Fixtures\OptimisticLock;
6+
7+
use Cycle\Annotated\Annotation\Column;
8+
use Cycle\Annotated\Annotation\Embeddable;
9+
10+
#[Embeddable]
11+
class Author
12+
{
13+
public function __construct(
14+
#[Column(type: 'string', name: 'author_first_name')]
15+
public string $firstName,
16+
17+
#[Column(type: 'string', name: 'author_last_name')]
18+
public string $lastName,
19+
)
20+
{
21+
22+
}
23+
}

tests/Behavior/Fixtures/OptimisticLock/Comment.php

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Cycle\Annotated\Annotation\Column;
88
use Cycle\Annotated\Annotation\Entity;
9+
use Cycle\Annotated\Annotation\Relation\Embedded;
910
use Cycle\ORM\Entity\Behavior\OptimisticLock;
1011

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

19+
#[Embedded(target: Author::class)]
20+
public Author $author;
21+
1822
public ?string $content = null;
1923
public ?int $versionInt = null;
2024
public ?string $versionStr = null;
2125
public ?\DateTimeImmutable $versionDatetime = null;
2226
public ?string $versionMicrotime = null;
2327
public ?int $versionCustom = null;
28+
29+
public function __construct()
30+
{
31+
$this->author = new Author(firstName: 'First', lastName: 'Last');
32+
}
2433
}

tests/Behavior/Functional/Driver/Common/BaseSchemaTest.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common;
66

77
use Cycle\Annotated\Entities;
8+
use Cycle\Annotated\Embeddings;
89
use Cycle\Annotated\MergeColumns;
910
use Cycle\Annotated\MergeIndexes;
1011
use Cycle\ORM\Schema;
@@ -31,8 +32,11 @@ public function compileWithTokenizer(Tokenizer $tokenizer): void
3132
{
3233
$reader = new AttributeReader();
3334

35+
$classLocator = $tokenizer->classLocator();
36+
3437
$this->schema = new Schema((new Compiler())->compile($this->registry = new Registry($this->dbal), [
35-
new Entities($tokenizer->classLocator(), $reader),
38+
new Embeddings($classLocator, $reader),
39+
new Entities($classLocator, $reader),
3640
new ResetTables(),
3741
new MergeColumns($reader),
3842
new MergeIndexes($reader),

tests/Behavior/Functional/Driver/Common/OptimisticLock/ListenerTest.php

+23-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
use Cycle\ORM\Entity\Behavior\Exception\OptimisticLock\RecordIsLockedException;
99
use Cycle\ORM\Entity\Behavior\Listener\OptimisticLock;
1010
use Cycle\ORM\Entity\Behavior\Tests\Fixtures\OptimisticLock\Comment;
11+
use Cycle\ORM\Entity\Behavior\Tests\Fixtures\OptimisticLock\Author;
1112
use Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\BaseListenerTest;
1213
use Cycle\ORM\Entity\Behavior\Tests\Traits\TableTrait;
1314
use Cycle\ORM\Heap\Heap;
1415
use Cycle\ORM\Schema;
1516
use Cycle\ORM\SchemaInterface;
17+
use Cycle\ORM\Relation;
1618
use Cycle\ORM\Select;
1719
use Cycle\ORM\Transaction;
1820

@@ -34,6 +36,8 @@ public function setUp(): void
3436
'version_microtime' => 'string',
3537
'version_custom' => 'int,nullable',
3638
'content' => 'string,nullable',
39+
'author_first_name' => 'string',
40+
'author_last_name' => 'string',
3741
]
3842
);
3943

@@ -81,8 +85,25 @@ public function setUp(): void
8185
'versionDatetime' => 'datetime'
8286
],
8387
SchemaInterface::SCHEMA => [],
84-
SchemaInterface::RELATIONS => [],
88+
SchemaInterface::RELATIONS => [
89+
'author' => [
90+
Relation::TYPE => Relation::EMBEDDED,
91+
Relation::TARGET => Author::class,
92+
Relation::LOAD => Relation::LOAD_EAGER,
93+
Relation::SCHEMA => [],
94+
]
95+
],
8596
],
97+
Author::class => [
98+
SchemaInterface::ENTITY => Author::class,
99+
SchemaInterface::DATABASE => 'default',
100+
SchemaInterface::TABLE => 'comments',
101+
SchemaInterface::PRIMARY_KEY => ['id'],
102+
SchemaInterface::COLUMNS => [
103+
'first_name' => 'author_first_name',
104+
'last_name' => 'author_last_name',
105+
],
106+
]
86107
]));
87108
}
88109

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

96117
$this->orm = $this->orm->with(heap: new Heap());
97118
$select = new Select($this->orm, Comment::class);
98-
119+
99120
$comment = $select->fetchOne();
100121

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

0 commit comments

Comments
 (0)