Add initialize groups
Todo: Integrate this in the initializer
This commit is contained in:
parent
26e3ed1bf1
commit
4b0b44d302
|
@ -4,16 +4,22 @@ namespace App;
|
|||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Group extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public $fillable = ['nami_id', 'name'];
|
||||
public $fillable = ['nami_id', 'name', 'parent_id'];
|
||||
public $timestamps = false;
|
||||
|
||||
public static function nami(int $id): self
|
||||
{
|
||||
return static::firstWhere('nami_id', $id);
|
||||
}
|
||||
|
||||
public function parent(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(static::class, 'parent_id');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace App\Initialize;
|
||||
|
||||
use App\Group;
|
||||
use Zoomyboy\LaravelNami\Api;
|
||||
use Zoomyboy\LaravelNami\NamiUser;
|
||||
|
||||
class InitializeGroups {
|
||||
|
||||
private Api $api;
|
||||
|
||||
public function __construct(Api $api) {
|
||||
$this->api = $api;
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$this->api->groups()->each(function($group) {
|
||||
$parent = Group::updateOrCreate(['nami_id' => $group->id], ['nami_id' => $group->id, 'name' => $group->name]);
|
||||
|
||||
$this->api->subgroupsOf($group->id)->each(function ($subgroup) use ($parent) {
|
||||
Group::updateOrCreate(['nami_id' => $subgroup->id], ['nami_id' => $subgroup->id, 'name' => $subgroup->name, 'parent_id' => $parent->id]);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateGroupsParentIdColumn extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('groups', function (Blueprint $table) {
|
||||
$table->foreignId('parent_id')->nullable()->constrained('groups');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('groups', function (Blueprint $table) {
|
||||
$table->dropForeign(['group_id']);
|
||||
$table->dropColumn('group_id');
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1 +1 @@
|
|||
Subproject commit 876e026fd0db17f694323e253ace58a6ffe90d87
|
||||
Subproject commit 829dd01358858c887dddaeadd6e4ddebd8f9fb0b
|
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Group as GroupModel;
|
||||
use App\Initialize\InitializeGroups;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Tests\TestCase;
|
||||
use Zoomyboy\LaravelNami\Api;
|
||||
use Zoomyboy\LaravelNami\Group;
|
||||
|
||||
class InitializeGroupsTest extends TestCase
|
||||
{
|
||||
|
||||
use DatabaseTransactions;
|
||||
|
||||
private Api $api;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->api = $this->createStub(Api::class);
|
||||
}
|
||||
|
||||
public function test_it_doesnt_initialize_groups_when_nothing_sent(): void
|
||||
{
|
||||
$this->api->method('groups')->willReturn(collect([]));
|
||||
|
||||
$task = new InitializeGroups($this->api);
|
||||
$task->handle();
|
||||
|
||||
$this->assertDatabaseCount('groups', 0);
|
||||
}
|
||||
|
||||
public function test_it_synchs_a_group_with_a_single_node_and_no_children(): void
|
||||
{
|
||||
$this->api->method('groups')->will($this->returnValueMap([
|
||||
[
|
||||
null,
|
||||
collect([(new Group())->setParentId(null)->setId(150)->setName('lorem')])
|
||||
]
|
||||
]));
|
||||
$this->api->method('subgroupsOf')->willReturn(collect([]));
|
||||
|
||||
(new InitializeGroups($this->api))->handle();
|
||||
|
||||
$this->assertDatabaseHas('groups', [
|
||||
'nami_id' => 150,
|
||||
'name' => 'lorem',
|
||||
'parent_id' => null
|
||||
]);
|
||||
}
|
||||
|
||||
public function testItDoesntCreateAGroupTwiceWithTheSameNamiId(): void
|
||||
{
|
||||
GroupModel::factory()->create(['nami_id' => 150]);
|
||||
$this->api->method('groups')->will($this->returnValueMap([
|
||||
[
|
||||
null,
|
||||
collect([(new Group())->setParentId(null)->setId(150)->setName('lorem')])
|
||||
]
|
||||
]));
|
||||
$this->api->method('subgroupsOf')->willReturn(collect([]));
|
||||
|
||||
(new InitializeGroups($this->api))->handle();
|
||||
|
||||
$this->assertDatabaseCount('groups', 1);
|
||||
}
|
||||
|
||||
public function testItSynchsSubgroups(): void
|
||||
{
|
||||
GroupModel::factory()->create(['nami_id' => 150]);
|
||||
$this->api->method('groups')->willReturn(
|
||||
collect([(new Group())->setParentId(null)->setId(150)->setName('lorem')])
|
||||
);
|
||||
$this->api->method('subgroupsOf')->willReturn(
|
||||
collect([(new Group())->setParentId(150)->setId(200)->setName('subgroup')])
|
||||
);
|
||||
|
||||
(new InitializeGroups($this->api))->handle();
|
||||
|
||||
$this->assertDatabaseCount('groups', 2);
|
||||
$subgroup = GroupModel::where('nami_id', 200)->firstOrFail();
|
||||
$this->assertEquals('subgroup', $subgroup->name);
|
||||
$this->assertEquals(150, $subgroup->parent->nami_id);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue