70 lines
2.3 KiB
PHP
70 lines
2.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Unit;
|
||
|
|
|
||
|
|
use App\Actions\Prompts\ListPrompts;
|
||
|
|
use PHPUnit\Framework\TestCase;
|
||
|
|
|
||
|
|
class ListPromptsTest extends TestCase
|
||
|
|
{
|
||
|
|
public function test_constructor_defaults(): void
|
||
|
|
{
|
||
|
|
$query = new ListPrompts;
|
||
|
|
|
||
|
|
$this->assertNull($query->search);
|
||
|
|
$this->assertNull($query->tag);
|
||
|
|
$this->assertFalse($query->favoritesOnly);
|
||
|
|
$this->assertSame('created_at', $query->sort);
|
||
|
|
$this->assertSame('desc', $query->direction);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_constructor_accepts_custom_values(): void
|
||
|
|
{
|
||
|
|
$query = new ListPrompts(
|
||
|
|
search: 'regex',
|
||
|
|
tag: 'coding',
|
||
|
|
favoritesOnly: true,
|
||
|
|
sort: 'name',
|
||
|
|
direction: 'asc',
|
||
|
|
);
|
||
|
|
|
||
|
|
$this->assertSame('regex', $query->search);
|
||
|
|
$this->assertSame('coding', $query->tag);
|
||
|
|
$this->assertTrue($query->favoritesOnly);
|
||
|
|
$this->assertSame('name', $query->sort);
|
||
|
|
$this->assertSame('asc', $query->direction);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_normalized_sort_passes_whitelisted_columns(): void
|
||
|
|
{
|
||
|
|
foreach (ListPrompts::SORTABLE_COLUMNS as $column) {
|
||
|
|
$this->assertSame($column, (new ListPrompts(sort: $column))->normalizedSort());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_normalized_sort_falls_back_to_created_at(): void
|
||
|
|
{
|
||
|
|
$this->assertSame(
|
||
|
|
'created_at',
|
||
|
|
(new ListPrompts(sort: 'name; DROP TABLE prompts'))->normalizedSort()
|
||
|
|
);
|
||
|
|
$this->assertSame('created_at', (new ListPrompts(sort: ''))->normalizedSort());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_normalized_direction_passes_known_values(): void
|
||
|
|
{
|
||
|
|
$this->assertSame('asc', (new ListPrompts(direction: 'asc'))->normalizedDirection());
|
||
|
|
$this->assertSame('desc', (new ListPrompts(direction: 'desc'))->normalizedDirection());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_normalized_direction_defaults_depend_on_column(): void
|
||
|
|
{
|
||
|
|
$this->assertSame('asc', (new ListPrompts(sort: 'name', direction: 'sideways'))->normalizedDirection());
|
||
|
|
$this->assertSame('desc', (new ListPrompts(sort: 'usage_count', direction: 'sideways'))->normalizedDirection());
|
||
|
|
$this->assertSame(
|
||
|
|
'desc',
|
||
|
|
(new ListPrompts(sort: 'not-a-column', direction: 'sideways'))->normalizedDirection()
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|