|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Tempest\Integration\Http\Rules; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\Test; |
| 8 | +use Tempest\Database\Migrations\CreateMigrationsTable; |
| 9 | +use Tempest\Validation\Rules\Exists; |
| 10 | +use Tests\Tempest\Fixtures\Migrations\CreateAuthorTable; |
| 11 | +use Tests\Tempest\Fixtures\Migrations\CreateBookTable; |
| 12 | +use Tests\Tempest\Fixtures\Migrations\CreatePublishersTable; |
| 13 | +use Tests\Tempest\Fixtures\Modules\Books\Models\Author; |
| 14 | +use Tests\Tempest\Fixtures\Modules\Books\Models\Book; |
| 15 | +use Tests\Tempest\Integration\FrameworkIntegrationTestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * @internal |
| 19 | + */ |
| 20 | +final class ExistsRuleTest extends FrameworkIntegrationTestCase |
| 21 | +{ |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + parent::setUp(); |
| 25 | + |
| 26 | + $this->migrate( |
| 27 | + CreateMigrationsTable::class, |
| 28 | + CreatePublishersTable::class, |
| 29 | + CreateAuthorTable::class, |
| 30 | + CreateBookTable::class, |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + #[Test] |
| 35 | + public function validates_existing_record_returns_true(): void |
| 36 | + { |
| 37 | + $book = Book::create(title: 'Timeline Taxi'); |
| 38 | + |
| 39 | + $rule = new Exists(Book::class); |
| 40 | + |
| 41 | + $this->assertTrue($rule->isValid($book->id)); |
| 42 | + } |
| 43 | + |
| 44 | + #[Test] |
| 45 | + public function validates_non_existent_record_returns_false(): void |
| 46 | + { |
| 47 | + $rule = new Exists(Book::class); |
| 48 | + |
| 49 | + $this->assertFalse($rule->isValid(99999)); |
| 50 | + $this->assertFalse($rule->isValid(12345)); |
| 51 | + } |
| 52 | + |
| 53 | + #[Test] |
| 54 | + public function validates_multiple_existing_records(): void |
| 55 | + { |
| 56 | + $book1 = Book::create(title: 'The Lord of the Rings'); |
| 57 | + $book2 = Book::create(title: 'The Silmarillion'); |
| 58 | + $book3 = Book::create(title: 'Unfinished Tales'); |
| 59 | + |
| 60 | + $rule = new Exists(Book::class); |
| 61 | + |
| 62 | + $this->assertTrue($rule->isValid($book1->id->id)); |
| 63 | + $this->assertTrue($rule->isValid($book2->id->id)); |
| 64 | + $this->assertTrue($rule->isValid($book3->id->id)); |
| 65 | + |
| 66 | + $this->assertFalse($rule->isValid(99999)); |
| 67 | + } |
| 68 | + |
| 69 | + #[Test] |
| 70 | + public function validates_different_model_types(): void |
| 71 | + { |
| 72 | + $author = Author::create(name: 'B. Roose'); |
| 73 | + $book = Book::create(title: 'Timeline Taxi'); |
| 74 | + |
| 75 | + $authorRule = new Exists(Author::class); |
| 76 | + $bookRule = new Exists(Book::class); |
| 77 | + |
| 78 | + $this->assertTrue($authorRule->isValid($author->id->id)); |
| 79 | + $this->assertTrue($bookRule->isValid($book->id->id)); |
| 80 | + |
| 81 | + $this->assertFalse($authorRule->isValid(99999)); |
| 82 | + $this->assertFalse($bookRule->isValid(99999)); |
| 83 | + |
| 84 | + $author2 = Author::create(name: 'B. Roose'); |
| 85 | + $book2 = Book::create(title: 'Timeline Taxi 2'); |
| 86 | + |
| 87 | + $this->assertTrue($authorRule->isValid($author2->id->id)); |
| 88 | + $this->assertTrue($bookRule->isValid($book2->id->id)); |
| 89 | + } |
| 90 | + |
| 91 | + #[Test] |
| 92 | + public function validates_edge_cases_with_large_id_numbers(): void |
| 93 | + { |
| 94 | + $rule = new Exists(Book::class); |
| 95 | + |
| 96 | + $this->assertFalse($rule->isValid(PHP_INT_MAX)); |
| 97 | + $this->assertFalse($rule->isValid(999999999)); |
| 98 | + $this->assertFalse($rule->isValid(2147483647)); // Max 32-bit integer |
| 99 | + } |
| 100 | + |
| 101 | + #[Test] |
| 102 | + public function validates_after_record_deletion(): void |
| 103 | + { |
| 104 | + $book = Book::create(title: 'Timeline Taxi Draft'); |
| 105 | + $bookId = $book->id->id; |
| 106 | + |
| 107 | + $rule = new Exists(Book::class); |
| 108 | + |
| 109 | + $this->assertTrue($rule->isValid($bookId)); |
| 110 | + |
| 111 | + $book->delete(); |
| 112 | + |
| 113 | + $this->assertFalse($rule->isValid($bookId)); |
| 114 | + } |
| 115 | + |
| 116 | + #[Test] |
| 117 | + public function validates_with_sequential_id_creation(): void |
| 118 | + { |
| 119 | + $rule = new Exists(Book::class); |
| 120 | + $createdIds = []; |
| 121 | + |
| 122 | + for ($i = 1; $i <= 5; $i++) { |
| 123 | + $book = Book::create(title: "Book {$i}"); |
| 124 | + $createdIds[] = $book->id->id; |
| 125 | + |
| 126 | + $this->assertTrue($rule->isValid($book->id->id)); |
| 127 | + } |
| 128 | + |
| 129 | + foreach ($createdIds as $id) { |
| 130 | + $this->assertTrue($rule->isValid($id)); |
| 131 | + } |
| 132 | + |
| 133 | + $maxId = max($createdIds); |
| 134 | + $this->assertFalse($rule->isValid($maxId + 1)); |
| 135 | + $this->assertFalse($rule->isValid($maxId + 100)); |
| 136 | + } |
| 137 | +} |
0 commit comments