Skip to content

Commit 3dabc1d

Browse files
committed
refactor: minor clean up
1 parent 73ec523 commit 3dabc1d

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

packages/database/src/Builder/QueryBuilders/SelectQueryBuilder.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ final class SelectQueryBuilder implements BuildsQuery, SupportsWhereStatements
5252

5353
public array $bindings = [];
5454

55+
private Database $database {
56+
get => get(Database::class, $this->onDatabase);
57+
}
58+
59+
private DatabaseContext $context {
60+
get => new DatabaseContext(dialect: $this->database->dialect);
61+
}
62+
5563
public ImmutableArray $wheres {
5664
get => $this->select->where;
5765
set => $this->select->where;
@@ -165,7 +173,7 @@ public function all(mixed ...$bindings): array
165173

166174
return map($query->fetch())
167175
->with(SelectModelMapper::class)
168-
->in(new DatabaseContext(get(Database::class, $this->onDatabase)->dialect))
176+
->in($this->context)
169177
->to($this->model->getName());
170178
}
171179

tests/Integration/Mapper/MapperContextTest.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Tempest\Integration\Mapper;
44

55
use PHPUnit\Framework\Attributes\Test;
6+
use PHPUnit\Framework\Attributes\TestWith;
67
use Tempest\Core\Priority;
78
use Tempest\Mapper;
89
use Tests\Tempest\Fixtures\Modules\Books\Models\Author;
@@ -25,30 +26,34 @@ public function fallbacks_to_default_context(): void
2526
}
2627

2728
#[Test]
28-
public function uses_serializers_from_given_context(): void
29+
#[TestWith(['custom'], name: 'string')]
30+
#[TestWith([TestMapperContextEnum::VALUE], name: 'enum')]
31+
public function uses_serializers_from_given_context(mixed $context): void
2932
{
3033
$author = new Author(
3134
name: 'test',
3235
);
3336

3437
$factory = $this->container->get(Mapper\SerializerFactory::class);
35-
$factory->addSerializer('string', CustomStringSerializer::class, context: 'custom', priority: Priority::HIGHEST);
38+
$factory->addSerializer('string', CustomStringSerializer::class, context: $context, priority: Priority::HIGHEST);
3639

3740
$serialized = Mapper\map($author)
38-
->in('custom')
41+
->in($context)
3942
->toJson();
4043

4144
$this->assertSame('{"name":"{\"type\":\"string\",\"value\":\"test\"}","type":"a","books":[],"publisher":null,"id":null}', $serialized);
4245
}
4346

4447
#[Test]
45-
public function uses_casters_from_given_context(): void
48+
#[TestWith(['custom'], name: 'string')]
49+
#[TestWith([TestMapperContextEnum::VALUE], name: 'enum')]
50+
public function uses_casters_from_given_context(mixed $context): void
4651
{
4752
$factory = $this->container->get(Mapper\CasterFactory::class);
48-
$factory->addCaster('string', CustomStringSerializer::class, context: 'custom');
53+
$factory->addCaster('string', CustomStringSerializer::class, context: $context);
4954

5055
$author = Mapper\make(Author::class)
51-
->in('custom')
56+
->in($context)
5257
->from('{"name":"{\"type\":\"string\",\"value\":\"test\"}","type":"a","books":[],"publisher":null,"id":1}');
5358

5459
$this->assertInstanceOf(Author::class, $author);
@@ -76,3 +81,8 @@ public function cast(mixed $input): mixed
7681
return $data['value'] ?? null;
7782
}
7883
}
84+
85+
enum TestMapperContextEnum: string
86+
{
87+
case VALUE = 'value';
88+
}

0 commit comments

Comments
 (0)