|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\Integration\CaseTemplate; |
| 6 | + |
| 7 | +use Cycle\ORM\Entity\Behavior\Tests\Traits\TableTrait; |
| 8 | +use Cycle\ORM\Select; |
| 9 | +use Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\Integration\BaseTest; |
| 10 | + |
| 11 | +abstract class CaseTest extends BaseTest |
| 12 | +{ |
| 13 | + use TableTrait; |
| 14 | + |
| 15 | + public function testPreparedData(): void |
| 16 | + { |
| 17 | + // Get entity |
| 18 | + $user = (new Select($this->orm, Entity\User::class)) |
| 19 | + ->load('posts') |
| 20 | + ->wherePK(2) |
| 21 | + ->fetchOne(); |
| 22 | + \assert($user instanceof Entity\User); |
| 23 | + |
| 24 | + // Check results |
| 25 | + self::assertNotNull($user->createdAt); |
| 26 | + self::assertNotNull($user->updatedAt); |
| 27 | + self::assertSame(0, $user->createdAt <=> $user->updatedAt); |
| 28 | + } |
| 29 | + |
| 30 | + public function testCreatedAt(): void |
| 31 | + { |
| 32 | + // Get entity |
| 33 | + $user = $this->orm->make(Entity\User::class, ['login' => 'new-user', 'passwordHash' => '123456789']); |
| 34 | + |
| 35 | + self::assertFalse(isset($user->createdAt)); |
| 36 | + $this->save($user); |
| 37 | + |
| 38 | + self::assertNotNull($user->createdAt); |
| 39 | + |
| 40 | + // Reload entity |
| 41 | + $id = $user->id; |
| 42 | + $this->cleanHeap(); |
| 43 | + $user = (new Select($this->orm, Entity\User::class)) |
| 44 | + ->wherePK($id) |
| 45 | + ->fetchOne(); |
| 46 | + self::assertNotNull($user->createdAt); |
| 47 | + } |
| 48 | + |
| 49 | + public function testUpdatedAt(): void |
| 50 | + { |
| 51 | + // Get entity |
| 52 | + $user = (new Select($this->orm, Entity\User::class)) |
| 53 | + ->wherePK(2) |
| 54 | + ->fetchOne(); |
| 55 | + \assert($user instanceof Entity\User); |
| 56 | + $updatedAt = $user->updatedAt; |
| 57 | + |
| 58 | + // Change data |
| 59 | + $user->passwordHash = 'new-password-hash'; |
| 60 | + $this->save($user); |
| 61 | + |
| 62 | + // Check results |
| 63 | + self::assertSame(1, $user->updatedAt <=> $updatedAt); |
| 64 | + } |
| 65 | + |
| 66 | + protected function setUp(): void |
| 67 | + { |
| 68 | + // Init DB |
| 69 | + parent::setUp(); |
| 70 | + $this->prepareOrm(__DIR__ . '/Entity'); |
| 71 | + $this->save(...$this->fillData()); |
| 72 | + $this->orm->getHeap()->clean(); |
| 73 | + } |
| 74 | + |
| 75 | + private function fillData(): iterable |
| 76 | + { |
| 77 | + /** |
| 78 | + * @var callable(class-string, array): object $c |
| 79 | + */ |
| 80 | + $c = \Closure::fromCallable([$this->orm, 'make']); |
| 81 | + |
| 82 | + // Users |
| 83 | + $u1 = $c(Entity\User::class, ['login' => 'user-1', 'passwordHash' => '123456789']); |
| 84 | + $u2 = $c(Entity\User::class, ['login' => 'user-2', 'passwordHash' => '852741963']); |
| 85 | + $u3 = $c(Entity\User::class, ['login' => 'user-3', 'passwordHash' => '321654987']); |
| 86 | + $u4 = $c(Entity\User::class, ['login' => 'user-4', 'passwordHash' => '321456987']); |
| 87 | + |
| 88 | + // Posts |
| 89 | + $p1 = $c(Entity\Post::class, ['slug' => 'slug-string-1', 'title' => 'Title 1', 'public' => true, 'content' => 'Foo-bar-baz content 1']); |
| 90 | + $p2 = $c(Entity\Post::class, ['slug' => 'slug-string-2', 'title' => 'Title 2', 'public' => true, 'content' => 'Foo-bar-baz content 2']); |
| 91 | + $p3 = $c(Entity\Post::class, ['slug' => 'slug-string-3', 'title' => 'Title 3', 'public' => false, 'content' => 'Foo-bar-baz content 3']); |
| 92 | + $p4 = $c(Entity\Post::class, ['slug' => 'slug-string-4', 'title' => 'Title 4', 'public' => true, 'content' => 'Foo-bar-baz content 4']); |
| 93 | + $p5 = $c(Entity\Post::class, ['slug' => 'slug-string-5', 'title' => 'Title 5', 'public' => true, 'content' => 'Foo-bar-baz content 5']); |
| 94 | + $p6 = $c(Entity\Post::class, ['slug' => 'slug-string-6', 'title' => 'Title 6', 'public' => true, 'content' => 'Foo-bar-baz content 6']); |
| 95 | + |
| 96 | + // Link posts with users |
| 97 | + $u1->posts = [$p1]; |
| 98 | + $u2->posts = [$p2, $p3]; |
| 99 | + $u3->posts = [$p4, $p5, $p6]; |
| 100 | + |
| 101 | + // Comments |
| 102 | + $c1 = $c(Entity\Comment::class, ['post' => $p1, 'user' => $u2, 'content' => 'Foo-bar-baz comment 1', 'public' => true]); |
| 103 | + $c2 = $c(Entity\Comment::class, ['post' => $p1, 'user' => $u1, 'content' => 'Reply to comment 1', 'public' => true]); |
| 104 | + $c3 = $c(Entity\Comment::class, ['post' => $p2, 'user' => $u1, 'content' => 'Foo-bar-baz comment 2', 'public' => true]); |
| 105 | + $c4 = $c(Entity\Comment::class, ['post' => $p2, 'user' => $u2, 'content' => 'Reply to comment 2', 'public' => true]); |
| 106 | + $c5 = $c(Entity\Comment::class, ['post' => $p2, 'user' => $u3, 'content' => 'Foo-bar-baz comment 3', 'public' => true]); |
| 107 | + $c6 = $c(Entity\Comment::class, ['post' => $p2, 'user' => $u4, 'content' => 'Hidden comment', 'public' => false]); |
| 108 | + $c7 = $c(Entity\Comment::class, ['post' => $p2, 'user' => $u4, 'content' => 'Yet another comment', 'public' => true]); |
| 109 | + |
| 110 | + // Tags |
| 111 | + $t1 = $c(Entity\Tag::class, ['label' => 'tag-1']); |
| 112 | + $t2 = $c(Entity\Tag::class, ['label' => 'tag-2']); |
| 113 | + $t3 = $c(Entity\Tag::class, ['label' => 'tag-3']); |
| 114 | + $t4 = $c(Entity\Tag::class, ['label' => 'tag-4']); |
| 115 | + $t5 = $c(Entity\Tag::class, ['label' => 'tag-5']); |
| 116 | + |
| 117 | + // Link tags with posts |
| 118 | + $t1->posts = [$p1, $p2]; |
| 119 | + $t2->posts = [$p2, $p3]; |
| 120 | + $t3->posts = [$p3, $p4, $p5]; |
| 121 | + $t4->posts = [$p4, $p5]; |
| 122 | + $t5->posts = [$p5, $p6, $p1]; |
| 123 | + |
| 124 | + // yield all the entities |
| 125 | + yield from [$u1, $u2, $u3, $u4]; |
| 126 | + yield from [$p1, $p2, $p3, $p4, $p5, $p6]; |
| 127 | + yield from [$c1, $c2, $c3, $c4, $c5, $c6, $c7]; |
| 128 | + yield from [$t1, $t2, $t3, $t4, $t5]; |
| 129 | + } |
| 130 | +} |
0 commit comments