Skip to content

Commit 384fb55

Browse files
committed
add Exists validation rule with comprehensive tests
Signed-off-by: Tonko Mulder <[email protected]>
1 parent cf504ad commit 384fb55

File tree

4 files changed

+281
-0
lines changed

4 files changed

+281
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Validation\Rules;
6+
7+
use Attribute;
8+
use InvalidArgumentException;
9+
use Tempest\Database\Id;
10+
use Tempest\Validation\Rule;
11+
12+
use function Tempest\Database\query;
13+
14+
#[Attribute(Attribute::TARGET_PROPERTY)]
15+
final readonly class Exists implements Rule
16+
{
17+
public function __construct(
18+
private string $model,
19+
) {
20+
if (! class_exists($this->model)) {
21+
throw new InvalidArgumentException("Model {$this->model} does not exist");
22+
}
23+
}
24+
25+
public function isValid(mixed $value): bool
26+
{
27+
if ((! is_numeric($value) || is_float($value)) && ! is_object($value)) {
28+
return false;
29+
}
30+
$id = is_object($value) ? $value : new Id($value);
31+
32+
$model = query($this->model)
33+
->select()
34+
->get(id: $id);
35+
36+
return $model !== null;
37+
}
38+
39+
public function message(): string
40+
{
41+
return sprintf('Record for model %1$s does not exist', $this->model);
42+
}
43+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Validation\Tests\Rules;
6+
7+
use InvalidArgumentException;
8+
use PHPUnit\Framework\Attributes\Test;
9+
use PHPUnit\Framework\TestCase;
10+
use Tempest\Validation\Rules\Exists;
11+
use Tests\Tempest\Fixtures\Rules\ValidateExistsModel;
12+
13+
/**
14+
* @internal
15+
*/
16+
final class ExistsTest extends TestCase
17+
{
18+
#[Test]
19+
public function throws_exception_for_invalid_model_class(): void
20+
{
21+
$this->expectException(InvalidArgumentException::class);
22+
$this->expectExceptionMessage('Model NonExistentModel does not exist');
23+
24+
new Exists('NonExistentModel');
25+
}
26+
27+
#[Test]
28+
public function returns_false_for_null_or_non_integer_values(): void
29+
{
30+
$rule = new Exists(ValidateExistsModel::class);
31+
32+
$this->assertFalse($rule->isValid('string'));
33+
$this->assertFalse($rule->isValid(1.5));
34+
$this->assertFalse($rule->isValid([]));
35+
$this->assertFalse($rule->isValid(true));
36+
$this->assertFalse($rule->isValid(null));
37+
}
38+
39+
#[Test]
40+
public function returns_correct_error_message(): void
41+
{
42+
$rule = new Exists(ValidateExistsModel::class);
43+
44+
$expectedMessage = sprintf('Record for model %s does not exist', ValidateExistsModel::class);
45+
$this->assertSame($expectedMessage, $rule->message());
46+
}
47+
48+
#[Test]
49+
public function can_be_constructed_with_valid_model_class(): void
50+
{
51+
$rule = new Exists(ValidateExistsModel::class);
52+
53+
$this->assertInstanceOf(Exists::class, $rule);
54+
$this->assertStringContainsString(ValidateExistsModel::class, $rule->message());
55+
}
56+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Tempest\Fixtures\Rules;
6+
7+
use Tempest\Database\Id;
8+
9+
/** @internal */
10+
final class ValidateExistsModel
11+
{
12+
public function __construct(
13+
public Id $id,
14+
public string $name,
15+
) {}
16+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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\Modules\Books\Models\Author;
13+
use Tests\Tempest\Fixtures\Modules\Books\Models\Book;
14+
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
15+
16+
/**
17+
* @internal
18+
*/
19+
final class ExistsRuleTest extends FrameworkIntegrationTestCase
20+
{
21+
#[Test]
22+
public function validates_existing_record_returns_true(): void
23+
{
24+
$this->migrate(
25+
CreateMigrationsTable::class,
26+
CreateAuthorTable::class,
27+
CreateBookTable::class,
28+
);
29+
30+
$book = Book::create(title: 'Timeline Taxi');
31+
32+
$rule = new Exists(Book::class);
33+
34+
$this->assertTrue($rule->isValid($book->id));
35+
}
36+
37+
#[Test]
38+
public function validates_non_existent_record_returns_false(): void
39+
{
40+
$this->migrate(
41+
CreateMigrationsTable::class,
42+
CreateAuthorTable::class,
43+
CreateBookTable::class,
44+
);
45+
46+
$rule = new Exists(Book::class);
47+
48+
$this->assertFalse($rule->isValid(99999));
49+
$this->assertFalse($rule->isValid(12345));
50+
}
51+
52+
#[Test]
53+
public function validates_multiple_existing_records(): void
54+
{
55+
$this->migrate(
56+
CreateMigrationsTable::class,
57+
CreateAuthorTable::class,
58+
CreateBookTable::class,
59+
);
60+
61+
$book1 = Book::create(title: 'The Lord of the Rings');
62+
$book2 = Book::create(title: 'The Silmarillion');
63+
$book3 = Book::create(title: 'Unfinished Tales');
64+
65+
$rule = new Exists(Book::class);
66+
67+
$this->assertTrue($rule->isValid($book1->id->id));
68+
$this->assertTrue($rule->isValid($book2->id->id));
69+
$this->assertTrue($rule->isValid($book3->id->id));
70+
71+
$this->assertFalse($rule->isValid(99999));
72+
}
73+
74+
#[Test]
75+
public function validates_different_model_types(): void
76+
{
77+
$this->migrate(
78+
CreateMigrationsTable::class,
79+
CreateAuthorTable::class,
80+
CreateBookTable::class,
81+
);
82+
83+
$author = Author::create(name: 'B. Roose');
84+
$book = Book::create(title: 'Timeline Taxi');
85+
86+
$authorRule = new Exists(Author::class);
87+
$bookRule = new Exists(Book::class);
88+
89+
$this->assertTrue($authorRule->isValid($author->id->id));
90+
$this->assertTrue($bookRule->isValid($book->id->id));
91+
92+
$this->assertFalse($authorRule->isValid(99999));
93+
$this->assertFalse($bookRule->isValid(99999));
94+
95+
$author2 = Author::create(name: 'B. Roose');
96+
$book2 = Book::create(title: 'Timeline Taxi 2');
97+
98+
$this->assertTrue($authorRule->isValid($author2->id->id));
99+
$this->assertTrue($bookRule->isValid($book2->id->id));
100+
}
101+
102+
#[Test]
103+
public function validates_edge_cases_with_large_id_numbers(): void
104+
{
105+
$this->migrate(
106+
CreateMigrationsTable::class,
107+
CreateAuthorTable::class,
108+
CreateBookTable::class,
109+
);
110+
111+
$rule = new Exists(Book::class);
112+
113+
$this->assertFalse($rule->isValid(PHP_INT_MAX));
114+
$this->assertFalse($rule->isValid(999999999));
115+
$this->assertFalse($rule->isValid(2147483647)); // Max 32-bit integer
116+
}
117+
118+
#[Test]
119+
public function validates_after_record_deletion(): void
120+
{
121+
$this->migrate(
122+
CreateMigrationsTable::class,
123+
CreateAuthorTable::class,
124+
CreateBookTable::class,
125+
);
126+
127+
$book = Book::create(title: 'Timeline Taxi Draft');
128+
$bookId = $book->id->id;
129+
130+
$rule = new Exists(Book::class);
131+
132+
$this->assertTrue($rule->isValid($bookId));
133+
134+
$book->delete();
135+
136+
$this->assertFalse($rule->isValid($bookId));
137+
}
138+
139+
#[Test]
140+
public function validates_with_sequential_id_creation(): void
141+
{
142+
$this->migrate(
143+
CreateMigrationsTable::class,
144+
CreateAuthorTable::class,
145+
CreateBookTable::class,
146+
);
147+
148+
$rule = new Exists(Book::class);
149+
$createdIds = [];
150+
151+
for ($i = 1; $i <= 5; $i++) {
152+
$book = Book::create(title: "Book {$i}");
153+
$createdIds[] = $book->id->id;
154+
155+
$this->assertTrue($rule->isValid($book->id->id));
156+
}
157+
158+
foreach ($createdIds as $id) {
159+
$this->assertTrue($rule->isValid($id));
160+
}
161+
162+
$maxId = max($createdIds);
163+
$this->assertFalse($rule->isValid($maxId + 1));
164+
$this->assertFalse($rule->isValid($maxId + 100));
165+
}
166+
}

0 commit comments

Comments
 (0)