public function testGetSetSchema()
 {
     $schema = array('hooray' => array('a' => 'new schema'));
     $this->assertTrue(is_array($this->type->getSchema()));
     $this->assertEmpty($this->type->getSchema());
     $this->type->setSchema($schema);
     $this->assertSame($schema, $this->type->getSchema());
 }
 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;
 }
 /**
  * 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;
 }