public function storageProvider()
 {
     $length = 200;
     $data = $this->generateString($length);
     $expectedChunk = new Chunk();
     $expectedChunk->setData($data);
     $expectedChunk->setHash($this->getFactory()->createHash($data));
     $expectedChunk->setLength($length);
     $expectedChunk->setPolicyCollection(new PolicyCollection());
     $this->getDatabase()->deleteAll();
     return array(array($expectedChunk));
 }
 /**
  * @expectedException \Symcloud\Component\ChunkStorage\Exception\ChunkNotFoundException
  */
 public function testDownloadNotExists()
 {
     $data = 'This is my data';
     $hash = $this->getFactory()->createHash($data);
     $chunk = new Chunk();
     $chunk->setHash($hash);
     $chunk->setData($data);
     $factory = $this->prophesize(FactoryInterface::class);
     $factory->createHash($data)->willReturn($hash);
     $database = $this->prophesize(DatabaseInterface::class);
     $database->fetch('my-hash', Chunk::class)->willThrow(new ChunkNotFoundException($hash));
     $database->store(Argument::type(ChunkInterface::class))->should(new NoCallsPrediction());
     $manager = new ChunkManager($factory->reveal(), $database->reveal());
     $manager->download('my-hash');
 }
 /**
  * @param $data
  *
  * @return ChunkInterface
  */
 public function upload($data)
 {
     $chunk = new Chunk();
     $chunk->setData($data);
     $chunk->setLength(strlen($data));
     $chunk->setPolicyCollection(new PolicyCollection());
     $chunk->setHash($this->factory->createHash($data));
     if ($this->database->contains($chunk->getHash(), Chunk::class)) {
         return $chunk;
     }
     return $this->database->store($chunk, array(ReplicatorInterface::OPTION_NAME => ReplicatorInterface::TYPE_FULL));
 }
 public function testDownload()
 {
     $mimeType = 'application/json';
     $size = 999;
     $data = $this->generateString(200);
     $fileName = tempnam('', 'splitter-test-file');
     file_put_contents($fileName, $data);
     $fileHash = 'my-hash';
     $chunk1 = new Chunk();
     $chunk1->setHash('hash1');
     $chunk1->setData(substr($data, 0, 100));
     $chunk2 = new Chunk();
     $chunk2->setHash('hash2');
     $chunk2->setData(substr($data, 100, 100));
     $file = new ChunkFile();
     $file->setHash($fileHash);
     $file->setChunks(array($chunk1, $chunk2));
     $file->setMimeType($mimeType);
     $file->setSize($size);
     $fileSplitter = new FileSplitter(100);
     $chunkManager = $this->prophesize(ChunkManagerInterface::class);
     $factory = $this->prophesize(FactoryInterface::class);
     $proxyFactory = new LazyLoadingValueHolderFactory();
     $chunkManager->upload()->should(new NoCallsPrediction());
     $chunkManager->download()->should(new NoCallsPrediction());
     $chunkManager->downloadProxy($chunk1->getHash())->willReturn($chunk1);
     $chunkManager->downloadProxy($chunk2->getHash())->willReturn($chunk2);
     $factory->createHash()->should(new NoCallsPrediction());
     $factory->createFileHash()->should(new NoCallsPrediction());
     $factory->createProxy(Argument::type('string'), Argument::type('callable'))->will(function ($args) use($proxyFactory) {
         return $proxyFactory->createProxy($args[0], $args[1]);
     });
     $manager = new ChunkFileManager($fileSplitter, $chunkManager->reveal(), $factory->reveal());
     $result = $manager->download($fileHash, array($chunk1->getHash(), $chunk2->getHash()), $mimeType, $size);
     $this->assertEquals($file->getHash(), $result->getHash());
     $this->assertEquals($file->getChunks(), $result->getChunks());
     $this->assertEquals($mimeType, $result->getMimeType());
     $this->assertEquals($size, $result->getSize());
 }
 public function storageProvider()
 {
     $factory = $this->getFactory();
     $size = 200;
     $mimeType = 'application/json';
     list($data, $fileName) = $this->generateTestFile($size);
     $chunk1 = new Chunk();
     $chunk1->setData(substr($data, 0, 100));
     $chunk1->setHash($factory->createHash($chunk1->getData()));
     $chunk1->setLength(strlen($chunk1->getData()));
     $chunk2 = new Chunk();
     $chunk2->setData(substr($data, 100, 100));
     $chunk2->setHash($factory->createHash($chunk2->getData()));
     $chunk2->setLength(strlen($chunk2->getData()));
     $chunks = array($chunk1, $chunk2);
     $fileHash = $factory->createFileHash($fileName);
     return array(array($fileName, $data, $fileHash, $chunks, $mimeType, $size));
 }