From 7e9342b63cd47ae51a4c23c6d8272cd682aac1bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Tue, 6 Aug 2024 19:19:56 +0200 Subject: [PATCH] test(IT): Test account delete MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- .../Account/SaveDeleteController.php | 37 ++++---- lib/SP/Mvc/Controller/ItemTrait.php | 4 + .../Account/SaveDeleteControllerTest.php | 86 +++++++++++++++++++ 3 files changed, 107 insertions(+), 20 deletions(-) create mode 100644 tests/SP/Modules/Web/Controllers/Account/SaveDeleteControllerTest.php diff --git a/app/modules/web/Controllers/Account/SaveDeleteController.php b/app/modules/web/Controllers/Account/SaveDeleteController.php index c1e8b6fb..12b8c51a 100644 --- a/app/modules/web/Controllers/Account/SaveDeleteController.php +++ b/app/modules/web/Controllers/Account/SaveDeleteController.php @@ -32,12 +32,16 @@ use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Domain\Account\Ports\AccountService; use SP\Domain\Core\Acl\AclActionsInterface; +use SP\Domain\Core\Exceptions\SPException; use SP\Domain\CustomField\Ports\CustomFieldDataService; use SP\Domain\Http\Dtos\JsonMessage; use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Mvc\Controller\ItemTrait; use SP\Mvc\Controller\WebControllerHelper; +use function SP\__u; +use function SP\processException; + /** * Class SaveDeleteController */ @@ -46,46 +50,39 @@ final class SaveDeleteController extends AccountControllerBase use ItemTrait; use JsonTrait; - private AccountService $accountService; - private CustomFieldDataService $customFieldService; - public function __construct( - Application $application, - WebControllerHelper $webControllerHelper, - AccountService $accountService, - CustomFieldDataService $customFieldService + Application $application, + WebControllerHelper $webControllerHelper, + private readonly AccountService $accountService, + private readonly CustomFieldDataService $customFieldService ) { - parent::__construct( - $application, - $webControllerHelper - ); - - $this->accountService = $accountService; - $this->customFieldService = $customFieldService; + parent::__construct($application, $webControllerHelper); } /** * Saves delete action * - * @param int $id Account's ID + * @param int $id Account's ID * * @return bool * @throws JsonException + * @throws SPException */ public function saveDeleteAction(int $id): bool { try { - $accountDetails = $this->accountService->getByIdEnriched($id)->getAccountVData(); + $accountDetails = $this->accountService->getByIdEnriched($id); $this->accountService->delete($id); $this->eventDispatcher->notify( 'delete.account', new Event( - $this, EventMessage::factory() - ->addDescription(__u('Account removed')) - ->addDetail(__u('Account'), $accountDetails->getName()) - ->addDetail(__u('Client'), $accountDetails->getClientName()) + $this, + EventMessage::factory() + ->addDescription(__u('Account removed')) + ->addDetail(__u('Account'), $accountDetails->getName()) + ->addDetail(__u('Client'), $accountDetails->getClientName()) ) ); diff --git a/lib/SP/Mvc/Controller/ItemTrait.php b/lib/SP/Mvc/Controller/ItemTrait.php index e1ee340b..d1ddda80 100644 --- a/lib/SP/Mvc/Controller/ItemTrait.php +++ b/lib/SP/Mvc/Controller/ItemTrait.php @@ -169,6 +169,10 @@ trait ItemTrait array|int $itemId, CustomFieldDataService $customFieldService ): void { + if (!is_array($itemId)) { + $itemId = [$itemId]; + } + $customFieldService->delete($itemId, $moduleId); } diff --git a/tests/SP/Modules/Web/Controllers/Account/SaveDeleteControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/SaveDeleteControllerTest.php new file mode 100644 index 00000000..d3c7317b --- /dev/null +++ b/tests/SP/Modules/Web/Controllers/Account/SaveDeleteControllerTest.php @@ -0,0 +1,86 @@ +. + */ + +declare(strict_types=1); + +namespace SP\Tests\Modules\Web\Controllers\Account; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\MockObject\Exception; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; +use ReflectionClass; +use SP\Domain\Config\Ports\ConfigService; +use SP\Domain\Core\Exceptions\InvalidClassException; +use SP\Infrastructure\Database\QueryData; +use SP\Infrastructure\Database\QueryResult; +use SP\Infrastructure\File\FileException; +use SP\Tests\IntegrationTestCase; + +/** + * Class SaveDeleteControllerTest + */ +#[Group('integration')] +class SaveDeleteControllerTest extends IntegrationTestCase +{ + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + * @throws InvalidClassException + * @throws FileException + */ + public function testSaveDeleteAction() + { + $configService = self::createStub(ConfigService::class); + $configService->method('getByParam')->willReturnArgument(0); + + $definitions = $this->getModuleDefinitions(); + $definitions[ConfigService::class] = $configService; + + $container = $this->buildContainer( + $definitions, + $this->buildRequest('post', 'index.php', ['r' => 'account/saveDelete/1']) + ); + + $this->runApp($container); + + $this->expectOutputString( + '{"status":0,"description":"Account removed","data":[],"messages":[]}' + ); + } + + protected function getDatabaseReturn(): callable + { + return function (QueryData $queryData): QueryResult { + if (!empty($queryData->getMapClassName())) { + $reflection = new ReflectionClass($queryData->getMapClassName()); + return new QueryResult([$reflection->newInstance()], 1, 100); + } + + return new QueryResult([], 1, 100); + }; + } +}