From 6714c89220acb62a4295d94e7e69adc9a3a1329b Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Sun, 18 Jul 2021 10:11:39 -0300 Subject: [PATCH] Remove interact from command and implement bootstrap method Signed-off-by: Vitor Mattos --- lib/Command/BoardImport.php | 15 +--- lib/Controller/BoardImportApiController.php | 1 - lib/Service/ABoardImportService.php | 7 +- lib/Service/BoardImportCommandService.php | 83 ++++++++++--------- lib/Service/BoardImportService.php | 33 +++----- lib/Service/BoardImportTrelloService.php | 4 + tests/unit/Service/BoardImportServiceTest.php | 37 +++++---- 7 files changed, 91 insertions(+), 89 deletions(-) diff --git a/lib/Command/BoardImport.php b/lib/Command/BoardImport.php index ba4ad8a72..2c825ae9e 100644 --- a/lib/Command/BoardImport.php +++ b/lib/Command/BoardImport.php @@ -72,20 +72,6 @@ class BoardImport extends Command { ; } - /** - * @inheritDoc - * - * @return void - */ - protected function interact(InputInterface $input, OutputInterface $output) { - $this->boardImportCommandService - ->setInput($input) - ->setOutput($output) - ->setSystem($input->getOption('system')) - ->setConfigInstance($input->getOption('config')) - ->validate(); - } - /** * @param InputInterface $input * @param OutputInterface $output @@ -97,6 +83,7 @@ class BoardImport extends Command { ->boardImportCommandService ->setInput($input) ->setOutput($output) + ->setCommand($this) ->import(); $output->writeln('Done!'); return 0; diff --git a/lib/Controller/BoardImportApiController.php b/lib/Controller/BoardImportApiController.php index febad393b..b6c32183f 100644 --- a/lib/Controller/BoardImportApiController.php +++ b/lib/Controller/BoardImportApiController.php @@ -57,7 +57,6 @@ class BoardImportApiController extends ApiController { $config->owner = $this->userId; $this->boardImportService->setConfigInstance($config); $this->boardImportService->setData(json_decode(json_encode($data))); - $this->boardImportService->validate(); $this->boardImportService->import(); return new DataResponse($this->boardImportService->getBoard(), Http::STATUS_OK); } diff --git a/lib/Service/ABoardImportService.php b/lib/Service/ABoardImportService.php index ef36fb8e3..614c31225 100644 --- a/lib/Service/ABoardImportService.php +++ b/lib/Service/ABoardImportService.php @@ -63,7 +63,12 @@ abstract class ABoardImportService { abstract public function assignCardsToLabels(): void; - abstract public function validateUsers(): void; + /** + * Configure import service + * + * @return void + */ + abstract public function bootstrap(): void; public function setImportService(BoardImportService $service): void { $this->boardImportService = $service; diff --git a/lib/Service/BoardImportCommandService.php b/lib/Service/BoardImportCommandService.php index d286b8a30..bff49e5a0 100644 --- a/lib/Service/BoardImportCommandService.php +++ b/lib/Service/BoardImportCommandService.php @@ -48,14 +48,9 @@ class BoardImportCommandService extends BoardImportService { */ private $output; - /** - * Define Command instance - * - * @param Command $command - * @return void - */ - public function setCommand(Command $command): void { + public function setCommand(Command $command): self { $this->command = $command; + return $this; } public function getCommand(): Command { @@ -80,13 +75,19 @@ class BoardImportCommandService extends BoardImportService { return $this->output; } - public function validate(): void { - $this->validateData(); - parent::validate(); - } - protected function validateConfig(): void { try { + $config = $this->getInput()->getOption('config'); + if (is_string($config)) { + if (!is_file($config)) { + throw new NotFoundException('Please inform a valid config json file'); + } + $config = json_decode(file_get_contents($config)); + if (!$config instanceof \stdClass) { + throw new NotFoundException('Please inform a valid config json file'); + } + $this->setConfigInstance($config); + } parent::validateConfig(); return; } catch (NotFoundException $e) { @@ -104,7 +105,7 @@ class BoardImportCommandService extends BoardImportService { return $answer; }); $configFile = $helper->ask($this->getInput(), $this->getOutput(), $question); - $this->setConfigInstance($configFile); + $config = $this->getInput()->setOption('config', $configFile); } catch (ConflictException $e) { $this->getOutput()->writeln('Invalid config file'); $this->getOutput()->writeln(array_map(function (array $v): string { @@ -114,7 +115,6 @@ class BoardImportCommandService extends BoardImportService { $schemaPath = __DIR__ . '/fixtures/config-' . $this->getSystem() . '-schema.json'; $this->getOutput()->writeln(print_r(file_get_contents($schemaPath), true)); $this->getInput()->setOption('config', null); - $this->setConfigInstance(''); } parent::validateConfig(); return; @@ -139,34 +139,41 @@ class BoardImportCommandService extends BoardImportService { return; } - private function validateData(): self { - $filename = $this->getInput()->getOption('data'); - if (!is_string($filename) || empty($filename) || !is_file($filename)) { - $helper = $this->getCommand()->getHelper('question'); - $question = new Question( - 'Please inform a valid data json file: ', - 'data.json' - ); - $question->setValidator(function (string $answer) { - if (!is_file($answer)) { - throw new \RuntimeException( - 'Data file not found' - ); - } - return $answer; - }); - $data = $helper->ask($this->getInput(), $this->getOutput(), $question); - $this->getInput()->setOption('data', $data); + protected function validateData(): void { + $data = $this->getInput()->getOption('data'); + if (is_string($data)) { + $data = json_decode(file_get_contents($data)); + if ($data instanceof \stdClass) { + $this->setData($data); + return; + } } - $this->setData(json_decode(file_get_contents($filename))); - if (!$this->getData()) { - $this->getOutput()->writeln('Is not a json file: ' . $filename . ''); - $this->validateData(); - } - return $this; + $helper = $this->getCommand()->getHelper('question'); + $question = new Question( + 'Please inform a valid data json file: ', + 'data.json' + ); + $question->setValidator(function (string $answer) { + if (!is_file($answer)) { + throw new \RuntimeException( + 'Data file not found' + ); + } + return $answer; + }); + $data = $helper->ask($this->getInput(), $this->getOutput(), $question); + $this->getInput()->setOption('data', $data); + $this->validateData(); + } + + public function bootstrap(): void { + $this->setSystem($this->getInput()->getOption('system')); + parent::bootstrap(); } public function import(): void { + $this->getOutput()->writeln('Starting import...'); + $this->bootstrap(); $this->getOutput()->writeln('Importing board...'); $this->importBoard(); $this->getOutput()->writeln('Assign users to board...'); diff --git a/lib/Service/BoardImportService.php b/lib/Service/BoardImportService.php index 49a8bc4f4..a295f12a2 100644 --- a/lib/Service/BoardImportService.php +++ b/lib/Service/BoardImportService.php @@ -111,10 +111,10 @@ class BoardImportService { $this->cardMapper = $cardMapper; $this->assignmentMapper = $assignmentMapper; $this->commentsManager = $commentsManager; - $this->setData(new \stdClass()); } public function import(): void { + $this->bootstrap(); try { $this->importBoard(); $this->importAcl(); @@ -129,12 +129,6 @@ class BoardImportService { } } - public function validate(): void { - $this->validateSystem(); - $this->validateConfig(); - $this->validateUsers(); - } - public function validateSystem(): void { if (!in_array($this->getSystem(), $this->getAllowedImportSystems())) { throw new NotFoundException('Invalid system'); @@ -327,7 +321,7 @@ class BoardImportService { $this->getImportSystem()->importParticipants(); } - final public function setData(\stdClass $data): void { + public function setData(\stdClass $data): void { $this->data = $data; } @@ -355,7 +349,7 @@ class BoardImportService { * @param string $configName config name * @return mixed */ - public function getConfig(string $configName = null) { + public function getConfig(string $configName) { if (!property_exists($this->config, $configName)) { return; } @@ -363,19 +357,10 @@ class BoardImportService { } /** - * @param mixed $config + * @param \stdClass $config * @return self */ public function setConfigInstance($config): self { - if (is_string($config)) { - if (!is_file($config)) { - throw new NotFoundException('Please inform a valid config json file'); - } - $config = json_decode(file_get_contents($config)); - if (!is_object($config)) { - throw new NotFoundException('Please inform a valid config json file'); - } - } $this->config = $config; return $this; } @@ -413,7 +398,13 @@ class BoardImportService { $this->setConfig('owner', $owner); } - public function validateUsers(): void { - $this->getImportSystem()->validateUsers(); + protected function validateData(): void { + } + + public function bootstrap(): void { + $this->validateSystem(); + $this->validateConfig(); + $this->validateData(); + $this->getImportSystem()->bootstrap(); } } diff --git a/lib/Service/BoardImportTrelloService.php b/lib/Service/BoardImportTrelloService.php index adf8b0abe..1598e20f2 100644 --- a/lib/Service/BoardImportTrelloService.php +++ b/lib/Service/BoardImportTrelloService.php @@ -66,6 +66,10 @@ class BoardImportTrelloService extends ABoardImportService { $this->l10n = $l10n; } + public function bootstrap(): void { + $this->validateUsers(); + } + public function validateUsers(): void { if (empty($this->getImportService()->getConfig('uidRelation'))) { return; diff --git a/tests/unit/Service/BoardImportServiceTest.php b/tests/unit/Service/BoardImportServiceTest.php index a24022370..9d0ccbd6b 100644 --- a/tests/unit/Service/BoardImportServiceTest.php +++ b/tests/unit/Service/BoardImportServiceTest.php @@ -76,6 +76,22 @@ class BoardImportServiceTest extends \Test\TestCase { $this->assignmentMapper, $this->commentsManager ); + + $this->boardImportService->setSystem('trello'); + + $data = json_decode(file_get_contents(__DIR__ . '/../../data/data-trello.json')); + $this->boardImportService->setData($data); + + $configInstance = json_decode(file_get_contents(__DIR__ . '/../../data/config-trello.json')); + $this->boardImportService->setConfigInstance($configInstance); + + $owner = $this->createMock(IUser::class); + $owner + ->method('getUID') + ->willReturn('admin'); + $this->userManager + ->method('get') + ->willReturn($owner); } public function testImportSuccess() { @@ -91,25 +107,18 @@ class BoardImportServiceTest extends \Test\TestCase { } public function testImportBoard() { - $this->boardImportService->setSystem('trello'); - $data = json_decode(file_get_contents(__DIR__ . '/../../data/data-trello.json')); - $this->boardImportService->setData($data); - $configInstance = json_decode(file_get_contents(__DIR__ . '/../../data/config-trello.json')); - $this->boardImportService->setConfigInstance($configInstance); - - $owner = $this->createMock(IUser::class); - $owner - ->method('getUID') - ->willReturn('owner'); - $this->userManager - ->method('get') - ->willReturn($owner); $this->boardImportService->validateOwner(); $actual = $this->boardImportService->importBoard(); $this->assertNull($actual); $board = $this->boardImportService->getBoard(); $this->assertEquals('Test Board Name', $board->getTitle()); - $this->assertEquals('owner', $board->getOwner()); + $this->assertEquals('admin', $board->getOwner()); $this->assertEquals('0800fd', $board->getColor()); } + + public function testImportAcl() { + $this->markTestIncomplete(); + $actual = $this->boardImportService->importAcl(); + $this->assertNull($actual); + } }