Commit f8bf96a1 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Return 404 on not found group when requesting delete

parent 778cfdc0
......@@ -2,8 +2,10 @@
namespace Source\UseCases\Groups\DeleteGroup;
use Source\Authorization\Permissions;
use Source\Exceptions\DeleteFailedException;
use Source\Gateways\Groups\GroupsRepository;
use Source\Exceptions\EntityNotFoundException;
class DeleteGroup implements DeleteGroupUseCase
{
......@@ -22,6 +24,20 @@ class DeleteGroup implements DeleteGroupUseCase
*/
public function delete(string $groupId, Presenter $presenter): void
{
$group = $this->groups->get($groupId);
if (!$group) {
throw new EntityNotFoundException();
}
$reflection = new \ReflectionClass(Permissions::class);
foreach ($reflection->getConstants() as $constant) {
if ($group->getTitle() === $constant) {
throw new DeleteFailedException('Cannot delete a default permission group');
}
}
if (!$this->groups->delete($groupId)) {
throw new DeleteFailedException('Unable to delete group.');
}
......
......@@ -9,6 +9,7 @@ interface DeleteGroupUseCase
* @param string $groupId
* @param \Source\UseCases\Groups\DeleteGroup\Presenter $presenter
* @throws \Source\Exceptions\DeleteFailedException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function delete(string $groupId, Presenter $presenter): void;
}
......@@ -8,7 +8,7 @@ use Source\Gateways\Groups\GroupsRepository;
class InMemoryGroupsRepositoryStub implements GroupsRepository
{
protected ?Group $group;
protected ?Group $group = null;
public function setGroupToReturnOnGet(Group $group): void
{
......
......@@ -31,7 +31,7 @@ class DeleteGroupApiTest extends AuthenticatesWithApplicationTestCase
protected function stubGroupsRepository(): void
{
$this->app->bind(GroupsRepository::class, static function () {
$this->app->singleton(GroupsRepository::class, static function () {
return new InMemoryGroupsRepositoryStub();
});
}
......@@ -75,14 +75,7 @@ class DeleteGroupApiTest extends AuthenticatesWithApplicationTestCase
$this->authenticate();
$this->handleTest('asdfasfd non existent group');
$this->response->assertStatus(200);
$this->response->assertJson(
[
'message' => 'Group deleted.',
'status' => 'success',
'code' => 200,
]
);
$this->response->assertStatus(404);
}
/**
......@@ -111,12 +104,16 @@ class DeleteGroupApiTest extends AuthenticatesWithApplicationTestCase
/**
* @test
* @throws EntityNotFoundException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function it_handles_a_failed_delete(): void
{
$this->authenticate();
$this->stubGroupsRepository();
/** @var InMemoryGroupsRepositoryStub $groups */
$groups = $this->app->make(GroupsRepository::class);
$groups->setGroupToReturnOnGet(new Group(1, '', ''));
$this->handleTest('asdf');
$this->response->assertStatus(200);
......
......@@ -6,6 +6,7 @@ namespace Tests\Unit\Source\UseCases\Groups\DeleteGroup;
use Source\Entities\Group;
use PHPUnit\Framework\TestCase;
use Source\Exceptions\DeleteFailedException;
use Source\Exceptions\EntityNotFoundException;
use Source\UseCases\Groups\DeleteGroup\DeleteGroup;
use Source\Gateways\Groups\InMemoryGroupsRepository;
use Source\UseCases\Groups\DeleteGroup\ResponseModel;
......@@ -32,6 +33,7 @@ class UseCaseTest extends TestCase
/**
* @param string $groupId
* @throws DeleteFailedException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function handleTest(string $groupId): void
{
......@@ -43,17 +45,31 @@ class UseCaseTest extends TestCase
/**
* @test
* @throws DeleteFailedException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function presenter_was_called(): void
{
$this->groupsRepository->create(new Group(123, '', ''));
$this->handleTest('123');
$this->assertTrue($this->presenter->wasPresenterCalled());
}
/**
* @test
* @throws \Source\Exceptions\DeleteFailedException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_throws_404_on_non_existient_group(): void
{
$this->expectException(EntityNotFoundException::class);
$this->handleTest('asdf');
}
/**
* @test
* @throws DeleteFailedException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_deletes_a_group(): void
{
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment