Skip to content

Commit

Permalink
fixes, more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jun 28, 2024
1 parent 5369778 commit f6153ae
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/Fields/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public function resolveRelatableQuery(Request $request, Model $model): Builder
*/
public function resolveHydrate(Request $request, Model $model, mixed $value): void
{
$value = explode(':', $value);
$value = is_null($value) ? $value : explode(':', $value);

$model->setAttribute($this->getRelation($model)->getMorphType(), $value[0]);

$related = tap(new $value[0], static function (Model $related) use ($value): void {
$related->forceFill([$related->getKeyName() => $value[1]]);
});
$related = is_null($value)
? $value
: tap(new $value[0], static function (Model $related) use ($value): void {
$related->forceFill([$related->getKeyName() => $value[1]]);
});

parent::resolveHydrate($request, $model, $related);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/MorphToController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class MorphToController extends Controller
/**
* Handle the incoming request.
*/
public function __invoke(Request $request, Model $model, ?Model $related = null): Response
public function __invoke(Request $request, Model $model): Response
{
$field = $request->route('field');

$data = $field->toInput($request, $related ?: $model);
$data = $field->toInput($request, $model);

return ResponseFactory::view($data['template'], $data);
}
Expand Down
39 changes: 39 additions & 0 deletions tests/Http/MorphToControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Cone\Root\Tests\Http;

use Cone\Root\Fields\MorphTo;
use Cone\Root\Root;
use Cone\Root\Tests\TestCase;
use Cone\Root\Tests\User;

class MorphToControllerTest extends TestCase
{
protected MorphTo $field;

protected User $admin;

public function setUp(): void
{
parent::setUp();

$this->field = Root::instance()
->resources
->resolve('users')
->resolveFields($this->app['request'])
->first(function ($field) {
return $field->getModelAttribute() === 'employer';
});

$this->admin = User::factory()->create();
}

public function test_a_relation_controller_handles_request(): void
{
$this->actingAs($this->admin)
->get('/root/users/'.$this->admin->getKey().'/fields/employer')
->assertOk()
->assertViewIs('root::fields.morph-to')
->assertViewHas($this->field->toInput($this->app['request'], $this->admin));
}
}
6 changes: 6 additions & 0 deletions tests/Http/ResourceControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public function test_a_resource_controller_handles_destroy(): void
{
$user = User::factory()->create();

$this->actingAs($this->admin)
->delete($this->resource->getUri().'/'.$user->getKey())
->assertRedirect();

$this->assertTrue($user->refresh()->trashed());

$this->actingAs($this->admin)
->delete($this->resource->getUri().'/'.$user->getKey())
->assertRedirect();
Expand Down
4 changes: 2 additions & 2 deletions tests/Resources/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function test_a_resource_resolves_query(): void
$this->assertSame(['documents'], array_keys($query->getEagerLoads()));

$this->assertSame(
'select * from "users"',
'select * from "users" where "users"."deleted_at" is null',
$query->toRawSql()
);
}
Expand All @@ -68,7 +68,7 @@ public function test_a_resource_resolves_filtered_query(): void
$query = $this->resource->resolveFilteredQuery($this->app['request']);

$this->assertSame(
'select * from "users" where ("users"."name" like \'%test%\' or "users"."email" like \'%test%\') order by "users"."id" asc',
'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()
);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use Cone\Root\Fields\HasMany;
use Cone\Root\Fields\ID;
use Cone\Root\Fields\Media;
use Cone\Root\Fields\MorphTo;
use Cone\Root\Fields\Select;
use Cone\Root\Fields\Text;
use Cone\Root\Resources\Resource;
use Cone\Root\Tests\Actions\SendPasswordResetNotification;
use Cone\Root\Tests\Team;
use Cone\Root\Tests\User;
use Cone\Root\Tests\Widgets\UsersCount;
use Cone\Root\Tests\Widgets\UsersTrend;
Expand Down Expand Up @@ -59,6 +61,14 @@ public function fields(Request $request): array
]),
];
}),

MorphTo::make('Employer')
->display('name')
->nullable()
->types([
User::class,
Team::class,
]),
];
}

Expand Down
17 changes: 16 additions & 1 deletion tests/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;

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

protected $guarded = [];

Expand All @@ -25,6 +26,15 @@ protected static function newFactory(): UserFactory
return new class() extends UserFactory
{
protected $model = User::class;

public function definition(): array
{
return array_merge(parent::definition(), [
'employer_id' => null,
'employer_type' => null,
'deleted_at' => null,
]);
}
};
}

Expand Down Expand Up @@ -53,4 +63,9 @@ public function teams(): BelongsToMany
return $this->belongsToMany(Team::class)
->withPivot(['id', 'role']);
}

public function employer(): MorphTo
{
return $this->morphTo();
}
}
34 changes: 34 additions & 0 deletions tests/migrations/2024_06_14_213755_alter_users_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->after('id', function (Blueprint $table) {
$table->nullableMorphs('employer');
});
$table->after('updated_at', function (Blueprint $table) {
$table->softDeletes();
});
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropMorphs('employer');
$table->dropSoftDeletes();
});
}
};

0 comments on commit f6153ae

Please sign in to comment.