Skip to content

Commit

Permalink
patch tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adr-mo committed May 22, 2024
1 parent 64f4d09 commit 345212c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,16 @@

namespace Tests\Core\Category\RealTime\Application\UseCase\FindHostCategory;

use Symfony\Component\HttpFoundation\Response;
use Core\Application\Common\UseCase\AbstractPresenter;
use Core\Application\Common\UseCase\ResponseStatusInterface;
use Core\Category\RealTime\Application\UseCase\FindHostCategory\FindHostCategoryResponse;
use Core\Category\RealTime\Application\UseCase\FindHostCategory\FindHostCategoryPresenterInterface;

class FindHostCategoryPresenterStub extends AbstractPresenter implements FindHostCategoryPresenterInterface
{
/**
* @var FindHostCategoryResponse
*/
public $response;
public FindHostCategoryResponse|ResponseStatusInterface $response;

public function __construct()
{
}

/**
* @return Response
*/
public function show(): Response
{
return new Response();
}

/**
* @param FindHostCategoryResponse $response
*/
public function present(mixed $response): void
public function presentResponse(FindHostCategoryResponse|ResponseStatusInterface $response): void
{
$this->response = $response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,80 +18,133 @@
* For more information : contact@centreon.com
*
*/

declare(strict_types=1);

namespace Tests\Core\Category\RealTime\Application\UseCase\FindHostCategory;

use Centreon\Domain\Contact\Interfaces\ContactInterface;
use Core\Application\Common\UseCase\ErrorResponse;
use Centreon\Domain\RequestParameters\Interfaces\RequestParametersInterface;
use Core\Application\Common\UseCase\ForbiddenResponse;
use Core\Category\RealTime\Application\Exception\HostCategoryException;
use Core\Category\RealTime\Application\Repository\ReadHostCategoryRepositoryInterface as RealTimeRepositoryInterface;
use Core\Category\RealTime\Application\UseCase\FindHostCategory\FindHostCategory;
use Core\HostCategory\Application\Repository\ReadHostCategoryRepositoryInterface as ConfigurationRepositoryInterface;
use Core\Infrastructure\Common\Presenter\PresenterFormatterInterface;
use Core\Security\AccessGroup\Application\Repository\ReadAccessGroupRepositoryInterface;
use Core\Tag\RealTime\Application\Repository\ReadTagRepositoryInterface;
use Core\Security\AccessGroup\Domain\Model\AccessGroup;
use Core\Tag\RealTime\Domain\Model\Tag;
use Tests\Core\Category\RealTime\Application\UseCase\FindHostCategory\FindHostCategoryPresenterStub;

beforeEach(function () {
$this->presenter = new FindHostCategoryPresenterStub();

beforeEach(function (): void {
$this->useCase = new FindHostCategory(
$this->repository = $this->createMock(RealTimeRepositoryInterface::class),
$this->configurationRepository = $this->createMock(ConfigurationRepositoryInterface::class),
$this->user = $this->createMock(ContactInterface::class),
$this->accessGroupRepository = $this->createMock(ReadAccessGroupRepositoryInterface::class),
$this->requestParameters = $this->createMock(RequestParametersInterface::class)
);
$this->presenter = new FindHostCategoryPresenterStub($this->createMock(PresenterFormatterInterface::class));
$this->accessGroup = new AccessGroup(1, 'AG-name', 'AG-alias');
$this->category = new Tag(1, 'host-category-name', Tag::HOST_CATEGORY_TYPE_ID);
$this->repository = $this->createMock(ReadTagRepositoryInterface::class);
$this->user = $this->createMock(ContactInterface::class);
$this->readAccessGroupRepository = $this->createMock(ReadAccessGroupRepositoryInterface::class);
$this->tagRepository = $this->createMock(ReadTagRepositoryInterface::class);
});

it('should find all categories as admin', function () {
it('should present a Forbidden response when user does not have sufficient rights (missing page access)', function (): void {
$this->user
->expects($this->any())
->method('hasTopologyRole')
->willReturn(false);

($this->useCase)($this->presenter);

expect($this->presenter->response)
->toBeInstanceOf(ForbiddenResponse::class)
->and($this->presenter->response->getMessage())
->toBe(HostCategoryException::listingNotAllowed()->getMessage());
});

it('should find all categories as admin', function (): void {
$this->user
->expects($this->any())
->method('hasTopologyRole')
->willReturn(true);

$this->user
->expects($this->once())
->method('isAdmin')
->willReturn(true);
$this->tagRepository->expects($this->once())
->method('findAllByTypeId')

$this->repository->expects($this->once())
->method('findAll')
->willReturn([$this->category]);

$useCase = new FindHostCategory($this->tagRepository, $this->user, $this->readAccessGroupRepository);
$useCase($this->presenter);
($this->useCase)($this->presenter);

expect($this->presenter->response->tags)->toHaveCount(1);
expect($this->presenter->response->tags[0]['id'])->toBe($this->category->getId());
expect($this->presenter->response->tags[0]['name'])->toBe($this->category->getName());
});

it('should find all categories as non-admin', function () {
it('should find all categories as user with no filters on categories', function (): void {
$this->user
->expects($this->any())
->method('hasTopologyRole')
->willReturn(true);

$this->user
->expects($this->once())
->method('isAdmin')
->willReturn(false);
$this->readAccessGroupRepository

$this->accessGroupRepository
->expects($this->once())
->method('findByContact')
->willReturn([]);
$this->tagRepository->expects($this->once())
->method('findAllByTypeIdAndAccessGroups')
->willReturn([$this->accessGroup]);

$this->configurationRepository
->expects($this->once())
->method('hasAclFilterOnHostCategories')
->willReturn(false);

$this->repository->expects($this->once())
->method('findAll')
->willReturn([$this->category]);

$useCase = new FindHostCategory($this->tagRepository, $this->user, $this->readAccessGroupRepository);
$useCase($this->presenter);
($this->useCase)($this->presenter);

expect($this->presenter->response->tags)->toHaveCount(1);
expect($this->presenter->response->tags[0]['id'])->toBe($this->category->getId());
expect($this->presenter->response->tags[0]['name'])->toBe($this->category->getName());
});

it('should present an ErrorResponse on repository error', function () {
$this->user
it('should find all categories as user with filters on categories', function (): void {
$this->user
->expects($this->any())
->method('hasTopologyRole')
->willReturn(true);

$this->user
->expects($this->once())
->method('isAdmin')
->willReturn(false);

$this->accessGroupRepository
->expects($this->once())
->method('findByContact')
->willReturn([$this->accessGroup]);

$this->configurationRepository
->expects($this->once())
->method('hasAclFilterOnHostCategories')
->willReturn(true);
$this->tagRepository->expects($this->once())
->method('findAllByTypeId')
->willThrowException(new \Exception());

$useCase = new FindHostCategory($this->tagRepository, $this->user, $this->readAccessGroupRepository);
$useCase($this->presenter);
$this->repository->expects($this->once())
->method('findAllByAccessGroupIds')
->willReturn([$this->category]);

expect($this->presenter->getResponseStatus())->toBeInstanceOf(ErrorResponse::class);
expect($this->presenter->getResponseStatus()?->getMessage())->toBe(
'An error occurred while retrieving host categories'
);
($this->useCase)($this->presenter);

expect($this->presenter->response->tags)->toHaveCount(1);
expect($this->presenter->response->tags[0]['id'])->toBe($this->category->getId());
expect($this->presenter->response->tags[0]['name'])->toBe($this->category->getName());
});

0 comments on commit 345212c

Please sign in to comment.