Skip to content

Commit

Permalink
Move validation of value in TaskType
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 committed Dec 16, 2024
1 parent 74ed055 commit 646ddc6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
14 changes: 14 additions & 0 deletions classes/Task/TaskType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

namespace PrestaShop\Module\AutoUpgrade\Task;

use InvalidArgumentException;

class TaskType
{
const TASK_TYPE_BACKUP = 'backup';
Expand All @@ -38,4 +40,16 @@ class TaskType
self::TASK_TYPE_UPDATE,
self::TASK_TYPE_RESTORE,
];

/**
* @return self::TASK_TYPE_*
*/
public static function fromString(string $type)
{
if (!in_array($type, TaskType::ALL_TASKS)) {
throw new InvalidArgumentException('Unknown log type ' . $type);
}

return $type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

use PrestaShop\Module\AutoUpgrade\AjaxResponseBuilder;
use PrestaShop\Module\AutoUpgrade\Twig\PageSelectors;
use PrestaShop\Module\AutoUpgrade\Twig\UpdateSteps;
use Symfony\Component\HttpFoundation\Response;

abstract class AbstractPageWithStepController extends AbstractPageController
Expand Down
16 changes: 3 additions & 13 deletions controllers/admin/self-managed/LogsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

namespace PrestaShop\Module\AutoUpgrade\Controller;

use InvalidArgumentException;
use PrestaShop\Module\AutoUpgrade\AjaxResponseBuilder;
use PrestaShop\Module\AutoUpgrade\Task\TaskType;
use PrestaShop\Module\AutoUpgrade\Twig\PageSelectors;
Expand All @@ -37,8 +36,9 @@ class LogsController extends AbstractGlobalController
{
public function getDownloadLogsButton(): JsonResponse
{
$type = $this->request->request->get('download-logs-type');
$this->validateLogsType($type);
$type = TaskType::fromString(
$this->request->request->get('download-logs-type')
);

return AjaxResponseBuilder::hydrationResponse(
PageSelectors::DOWNLOAD_LOGS_PARENT_ID,
Expand All @@ -48,14 +48,4 @@ public function getDownloadLogsButton(): JsonResponse
)
);
}

/**
* @throws InvalidArgumentException
*/
private function validateLogsType(string $type): void
{
if (!in_array($type, TaskType::ALL_TASKS)) {
throw new InvalidArgumentException('Unknown log type ' . $type);
}
}
}
22 changes: 22 additions & 0 deletions tests/unit/Task/TaskTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use PHPUnit\Framework\TestCase;
use PrestaShop\Module\AutoUpgrade\Task\TaskType;

class TaskTypeTest extends TestCase
{
public function testFromStringReturnsValidTaskType()
{
$this->assertSame(TaskType::TASK_TYPE_BACKUP, TaskType::fromString('backup'));
$this->assertSame(TaskType::TASK_TYPE_UPDATE, TaskType::fromString('update'));
$this->assertSame(TaskType::TASK_TYPE_RESTORE, TaskType::fromString('restore'));
}

public function testFromStringThrowsExceptionForInvalidTaskType()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown log type invalid_task');

TaskType::fromString('invalid_task');
}
}

0 comments on commit 646ddc6

Please sign in to comment.