adrema/tests/Unit/InitializeGroupsTest.php

123 lines
4.1 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Unit;
use App\Group as GroupModel;
use App\Initialize\InitializeGroups;
use Illuminate\Foundation\Testing\DatabaseTransactions;
2022-03-06 02:55:14 +01:00
use PHPUnit\Framework\MockObject\Stub;
use Tests\TestCase;
use Zoomyboy\LaravelNami\Api;
2023-02-17 14:54:35 +01:00
use Zoomyboy\LaravelNami\Data\Group;
class InitializeGroupsTest extends TestCase
{
use DatabaseTransactions;
2022-03-06 02:55:14 +01:00
private Stub $api;
public function setUp(): void
{
parent::setUp();
$this->api = $this->createStub(Api::class);
}
2022-03-11 20:19:17 +01:00
public function testItDoesntInitializeGroupsWhenNothingSent(): void
{
$this->api->method('groups')->willReturn(collect([]));
$task = new InitializeGroups($this->api);
$task->handle();
$this->assertDatabaseCount('groups', 0);
}
2022-03-11 20:19:17 +01:00
public function testItSynchsAGroupWithASingleNodeAndNoChildren(): void
{
2023-02-17 14:54:35 +01:00
$parentGroup = Group::from(['id' => 150, 'name' => 'lorem', 'parentId' => null]);
$this->api->method('groups')->will($this->returnValueMap([
2023-02-17 14:54:35 +01:00
[null, collect([$parentGroup])],
[$parentGroup, collect([])],
]));
2023-02-17 14:54:35 +01:00
$this->api->method('groups')->willReturn(collect([]));
(new InitializeGroups($this->api))->handle();
$this->assertDatabaseHas('groups', [
'nami_id' => 150,
'name' => 'lorem',
2022-03-11 20:19:17 +01:00
'parent_id' => null,
]);
}
public function testItDoesntCreateAGroupTwiceWithTheSameNamiId(): void
{
GroupModel::factory()->create(['nami_id' => 150]);
2023-02-17 14:54:35 +01:00
$parentGroup = Group::from(['id' => 150, 'name' => 'lorem', 'parentId' => null]);
$this->api->method('groups')->will($this->returnValueMap([
2023-02-17 14:54:35 +01:00
[null, collect([$parentGroup])],
[$parentGroup, collect([])],
]));
(new InitializeGroups($this->api))->handle();
$this->assertDatabaseCount('groups', 1);
}
public function testItSynchsSubgroups(): void
{
2023-02-17 14:54:35 +01:00
$parentGroup = Group::from(['id' => 150, 'name' => 'lorem', 'parentId' => null]);
$subgroup = Group::from(['id' => 200, 'name' => 'subgroup', 'parentId' => 150]);
$this->api->method('groups')->will($this->returnValueMap([
[null, collect([$parentGroup])],
[$parentGroup, collect([$subgroup])],
[$subgroup, collect([])],
]));
(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);
}
2022-03-15 10:24:04 +01:00
public function testItSynchsSubgroupsOfSubgroups(): void
{
2023-02-17 14:54:35 +01:00
$parentGroup = Group::from(['id' => 150, 'name' => 'lorem', 'parentId' => null]);
$subgroup = Group::from(['id' => 200, 'name' => 'subgroup', 'parentId' => 150]);
$subsubgroup = Group::from(['id' => 250, 'name' => 'subsubgroup', 'parentId' => 200]);
$this->api->method('groups')->will($this->returnValueMap([
[null, collect([$parentGroup])],
[$parentGroup, collect([$subgroup])],
[$subgroup, collect([$subsubgroup])],
[$subsubgroup, collect([])],
]));
2022-03-15 10:24:04 +01:00
(new InitializeGroups($this->api))->handle();
$this->assertDatabaseCount('groups', 3);
}
2022-02-17 16:42:56 +01:00
public function testItAssignsIdAndParentToAnExistingSubgroup(): void
{
GroupModel::factory()->create(['nami_id' => 200]);
2023-02-17 14:54:35 +01:00
$parentGroup = Group::from(['id' => 150, 'name' => 'root', 'parentId' => null]);
$subgroup = Group::from(['id' => 200, 'name' => 'child', 'parentId' => 150]);
$this->api->method('groups')->will($this->returnValueMap([
[null, collect([$parentGroup])],
[$parentGroup, collect([$subgroup])],
[$subgroup, collect([])],
]));
2022-02-17 16:42:56 +01:00
(new InitializeGroups($this->api))->handle();
$this->assertDatabaseCount('groups', 2);
$this->assertDatabaseHas('groups', [
'nami_id' => 200,
'name' => 'child',
'parent_id' => GroupModel::firstWhere('nami_id', 150)->id,
]);
}
}