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
18 changes: 0 additions & 18 deletions CHANGELOG.md

This file was deleted.

2 changes: 1 addition & 1 deletion src/OptimisticLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct(
private string $field = 'version',
?string $column = null,
/** @Enum({"microtime", "random-string", "increment", "datetime"}) */
#[ExpectedValues(valuesFromClass: Listener::class)]
#[ExpectedValues(valuesFromClass: self::class)]
private ?string $rule = null,
) {
$this->column = $column;
Expand Down
2 changes: 1 addition & 1 deletion tests/Behavior/Functional/Driver/Common/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getDriver(): DriverInterface
return static::$driverCache[static::DRIVER] = $this->driver;
}

public function setUp(): void
protected function setUp(): void
{
$this->setUpLogger($this->getDriver());
if (self::$config['debug'] ?? false) {
Expand Down
109 changes: 109 additions & 0 deletions tests/Behavior/Functional/Driver/Common/Integration/BaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\Integration;

use Cycle\Annotated\Embeddings;
use Cycle\Annotated\Entities;
use Cycle\Annotated\MergeColumns;
use Cycle\Annotated\MergeIndexes;
use Cycle\Annotated\TableInheritance;
use Cycle\ORM\Collection\ArrayCollectionFactory;
use Cycle\ORM\Config\RelationConfig;
use Cycle\ORM\Entity\Behavior\EventDrivenCommandGenerator;
use Cycle\ORM\Entity\Behavior\Tests\Utils\SimpleContainer;
use Cycle\ORM\Factory;
use Cycle\ORM\ORM;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\Schema;
use Cycle\ORM\SchemaInterface;
use Cycle\ORM\Transaction\UnitOfWork;
use Cycle\Schema\Compiler;
use Cycle\Schema\Generator\ForeignKeys;
use Cycle\Schema\Generator\GenerateModifiers;
use Cycle\Schema\Generator\GenerateRelations;
use Cycle\Schema\Generator\GenerateTypecast;
use Cycle\Schema\Generator\RenderModifiers;
use Cycle\Schema\Generator\RenderRelations;
use Cycle\Schema\Generator\RenderTables;
use Cycle\Schema\Generator\ResetTables;
use Cycle\Schema\Generator\SyncTables;
use Cycle\Schema\Generator\ValidateEntities;
use Cycle\Schema\Registry;
use Spiral\Attributes\AttributeReader;
use Spiral\Tokenizer\Config\TokenizerConfig;
use Spiral\Tokenizer\Tokenizer;

abstract class BaseTest extends \Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\BaseTest
{
protected SchemaInterface $schema;
protected ORMInterface $orm;

public function tearDown(): void
{
$this->disableProfiling();
unset($this->orm, $this->schema);
parent::tearDown();
}

/**
* @param non-empty-array<non-empty-string>|non-empty-string $entitiesPath
*/
protected function prepareOrm(string|array $entitiesPath): void
{
$tokenizer = new Tokenizer(new TokenizerConfig([
'directories' => (array) $entitiesPath,
'exclude' => [],
]));

$reader = new AttributeReader();

$classLocator = $tokenizer->classLocator();

$this->schema = new Schema((new Compiler())->compile(new Registry($this->dbal), [
new ResetTables(),
new Embeddings($classLocator, $reader),
new Entities($classLocator, $reader),
new TableInheritance($reader),
new MergeColumns($reader),
new GenerateRelations(),
new GenerateModifiers(),
new ValidateEntities(),
new RenderTables(),
new RenderRelations(),
new RenderModifiers(),
new ForeignKeys(),
new MergeIndexes($reader),
new SyncTables(),
new GenerateTypecast(),
]));

$this->orm = new ORM(
new Factory(
$this->dbal,
RelationConfig::getDefault(),
null,
new ArrayCollectionFactory(),
),
$this->schema,
new EventDrivenCommandGenerator($this->schema, new SimpleContainer()),
);
}

protected function save(object ...$entities): void
{
$uow = new UnitOfWork($this->orm);

foreach ($entities as $entity) {
$uow->persistDeferred($entity);
}
$result = $uow->run();
$result->isSuccess() or throw $uow->getLastError();
}

protected function cleanHeap(): void
{
$this->orm->getHeap()->clean();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\Integration\CaseTemplate;

use Cycle\ORM\Entity\Behavior\Tests\Traits\TableTrait;
use Cycle\ORM\Select;
use Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\Integration\BaseTest;

abstract class CaseTest extends BaseTest
{
use TableTrait;

public function testPreparedData(): void
{
// Get entity
$user = (new Select($this->orm, Entity\User::class))
->load('posts')
->wherePK(2)
->fetchOne();
\assert($user instanceof Entity\User);

// Check results
self::assertNotNull($user->createdAt);
self::assertNotNull($user->updatedAt);
self::assertSame(0, $user->createdAt <=> $user->updatedAt);
}

public function testCreatedAt(): void
{
// Get entity
$user = $this->orm->make(Entity\User::class, ['login' => 'new-user', 'passwordHash' => '123456789']);

self::assertFalse(isset($user->createdAt));
$this->save($user);

self::assertNotNull($user->createdAt);

// Reload entity
$id = $user->id;
$this->cleanHeap();
$user = (new Select($this->orm, Entity\User::class))
->wherePK($id)
->fetchOne();
self::assertNotNull($user->createdAt);
}

public function testUpdatedAt(): void
{
// Get entity
$user = (new Select($this->orm, Entity\User::class))
->wherePK(2)
->fetchOne();
\assert($user instanceof Entity\User);
$updatedAt = $user->updatedAt;

// Change data
$user->passwordHash = 'new-password-hash';
$this->save($user);

// Check results
self::assertSame(1, $user->updatedAt <=> $updatedAt);
}

protected function setUp(): void
{
// Init DB
parent::setUp();
$this->prepareOrm(__DIR__ . '/Entity');
$this->save(...$this->fillData());
$this->orm->getHeap()->clean();
}

private function fillData(): iterable
{
/**
* @var callable(class-string, array): object $c
*/
$c = \Closure::fromCallable([$this->orm, 'make']);

// Users
$u1 = $c(Entity\User::class, ['login' => 'user-1', 'passwordHash' => '123456789']);
$u2 = $c(Entity\User::class, ['login' => 'user-2', 'passwordHash' => '852741963']);
$u3 = $c(Entity\User::class, ['login' => 'user-3', 'passwordHash' => '321654987']);
$u4 = $c(Entity\User::class, ['login' => 'user-4', 'passwordHash' => '321456987']);

// Posts
$p1 = $c(Entity\Post::class, ['slug' => 'slug-string-1', 'title' => 'Title 1', 'public' => true, 'content' => 'Foo-bar-baz content 1']);
$p2 = $c(Entity\Post::class, ['slug' => 'slug-string-2', 'title' => 'Title 2', 'public' => true, 'content' => 'Foo-bar-baz content 2']);
$p3 = $c(Entity\Post::class, ['slug' => 'slug-string-3', 'title' => 'Title 3', 'public' => false, 'content' => 'Foo-bar-baz content 3']);
$p4 = $c(Entity\Post::class, ['slug' => 'slug-string-4', 'title' => 'Title 4', 'public' => true, 'content' => 'Foo-bar-baz content 4']);
$p5 = $c(Entity\Post::class, ['slug' => 'slug-string-5', 'title' => 'Title 5', 'public' => true, 'content' => 'Foo-bar-baz content 5']);
$p6 = $c(Entity\Post::class, ['slug' => 'slug-string-6', 'title' => 'Title 6', 'public' => true, 'content' => 'Foo-bar-baz content 6']);

// Link posts with users
$u1->posts = [$p1];
$u2->posts = [$p2, $p3];
$u3->posts = [$p4, $p5, $p6];

// Comments
$c1 = $c(Entity\Comment::class, ['post' => $p1, 'user' => $u2, 'content' => 'Foo-bar-baz comment 1', 'public' => true]);
$c2 = $c(Entity\Comment::class, ['post' => $p1, 'user' => $u1, 'content' => 'Reply to comment 1', 'public' => true]);
$c3 = $c(Entity\Comment::class, ['post' => $p2, 'user' => $u1, 'content' => 'Foo-bar-baz comment 2', 'public' => true]);
$c4 = $c(Entity\Comment::class, ['post' => $p2, 'user' => $u2, 'content' => 'Reply to comment 2', 'public' => true]);
$c5 = $c(Entity\Comment::class, ['post' => $p2, 'user' => $u3, 'content' => 'Foo-bar-baz comment 3', 'public' => true]);
$c6 = $c(Entity\Comment::class, ['post' => $p2, 'user' => $u4, 'content' => 'Hidden comment', 'public' => false]);
$c7 = $c(Entity\Comment::class, ['post' => $p2, 'user' => $u4, 'content' => 'Yet another comment', 'public' => true]);

// Tags
$t1 = $c(Entity\Tag::class, ['label' => 'tag-1']);
$t2 = $c(Entity\Tag::class, ['label' => 'tag-2']);
$t3 = $c(Entity\Tag::class, ['label' => 'tag-3']);
$t4 = $c(Entity\Tag::class, ['label' => 'tag-4']);
$t5 = $c(Entity\Tag::class, ['label' => 'tag-5']);

// Link tags with posts
$t1->posts = [$p1, $p2];
$t2->posts = [$p2, $p3];
$t3->posts = [$p3, $p4, $p5];
$t4->posts = [$p4, $p5];
$t5->posts = [$p5, $p6, $p1];

// yield all the entities
yield from [$u1, $u2, $u3, $u4];
yield from [$p1, $p2, $p3, $p4, $p5, $p6];
yield from [$c1, $c2, $c3, $c4, $c5, $c6, $c7];
yield from [$t1, $t2, $t3, $t4, $t5];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\Integration\CaseTemplate\Entity;

use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Relation\BelongsTo;
use Cycle\ORM\Entity\Behavior\CreatedAt;
use Cycle\ORM\Entity\Behavior\SoftDelete;
use Cycle\ORM\Entity\Behavior\UpdatedAt;

#[Entity(role: Comment::ROLE, table: 'comment')]
#[CreatedAt(field: 'createdAt', column: 'created_at')]
#[UpdatedAt(field: 'updatedAt', column: 'updated_at')]
#[SoftDelete(field: 'deletedAt', column: 'deleted_at')]
class Comment
{
public const ROLE = 'comment';

#[Column(type: 'bigPrimary')]
public ?int $id = null;

#[Column(type: 'boolean')]
public bool $public = false;

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

#[Column(type: 'datetime', nullable: true)]
public ?\DateTimeImmutable $published_at = null;

public \DateTimeImmutable $createdAt;
public \DateTimeImmutable $updatedAt;
public ?\DateTimeImmutable $deletedAt = null;

#[BelongsTo(target: User::class, innerKey: 'userId')]
public User $user;

#[BelongsTo(target: Post::class, innerKey: 'postId')]
public ?Post $post = null;

#[Column(type: 'bigInteger', name: 'user_id')]
public ?int $userId = null;

#[Column(type: 'bigInteger', name: 'post_id')]
public ?int $postId = null;

private function __construct() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\Integration\CaseTemplate\Entity;

use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Relation\BelongsTo;
use Cycle\Annotated\Annotation\Relation\HasMany;
use Cycle\Annotated\Annotation\Relation\ManyToMany;
use Cycle\ORM\Entity\Behavior\CreatedAt;
use Cycle\ORM\Entity\Behavior\OptimisticLock;
use Cycle\ORM\Entity\Behavior\SoftDelete;
use Cycle\ORM\Entity\Behavior\UpdatedAt;

#[Entity(role: Post::ROLE, table: 'post')]
#[CreatedAt(field: 'createdAt', column: 'created_at')]
#[UpdatedAt(field: 'updatedAt', column: 'updated_at')]
#[SoftDelete(field: 'deletedAt', column: 'deleted_at')]
#[OptimisticLock(field: 'version', rule: OptimisticLock::RULE_INCREMENT)]
class Post
{
public const ROLE = 'post';

#[Column(type: 'bigPrimary')]
public ?int $id = null;

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

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

#[Column(type: 'boolean')]
public bool $public = false;

#[Column(type: 'text')]
public string $content = '';

#[Column(type: 'datetime', nullable: true)]
public ?\DateTimeImmutable $published_at = null;

public int $version = 0;
public \DateTimeImmutable $created_at;
public \DateTimeImmutable $updated_at;
public ?\DateTimeImmutable $deleted_at = null;

#[BelongsTo(target: User::class, innerKey: 'userId', fkAction: 'NO ACTION')]
public User $user;

#[Column(type: 'bigInteger', name: 'user_id')]
public ?int $userId = null;

/** @var iterable<Tag> */
#[ManyToMany(target: Tag::class, innerKey: 'id', outerKey: 'id', throughInnerKey: 'postId', throughOuterKey: 'tagId', through: PostTag::class)]
public iterable $tags = [];

/** @var iterable<Comment> */
#[HasMany(target: Comment::class, innerKey: 'id', outerKey: 'postId', fkCreate: false)]
public iterable $comments = [];

private function __construct() {}
}
Loading
Loading