Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 22, 2024
1 parent 8fdff9e commit d255759
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 19 deletions.
34 changes: 17 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions tests/Filters/MediaSearchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Cone\Root\Tests\Filters;

use Cone\Root\Filters\MediaSearch;
use Cone\Root\Tests\TestCase;

class MediaSearchTest extends TestCase
{
public function test_a_media_search_has_searchable_attributes(): void
{
$filter = new MediaSearch(['file_name', 'name']);

$this->assertSame(
['file_name' => null, 'name' => null],
$filter->getSearchableAttributes()
);
}
}
31 changes: 31 additions & 0 deletions tests/Filters/SearchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Cone\Root\Tests\Filters;

use Cone\Root\Fields\BelongsToMany;
use Cone\Root\Fields\Fields;
use Cone\Root\Fields\Text;
use Cone\Root\Filters\Search;
use Cone\Root\Tests\TestCase;
use Cone\Root\Tests\User;

class SearchTest extends TestCase
{
public function test_a_search_filter_modifies_query(): void
{
$filter = new Search(new Fields([
new Text('Name'),
new BelongsToMany('Teams'),
]));

$this->assertSame(
'select * from "users" where "users"."deleted_at" is null',
$filter->apply($this->app['request'], User::query(), null)->toRawSql()
);

$this->assertSame(
'select * from "users" where ("users"."name" like \'%foo%\' or exists (select * from "teams" inner join "team_user" on "teams"."id" = "team_user"."team_id" where "users"."id" = "team_user"."user_id" and "teams"."id" like \'%foo%\')) and "users"."deleted_at" is null',
$filter->apply($this->app['request'], User::query(), 'foo')->toRawSql()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class SelectFilterTest extends TestCase
class SelectTest extends TestCase
{
protected Select $filter;

Expand Down
36 changes: 36 additions & 0 deletions tests/Filters/SortTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Cone\Root\Tests\Filters;

use Cone\Root\Fields\Fields;
use Cone\Root\Fields\MorphTo;
use Cone\Root\Fields\Text;
use Cone\Root\Filters\Sort;
use Cone\Root\Tests\TestCase;
use Cone\Root\Tests\User;

class SortTest extends TestCase
{
public function test_a_sort_filter_modifies_query(): void
{
$filter = new Sort(new Fields([
Text::make('Name')->sortable(),
MorphTo::make('Employer')->sortable(column: 'name'),
]));

$this->assertSame(
'select * from "users" where "users"."deleted_at" is null',
$filter->apply($this->app['request'], User::query(), ['by' => 'dummy'])->toRawSql()
);

$this->assertSame(
'select * from "users" where "users"."deleted_at" is null order by "users"."name" desc',
$filter->apply($this->app['request'], User::query(), ['by' => 'name'])->toRawSql()
);

$this->assertSame(
'select * from "users" where "users"."deleted_at" is null order by (select "users"."name" from "users" where "users"."employer_id" = "users"."id" and "users"."deleted_at" is null) desc',
$filter->apply($this->app['request'], User::query(), ['by' => 'employer'])->toRawSql()
);
}
}
2 changes: 1 addition & 1 deletion tests/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function teams(): BelongsToMany

public function employer(): MorphTo
{
return $this->morphTo();
return $this->morphTo(ownerKey: 'id');
}

public function shouldTwoFactorAuthenticate(Request $request): bool
Expand Down

0 comments on commit d255759

Please sign in to comment.