Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jan 29, 2024
1 parent 80d0d9c commit 7530822
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ protected function resolveField(Request $request, Field $field): void
*/
protected function resolveFilter(Request $request, Filter $filter): void
{
$filter->setKey(sprintf('%s:%s', $this->getKey(), $filter->getKey()));
$filter->setKey(sprintf('%s_%s', $this->getKey(), $filter->getKey()));
}

/**
Expand Down
1 change: 0 additions & 1 deletion tests/Actions/ActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Cone\Root\Tests\Actions;

use Cone\Root\Fields\Text;
use Cone\Root\Tests\SendPasswordResetNotification;
use Cone\Root\Tests\TestCase;
use Cone\Root\Tests\User;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Cone\Root\Tests;
namespace Cone\Root\Tests\Actions;

use Cone\Root\Actions\Action;
use Illuminate\Database\Eloquent\Collection;
Expand Down
74 changes: 74 additions & 0 deletions tests/Resources/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,78 @@ public function test_a_resource_has_icon(): void
$this->resource->icon('users');
$this->assertSame('users', $this->resource->getIcon());
}

public function test_a_resource_resolves_query(): void
{
$this->resource->with(['documents']);

$query = $this->resource->resolveQuery($this->app['request']);

$this->assertInstanceOf(User::class, $query->getModel());
$this->assertSame(['documents'], array_keys($query->getEagerLoads()));

$this->assertSame(
'select * from "users" where "users"."deleted_at" is null',
$query->toRawSql()
);
}

public function test_a_resource_resolves_filtered_query(): void
{
$this->app['request']->merge([
'users_sort' => ['order' => 'asc', 'by' => 'id'],
'users_search' => 'test',
]);

$query = $this->resource->resolveFilteredQuery($this->app['request']);

$this->assertSame(
'select * from "users" where ("users"."name" like \'%test%\' or "users"."email" like \'%test%\') and "users"."deleted_at" is null order by "users"."id" asc',
$query->toRawSql()
);
}

public function test_a_resource_resolves_filters(): void
{
$filters = $this->resource->resolveFilters($this->app['request']);

$this->assertTrue($filters->isNotEmpty());
}

public function test_a_resource_resolves_actions(): void
{
$actions = $this->resource->resolveActions($this->app['request']);

$this->assertTrue($actions->isNotEmpty());
}

public function test_a_resource_resolves_fields(): void
{
$fields = $this->resource->resolveFields($this->app['request']);

$this->assertTrue($fields->isNotEmpty());
}

public function test_a_resource_resolves_widgets(): void
{
$widgets = $this->resource->resolveWidgets($this->app['request']);

$this->assertTrue($widgets->isNotEmpty());
}

public function test_a_resource_registers_routes(): void
{
$this->app['router']->prefix('root')->group(function ($router) {
$this->resource->registerRoutes($this->app['request'], $router);
});

$action = $this->resource->resolveActions($this->app['request'])->first();

$this->assertSame('/root/users/actions/send-password-reset-notification', $action->getUri());

$this->assertArrayHasKey(
trim($action->getUri(), '/'),
$this->app['router']->getRoutes()->get('POST')
);
}
}
31 changes: 31 additions & 0 deletions tests/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,41 @@

namespace Cone\Root\Tests\Resources;

use Cone\Root\Fields\Email;
use Cone\Root\Fields\ID;
use Cone\Root\Fields\Text;
use Cone\Root\Resources\Resource;
use Cone\Root\Tests\Actions\SendPasswordResetNotification;
use Cone\Root\Tests\User;
use Cone\Root\Tests\Widgets\UsersCount;
use Cone\Root\Tests\Widgets\UsersTrend;
use Illuminate\Http\Request;

class UserResource extends Resource
{
protected string $model = User::class;

public function actions(Request $request): array
{
return [
new SendPasswordResetNotification(),
];
}

public function fields(Request $request): array
{
return [
ID::make(),
Text::make('Name')->searchable(),
Email::make('Email')->searchable(),
];
}

public function widgets(Request $request): array
{
return [
new UsersCount(),
new UsersTrend(),
];
}
}
2 changes: 2 additions & 0 deletions tests/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model implements MustVerifyEmail
{
use HasFactory;
use SoftDeletes;

protected $guarded = [];

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

namespace Cone\Root\Tests\Widgets;

use Cone\Root\Widgets\Value;

class UsersCount extends Value
{
//
}
10 changes: 10 additions & 0 deletions tests/Widgets/UsersTrend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Cone\Root\Tests\Widgets;

use Cone\Root\Widgets\Trend;

class UsersTrend extends Trend
{
//
}
20 changes: 20 additions & 0 deletions tests/Widgets/WidgetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Cone\Root\Tests\Widgets;

use Cone\Root\Tests\TestCase;

class WidgetTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

//
}

public function test_a_widget_has_template(): void
{
$this->assertTrue(true);
}
}

0 comments on commit 7530822

Please sign in to comment.