public function testReduceIndicesGivenArrayWithoutAnyMatch()
 {
     $indices = [['index' => self::INDEX . '-fake', 'type' => 'notExisting']];
     $type = $this->getMockBuilder('\\Elastification\\BackupRestore\\Entity\\Mappings\\Type')->disableOriginalConstructor()->getMock();
     $type->expects($this->once())->method('getName')->willReturn(self::TYPE);
     $this->index->expects($this->once())->method('getName')->willReturn(self::INDEX);
     $this->index->expects($this->once())->method('getTypes')->willReturn([self::TYPE => $type]);
     $this->index->expects($this->once())->method('countTypes')->willReturn(0);
     $this->mappings->addIndex($this->index);
     $this->assertSame(1, $this->mappings->countIndices());
     $this->mappings->reduceIndices($indices);
     $this->assertSame(0, $this->mappings->countIndices());
 }
 /**
  * Forces User to select index/type from existing ones
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  * @param RestoreStrategy\MappingAction $mappingAction
  * @return RestoreStrategy\MappingAction
  * @author Daniel Wendlandt
  */
 private function askForExistingIndexType(InputInterface $input, OutputInterface $output, RestoreStrategy\MappingAction $mappingAction)
 {
     $existingQuestions = array();
     /** @var Index $index */
     foreach ($this->targetMappings->getIndices() as $index) {
         /** @var Type $type */
         foreach ($index->getTypes() as $type) {
             $existingQuestions[] = $index->getName() . '/' . $type->getName();
         }
     }
     $questionExisting = new ChoiceQuestion('Please select one of this existing indices/types', $existingQuestions);
     $helper = $this->getHelper('question');
     $answerExisting = $helper->ask($input, $output, $questionExisting);
     $exploded = explode('/', $answerExisting);
     $mappingAction->setTargetIndex($exploded[0]);
     $mappingAction->setTargetType($exploded[1]);
     $this->askForMappingStrategy($input, $output, $mappingAction);
     return $mappingAction;
 }
 public function testStoreMappings()
 {
     $path = '/tmp/test-path';
     $mappings = new Mappings();
     $type1 = new Mappings\Type();
     $type1->setName('type1');
     $type1->setSchema(array('properties' => array()));
     $type2 = new Mappings\Type();
     $type2->setName('type2');
     $type2->setSchema(array('properties' => array()));
     $index1 = new Mappings\Index();
     $index1->setName('index1');
     $index1->addType($type1);
     $index1->addType($type2);
     $mappings->addIndex($index1);
     $folderPath = $path . DIRECTORY_SEPARATOR . FilesystemRepositoryInterface::DIR_SCHEMA . DIRECTORY_SEPARATOR . $index1->getName();
     $schemaPath1 = $folderPath . DIRECTORY_SEPARATOR . $type1->getName() . FilesystemRepositoryInterface::FILE_EXTENSION;
     $schemaPath2 = $folderPath . DIRECTORY_SEPARATOR . $type2->getName() . FilesystemRepositoryInterface::FILE_EXTENSION;
     $this->filesystem->expects($this->once())->method('mkdir')->with($folderPath);
     $this->filesystem->expects($this->exactly(2))->method('dumpFile')->withConsecutive(array($schemaPath1, json_encode($type1->getSchema())), array($schemaPath2, json_encode($type2->getSchema())));
     $createdFiles = $this->filesystemRepository->storeMappings($path, $mappings);
     $this->assertSame(2, $createdFiles);
 }
 /**
  * Get mappings for all indices
  *
  * @param string $host
  * @param int $port
  * @return Mappings
  * @throws \Exception
  * @author Daniel Wendlandt
  */
 public function getAllMappings($host, $port = 9200)
 {
     $this->checkServerInfo($host, $port);
     $request = $this->requestFactory->create('Index\\GetMappingRequest', $this->serverInfo->version, null, null, $this->getSerializer());
     $client = $this->getClient($host, $port);
     $response = $client->send($request);
     $mappings = new Mappings();
     foreach ($response->getData() as $indexName => $typeMappings) {
         $index = new Mappings\Index();
         $index->setName($indexName);
         foreach ($typeMappings['mappings'] as $typeName => $schema) {
             $type = new Mappings\Type();
             $type->setName($typeName);
             $type->setSchema($schema);
             $index->addType($type);
         }
         $mappings->addIndex($index);
     }
     return $mappings;
 }
 /**
  * Process all given indices/types for only one main strategy
  *
  * @param string $strategy
  * @param Mappings $mappings
  * @author Daniel Wendlandt
  */
 public function processMappingsForStrategy($strategy, Mappings $mappings)
 {
     $this->mappings = array();
     /** @var Index $index */
     foreach ($mappings->getIndices() as $index) {
         /** @var Type $type */
         foreach ($index->getTypes() as $type) {
             $mappingAction = new MappingAction();
             $mappingAction->setStrategy($strategy);
             $mappingAction->setSourceIndex($index->getName());
             $mappingAction->setSourceType($type->getName());
             $mappingAction->setTargetIndex($index->getName());
             $mappingAction->setTargetType($type->getName());
             $this->addMappingAction($mappingAction);
         }
     }
 }
 /**
  * Loads the mappings that are located in the filesystem of stored backup
  *
  * @param string $filepath
  * @return Mappings
  * @throws \Exception
  * @author Daniel Wendlandt
  */
 public function loadMappings($filepath)
 {
     $schemaFolderPath = $filepath . DIRECTORY_SEPARATOR . self::DIR_SCHEMA;
     if (!$this->filesytsem->exists($schemaFolderPath)) {
         throw new \Exception('Schema folder does not exist in ' . $filepath);
     }
     /** @var Finder $finder */
     $finder = new $this->finder();
     $indices = array();
     /** @var SplFileInfo $file */
     foreach ($finder->files()->in($schemaFolderPath)->name('*' . self::FILE_EXTENSION) as $file) {
         $indexName = $file->getRelativePath();
         if (!isset($indices[$indexName])) {
             $index = new Mappings\Index();
             $index->setName($indexName);
             $indices[$indexName] = $index;
         }
         /** @var Mappings\Index $index */
         $index = $indices[$indexName];
         //perform type;
         $type = new Mappings\Type();
         $type->setName($file->getBasename(self::FILE_EXTENSION));
         $type->setSchema(json_decode($file->getContents(), true));
         $index->addType($type);
     }
     $mappings = new Mappings();
     $mappings->setIndices($indices);
     return $mappings;
 }