2021-11-30 17:39:33 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Api\v1\Controllers;
|
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Api\v1\Controllers\GroupController;
|
2023-02-27 00:32:49 +01:00
|
|
|
use App\Api\v1\Requests\GroupAssignRequest;
|
|
|
|
use App\Api\v1\Requests\GroupStoreRequest;
|
|
|
|
use App\Api\v1\Resources\GroupResource;
|
|
|
|
use App\Api\v1\Resources\TwoFAccountReadResource;
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Facades\Groups;
|
2021-12-02 13:15:53 +01:00
|
|
|
use App\Models\Group;
|
2023-02-27 00:32:49 +01:00
|
|
|
use App\Models\User;
|
2021-11-30 17:39:33 +01:00
|
|
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
2023-02-27 00:32:49 +01:00
|
|
|
use Illuminate\Http\Request;
|
2021-11-30 17:39:33 +01:00
|
|
|
use Mockery;
|
2022-11-22 15:15:52 +01:00
|
|
|
use Tests\TestCase;
|
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;
|
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Http\Request mocked request
|
|
|
|
*/
|
|
|
|
protected $request;
|
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
public function setUp() : void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->groupStoreRequest = Mockery::mock(GroupStoreRequest::class);
|
|
|
|
$this->request = Mockery::mock(Request::class);
|
|
|
|
|
|
|
|
$this->request->shouldReceive('user')
|
|
|
|
->andReturn(new User());
|
2021-11-30 17:39:33 +01:00
|
|
|
|
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);
|
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$response = $this->controller->index($this->request);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->assertContainsOnlyInstancesOf(GroupResource::class, $response->collection);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->groupStoreRequest->shouldReceive([
|
|
|
|
'validated' => ['name' => $group->name],
|
|
|
|
'user' => new User(),
|
|
|
|
])
|
|
|
|
->once();
|
2021-11-30 17:39:33 +01:00
|
|
|
|
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);
|
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->assertInstanceOf(Group::class, $response->original);
|
|
|
|
// $this->assertInstanceOf(GroupResource::class, $response);
|
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);
|
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->assertInstanceOf(GroupResource::class, $response);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->groupStoreRequest->shouldReceive([
|
|
|
|
'validated' => ['name' => $group->name],
|
|
|
|
'user' => new User(),
|
|
|
|
])
|
|
|
|
->once();
|
2021-11-30 17:39:33 +01:00
|
|
|
|
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);
|
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->assertInstanceOf(GroupResource::class, $response);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_assignAccounts_returns_api_resource_assigned_using_groupService()
|
|
|
|
{
|
2022-11-22 15:15:52 +01:00
|
|
|
$group = Group::factory()->make();
|
2023-02-27 00:32:49 +01:00
|
|
|
$groupAssignRequest = Mockery::mock(GroupAssignRequest::class);
|
|
|
|
$user = new User();
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$groupAssignRequest->shouldReceive([
|
|
|
|
'validated' => ['ids' => $group->id],
|
|
|
|
'user' => $user,
|
|
|
|
])
|
|
|
|
->once();
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2022-07-30 11:25:19 +02:00
|
|
|
Groups::shouldReceive('assign')
|
2023-02-27 00:32:49 +01:00
|
|
|
->with($group->id, $user, $group)
|
2021-11-30 17:39:33 +01:00
|
|
|
->once();
|
|
|
|
|
|
|
|
$response = $this->controller->assignAccounts($groupAssignRequest, $group);
|
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->assertInstanceOf(GroupResource::class, $response);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_accounts_returns_api_resources_fetched_using_groupService()
|
|
|
|
{
|
2021-12-02 13:15:53 +01:00
|
|
|
$group = Group::factory()->make();
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$response = $this->controller->accounts($group, $this->request);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->assertContainsOnlyInstancesOf(TwoFAccountReadResource::class, $response->collection);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_destroy_uses_group_service()
|
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$group = Group::factory()->make();
|
|
|
|
$group->id = 0;
|
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()
|
2023-02-27 00:32:49 +01:00
|
|
|
->with($group->id, $this->request->user())
|
|
|
|
->andReturn(0);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$response = $this->controller->destroy($group, $this->request);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
|
|
|
$this->assertInstanceOf('Illuminate\Http\JsonResponse', $response);
|
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|