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

176 lines
4.8 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\Models\User|\Illuminate\Contracts\Auth\Authenticatable
*/
protected $user;
/**
*
*/
2021-11-30 17:39:33 +01:00
public function setUp() : void
{
parent::setUp();
$this->user = new User();
// 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
}
/**
* @test
*/
public function test_index_returns_api_resources()
2021-11-30 17:39:33 +01:00
{
$user = Mockery::mock(User::class);
$request = Mockery::mock(Request::class);
$groups = Group::factory()->count(3)->make();
$controller = new GroupController();
$user->shouldReceive('groups->withCount->get')
->once()
->andReturn($groups);
$request->shouldReceive('user')
->andReturn($user);
Groups::shouldReceive('prependTheAllGroup')
2021-11-30 17:39:33 +01:00
->once()
->andReturn($groups);
$response = $controller->index($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_uses_validated_data_and_returns_api_resource()
2021-11-30 17:39:33 +01:00
{
$request = Mockery::mock(GroupStoreRequest::class);
$controller = new GroupController();
$group = Group::factory()->for($this->user)->make();
$validated = ['name' => $group->name];
$request->shouldReceive([
'validated' => $validated,
'user->groups->create' => $group,
]);
2021-11-30 17:39:33 +01:00
$response = $controller->store($request);
2021-11-30 17:39:33 +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-11-30 17:39:33 +01:00
{
$controller = Mockery::mock(GroupController::class)->makePartial();
2021-12-02 13:15:53 +01:00
$group = Group::factory()->make();
2021-11-30 17:39:33 +01:00
$response = $controller->show($group);
2021-11-30 17:39:33 +01:00
$this->assertInstanceOf(GroupResource::class, $response);
2021-11-30 17:39:33 +01:00
}
/**
* @test
*/
public function test_update_validates_data_and_returns_api_resource()
2021-11-30 17:39:33 +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
$request->shouldReceive([
'validated' => $validated,
]);
2021-11-30 17:39:33 +01:00
$response = $controller->update($request, $group);
2021-11-30 17:39:33 +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()
{
$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
$request->shouldReceive([
'validated' => $validated,
'user' => $this->user,
]);
2021-11-30 17:39:33 +01:00
Groups::shouldReceive('assign')
->with($group->id, $this->user, $group)
2021-11-30 17:39:33 +01:00
->once();
$response = $controller->assignAccounts($request, $group);
2021-11-30 17:39:33 +01:00
$this->assertInstanceOf(GroupResource::class, $response);
2021-11-30 17:39:33 +01:00
}
/**
* @test
*/
public function test_accounts_returns_api_resources()
2021-11-30 17:39:33 +01:00
{
$controller = Mockery::mock(GroupController::class)->makePartial();
$group = Group::factory()->make();
$response = $controller->accounts($group);
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()
{
$controller = Mockery::mock(GroupController::class)->makePartial();
$group = Group::factory()->make();
$group->id = 0;
2021-11-30 17:39:33 +01:00
$response = $controller->destroy($group);
2021-11-30 17:39:33 +01:00
$this->assertInstanceOf('Illuminate\Http\JsonResponse', $response);
$this->assertEquals(204, $response->status());
2021-11-30 17:39:33 +01:00
}
2022-11-22 15:15:52 +01:00
}