Skip to content

Commit d255759

Browse files
committed
tests
1 parent 8fdff9e commit d255759

File tree

6 files changed

+105
-19
lines changed

6 files changed

+105
-19
lines changed

composer.lock

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Filters/MediaSearchTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Cone\Root\Tests\Filters;
4+
5+
use Cone\Root\Filters\MediaSearch;
6+
use Cone\Root\Tests\TestCase;
7+
8+
class MediaSearchTest extends TestCase
9+
{
10+
public function test_a_media_search_has_searchable_attributes(): void
11+
{
12+
$filter = new MediaSearch(['file_name', 'name']);
13+
14+
$this->assertSame(
15+
['file_name' => null, 'name' => null],
16+
$filter->getSearchableAttributes()
17+
);
18+
}
19+
}

tests/Filters/SearchTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Cone\Root\Tests\Filters;
4+
5+
use Cone\Root\Fields\BelongsToMany;
6+
use Cone\Root\Fields\Fields;
7+
use Cone\Root\Fields\Text;
8+
use Cone\Root\Filters\Search;
9+
use Cone\Root\Tests\TestCase;
10+
use Cone\Root\Tests\User;
11+
12+
class SearchTest extends TestCase
13+
{
14+
public function test_a_search_filter_modifies_query(): void
15+
{
16+
$filter = new Search(new Fields([
17+
new Text('Name'),
18+
new BelongsToMany('Teams'),
19+
]));
20+
21+
$this->assertSame(
22+
'select * from "users" where "users"."deleted_at" is null',
23+
$filter->apply($this->app['request'], User::query(), null)->toRawSql()
24+
);
25+
26+
$this->assertSame(
27+
'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',
28+
$filter->apply($this->app['request'], User::query(), 'foo')->toRawSql()
29+
);
30+
}
31+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Illuminate\Database\Eloquent\Builder;
88
use Illuminate\Http\Request;
99

10-
class SelectFilterTest extends TestCase
10+
class SelectTest extends TestCase
1111
{
1212
protected Select $filter;
1313

tests/Filters/SortTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Cone\Root\Tests\Filters;
4+
5+
use Cone\Root\Fields\Fields;
6+
use Cone\Root\Fields\MorphTo;
7+
use Cone\Root\Fields\Text;
8+
use Cone\Root\Filters\Sort;
9+
use Cone\Root\Tests\TestCase;
10+
use Cone\Root\Tests\User;
11+
12+
class SortTest extends TestCase
13+
{
14+
public function test_a_sort_filter_modifies_query(): void
15+
{
16+
$filter = new Sort(new Fields([
17+
Text::make('Name')->sortable(),
18+
MorphTo::make('Employer')->sortable(column: 'name'),
19+
]));
20+
21+
$this->assertSame(
22+
'select * from "users" where "users"."deleted_at" is null',
23+
$filter->apply($this->app['request'], User::query(), ['by' => 'dummy'])->toRawSql()
24+
);
25+
26+
$this->assertSame(
27+
'select * from "users" where "users"."deleted_at" is null order by "users"."name" desc',
28+
$filter->apply($this->app['request'], User::query(), ['by' => 'name'])->toRawSql()
29+
);
30+
31+
$this->assertSame(
32+
'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',
33+
$filter->apply($this->app['request'], User::query(), ['by' => 'employer'])->toRawSql()
34+
);
35+
}
36+
}

tests/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function teams(): BelongsToMany
7171

7272
public function employer(): MorphTo
7373
{
74-
return $this->morphTo();
74+
return $this->morphTo(ownerKey: 'id');
7575
}
7676

7777
public function shouldTwoFactorAuthenticate(Request $request): bool

0 commit comments

Comments
 (0)