-
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.
Merge branch 'master' into III-4932-remove-add_location_name_to_coord…
…inates_lookup
- Loading branch information
Showing
6 changed files
with
302 additions
and
12 deletions.
There are no files selected for viewing
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,91 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CultuurNet\UDB3\Console\Command; | ||
|
||
use CultuurNet\UDB3\Place\Canonical\DBALDuplicatePlaceRepository; | ||
use CultuurNet\UDB3\Place\Canonical\ImportDuplicatePlacesProcessor; | ||
use Symfony\Component\Console\Command\Command as BaseCommand; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\ConfirmationQuestion; | ||
|
||
class ImportDuplicatePlaces extends BaseCommand | ||
{ | ||
private const FORCE = 'force'; | ||
|
||
private ImportDuplicatePlacesProcessor $importDuplicatePlacesProcessor; | ||
private DBALDuplicatePlaceRepository $dbalDuplicatePlaceRepository; | ||
|
||
public function __construct( | ||
DBALDuplicatePlaceRepository $dbalDuplicatePlaceRepository, | ||
ImportDuplicatePlacesProcessor $importDuplicatePlacesProcessor | ||
) { | ||
parent::__construct(); | ||
|
||
$this->importDuplicatePlacesProcessor = $importDuplicatePlacesProcessor; | ||
$this->dbalDuplicatePlaceRepository = $dbalDuplicatePlaceRepository; | ||
} | ||
|
||
public function configure(): void | ||
{ | ||
$this | ||
->setName('place:duplicate-places:import') | ||
->setDescription('Import duplicate places from the import tables, set clusters ready for processing') | ||
->addOption( | ||
self::FORCE, | ||
null, | ||
InputOption::VALUE_NONE, | ||
'Skip confirmation.' | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): ?int | ||
{ | ||
$howManyPlacesAreToBeImported = $this->dbalDuplicatePlaceRepository->howManyPlacesAreToBeImported(); | ||
$howManyPlacesAreToBeDeleted = count($this->dbalDuplicatePlaceRepository->getPlacesNoLongerInCluster()); | ||
|
||
if ($howManyPlacesAreToBeImported === 0 && $howManyPlacesAreToBeDeleted === 0) { | ||
$output->writeln('duplicate_places is already synced'); | ||
return self::SUCCESS; | ||
} | ||
|
||
if (!$this->askConfirmation( | ||
$input, | ||
$output, | ||
sprintf( | ||
'This action will sync a total of %d new places, and remove %d places from the duplicate places table. Do you want to continue? [y/N] ', | ||
$howManyPlacesAreToBeImported, | ||
$howManyPlacesAreToBeDeleted, | ||
) | ||
)) { | ||
return self::SUCCESS; | ||
} | ||
|
||
$this->importDuplicatePlacesProcessor->sync(); | ||
|
||
$output->writeln('Duplicate places were synced and old clusters were removed. You probably want to run place:process-duplicates to give canonicals to the new clusters now.'); | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
private function askConfirmation(InputInterface $input, OutputInterface $output, string $message): bool | ||
{ | ||
if ($input->getOption(self::FORCE)) { | ||
return true; | ||
} | ||
|
||
return $this | ||
->getHelper('question') | ||
->ask( | ||
$input, | ||
$output, | ||
new ConfirmationQuestion( | ||
$message, | ||
true | ||
) | ||
); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CultuurNet\UDB3\Console\Command; | ||
|
||
use CultuurNet\UDB3\Place\Canonical\DBALDuplicatePlaceRepository; | ||
use CultuurNet\UDB3\Place\Canonical\ImportDuplicatePlacesProcessor; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Ramsey\Uuid\Uuid; | ||
use Symfony\Component\Console\Helper\HelperSet; | ||
use Symfony\Component\Console\Helper\QuestionHelper; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class ImportDuplicatePlacesTest extends TestCase | ||
{ | ||
/** @var DBALDuplicatePlaceRepository|MockObject */ | ||
private $dbalDuplicatePlaceRepository; | ||
/** @var ImportDuplicatePlacesProcessor|MockObject */ | ||
private $importDuplicatePlacesProcessor; | ||
/** @var InputInterface|MockObject */ | ||
private $input; | ||
/** @var OutputInterface|MockObject */ | ||
private $output; | ||
private ImportDuplicatePlaces $command; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->dbalDuplicatePlaceRepository = $this->createMock(DBALDuplicatePlaceRepository::class); | ||
$this->importDuplicatePlacesProcessor = $this->createMock(ImportDuplicatePlacesProcessor::class); | ||
$this->input = $this->createMock(InputInterface::class); | ||
$this->output = $this->createMock(OutputInterface::class); | ||
|
||
$this->command = new ImportDuplicatePlaces( | ||
$this->dbalDuplicatePlaceRepository, | ||
$this->importDuplicatePlacesProcessor | ||
); | ||
} | ||
|
||
public function testExecuteSucceedsWhenTablesAreAlreadySynced(): void | ||
{ | ||
$this->dbalDuplicatePlaceRepository | ||
->expects($this->once()) | ||
->method('howManyPlacesAreToBeImported') | ||
->willReturn(0); | ||
|
||
$this->dbalDuplicatePlaceRepository | ||
->expects($this->once()) | ||
->method('getPlacesNoLongerInCluster') | ||
->willReturn([]); | ||
|
||
$this->output | ||
->expects($this->once()) | ||
->method('writeln') | ||
->with('duplicate_places is already synced'); | ||
|
||
$this->assertEquals(0, $this->command->run($this->input, $this->output)); | ||
} | ||
|
||
public function testExecuteConfirmsAndSyncsWhenChangesAreWithinLimits(): void | ||
{ | ||
$this->dbalDuplicatePlaceRepository | ||
->expects($this->once()) | ||
->method('howManyPlacesAreToBeImported') | ||
->willReturn(50); | ||
|
||
$this->dbalDuplicatePlaceRepository | ||
->expects($this->once()) | ||
->method('getPlacesNoLongerInCluster') | ||
->willReturn([Uuid::uuid4()]); | ||
|
||
$helper = $this->createMock(QuestionHelper::class); | ||
$helper->expects($this->once()) | ||
->method('ask') | ||
->willReturn(true); | ||
$this->command->setHelperSet(new HelperSet(['question' => $helper])); | ||
|
||
$this->importDuplicatePlacesProcessor | ||
->expects($this->once()) | ||
->method('sync'); | ||
|
||
$this->assertEquals(0, $this->command->run($this->input, $this->output)); | ||
} | ||
} |
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