|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Tempest\Integration\Validator; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\PreCondition; |
| 8 | +use PHPUnit\Framework\Attributes\Test; |
| 9 | +use Tempest\Database\Migrations\CreateMigrationsTable; |
| 10 | +use Tempest\Validation\Rules\Exists; |
| 11 | +use Tests\Tempest\Fixtures\Migrations\CreateAuthorTable; |
| 12 | +use Tests\Tempest\Fixtures\Migrations\CreateBookTable; |
| 13 | +use Tests\Tempest\Fixtures\Migrations\CreatePublishersTable; |
| 14 | +use Tests\Tempest\Fixtures\Modules\Books\Models\Author; |
| 15 | +use Tests\Tempest\Fixtures\Modules\Books\Models\Book; |
| 16 | +use Tests\Tempest\Integration\FrameworkIntegrationTestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * @internal |
| 20 | + */ |
| 21 | +final class ExistsRuleTest extends FrameworkIntegrationTestCase |
| 22 | +{ |
| 23 | + #[PreCondition] |
| 24 | + protected function configure(): void |
| 25 | + { |
| 26 | + $this->migrate( |
| 27 | + CreateMigrationsTable::class, |
| 28 | + CreatePublishersTable::class, |
| 29 | + CreateAuthorTable::class, |
| 30 | + CreateBookTable::class, |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + #[Test] |
| 35 | + public function existing_record_return_true(): void |
| 36 | + { |
| 37 | + $book = Book::create(title: 'Timeline Taxi'); |
| 38 | + |
| 39 | + $this->assertTrue(new Exists(Book::class)->isValid($book->id)); |
| 40 | + $this->assertTrue(new Exists('books', column: 'id')->isValid($book->id)); |
| 41 | + $this->assertTrue(new Exists('books', column: 'title')->isValid('Timeline Taxi')); |
| 42 | + } |
| 43 | + |
| 44 | + #[Test] |
| 45 | + public function non_existent_record_returns_false(): void |
| 46 | + { |
| 47 | + Book::create(title: 'Timeline Taxi'); |
| 48 | + |
| 49 | + $this->assertFalse(new Exists(Book::class)->isValid(99999)); |
| 50 | + $this->assertFalse(new Exists(Book::class)->isValid(12345)); |
| 51 | + $this->assertFalse(new Exists(Book::class, column: 'title')->isValid('Timeline Taxi 2')); |
| 52 | + } |
| 53 | + |
| 54 | + #[Test] |
| 55 | + public function validates_multiple_existing_records(): void |
| 56 | + { |
| 57 | + $book1 = Book::create(title: 'The Lord of the Rings'); |
| 58 | + $book2 = Book::create(title: 'The Silmarillion'); |
| 59 | + $book3 = Book::create(title: 'Unfinished Tales'); |
| 60 | + |
| 61 | + $this->assertTrue(new Exists(Book::class)->isValid($book1->id)); |
| 62 | + $this->assertTrue(new Exists(Book::class)->isValid($book2->id)); |
| 63 | + $this->assertTrue(new Exists(Book::class)->isValid($book3->id)); |
| 64 | + $this->assertFalse(new Exists(Book::class)->isValid(99999)); |
| 65 | + } |
| 66 | +} |
0 commit comments