public function testResolveCollision()
 {
     $namer = $this->getMockBuilder('Iphp\\FileStoreBundle\\Naming\\DefaultNamer')->disableOriginalConstructor()->getMock();
     $this->container->expects($this->once())->method('get')->with('test.service')->will($this->returnValue($namer));
     $namer->expects($this->once())->method('resolveCollision')->with('sample-file-name', 1)->will($this->returnValue('sample-file-name_1'));
     $this->assertSame($this->invoker->resolveCollision('test.service', 'sample-file-name', 1), 'sample-file-name_1');
 }
 /**
  * Exception about need namer for resolving collisions
  * @expectedException \Exception
  */
 function testResolveFileCollisionNoNamer()
 {
     $propertyMapping = $this->getPropertyMapping(new DummyEntity(), array('namer' => null));
     $this->namerServiceInvoker->expects($this->never())->method('rename');
     $this->fileStorage->expects($this->once())->method('fileExists')->with('/www/web/images/ab cd')->will($this->returnValue(true));
     $propertyMapping->prepareFileName('ab cd', $this->fileStorage);
 }
 /**
  * @param $fileName
  * @param $clientOriginalName
  * @param int $attempt
  * @return string new file path
  * @throws \Exception
  */
 public function resolveFileCollision($fileName, $clientOriginalName, $attempt = 1)
 {
     if ($this->hasNamer()) {
         $firstNamer = current($this->config['namer']);
         return $this->namerServiceInvoker->resolveCollision($firstNamer['service'], $fileName, $attempt);
     }
     throw new \Exception('Filename resolving collision not supported (namer is empty).Duplicate filename ' . $fileName);
 }
 public function testConfiguredNamerRetrievedFromInvoker()
 {
     $obj = new DummyEntity();
     $class = new \ReflectionClass($obj);
     $mappingsConfig = array('dummy_file' => array('upload_dir' => '/www/web/images', 'upload_path' => '/images', 'namer' => array('translit' => array('service' => 'iphp.filestore.namer.default'))));
     $uploadable = Mocks::getUploadableMock($this);
     $fileField = Mocks::getUploadableFieldMock($this);
     $fileField->expects($this->any())->method('getMapping')->will($this->returnValue('dummy_file'));
     $fileField->expects($this->once())->method('getFileUploadPropertyName')->will($this->returnValue('file'));
     $fileField->expects($this->once())->method('getFileDataPropertyName')->will($this->returnValue('file'));
     $this->driver->expects($this->once())->method('readUploadable')->with($class)->will($this->returnValue($uploadable));
     $this->driver->expects($this->once())->method('readUploadableFields')->with($class)->will($this->returnValue(array($fileField)));
     $factory = new PropertyMappingFactory($this->namerServiceInvoker, $this->driver, $mappingsConfig);
     $mappings = $factory->getMappingsFromObject($obj, $class);
     $this->assertEquals(1, count($mappings));
     if (count($mappings) > 0) {
         $mapping = $mappings[0];
         $this->namerServiceInvoker->expects($this->once())->method('rename')->with('iphp.filestore.namer.default', 'translit', $mapping, 'ab cde', array())->will($this->returnValue('ab-cde'));
         $this->assertEquals($mapping->useFileNamer('ab cde'), 'ab-cde');
         $this->assertTrue($mapping->hasNamer());
     }
 }