2021-11-30 17:39:33 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Api\v1\Controllers;
|
|
|
|
|
2021-12-02 13:15:53 +01:00
|
|
|
use App\Models\Group;
|
2021-11-30 17:39:33 +01:00
|
|
|
use Tests\TestCase;
|
2021-12-02 13:15:53 +01:00
|
|
|
use App\Models\TwoFAccount;
|
2022-07-30 11:25:19 +02:00
|
|
|
use App\Facades\Groups;
|
2022-07-29 18:34:27 +02:00
|
|
|
use App\Services\SettingService;
|
2021-11-30 17:39:33 +01:00
|
|
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
|
|
|
use App\Api\v1\Controllers\GroupController;
|
|
|
|
use Mockery;
|
2022-07-29 18:34:27 +02:00
|
|
|
use Mockery\MockInterface;
|
2021-11-30 17:39:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \App\Api\v1\Controllers\GroupController
|
|
|
|
*/
|
|
|
|
class GroupControllerTest extends TestCase
|
|
|
|
{
|
|
|
|
use WithoutMiddleware;
|
|
|
|
|
|
|
|
/**
|
2022-07-29 18:34:27 +02:00
|
|
|
* @var \App\Api\v1\Controllers\GroupController tested controller
|
2021-11-30 17:39:33 +01:00
|
|
|
*/
|
|
|
|
protected $controller;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \App\Api\v1\Requests\GroupStoreRequest mocked request
|
|
|
|
*/
|
|
|
|
protected $groupStoreRequest;
|
|
|
|
|
|
|
|
|
|
|
|
public function setUp() : void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->groupStoreRequest = Mockery::mock('App\Api\v1\Requests\GroupStoreRequest');
|
|
|
|
|
2022-07-29 18:34:27 +02:00
|
|
|
$this->controller = new GroupController();
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_index_returns_api_resources_using_groupService()
|
|
|
|
{
|
2021-12-02 13:15:53 +01:00
|
|
|
$groups = Group::factory()->count(3)->make();
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2022-07-30 11:25:19 +02:00
|
|
|
Groups::shouldReceive('getAll')
|
2021-11-30 17:39:33 +01:00
|
|
|
->once()
|
|
|
|
->andReturn($groups);
|
|
|
|
|
|
|
|
$response = $this->controller->index();
|
|
|
|
|
|
|
|
$this->assertContainsOnlyInstancesOf('App\Api\v1\Resources\GroupResource', $response->collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_store_returns_api_resource_stored_using_groupService()
|
|
|
|
{
|
2021-12-02 13:15:53 +01:00
|
|
|
$group = Group::factory()->make();
|
2021-11-30 17:39:33 +01:00
|
|
|
|
|
|
|
$this->groupStoreRequest->shouldReceive('validated')
|
|
|
|
->once()
|
|
|
|
->andReturn(['name' => $group->name]);
|
|
|
|
|
2022-07-30 11:25:19 +02:00
|
|
|
Groups::shouldReceive('create')
|
2021-11-30 17:39:33 +01:00
|
|
|
->once()
|
|
|
|
->andReturn($group);
|
|
|
|
|
|
|
|
$response = $this->controller->store($this->groupStoreRequest);
|
|
|
|
|
2021-12-02 13:15:53 +01:00
|
|
|
$this->assertInstanceOf('App\Models\Group', $response->original);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_show_returns_api_resource()
|
|
|
|
{
|
2021-12-02 13:15:53 +01:00
|
|
|
$group = Group::factory()->make();
|
2021-11-30 17:39:33 +01:00
|
|
|
|
|
|
|
$response = $this->controller->show($group);
|
|
|
|
|
|
|
|
$this->assertInstanceOf('App\Api\v1\Resources\GroupResource', $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_update_returns_api_resource_updated_using_groupService()
|
|
|
|
{
|
2021-12-02 13:15:53 +01:00
|
|
|
$group = Group::factory()->make();
|
2021-11-30 17:39:33 +01:00
|
|
|
|
|
|
|
$this->groupStoreRequest->shouldReceive('validated')
|
|
|
|
->once()
|
|
|
|
->andReturn(['name' => $group->name]);
|
|
|
|
|
2022-07-30 11:25:19 +02:00
|
|
|
Groups::shouldReceive('update')
|
2021-11-30 17:39:33 +01:00
|
|
|
->once()
|
|
|
|
->andReturn($group);
|
|
|
|
|
|
|
|
$response = $this->controller->update($this->groupStoreRequest, $group);
|
|
|
|
|
|
|
|
$this->assertInstanceOf('App\Api\v1\Resources\GroupResource', $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_assignAccounts_returns_api_resource_assigned_using_groupService()
|
|
|
|
{
|
2021-12-02 13:15:53 +01:00
|
|
|
$group = Group::factory()->make();
|
2021-11-30 17:39:33 +01:00
|
|
|
$groupAssignRequest = Mockery::mock('App\Api\v1\Requests\GroupAssignRequest');
|
|
|
|
|
|
|
|
$groupAssignRequest->shouldReceive('validated')
|
|
|
|
->once()
|
|
|
|
->andReturn(['ids' => $group->id]);
|
|
|
|
|
2022-07-30 11:25:19 +02:00
|
|
|
Groups::shouldReceive('assign')
|
2021-11-30 17:39:33 +01:00
|
|
|
->with($group->id, $group)
|
|
|
|
->once();
|
|
|
|
|
|
|
|
$response = $this->controller->assignAccounts($groupAssignRequest, $group);
|
|
|
|
|
|
|
|
$this->assertInstanceOf('App\Api\v1\Resources\GroupResource', $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_accounts_returns_api_resources_fetched_using_groupService()
|
|
|
|
{
|
2021-12-02 13:15:53 +01:00
|
|
|
$group = Group::factory()->make();
|
2022-07-29 18:34:27 +02:00
|
|
|
|
|
|
|
$this->mock(SettingService::class, function (MockInterface $mock) {
|
|
|
|
$mock->shouldReceive('get')
|
|
|
|
->with('useEncryption')
|
|
|
|
->andReturn(false);
|
|
|
|
});
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2021-12-02 13:15:53 +01:00
|
|
|
$twofaccounts = TwoFAccount::factory()->count(3)->make();
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2022-07-30 11:25:19 +02:00
|
|
|
Groups::shouldReceive('getAccounts')
|
2021-11-30 17:39:33 +01:00
|
|
|
->with($group)
|
|
|
|
->once()
|
|
|
|
->andReturn($twofaccounts);
|
|
|
|
|
|
|
|
$response = $this->controller->accounts($group);
|
|
|
|
// TwoFAccountCollection
|
|
|
|
$this->assertContainsOnlyInstancesOf('App\Api\v1\Resources\TwoFAccountReadResource', $response->collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_destroy_uses_group_service()
|
|
|
|
{
|
2021-12-02 13:15:53 +01:00
|
|
|
$group = Group::factory()->make();
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2022-07-30 11:25:19 +02:00
|
|
|
Groups::shouldReceive('delete')
|
2021-11-30 17:39:33 +01:00
|
|
|
->once()
|
|
|
|
->with($group->id);
|
|
|
|
|
|
|
|
$response = $this->controller->destroy($group);
|
|
|
|
|
|
|
|
$this->assertInstanceOf('Illuminate\Http\JsonResponse', $response);
|
|
|
|
}
|
|
|
|
}
|