2FAuth/tests/Unit/Api/v1/Controllers/GroupControllerTest.php

180 lines
4.6 KiB
PHP
Raw Normal View History

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;
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;
use App\Models\User;
2021-11-30 17:39:33 +01:00
use Illuminate\Foundation\Testing\WithoutMiddleware;
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;
/**
* @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;
/**
* @var \Illuminate\Http\Request mocked request
*/
protected $request;
2021-11-30 17:39:33 +01:00
public function setUp() : void
{
parent::setUp();
$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
$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
Groups::shouldReceive('getAll')
2021-11-30 17:39:33 +01:00
->once()
->andReturn($groups);
$response = $this->controller->index($this->request);
2021-11-30 17:39:33 +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
$this->groupStoreRequest->shouldReceive([
'validated' => ['name' => $group->name],
'user' => new User(),
])
->once();
2021-11-30 17:39:33 +01:00
Groups::shouldReceive('create')
2021-11-30 17:39:33 +01:00
->once()
->andReturn($group);
$response = $this->controller->store($this->groupStoreRequest);
$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);
$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
$this->groupStoreRequest->shouldReceive([
'validated' => ['name' => $group->name],
'user' => new User(),
])
->once();
2021-11-30 17:39:33 +01:00
Groups::shouldReceive('update')
2021-11-30 17:39:33 +01:00
->once()
->andReturn($group);
$response = $this->controller->update($this->groupStoreRequest, $group);
$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();
$groupAssignRequest = Mockery::mock(GroupAssignRequest::class);
$user = new User();
2021-11-30 17:39:33 +01:00
$groupAssignRequest->shouldReceive([
'validated' => ['ids' => $group->id],
'user' => $user,
])
->once();
2021-11-30 17:39:33 +01:00
Groups::shouldReceive('assign')
->with($group->id, $user, $group)
2021-11-30 17:39:33 +01:00
->once();
$response = $this->controller->assignAccounts($groupAssignRequest, $group);
$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
$response = $this->controller->accounts($group, $this->request);
2021-11-30 17:39:33 +01:00
$this->assertContainsOnlyInstancesOf(TwoFAccountReadResource::class, $response->collection);
2021-11-30 17:39:33 +01:00
}
/**
* @test
*/
public function test_destroy_uses_group_service()
{
$group = Group::factory()->make();
$group->id = 0;
2021-11-30 17:39:33 +01:00
Groups::shouldReceive('delete')
2021-11-30 17:39:33 +01:00
->once()
->with($group->id, $this->request->user())
->andReturn(0);
2021-11-30 17:39:33 +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
}