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;
|
2023-08-01 11:28:27 +02:00
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
2024-06-26 14:29:13 +02:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-11-22 15:15:52 +01:00
|
|
|
use Tests\TestCase;
|
2021-11-30 17:39:33 +01:00
|
|
|
|
|
|
|
/**
|
2023-08-01 11:28:27 +02:00
|
|
|
* GroupControllerTest test class
|
2021-11-30 17:39:33 +01:00
|
|
|
*/
|
2023-08-01 11:28:27 +02:00
|
|
|
#[CoversClass(GroupController::class)]
|
2021-11-30 17:39:33 +01:00
|
|
|
class GroupControllerTest extends TestCase
|
|
|
|
{
|
|
|
|
use WithoutMiddleware;
|
|
|
|
|
|
|
|
/**
|
2023-03-07 15:17:07 +01:00
|
|
|
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
|
2023-03-02 14:32:53 +01:00
|
|
|
*/
|
|
|
|
protected $user;
|
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
public function setUp() : void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2024-09-26 23:50:01 +02:00
|
|
|
$this->user = new User;
|
2023-03-10 22:59:46 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
// We do not use $this->actingAs($this->user) to prevent intelephense
|
|
|
|
// static analysis error. Dumb, but I don't like errors...
|
|
|
|
$this->app['auth']->guard(null)->setUser($this->user);
|
|
|
|
$this->app['auth']->shouldUse(null);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2023-03-07 15:17:07 +01:00
|
|
|
public function test_index_returns_api_resources()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-03-07 15:17:07 +01:00
|
|
|
$user = Mockery::mock(User::class);
|
|
|
|
$request = Mockery::mock(Request::class);
|
|
|
|
$groups = Group::factory()->count(3)->make();
|
2024-09-26 23:50:01 +02:00
|
|
|
$controller = new GroupController;
|
2023-03-10 22:59:46 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
$user->shouldReceive('groups->withCount->get')
|
2023-03-02 14:32:53 +01:00
|
|
|
->once()
|
2023-03-07 15:17:07 +01:00
|
|
|
->andReturn($groups);
|
|
|
|
|
|
|
|
$request->shouldReceive('user')
|
|
|
|
->andReturn($user);
|
|
|
|
|
|
|
|
Groups::shouldReceive('prependTheAllGroup')
|
2021-11-30 17:39:33 +01:00
|
|
|
->once()
|
|
|
|
->andReturn($groups);
|
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
$response = $controller->index($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
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2023-03-07 15:17:07 +01:00
|
|
|
public function test_store_uses_validated_data_and_returns_api_resource()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-03-07 15:17:07 +01:00
|
|
|
$request = Mockery::mock(GroupStoreRequest::class);
|
2024-09-26 23:50:01 +02:00
|
|
|
$controller = new GroupController;
|
2023-03-07 15:17:07 +01:00
|
|
|
$group = Group::factory()->for($this->user)->make();
|
|
|
|
$validated = ['name' => $group->name];
|
|
|
|
|
|
|
|
$request->shouldReceive([
|
|
|
|
'validated' => $validated,
|
|
|
|
'user->groups->create' => $group,
|
2023-03-02 14:32:53 +01:00
|
|
|
]);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
$response = $controller->store($request);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2023-03-07 15:17:07 +01:00
|
|
|
public function test_show_returns_api_resource()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-03-07 15:17:07 +01:00
|
|
|
$controller = Mockery::mock(GroupController::class)->makePartial();
|
2023-03-10 22:59:46 +01:00
|
|
|
$group = Group::factory()->make();
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
$response = $controller->show($group);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->assertInstanceOf(GroupResource::class, $response);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2023-03-07 15:17:07 +01:00
|
|
|
public function test_update_validates_data_and_returns_api_resource()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-03-07 15:17:07 +01:00
|
|
|
$request = Mockery::mock(GroupStoreRequest::class);
|
|
|
|
$controller = Mockery::mock(GroupController::class)->makePartial();
|
|
|
|
$group = Group::factory()->make();
|
|
|
|
$validated = ['name' => $group->name];
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
$request->shouldReceive([
|
|
|
|
'validated' => $validated,
|
2023-03-02 14:32:53 +01:00
|
|
|
]);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
$response = $controller->update($request, $group);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->assertInstanceOf(GroupResource::class, $response);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2021-11-30 17:39:33 +01:00
|
|
|
public function test_assignAccounts_returns_api_resource_assigned_using_groupService()
|
|
|
|
{
|
2023-03-07 15:17:07 +01:00
|
|
|
$request = Mockery::mock(GroupAssignRequest::class);
|
|
|
|
$controller = Mockery::mock(GroupController::class)->makePartial();
|
|
|
|
$group = Group::factory()->make();
|
|
|
|
$validated = ['ids' => $group->id];
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
$request->shouldReceive([
|
2023-03-02 14:32:53 +01:00
|
|
|
'validated' => $validated,
|
2023-03-07 15:17:07 +01:00
|
|
|
'user' => $this->user,
|
2023-03-02 14:32:53 +01:00
|
|
|
]);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
Groups::shouldReceive('assign')
|
|
|
|
->with($group->id, $this->user, $group)
|
2021-11-30 17:39:33 +01:00
|
|
|
->once();
|
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
$response = $controller->assignAccounts($request, $group);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->assertInstanceOf(GroupResource::class, $response);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2023-03-07 15:17:07 +01:00
|
|
|
public function test_accounts_returns_api_resources()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-03-07 15:17:07 +01:00
|
|
|
$controller = Mockery::mock(GroupController::class)->makePartial();
|
|
|
|
$group = Group::factory()->make();
|
2023-03-02 14:32:53 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
$response = $controller->accounts($group);
|
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
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2021-11-30 17:39:33 +01:00
|
|
|
public function test_destroy_uses_group_service()
|
|
|
|
{
|
2023-03-07 15:17:07 +01:00
|
|
|
$controller = Mockery::mock(GroupController::class)->makePartial();
|
|
|
|
$group = Group::factory()->make();
|
|
|
|
$group->id = 0;
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-03-07 15:17:07 +01:00
|
|
|
$response = $controller->destroy($group);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
|
|
|
$this->assertInstanceOf('Illuminate\Http\JsonResponse', $response);
|
2023-03-07 15:17:07 +01:00
|
|
|
$this->assertEquals(204, $response->status());
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|