Skip to content

Commit 7f08545

Browse files
committed
test: add snake case relation tests
1 parent 28a441b commit 7f08545

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
namespace Tests\Tempest\Integration\Database\ModelInspector;
4+
5+
use Tempest\Database\BelongsTo;
6+
use Tempest\Database\Config\DatabaseDialect;
7+
use Tempest\Database\HasMany;
8+
use Tempest\Database\HasOne;
9+
use Tempest\Database\PrimaryKey;
10+
use Tempest\Database\Table;
11+
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
12+
13+
use function Tempest\Database\inspect;
14+
15+
final class SnakeCaseRelationTest extends FrameworkIntegrationTestCase
16+
{
17+
public function test_belongs_to_with_snake_case_property(): void
18+
{
19+
$model = inspect(model: SnakeCaseBookModel::class);
20+
$relation = $model->getRelation(name: 'main_author');
21+
22+
$this->assertInstanceOf(
23+
expected: BelongsTo::class,
24+
actual: $relation,
25+
);
26+
$this->assertSame(
27+
expected: 'LEFT JOIN author ON author.id = book.author_id',
28+
actual: $relation
29+
->getJoinStatement()
30+
->compile(dialect: DatabaseDialect::SQLITE),
31+
);
32+
}
33+
34+
public function test_has_one_with_snake_case_property(): void
35+
{
36+
$model = inspect(model: SnakeCaseAuthorModel::class);
37+
$relation = $model->getRelation(name: 'author_profile');
38+
39+
$this->assertInstanceOf(
40+
expected: HasOne::class,
41+
actual: $relation,
42+
);
43+
$this->assertSame(
44+
expected: 'LEFT JOIN profile ON profile.author_id = author.id',
45+
actual: $relation
46+
->getJoinStatement()
47+
->compile(dialect: DatabaseDialect::SQLITE),
48+
);
49+
}
50+
51+
public function test_has_many_with_snake_case_property(): void
52+
{
53+
$model = inspect(model: SnakeCaseAuthorModel::class);
54+
$relation = $model->getRelation(name: 'written_books');
55+
56+
$this->assertInstanceOf(
57+
expected: HasMany::class,
58+
actual: $relation,
59+
);
60+
$this->assertSame(
61+
expected: 'LEFT JOIN book ON book.author_id = author.id',
62+
actual: $relation
63+
->getJoinStatement()
64+
->compile(dialect: DatabaseDialect::SQLITE),
65+
);
66+
}
67+
68+
public function test_snake_case_property_is_recognized_as_relation(): void
69+
{
70+
$model = inspect(model: SnakeCaseBookModel::class);
71+
72+
$this->assertTrue(condition: $model->isRelation(name: 'main_author'));
73+
}
74+
75+
public function test_snake_case_property_excluded_from_select_fields(): void
76+
{
77+
$model = inspect(model: SnakeCaseAuthorModel::class);
78+
$fields = $model->getSelectFields();
79+
80+
$fieldNames = [];
81+
foreach ($fields as $field) {
82+
$fieldNames[] = $field;
83+
}
84+
85+
$this->assertNotContains(
86+
needle: 'author_profile',
87+
haystack: $fieldNames,
88+
);
89+
$this->assertNotContains(
90+
needle: 'written_books',
91+
haystack: $fieldNames,
92+
);
93+
$this->assertContains(
94+
needle: 'name',
95+
haystack: $fieldNames,
96+
);
97+
}
98+
}
99+
100+
#[Table(name: 'book')]
101+
final class SnakeCaseBookModel
102+
{
103+
public PrimaryKey $id;
104+
105+
public SnakeCaseAuthorModel $main_author;
106+
107+
public string $title;
108+
}
109+
110+
#[Table(name: 'author')]
111+
final class SnakeCaseAuthorModel
112+
{
113+
public PrimaryKey $id;
114+
115+
#[HasOne]
116+
public ?SnakeCaseProfileModel $author_profile = null;
117+
118+
/** @var \Tests\Tempest\Integration\Database\ModelInspector\SnakeCaseBookModel[] */
119+
public array $written_books = [];
120+
121+
public string $name;
122+
}
123+
124+
#[Table(name: 'profile')]
125+
final class SnakeCaseProfileModel
126+
{
127+
public PrimaryKey $id;
128+
129+
public SnakeCaseAuthorModel $author;
130+
131+
public string $bio;
132+
}

0 commit comments

Comments
 (0)