Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jun 14, 2024
1 parent edca092 commit 2efeec5
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 211 deletions.
401 changes: 193 additions & 208 deletions composer.lock

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions src/Http/Controllers/BelongsToManyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ public function destroy(Request $request, Model $model, Model $related): Redirec

$relation = $field->getRelation($model);

$pivot = $related->getRelation($relation->getPivotAccessor());

$pivot->delete();
$related->getRelation($relation->getPivotAccessor())->delete();

return Redirect::to($field->modelUrl($model))
->with('alerts.relation-deleted', Alert::success(__('The relation has been deleted!')));
Expand Down
39 changes: 39 additions & 0 deletions tests/Http/BelongsToManyControllerTest.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\BelongsToMany;
use Cone\Root\Root;
use Cone\Root\Tests\TestCase;
use Cone\Root\Tests\User;

class BelongsToManyControllerTest extends TestCase
{
protected User $admin;

protected BelongsToMany $field;

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

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

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

public function test_a_belongs_to_many_controller_handles_index(): void
{
$this->actingAs($this->admin)
->get('/root/users/'.$this->admin->getKey().'/fields/teams')
->assertOk()
->assertViewIs('root::resources.index')
->assertViewHas($this->field->toIndex($this->app['request'], $this->admin));
}
}
20 changes: 20 additions & 0 deletions tests/Pivot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Cone\Root\Tests;

use Illuminate\Database\Eloquent\Relations\Pivot as Model;

class Pivot extends Model
{
public function save(array $options = [])
{
$this->setAttribute($this->getKeyName(), $this->getKey());

return true;
}

public function delete()
{
return true;
}
}
14 changes: 14 additions & 0 deletions tests/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Cone\Root\Tests\Resources;

use Cone\Root\Fields\BelongsToMany;
use Cone\Root\Fields\Email;
use Cone\Root\Fields\HasMany;
use Cone\Root\Fields\ID;
use Cone\Root\Fields\Media;
use Cone\Root\Fields\Select;
use Cone\Root\Fields\Text;
use Cone\Root\Resources\Resource;
use Cone\Root\Tests\Actions\SendPasswordResetNotification;
Expand Down Expand Up @@ -45,6 +47,18 @@ public function fields(Request $request): array
Text::make('Mime Type'),
];
}),
BelongsToMany::make('Teams')
->display('name')
->asSubResource()
->withPivotFields(function () {
return [
Select::make('Role')
->options([
'admin' => 'Admin',
'member' => 'Member',
]),
];
}),
];
}

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

namespace Cone\Root\Tests;

use Illuminate\Database\Eloquent\Model;

class Team extends Model
{
protected $guarded = [];
}
4 changes: 4 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public function createApplication(): Application
$app = require __DIR__.'/app.php';

$app->booting(static function () use ($app): void {
$app->afterResolving('migrator', function ($migrator) {
$migrator->path(__DIR__.'/migrations');
});

$app->bind(UserInterface::class, User::class);

Root::instance()->resources->register([
Expand Down
8 changes: 8 additions & 0 deletions tests/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Database\Factories\UserFactory;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand Down Expand Up @@ -44,4 +45,11 @@ public function documents(): MorphToMany
{
return $this->media()->withPivotValue('collection', 'documents');
}

public function teams(): BelongsToMany
{
return $this->belongsToMany(Team::class)
->using(Pivot::class)
->withPivot('role');
}
}
28 changes: 28 additions & 0 deletions tests/migrations/2024_06_14_213747_create_teams_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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::create('teams', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::drop('teams');
}
};
32 changes: 32 additions & 0 deletions tests/migrations/2024_06_14_213755_create_team_user_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use App\Models\User;
use Cone\Root\Tests\Team;
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::create('team_user', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(User::class)->constrained()->cascadeOnDelete();
$table->foreignIdFor(Team::class)->constrained()->cascadeOnDelete();
$table->string('role');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::drop('team_user');
}
};

0 comments on commit 2efeec5

Please sign in to comment.