-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
317 additions
and
35 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/Application/VisaModel/Command/DeleteVisaModelCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Application\VisaModel\Command; | ||
|
||
use App\Application\CommandInterface; | ||
|
||
final class DeleteVisaModelCommand implements CommandInterface | ||
{ | ||
public function __construct( | ||
public readonly string $uuid, | ||
) { | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Application/VisaModel/Command/DeleteVisaModelCommandHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Application\VisaModel\Command; | ||
|
||
use App\Domain\VisaModel\Exception\VisaModelNotFoundException; | ||
use App\Domain\VisaModel\Repository\VisaModelRepositoryInterface; | ||
use App\Domain\VisaModel\VisaModel; | ||
|
||
final class DeleteVisaModelCommandHandler | ||
{ | ||
public function __construct( | ||
private VisaModelRepositoryInterface $visaModelRepository, | ||
) { | ||
} | ||
|
||
public function __invoke(DeleteVisaModelCommand $command): void | ||
{ | ||
$visaModel = $this->visaModelRepository->findOneByUuid($command->uuid); | ||
if (!$visaModel instanceof VisaModel) { | ||
throw new VisaModelNotFoundException(); | ||
} | ||
|
||
$this->visaModelRepository->remove($visaModel); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/Infrastructure/Controller/MyArea/Organization/VisaModel/DeleteVisaModelController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Infrastructure\Controller\MyArea\Organization\VisaModel; | ||
|
||
use App\Application\CommandBusInterface; | ||
use App\Application\QueryBusInterface; | ||
use App\Application\VisaModel\Command\DeleteVisaModelCommand; | ||
use App\Domain\VisaModel\Exception\VisaModelNotFoundException; | ||
use App\Infrastructure\Controller\MyArea\Organization\AbstractOrganizationController; | ||
use App\Infrastructure\Security\Voter\OrganizationVoter; | ||
use Symfony\Bundle\SecurityBundle\Security; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
use Symfony\Component\Routing\Requirement\Requirement; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Symfony\Component\Security\Http\Attribute\IsCsrfTokenValid; | ||
|
||
final class DeleteVisaModelController extends AbstractOrganizationController | ||
{ | ||
public function __construct( | ||
private RouterInterface $router, | ||
private CommandBusInterface $commandBus, | ||
QueryBusInterface $queryBus, | ||
Security $security, | ||
) { | ||
parent::__construct($queryBus, $security); | ||
} | ||
|
||
#[Route( | ||
'/organizations/{organizationUuid}/visa_models/{uuid}', | ||
name: 'app_config_visa_models_delete', | ||
requirements: ['organizationUuid' => Requirement::UUID, 'uuid' => Requirement::UUID], | ||
methods: ['DELETE'], | ||
)] | ||
#[IsCsrfTokenValid('delete-visa-model')] | ||
public function __invoke(string $organizationUuid, string $uuid): RedirectResponse | ||
{ | ||
$organization = $this->getOrganization($organizationUuid); | ||
|
||
if (!$this->security->isGranted(OrganizationVoter::EDIT, $organization)) { | ||
throw new AccessDeniedHttpException(); | ||
} | ||
|
||
try { | ||
$this->commandBus->handle(new DeleteVisaModelCommand($uuid)); | ||
} catch (VisaModelNotFoundException) { | ||
throw new NotFoundHttpException(); | ||
} | ||
|
||
return new RedirectResponse( | ||
url: $this->router->generate('app_config_visa_models_list', ['uuid' => $organizationUuid]), | ||
status: Response::HTTP_SEE_OTHER, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...Infrastructure/Controller/MyArea/Organization/VisaModel/DeleteVisaModelControllerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Integration\Infrastructure\Controller\MyArea\Organization\VisaModel; | ||
|
||
use App\Infrastructure\Persistence\Doctrine\Fixtures\OrganizationFixture; | ||
use App\Tests\Integration\Infrastructure\Controller\AbstractWebTestCase; | ||
use App\Tests\SessionHelper; | ||
|
||
final class DeleteVisaModelControllerTest extends AbstractWebTestCase | ||
{ | ||
use SessionHelper; | ||
|
||
public function testDelete(): void | ||
{ | ||
$client = $this->login('[email protected]'); | ||
$client->request('DELETE', '/mon-espace/organizations/' . OrganizationFixture::MAIN_ORG_ID . '/visa_models/7eca6579-c07e-4e8e-8f10-fda610d7ee73', [ | ||
'_token' => $this->generateCsrfToken($client, 'delete-visa-model'), | ||
]); | ||
|
||
$this->assertResponseStatusCodeSame(303); | ||
$client->followRedirect(); | ||
|
||
$this->assertResponseStatusCodeSame(200); | ||
$this->assertRouteSame('app_config_visa_models_list'); | ||
} | ||
|
||
public function testNotFound(): void | ||
{ | ||
$client = $this->login('[email protected]'); | ||
$client->request('DELETE', '/mon-espace/organizations/' . OrganizationFixture::MAIN_ORG_ID . '/visa_models/e18d61be-1797-4d6b-aa58-cd75e623a821', [ | ||
'_token' => $this->generateCsrfToken($client, 'delete-visa-model'), | ||
]); | ||
|
||
$this->assertResponseStatusCodeSame(404); | ||
} | ||
|
||
public function testOrganizationNotOwned(): void | ||
{ | ||
$client = $this->login(); | ||
$client->request('DELETE', '/mon-espace/organizations/' . OrganizationFixture::MAIN_ORG_ID . '/visa_models/7eca6579-c07e-4e8e-8f10-fda610d7ee73', [ | ||
'_token' => $this->generateCsrfToken($client, 'delete-visa-model'), | ||
]); | ||
|
||
$this->assertResponseStatusCodeSame(403); | ||
} | ||
|
||
public function testBadAccessToken(): void | ||
{ | ||
$client = $this->login('[email protected]'); | ||
$client->request('DELETE', '/mon-espace/organizations/' . OrganizationFixture::MAIN_ORG_ID . '/visa_models/7eca6579-c07e-4e8e-8f10-fda610d7ee73', [ | ||
'_token' => 'abc', | ||
]); | ||
|
||
$this->assertResponseRedirects('http://localhost/login', 302); | ||
} | ||
|
||
public function testOrganizationOrUserNotFound(): void | ||
{ | ||
$client = $this->login(); | ||
$client->request('DELETE', '/mon-espace/organizations/f5c1cea8-a61d-43a7-9b5d-4b8c9557c673/visa_models/7eca6579-c07e-4e8e-8f10-fda610d7ee73', [ | ||
'_token' => $this->generateCsrfToken($client, 'delete-visa-model'), | ||
]); | ||
$this->assertResponseStatusCodeSame(404); | ||
} | ||
|
||
public function testWithoutAuthenticatedUser(): void | ||
{ | ||
$client = static::createClient(); | ||
$client->request('DELETE', '/mon-espace/organizations/' . OrganizationFixture::MAIN_ORG_ID . '/visa_models/7eca6579-c07e-4e8e-8f10-fda610d7ee73'); | ||
$this->assertResponseRedirects('http://localhost/login', 302); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
tests/Unit/Application/VisaModel/Command/DeleteVisaModelCommandHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Unit\Application\VisaModel\Command; | ||
|
||
use App\Application\VisaModel\Command\DeleteVisaModelCommand; | ||
use App\Application\VisaModel\Command\DeleteVisaModelCommandHandler; | ||
use App\Domain\VisaModel\Exception\VisaModelNotFoundException; | ||
use App\Domain\VisaModel\Repository\VisaModelRepositoryInterface; | ||
use App\Domain\VisaModel\VisaModel; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class DeleteVisaModelCommandHandlerTest extends TestCase | ||
{ | ||
private MockObject $visaModelRepository; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->visaModelRepository = $this->createMock(VisaModelRepositoryInterface::class); | ||
} | ||
|
||
public function testRemove(): void | ||
{ | ||
$visaModel = $this->createMock(VisaModel::class); | ||
|
||
$this->visaModelRepository | ||
->expects(self::once()) | ||
->method('findOneByUuid') | ||
->with('f8216679-5a0b-4dd5-9e2b-b382d298c3b4') | ||
->willReturn($visaModel); | ||
|
||
$this->visaModelRepository | ||
->expects(self::once()) | ||
->method('remove') | ||
->with($visaModel); | ||
|
||
$handler = new DeleteVisaModelCommandHandler( | ||
$this->visaModelRepository, | ||
); | ||
$command = new DeleteVisaModelCommand('f8216679-5a0b-4dd5-9e2b-b382d298c3b4'); | ||
|
||
$handler($command); | ||
} | ||
|
||
public function testNotFound(): void | ||
{ | ||
$this->expectException(VisaModelNotFoundException::class); | ||
|
||
$this->visaModelRepository | ||
->expects(self::once()) | ||
->method('findOneByUuid') | ||
->with('f8216679-5a0b-4dd5-9e2b-b382d298c3b4') | ||
->willReturn(null); | ||
|
||
$this->visaModelRepository | ||
->expects(self::never()) | ||
->method('remove'); | ||
|
||
$handler = new DeleteVisaModelCommandHandler( | ||
$this->visaModelRepository, | ||
); | ||
$command = new DeleteVisaModelCommand('f8216679-5a0b-4dd5-9e2b-b382d298c3b4'); | ||
|
||
$handler($command); | ||
} | ||
} |
Oops, something went wrong.